github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrgrpc/example/client/client.go (about)

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