git.colasdn.top/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrmicro/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  	"os"
    10  	"time"
    11  
    12  	"github.com/micro/go-micro"
    13  	newrelic "github.com/newrelic/go-agent"
    14  	"github.com/newrelic/go-agent/_integrations/nrmicro"
    15  	proto "github.com/newrelic/go-agent/_integrations/nrmicro/example/proto"
    16  )
    17  
    18  func mustGetEnv(key string) string {
    19  	if val := os.Getenv(key); "" != val {
    20  		return val
    21  	}
    22  	panic(fmt.Sprintf("environment variable %s unset", key))
    23  }
    24  
    25  func main() {
    26  	cfg := newrelic.NewConfig("Micro Client", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    27  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    28  	app, err := newrelic.NewApplication(cfg)
    29  	if nil != err {
    30  		panic(err)
    31  	}
    32  	err = app.WaitForConnection(10 * time.Second)
    33  	if nil != err {
    34  		panic(err)
    35  	}
    36  	defer app.Shutdown(10 * time.Second)
    37  
    38  	txn := app.StartTransaction("client", nil, nil)
    39  	defer txn.End()
    40  
    41  	service := micro.NewService(
    42  		// Add the New Relic wrapper to the client which will create External
    43  		// segments for each out going call.
    44  		micro.WrapClient(nrmicro.ClientWrapper()),
    45  	)
    46  	service.Init()
    47  	ctx := newrelic.NewContext(context.Background(), txn)
    48  	c := proto.NewGreeterService("greeter", service.Client())
    49  
    50  	rsp, err := c.Hello(ctx, &proto.HelloRequest{
    51  		Name: "John",
    52  	})
    53  	if err != nil {
    54  		fmt.Println(err)
    55  		return
    56  	}
    57  	fmt.Println(rsp.Greeting)
    58  }