github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/README.md (about) 1 # IBM Instana Go Tracer 2 3 [![Build Status](https://circleci.com/gh/instana/go-sensor/tree/main.svg?style=svg)](https://circleci.com/gh/instana/go-sensor/tree/main) 4 [![PkgGoDev](https://pkg.go.dev/badge/github.com/instana/go-sensor)][pkg.go.dev] 5 [![OpenTracing](https://img.shields.io/badge/OpenTracing-enabled-blue.svg)](http://opentracing.io) 6 [![Go Report Card](https://goreportcard.com/badge/github.com/instana/go-sensor)](https://goreportcard.com/report/github.com/instana/go-sensor) 7 8 The IBM Instana Go Tracer is an SDK that collects traces, metrics, logs and provides profiling for Go applications. The tracer is part of the [IBM Instana Observability](https://www.ibm.com/products/instana) tool set. 9 10 ## Compatibility 11 12 |Tracer Version | Go version | 13 |-----|-----| 14 |v1.62.0 and higher|v1.21.0 and higher| 15 |v1.47.0 to v1.61.0|v1.13.0 and higher| 16 |Less than v1.47.0|v1.9.0 and higher| 17 18 > [!NOTE] 19 > Make sure to always use the latest version of the tracer, as it provides new features, improvements, security updates and fixes. 20 21 > [!IMPORTANT] 22 > Since v1.53.0, the Go Tracer uses fsm v1.0.1 internally. Customers using fsm prior to v1 in their projects will need to update it to v1. 23 24 ## Installation 25 26 To add the tracer to your project, run: 27 28 ```bash 29 go get -u github.com/instana/go-sensor@latest 30 ``` 31 32 > [!NOTE] 33 > As a good practice, add this command to your CI pipeline or your automated tool before building the application to keep the tracer up to date. 34 35 ## Usage 36 37 ### Initial Setup 38 39 Once the tracer is added to the project, import the package into the entrypoint file of your application: 40 41 ```go 42 import ( 43 ... 44 instana "github.com/instana/go-sensor" 45 ) 46 ``` 47 48 Create a reference to the collector and initialize it with a service name: 49 50 ```go 51 var ( 52 ... 53 col instana.TracerLogger 54 ) 55 56 func init() { 57 ... 58 col = instana.InitCollector(&instana.Options{ 59 Service: "My app", 60 }) 61 } 62 ``` 63 64 > [!NOTE] 65 > The tracer expects the Instana Agent to be up and running in the default port 42699. You can change the port with the environment variable ``INSTANA_AGENT_PORT``. 66 67 > [!NOTE] 68 > For non default options, like the Agent host and port, the tracer can be configured either via SDK options, environment variables or Agent options. 69 70 ### Collecting Metrics 71 72 Once the collector has been initialized with `instana.InitCollector`, application metrics such as memory, CPU consumption, active goroutine count etc will be automatically collected and reported to the Agent without further actions or configurations to the SDK. 73 This data is then already available in the dashboard. 74 75 ### Tracing Calls 76 77 Let's collect traces of calls received by an HTTP server. 78 79 Before any changes, your code should look something like this: 80 81 ```go 82 // endpointHandler is the standard http.Handler function 83 http.HandleFunc("/endpoint", endpointHandler) 84 85 log.Fatal(http.ListenAndServe(":9090", nil)) 86 ``` 87 88 Wrap the `endpointHandler` function with `instana.TracingHandlerFunc`. Now your code should look like this: 89 90 ```go 91 // endpointHandler is now wrapped by `instana.TracingHandlerFunc` 92 http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", endpointHandler)) 93 94 log.Fatal(http.ListenAndServe(":9090", nil)) 95 ``` 96 97 When running the application, every time `/endpoint` is called, the tracer will collect this data and send it to the Instana Agent. 98 You can monitor traces to this endpoint in the Instana UI. 99 100 ### Profiling 101 102 Unlike metrics, profiling needs to be enabled with the `EnableAutoProfile` option, as seen here: 103 104 ```go 105 col = instana.InitCollector(&instana.Options{ 106 Service: "My app", 107 EnableAutoProfile: true, 108 }) 109 ``` 110 111 You should be able to see your application profiling in the Instana UI under Analytics/Profiles. 112 113 ### Logging 114 115 In terms of logging, the SDK provides two distinct logging features: 116 117 1. Traditional logging, that is, logs reported to the standard output, usually used for debugging purposes 118 1. Instana logs, a feature that allows customers to report logs to the dashboard under Analytics/Logs 119 120 #### Traditional Logging 121 122 Many logs are provided by the SDK, usually prefixed with "INSTANA" and are useful to understand what the tracer is doing underneath. It can also be used for debugging and troubleshoot reasons. 123 Customers can also provide logs by calling one of the following: [Collector.Info()](https://pkg.go.dev/github.com/instana/go-sensor#Collector.Info), [Collector.Warn()](https://pkg.go.dev/github.com/instana/go-sensor#Collector.Warn), [Collector.Error()](https://pkg.go.dev/github.com/instana/go-sensor#Collector.Error), [Collector.Debug()](https://pkg.go.dev/github.com/instana/go-sensor#Collector.Debug). You can setup the log level via options or the `INSTANA_LOG_LEVEL` environment variable. 124 125 You can find detailed information in the [Instana documentation](https://www.ibm.com/docs/en/instana-observability/current?topic=technologies-monitoring-go#tracers-logs). 126 127 #### Instana Logs 128 129 Instana Logs are spans of the type `log.go` that are rendered in a special format in the dashboard. 130 You can create logs and report them to the agent or attach them as children of an existing span. 131 132 The code snippet below shows how to create logs and send them to the agent: 133 134 ```go 135 col := instana.InitCollector(&instana.Options{ 136 Service: "My Go App", 137 }) 138 139 col.StartSpan("log.go", []ot.StartSpanOption{ 140 ot.Tags{ 141 "log.level": "error", // available levels: info, warn, error, debug 142 "log.message": "error from log.go span", 143 }, 144 }...).Finish() // make sure to "finish" the span, so it's sent to the agent 145 ``` 146 147 This log can then be visualized in the dashboard under Analytics/Logs. You can add a filter by service name. In our example, the service name is "My Go App". 148 149 ### Opt-in Exit Spans 150 151 Go tracer support the opt-in feature for the exit spans. When enabled, the collector can start capturing exit spans, even without an entry span. This capability is particularly useful for scenarios like cronjobs and other background tasks, enabling the users to tailor the tracing according to their specific requirements. By setting the `INSTANA_ALLOW_ROOT_EXIT_SPAN` variable, users can choose whether the tracer should start a trace with an exit span or not. The environment variable can have 2 values. (1: Tracer should record exit spans for the outgoing calls, when it has no active entry span. 0 or any other values: Tracer should not start a trace with an exit span). 152 153 ```bash 154 export INSTANA_ALLOW_ROOT_EXIT_SPAN=1 155 ``` 156 157 ### Complete Example 158 159 [Basic Usage](./example/basic_usage/main.go) 160 ```go 161 package main 162 163 import ( 164 "log" 165 "net/http" 166 167 instana "github.com/instana/go-sensor" 168 ) 169 170 func main() { 171 col := instana.InitCollector(&instana.Options{ 172 Service: "Basic Usage", 173 EnableAutoProfile: true, 174 }) 175 176 http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", func(w http.ResponseWriter, r *http.Request) { 177 w.WriteHeader(http.StatusOK) 178 })) 179 180 log.Fatal(http.ListenAndServe(":7070", nil)) 181 } 182 ``` 183 184 ### Wrapping up 185 186 Let's quickly summarize what we have seen so far: 187 188 1. We learned how to install, import and initialize the Instana Go Tracer. 189 1. Once the tracer is initialized, application metrics are collected out of the box. 190 1. Application profiling can be enabled via the `EnableAutoProfile` option. 191 1. Tracing incoming HTTP requests by wrapping the Go standard library `http.Handler` with `instana.TracingHandlerFunc`. 192 193 With this knowledge it's already possible to make your Go application traceable by our SDK. 194 But there is much more you can do to enhance tracing for your application. 195 196 The basic functionality covers tracing for the following standard Go features: 197 198 1. HTTP incoming requests 199 1. HTTP outgoing requests 200 1. SQL drivers 201 202 As we already covered HTTP incoming requests, we suggest that you understand how to collect data from HTTP outgoing requests and SQL driver databases. 203 204 Another interesting feature is the usage of additional packages located under [instrumentation](./instrumentation/). Each of these packages provide tracing for specific Go packages like the AWS SDK, Gorm and Fiber. 205 206 ## What's Next 207 208 1. [Tracer Options](docs/options.md) 209 1. [Tracing HTTP Outgoing Requests](docs/roundtripper.md) 210 1. [Tracing SQL Driver Databases](docs/sql.md) 211 1. [Tracing Other Go Packages](docs/other_packages.md) 212 1. [Instrumenting Code Manually](docs/manual_instrumentation.md) 213 214 <!-- Links section --> 215 216 [godoc]: https://pkg.go.dev/github.com/instana/go-sensor/?tab=doc#pkg-examples 217 [pkg.go.dev]: https://pkg.go.dev/github.com/instana/go-sensor 218 [docs.autoprofile]: https://www.ibm.com/docs/en/obi/current?topic=technologies-monitoring-go#instana-autoprofile%E2%84%A2 219 [docs.configuration]: https://www.ibm.com/docs/en/obi/current?topic=go-collector-configuration 220 [docs.installation]: https://www.ibm.com/docs/en/obi/current?topic=go-collector-installation 221 [docs.howto.configuration]: https://www.ibm.com/docs/en/obi/current?topic=go-collector-common-operations#configuration 222 [docs.howto.instrumentation]: https://www.ibm.com/docs/en/obi/current?topic=go-collector-common-operations#instrumentation 223 [instana.DefaultOptions]: https://pkg.go.dev/github.com/instana/go-sensor#DefaultOptions