1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"io"
"os"
"time"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/_integrations/nrgrpc"
sampleapp "github.com/newrelic/go-agent/_integrations/nrgrpc/example/sampleapp"
"google.golang.org/grpc"
)
func doUnaryUnary(ctx context.Context, client sampleapp.SampleApplicationClient) {
msg, err := client.DoUnaryUnary(ctx, &sampleapp.Message{Text: "Hello DoUnaryUnary"})
if nil != err {
panic(err)
}
fmt.Println(msg.Text)
}
func doUnaryStream(ctx context.Context, client sampleapp.SampleApplicationClient) {
stream, err := client.DoUnaryStream(ctx, &sampleapp.Message{Text: "Hello DoUnaryStream"})
if nil != err {
panic(err)
}
for {
msg, err := stream.Recv()
if err == io.EOF {
break
}
if nil != err {
panic(err)
}
fmt.Println(msg.Text)
}
}
func doStreamUnary(ctx context.Context, client sampleapp.SampleApplicationClient) {
stream, err := client.DoStreamUnary(ctx)
if nil != err {
panic(err)
}
for i := 0; i < 3; i++ {
if err := stream.Send(&sampleapp.Message{Text: "Hello DoStreamUnary"}); nil != err {
if err == io.EOF {
break
}
panic(err)
}
}
msg, err := stream.CloseAndRecv()
if nil != err {
panic(err)
}
fmt.Println(msg.Text)
}
func doStreamStream(ctx context.Context, client sampleapp.SampleApplicationClient) {
stream, err := client.DoStreamStream(ctx)
if nil != err {
panic(err)
}
waitc := make(chan struct{})
go func() {
for {
msg, err := stream.Recv()
if err == io.EOF {
close(waitc)
return
}
if err != nil {
panic(err)
}
fmt.Println(msg.Text)
}
}()
for i := 0; i < 3; i++ {
if err := stream.Send(&sampleapp.Message{Text: "Hello DoStreamStream"}); err != nil {
panic(err)
}
}
stream.CloseSend()
<-waitc
}
func mustGetEnv(key string) string {
if val := os.Getenv(key); "" != val {
return val
}
panic(fmt.Sprintf("environment variable %s unset", key))
}
func main() {
cfg := newrelic.NewConfig("gRPC Client", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
app, err := newrelic.NewApplication(cfg)
if nil != err {
panic(err)
}
err = app.WaitForConnection(10 * time.Second)
if nil != err {
panic(err)
}
defer app.Shutdown(10 * time.Second)
txn := app.StartTransaction("main", nil, nil)
defer txn.End()
conn, err := grpc.Dial(
"localhost:8080",
grpc.WithInsecure(),
// Add the New Relic gRPC client instrumentation
grpc.WithUnaryInterceptor(nrgrpc.UnaryClientInterceptor),
grpc.WithStreamInterceptor(nrgrpc.StreamClientInterceptor),
)
if err != nil {
panic(err)
}
defer conn.Close()
client := sampleapp.NewSampleApplicationClient(conn)
ctx := newrelic.NewContext(context.Background(), txn)
doUnaryUnary(ctx, client)
doUnaryStream(ctx, client)
doStreamUnary(ctx, client)
doStreamStream(ctx, client)
}
|