| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"log" |
| 13 |
"time" |
| 14 |
|
| 15 |
"google.golang.org/grpc" |
| 16 |
"google.golang.org/grpc/credentials/insecure" |
| 17 |
"google.golang.org/grpc/metadata" |
| 18 |
|
| 19 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" |
| 20 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/example/api" |
| 21 |
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/example/config" |
| 22 |
) |
| 23 |
|
| 24 |
func main() { |
| 25 |
tp, err := config.Init() |
| 26 |
if err != nil { |
| 27 |
log.Fatal(err) |
| 28 |
} |
| 29 |
defer func() { |
| 30 |
if err := tp.Shutdown(context.Background()); err != nil { |
| 31 |
log.Printf("Error shutting down tracer provider: %v", err) |
| 32 |
} |
| 33 |
}() |
| 34 |
|
| 35 |
var conn *grpc.ClientConn |
| 36 |
conn, err = grpc.NewClient("127.0.0.1:7777", grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 37 |
grpc.WithStatsHandler(otelgrpc.NewClientHandler()), |
| 38 |
) |
| 39 |
if err != nil { |
| 40 |
log.Fatalf("did not connect: %s", err) |
| 41 |
} |
| 42 |
defer func() { _ = conn.Close() }() |
| 43 |
|
| 44 |
c := api.NewHelloServiceClient(conn) |
| 45 |
|
| 46 |
if err := callSayHello(c); err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
if err := callSayHelloClientStream(c); err != nil { |
| 50 |
log.Fatal(err) |
| 51 |
} |
| 52 |
if err := callSayHelloServerStream(c); err != nil { |
| 53 |
log.Fatal(err) |
| 54 |
} |
| 55 |
if err := callSayHelloBidiStream(c); err != nil { |
| 56 |
log.Fatal(err) |
| 57 |
} |
| 58 |
|
| 59 |
time.Sleep(10 * time.Millisecond) |
| 60 |
} |
| 61 |
|
| 62 |
func callSayHello(c api.HelloServiceClient) error { |
| 63 |
md := metadata.Pairs( |
| 64 |
"timestamp", time.Now().Format(time.StampNano), |
| 65 |
"client-id", "web-api-client-us-east-1", |
| 66 |
"user-id", "some-test-user-id", |
| 67 |
) |
| 68 |
|
| 69 |
ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 70 |
response, err := c.SayHello(ctx, &api.HelloRequest{Greeting: "World"}) |
| 71 |
if err != nil { |
| 72 |
return fmt.Errorf("calling SayHello: %w", err) |
| 73 |
} |
| 74 |
log.Printf("Response from server: %s", response.Reply) |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
func callSayHelloClientStream(c api.HelloServiceClient) error { |
| 79 |
md := metadata.Pairs( |
| 80 |
"timestamp", time.Now().Format(time.StampNano), |
| 81 |
"client-id", "web-api-client-us-east-1", |
| 82 |
"user-id", "some-test-user-id", |
| 83 |
) |
| 84 |
|
| 85 |
ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 86 |
stream, err := c.SayHelloClientStream(ctx) |
| 87 |
if err != nil { |
| 88 |
return fmt.Errorf("opening SayHelloClientStream: %w", err) |
| 89 |
} |
| 90 |
|
| 91 |
for i := range 5 { |
| 92 |
err := stream.Send(&api.HelloRequest{Greeting: "World"}) |
| 93 |
|
| 94 |
time.Sleep(time.Duration(i*50) * time.Millisecond) |
| 95 |
|
| 96 |
if err != nil { |
| 97 |
return fmt.Errorf("sending to SayHelloClientStream: %w", err) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
response, err := stream.CloseAndRecv() |
| 102 |
if err != nil { |
| 103 |
return fmt.Errorf("closing SayHelloClientStream: %w", err) |
| 104 |
} |
| 105 |
|
| 106 |
log.Printf("Response from server: %s", response.Reply) |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func callSayHelloServerStream(c api.HelloServiceClient) error { |
| 111 |
md := metadata.Pairs( |
| 112 |
"timestamp", time.Now().Format(time.StampNano), |
| 113 |
"client-id", "web-api-client-us-east-1", |
| 114 |
"user-id", "some-test-user-id", |
| 115 |
) |
| 116 |
|
| 117 |
ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 118 |
stream, err := c.SayHelloServerStream(ctx, &api.HelloRequest{Greeting: "World"}) |
| 119 |
if err != nil { |
| 120 |
return fmt.Errorf("opening SayHelloServerStream: %w", err) |
| 121 |
} |
| 122 |
|
| 123 |
for { |
| 124 |
response, err := stream.Recv() |
| 125 |
if errors.Is(err, io.EOF) { |
| 126 |
break |
| 127 |
} else if err != nil { |
| 128 |
return fmt.Errorf("receiving from SayHelloServerStream: %w", err) |
| 129 |
} |
| 130 |
|
| 131 |
log.Printf("Response from server: %s", response.Reply) |
| 132 |
time.Sleep(50 * time.Millisecond) |
| 133 |
} |
| 134 |
return nil |
| 135 |
} |
| 136 |
|
| 137 |
func callSayHelloBidiStream(c api.HelloServiceClient) error { |
| 138 |
md := metadata.Pairs( |
| 139 |
"timestamp", time.Now().Format(time.StampNano), |
| 140 |
"client-id", "web-api-client-us-east-1", |
| 141 |
"user-id", "some-test-user-id", |
| 142 |
) |
| 143 |
|
| 144 |
ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 145 |
stream, err := c.SayHelloBidiStream(ctx) |
| 146 |
if err != nil { |
| 147 |
return fmt.Errorf("opening SayHelloBidiStream: %w", err) |
| 148 |
} |
| 149 |
|
| 150 |
serverClosed := make(chan struct{}) |
| 151 |
clientClosed := make(chan struct{}) |
| 152 |
|
| 153 |
go func() { |
| 154 |
for range 5 { |
| 155 |
err := stream.Send(&api.HelloRequest{Greeting: "World"}) |
| 156 |
if err != nil { |
| 157 |
|
| 158 |
log.Fatalf("Error when sending to SayHelloBidiStream: %s", err) |
| 159 |
} |
| 160 |
|
| 161 |
time.Sleep(50 * time.Millisecond) |
| 162 |
} |
| 163 |
|
| 164 |
err := stream.CloseSend() |
| 165 |
if err != nil { |
| 166 |
|
| 167 |
log.Fatalf("Error when closing SayHelloBidiStream: %s", err) |
| 168 |
} |
| 169 |
|
| 170 |
clientClosed <- struct{}{} |
| 171 |
}() |
| 172 |
|
| 173 |
go func() { |
| 174 |
for { |
| 175 |
response, err := stream.Recv() |
| 176 |
if errors.Is(err, io.EOF) { |
| 177 |
break |
| 178 |
} else if err != nil { |
| 179 |
|
| 180 |
log.Fatalf("Error when receiving from SayHelloBidiStream: %s", err) |
| 181 |
} |
| 182 |
|
| 183 |
log.Printf("Response from server: %s", response.Reply) |
| 184 |
time.Sleep(50 * time.Millisecond) |
| 185 |
} |
| 186 |
|
| 187 |
serverClosed <- struct{}{} |
| 188 |
}() |
| 189 |
|
| 190 |
|
| 191 |
<-clientClosed |
| 192 |
<-serverClosed |
| 193 |
return nil |
| 194 |
} |