gRPC stats.Handler
implementation to measure and send performances metrics to New Relic.
nrgrpc.NewServerStatsHandler
creates a stats.Handler
instance for gRPC servers.
When the handler is passed to a gRPC server with grpc.StatsHandler
,
it will set a newrelic.Transaction
into a request context.Context
using newrelic.NewContext
.
So you can retrieve newrelic.Transaction
instances with newrelic.FromContext
.
func main() {
lis, err := net.Listen("tcp", ":3000")
if err != nil {
panic(err)
}
// Initiailze a `newrelic.Appliation`
cfg := newrelic.NewConfig("your_app","your_license_key")
nrapp, err := newrelic.NewApplication(cfg)
if err != nil {
panic(err)
}
s := grpc.NewServer(
// Create a `stats.Handler` from `newrelic.Application`
grpc.StatsHandler(nrgrpc.NewServerStatsHandler(nrapp)),
)
// Register server implementations
s.Serve(lis)
}
func main() {
// Initiailze a `newrelic.Appliation`
nrapp, err := newrelic.NewApplication(newrelic.Config{
AppName: "your_app",
License: "your_license_key",
})
if err != nil {
panic(err)
}
// Create a `grpc.ClientConn` with `stats.Handler`
conn, err := grpc.Dial(
":3000",
grpc.WithInsecure(),
grpc.WithStatsHandler(nrgrpc.NewClientStatsHandler()),
)
if err != nil {
panic(err)
}
// Register http handler using `https://godoc.org/github.com/newrelic/go-agent.WrapHandleFunc`.
// This wrapper sets `newrelic.Transaction` into the `http.Request`'s context.
nrhttp.WrapHandleFunc(app, "/foo", func(w http.ResponseWriter, r *http.Request) {
resp, err := NewFooServiceClient.BarCall(r.Context(), &BarRequest{})
// ...
})
}