github.com/lulzWill/go-agent@v2.1.2+incompatible/README.md (about) 1 # New Relic Go Agent [![GoDoc](https://godoc.org/github.com/lulzWill/go-agent?status.svg)](https://godoc.org/github.com/lulzWill/go-agent) 2 3 ## Description 4 5 The New Relic Go Agent allows you to monitor your Go applications with New 6 Relic. It helps you track transactions, outbound requests, database calls, and 7 other parts of your Go application's behavior and provides a running overview of 8 garbage collection, goroutine activity, and memory use. 9 10 All pull requests will be reviewed by the New Relic product team. Any questions or issues should be directed to our [support 11 site](http://support.newrelic.com/) or our [community 12 forum](http://forum.newrelic.com). 13 14 ## Requirements 15 16 Go 1.3+ is required, due to the use of http.Client's Timeout field. 17 18 Linux, OS X, and Windows (Vista, Server 2008 and later) are supported. 19 20 ## Getting Started 21 22 Here are the basic steps to instrumenting your application. For more 23 information, see [GUIDE.md](GUIDE.md). 24 25 #### Step 0: Installation 26 27 Installing the Go Agent is the same as installing any other Go library. The 28 simplest way is to run: 29 30 ``` 31 go get github.com/lulzWill/go-agent 32 ``` 33 34 Then import the `github.com/lulzWill/go-agent` package in your application. 35 36 #### Step 1: Create a Config and an Application 37 38 In your `main` function or an `init` block: 39 40 ```go 41 config := newrelic.NewConfig("Your Application Name", "__YOUR_NEW_RELIC_LICENSE_KEY__") 42 app, err := newrelic.NewApplication(config) 43 ``` 44 45 [more info](GUIDE.md#config-and-application), [application.go](application.go), 46 [config.go](config.go) 47 48 #### Step 2: Add Transactions 49 50 Transactions time requests and background tasks. Use `WrapHandle` and 51 `WrapHandleFunc` to create transactions for requests handled by the `http` 52 standard library package. 53 54 ```go 55 http.HandleFunc(newrelic.WrapHandleFunc(app, "/users", usersHandler)) 56 ``` 57 58 Alternatively, create transactions directly using the application's 59 `StartTransaction` method: 60 61 ```go 62 txn := app.StartTransaction("myTxn", optionalResponseWriter, optionalRequest) 63 defer txn.End() 64 ``` 65 66 [more info](GUIDE.md#transactions), [transaction.go](transaction.go) 67 68 #### Step 3: Instrument Segments 69 70 Segments show you where time in your transactions is being spent. At the 71 beginning of important functions, add: 72 73 ```go 74 defer newrelic.StartSegment(txn, "mySegmentName").End() 75 ``` 76 77 [more info](GUIDE.md#segments), [segments.go](segments.go) 78 79 ## Runnable Example 80 81 [examples/server/main.go](./examples/server/main.go) is an example that will 82 appear as "Example App" in your New Relic applications list. To run it: 83 84 ``` 85 env NEW_RELIC_LICENSE_KEY=__YOUR_NEW_RELIC_LICENSE_KEY__LICENSE__ \ 86 go run examples/server/main.go 87 ``` 88 89 Some endpoints exposed are [http://localhost:8000/](http://localhost:8000/) 90 and [http://localhost:8000/notice_error](http://localhost:8000/notice_error) 91 92 93 ## Basic Example 94 95 Before Instrumentation 96 97 ```go 98 package main 99 100 import ( 101 "io" 102 "net/http" 103 ) 104 105 func helloHandler(w http.ResponseWriter, r *http.Request) { 106 io.WriteString(w, "hello, world") 107 } 108 109 func main() { 110 http.HandleFunc("/", helloHandler) 111 http.ListenAndServe(":8000", nil) 112 } 113 ``` 114 115 After Instrumentation 116 117 ```go 118 package main 119 120 import ( 121 "fmt" 122 "io" 123 "net/http" 124 "os" 125 126 "github.com/lulzWill/go-agent" 127 ) 128 129 func helloHandler(w http.ResponseWriter, r *http.Request) { 130 io.WriteString(w, "hello, world") 131 } 132 133 func main() { 134 // Create a config. You need to provide the desired application name 135 // and your New Relic license key. 136 cfg := newrelic.NewConfig("Example App", "__YOUR_NEW_RELIC_LICENSE_KEY__") 137 138 // Create an application. This represents an application in the New 139 // Relic UI. 140 app, err := newrelic.NewApplication(cfg) 141 if err != nil { 142 fmt.Println(err) 143 os.Exit(1) 144 } 145 146 // Wrap helloHandler. The performance of this handler will be recorded. 147 http.HandleFunc(newrelic.WrapHandleFunc(app, "/", helloHandler)) 148 http.ListenAndServe(":8000", nil) 149 } 150 ``` 151 152 ## Support 153 154 You can find more detailed documentation [in the guide](GUIDE.md) and on 155 [the New Relic Documentation site](https://docs.newrelic.com/docs/agents/go-agent). 156 157 If you can't find what you're looking for there, reach out to us on our [support 158 site](http://support.newrelic.com/) or our [community 159 forum](http://forum.newrelic.com) and we'll be happy to help you. 160 161 Find a bug? Contact us via [support.newrelic.com](http://support.newrelic.com/), 162 or email support@newrelic.com.