github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrmicro/nrmicro_doc.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 // Package nrmicro instruments https://github.com/micro/go-micro. 5 // 6 // This package can be used to instrument Micro Servers, Clients, Producers, 7 // and Subscribers. 8 // 9 // Micro Servers 10 // 11 // To instrument a Micro Server, use the `micro.WrapHandler` 12 // (https://godoc.org/github.com/micro/go-micro#WrapHandler) option with 13 // `nrmicro.HandlerWrapper` and your `newrelic.Application` and pass it to the 14 // `micro.NewService` method. Example: 15 // 16 // cfg := newrelic.NewConfig("Micro Server", os.Getenv("NEW_RELIC_LICENSE_KEY")) 17 // app, _ := newrelic.NewApplication(cfg) 18 // service := micro.NewService( 19 // micro.WrapHandler(nrmicro.HandlerWrapper(app)), 20 // ) 21 // 22 // Alternatively, use the `server.WrapHandler` 23 // (https://godoc.org/github.com/micro/go-micro/server#WrapHandler) option with 24 // `nrmicro.HandlerWrapper` and your `newrelic.Application` and pass it to the 25 // `server.NewServer` method. Example: 26 // 27 // cfg := newrelic.NewConfig("Micro Server", os.Getenv("NEW_RELIC_LICENSE_KEY")) 28 // app, _ := newrelic.NewApplication(cfg) 29 // svr := server.NewServer( 30 // server.WrapHandler(nrmicro.HandlerWrapper(app)), 31 // ) 32 // 33 // If more than one wrapper is passed to `micro.WrapHandler` or 34 // `server.WrapHandler` as a list, be sure that the `nrmicro.HandlerWrapper` is 35 // first in this list. 36 // 37 // This wrapper creates transactions for inbound calls. The transaction is 38 // added to the call context and can be accessed in your method handlers using 39 // `newrelic.FromContext` 40 // (https://godoc.org/github.com/newrelic/go-agent#FromContext). 41 // 42 // When an error is returned and it is of type Micro `errors.Error` 43 // (https://godoc.org/github.com/micro/go-micro/errors#Error), the error that 44 // is recorded is based on the HTTP response code (found in the Code field). 45 // Values above 400 or below 100 that are not in the IgnoreStatusCodes 46 // (https://godoc.org/github.com/newrelic/go-agent#Config) configuration list 47 // are recorded as errors. A 500 response code and corresponding error is 48 // recorded when the error is of any other type. A 200 response code is 49 // recorded if no error is returned. 50 // 51 // Full server example: 52 // https://github.com/newrelic/go-agent/blob/master/_integrations/nrmicro/example/server/server.go 53 // 54 // Micro Clients 55 // 56 // There are three different ways to instrument a Micro Client and create 57 // External segments for `Call`, `Publish`, and `Stream` methods. 58 // 59 // No matter which way the Micro `client.Client` is wrapped, all calls to 60 // `Client.Call`, `Client.Publish`, or `Client.Stream` must be done with a 61 // context which contains a `newrelic.Transaction`. 62 // 63 // ctx = newrelic.NewContext(ctx, txn) 64 // err := cli.Call(ctx, req, &rsp) 65 // 66 // 1. The first option is to wrap the `Call`, `Publish`, and `Stream` methods 67 // on a client using the `micro.WrapClient` 68 // (https://godoc.org/github.com/micro/go-micro#WrapClient) option with 69 // `nrmicro.ClientWrapper` and pass it to the `micro.NewService` method. If 70 // more than one wrapper is passed to `micro.WrapClient`, ensure that the 71 // `nrmicro.ClientWrapper` is the first in the list. `ExternalSegment`s will be 72 // created each time a `Call` or `Stream` method is called on the 73 // client. `MessageProducerSegment`s will be created each time a `Publish` 74 // method is called on the client. Example: 75 // 76 // service := micro.NewService( 77 // micro.WrapClient(nrmicro.ClientWrapper()), 78 // ) 79 // cli := service.Client() 80 // 81 // It is also possible to use the `client.Wrap` 82 // (https://godoc.org/github.com/micro/go-micro/client#Wrap) option with 83 // `nrmicro.ClientWrapper` and pass it to the `client.NewClient` method to 84 // achieve the same result. 85 // 86 // cli := client.NewClient( 87 // client.Wrap(nrmicro.ClientWrapper()), 88 // ) 89 // 90 // 2. The second option is to wrap just the `Call` method on a client using the 91 // `micro.WrapCall` (https://godoc.org/github.com/micro/go-micro#WrapCall) 92 // option with `nrmicro.CallWrapper` and pass it to the `micro.NewService` 93 // method. If more than one wrapper is passed to `micro.WrapCall`, ensure that 94 // the `nrmicro.CallWrapper` is the first in the list. External segments will 95 // be created each time a `Call` method is called on the client. Example: 96 // 97 // service := micro.NewService( 98 // micro.WrapCall(nrmicro.CallWrapper()), 99 // ) 100 // cli := service.Client() 101 // 102 // It is also possible to use the `client.WrapCall` 103 // (https://godoc.org/github.com/micro/go-micro/client#WrapCall) option with 104 // `nrmicro.CallWrapper` and pass it to the `client.NewClient` method to 105 // achieve the same result. 106 // 107 // cli := client.NewClient( 108 // client.WrapCall(nrmicro.CallWrapper()), 109 // ) 110 // 111 // 3. The third option is to wrap the Micro Client directly using 112 // `nrmicro.ClientWrapper`. `ExternalSegment`s will be created each time a 113 // `Call` or `Stream` method is called on the client. 114 // `MessageProducerSegment`s will be created each time a `Publish` method is 115 // called on the client. Example: 116 // 117 // cli := client.NewClient() 118 // cli = nrmicro.ClientWrapper()(cli) 119 // 120 // Full client example: 121 // https://github.com/newrelic/go-agent/blob/master/_integrations/nrmicro/example/client/client.go 122 // 123 // Micro Producers 124 // 125 // To instrument a Micro Producer, wrap the Micro Client using the 126 // `nrmico.ClientWrapper` as described in option 1 or 3 above. 127 // `MessageProducerSegment`s will be created each time a `Publish` method is 128 // called on the client. Be sure the context passed to the `Publish` method 129 // contains a `newrelic.Transaction`. 130 // 131 // service := micro.NewService( 132 // micro.WrapClient(nrmicro.ClientWrapper()), 133 // ) 134 // cli := service.Client() 135 // 136 // // Add the transaction to the context 137 // ctx := newrelic.NewContext(context.Background(), txn) 138 // msg := cli.NewMessage("my.example.topic", "hello world") 139 // err := cli.Publish(ctx, msg) 140 // 141 // Full Publisher/Subscriber example: 142 // https://github.com/newrelic/go-agent/blob/master/_integrations/nrmicro/example/pubsub/main.go 143 // 144 // Micro Subscribers 145 // 146 // To instrument a Micro Subscriber use the `micro.WrapSubscriber` 147 // (https://godoc.org/github.com/micro/go-micro#WrapSubscriber) option with 148 // `nrmicro.SubscriberWrapper` and your `newrelic.Application` and pass it to 149 // the `micro.NewService` method. Example: 150 // 151 // cfg := newrelic.NewConfig("Micro Subscriber", os.Getenv("NEW_RELIC_LICENSE_KEY")) 152 // app, _ := newrelic.NewApplication(cfg) 153 // service := micro.NewService( 154 // micro.WrapSubscriber(nrmicro.SubscriberWrapper(app)), 155 // ) 156 // 157 // Alternatively, use the `server.WrapSubscriber` 158 // (https://godoc.org/github.com/micro/go-micro/server#WrapSubscriber) option 159 // with `nrmicro.SubscriberWrapper` and your `newrelic.Application` and pass it 160 // to the `server.NewServer` method. Example: 161 // 162 // cfg := newrelic.NewConfig("Micro Subscriber", os.Getenv("NEW_RELIC_LICENSE_KEY")) 163 // app, _ := newrelic.NewApplication(cfg) 164 // svr := server.NewServer( 165 // server.WrapSubscriber(nrmicro.SubscriberWrapper(app)), 166 // ) 167 // 168 // If more than one wrapper is passed to `micro.WrapSubscriber` or 169 // `server.WrapSubscriber` as a list, be sure that the `nrmicro.SubscriberWrapper` is 170 // first in this list. 171 // 172 // This wrapper creates background transactions for inbound calls. The 173 // transaction is added to the subscriber context and can be accessed in your 174 // subscriber handlers using `newrelic.FromContext`. 175 // 176 // If a Subscriber returns an error, it will be recorded and reported. 177 // 178 // Full Publisher/Subscriber example: 179 // https://github.com/newrelic/go-agent/blob/master/_integrations/nrmicro/example/pubsub/main.go 180 package nrmicro 181 182 import "github.com/newrelic/go-agent/internal" 183 184 func init() { internal.TrackUsage("integration", "framework", "micro") }