|
1 |
| -# Instana Go Collector |
| 1 | +# IBM Instana Go Tracer |
2 | 2 |
|
3 |
| - |
4 |
| - |
5 |
| -[](https://circleci.com/gh/instana/go-sensor/tree/main) |
| 3 | +[](https://circleci.com/gh/instana/go-sensor/tree/main) |
6 | 4 | [][pkg.go.dev]
|
7 | 5 | [](http://opentracing.io)
|
8 | 6 | [](https://goreportcard.com/report/github.com/instana/go-sensor)
|
9 | 7 |
|
10 |
| -The Go Collector is a runtime metrics collector, code execution tracer and profiler for applications and services written in Go. This module |
11 |
| -is a part of [Instana](https://instana.com) APM solution. |
| 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 | +|Up to v1.46.0|v1.9.0 and higher| |
| 15 | +|v1.47.0 and higher|v1.13.0 and higher| |
12 | 16 |
|
13 |
| -## Dependency |
14 |
| -- Since version 1.47 the Go Collector requires Go version 1.13 or later. |
15 |
| -- Since version 1.53.0, the Go Collector uses fsm v1.0.1 internally. Customers using fsm version prior to v1 in their projects may face compilation issues and will need to update the fsm version to v1. |
| 17 | +> [!NOTE] |
| 18 | +> Make sure to always use the latest version of the tracer, as it provides new features, improvements, security updates and fixes. |
| 19 | +
|
| 20 | +> [!IMPORTANT] |
| 21 | +> 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. |
16 | 22 |
|
17 | 23 | ## Installation
|
18 | 24 |
|
19 |
| -To add Instana Go Collector to your service run: |
| 25 | +To add the tracer to your project, run: |
20 | 26 |
|
21 | 27 | ```bash
|
22 |
| -$ go get github.com/instana/go-sensor |
| 28 | +go get -u github.com/instana/go-sensor@latest |
23 | 29 | ```
|
24 | 30 |
|
25 |
| -You might also consider installing [supplemental modules](https://www.ibm.com/docs/en/obi/current?topic=technologies-monitoring-go#supported-frameworks-and-libraries) |
26 |
| -that provide instrumentation for most popular 3rd-party packages. |
| 31 | +> [!NOTE] |
| 32 | +> 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. |
27 | 33 |
|
28 |
| -Please refer to [Instana Go Collector documentation][docs.installation] for further details on how to activate Go Collector and use it to |
29 |
| -instrument your application code. |
| 34 | +## Usage |
30 | 35 |
|
31 |
| -## Configuration |
| 36 | +### Initial Setup |
32 | 37 |
|
33 |
| -The Go Collector accepts both configuration from within the application code and via environment variables. The values provided via enironment |
34 |
| -variables take precedence. In case is no specific configuration provided, the values returned by |
35 |
| -[instana.DefaultOptions()][instana.DefaultOptions] will be used. |
| 38 | +Once the tracer is added to the project, import the package into the entrypoint file of your application: |
36 | 39 |
|
37 |
| -Please refer to [Go Collector Configuration page][docs.configuration] for detailed instructions. There is also the |
38 |
| -[Go Collector How To page][docs.howto.configuration] that covers the most common configuration use cases. |
| 40 | +```go |
| 41 | +import ( |
| 42 | + ... |
| 43 | + instana "github.com/instana/go-sensor" |
| 44 | +) |
| 45 | +``` |
39 | 46 |
|
40 |
| -## Usage |
| 47 | +Create a reference to the collector and initialize it with a service name: |
41 | 48 |
|
42 |
| -In order to trace the code execution, a few minor changes to your app's source code is needed. Please check the [examples section](#examples) |
43 |
| -and the [Go Collector How To guide][docs.howto.instrumentation] to learn about common instrumentation patterns. |
| 49 | +```go |
| 50 | +var ( |
| 51 | + ... |
| 52 | + col instana.TracerLogger |
| 53 | +) |
44 | 54 |
|
45 |
| -## Features |
| 55 | +func init() { |
| 56 | + ... |
| 57 | + col = instana.InitCollector(&instana.Options{ |
| 58 | + Service: "My app", |
| 59 | + }) |
| 60 | +} |
| 61 | +``` |
46 | 62 |
|
47 |
| -### Runtime metrics collection |
| 63 | +> [!NOTE] |
| 64 | +> 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``. |
48 | 65 |
|
49 |
| -Once [initialized](https://www.ibm.com/docs/en/obi/current?topic=go-collector-common-operations#how-to-initialize-go-collector), the Go Collector starts automatically |
50 |
| -collecting and sending the following runtime metrics to Instana in background: |
| 66 | +> [!NOTE] |
| 67 | +> For non default options, like the Agent host and port, the tracer can be configured either via SDK options, environment variables or Agent options. |
51 | 68 |
|
52 |
| -* Memory usage |
53 |
| -* Heap usage |
54 |
| -* GC activity |
55 |
| -* Goroutines |
| 69 | +### Collecting Metrics |
56 | 70 |
|
57 |
| -### Code execution tracing |
| 71 | +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. |
| 72 | +This data is then already available in the dashboard. |
58 | 73 |
|
59 |
| -Instana Go Collector provides an API to [instrument][docs.howto.instrumentation] function and method calls from within the application code |
60 |
| -to trace its execution. |
| 74 | +### Tracing Calls |
61 | 75 |
|
62 |
| -The core `github.com/instana/go-sensor` package is shipped with instrumentation wrappers for the standard library, including HTTP client and |
63 |
| -server, as well as SQL database drivers compatible with `database/sql`. There are also supplemental |
64 |
| -[instrumentation modules](https://www.ibm.com/docs/en/obi/current?topic=technologies-monitoring-go#supported-frameworks-and-libraries) provide code wrappers to instrument |
65 |
| -the most popular 3rd-party libraries. |
| 76 | +Let's collect traces of calls received by an HTTP server. |
66 | 77 |
|
67 |
| -Please check the [examples section](#examples) and the [Go Collector How To guide][docs.howto.instrumentation] to learn about common |
68 |
| -instrumentation patterns. |
| 78 | +Before any changes, your code should look something like this: |
69 | 79 |
|
70 |
| -#### OpenTracing |
| 80 | +```go |
| 81 | +// endpointHandler is the standard http.Handler function |
| 82 | +http.HandleFunc("/endpoint", endpointHandler) |
71 | 83 |
|
72 |
| -Instana Go Collector provides an interface compatible with [`github.com/opentracing/opentracing-go`](https://github.com/opentracing/opentracing-go) and thus can be used as a global tracer. However, the recommended approach is to use the Instana wrapper packages/functions [provided](./instrumentation) in the library. They set up a lot of semantic information which helps Instana get the best picture of the application possible. Sending proper tags is especially important when it comes to correlating calls to infrastructure and since they are strings mostly, there is a large room for making a mistake. |
| 84 | +log.Fatal(http.ListenAndServe(":9090", nil)) |
| 85 | +``` |
73 | 86 |
|
74 |
| -The Go Collector will remap OpenTracing HTTP headers into Instana headers, so parallel use with some other OpenTracing model is not possible. The Instana tracer is based on the OpenTracing Go basictracer with necessary modifications to map to the Instana tracing model. |
| 87 | +Wrap the `endpointHandler` function with `instana.TracingHandlerFunc`. Now your code should look like this: |
| 88 | + |
| 89 | +```go |
| 90 | +// endpointHandler is now wrapped by `instana.TracingHandlerFunc` |
| 91 | +http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", endpointHandler)) |
| 92 | + |
| 93 | +log.Fatal(http.ListenAndServe(":9090", nil)) |
| 94 | +``` |
| 95 | + |
| 96 | +When running the application, every time `/endpoint` is called, the tracer will collect this data and send it to the Instana Agent. |
| 97 | +You can monitor traces to this endpoint in the Instana UI. |
| 98 | + |
| 99 | +### Profiling |
| 100 | + |
| 101 | +Unlike metrics, profiling needs to be enabled with the `EnableAutoProfile` option, as seen here: |
| 102 | + |
| 103 | +```go |
| 104 | +col = instana.InitCollector(&instana.Options{ |
| 105 | + Service: "My app", |
| 106 | + EnableAutoProfile: true, |
| 107 | +}) |
| 108 | +``` |
75 | 109 |
|
76 |
| -### Trace continuation and propagation |
| 110 | +You should be able to see your application profiling in the Instana UI under Analytics/Profiles. |
77 | 111 |
|
78 |
| -Instana Go Collector ensures that application trace context will continued and propagated beyond the service boundaries using various |
79 |
| -methods, depending on the technology being used. Alongside with Instana-specific HTTP headers or message attributes, a number of open |
80 |
| -standards are supported, such as W3C Trace Context and OpenTelemetry. |
| 112 | +### Logging |
81 | 113 |
|
82 |
| -#### W3C Trace Context & OpenTelemetry |
| 114 | +In terms of logging, the SDK provides two distinct logging features: |
83 | 115 |
|
84 |
| -The instrumentation wrappers provided with Go Collector automatically inject and extract trace context provided via W3C Trace Context HTTP |
85 |
| -headers. |
| 116 | +1. Traditional logging, that is, logs reported to the standard output, usually used for debugging purposes |
| 117 | +1. Instana logs, a feature that allows customers to report logs to the dashboard under Analytics/Logs |
86 | 118 |
|
87 |
| -### Continuous profiling |
| 119 | +#### Traditional Logging |
88 | 120 |
|
89 |
| -[Instana AutoProfile™][docs.autoprofile] generates and reports process profiles to Instana. Unlike development-time and on-demand profilers, |
90 |
| -where a user must manually initiate profiling, AutoProfile™ automatically schedules and continuously performs profiling appropriate for |
91 |
| -critical production environments. |
| 121 | +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. |
| 122 | +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. |
| 123 | + |
| 124 | +You can find detailed information in the [Instana documentation](https://www.ibm.com/docs/en/instana-observability/current?topic=technologies-monitoring-go#tracers-logs). |
| 125 | + |
| 126 | +#### Instana Logs |
| 127 | + |
| 128 | +Instana Logs are spans of the type `log.go` that are rendered in a special format in the dashboard. |
| 129 | +You can create logs and report them to the agent or attach them as children of an existing span. |
| 130 | + |
| 131 | +The code snippet below shows how to create logs and send them to the agent: |
| 132 | + |
| 133 | +```go |
| 134 | +col := instana.InitCollector(&instana.Options{ |
| 135 | + Service: "My Go App", |
| 136 | +}) |
| 137 | + |
| 138 | +col.StartSpan("log.go", []ot.StartSpanOption{ |
| 139 | + ot.Tags{ |
| 140 | + "log.level": "error", // available levels: info, warn, error, debug |
| 141 | + "log.message": "error from log.go span", |
| 142 | + }, |
| 143 | +}...).Finish() // make sure to "finish" the span, so it's sent to the agent |
| 144 | +``` |
| 145 | + |
| 146 | +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". |
| 147 | + |
| 148 | +### Complete Example |
| 149 | + |
| 150 | +[Basic Usage](./example/basic_usage/main.go) |
| 151 | +```go |
| 152 | +package main |
| 153 | + |
| 154 | +import ( |
| 155 | + "log" |
| 156 | + "net/http" |
| 157 | + |
| 158 | + instana "github.com/instana/go-sensor" |
| 159 | +) |
| 160 | + |
| 161 | +func main() { |
| 162 | + col := instana.InitCollector(&instana.Options{ |
| 163 | + Service: "Basic Usage", |
| 164 | + EnableAutoProfile: true, |
| 165 | + }) |
| 166 | + |
| 167 | + http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", func(w http.ResponseWriter, r *http.Request) { |
| 168 | + w.WriteHeader(http.StatusOK) |
| 169 | + })) |
| 170 | + |
| 171 | + log.Fatal(http.ListenAndServe(":7070", nil)) |
| 172 | +} |
| 173 | +``` |
92 | 174 |
|
93 |
| -Please refer to the [Instana Go Collector docs](https://www.ibm.com/docs/en/obi/current?topic=go-collector-common-operations#instana-autoprofile%E2%84%A2) to learn how to activate and |
94 |
| -use continuous profiling for your applications and services. |
| 175 | +### Wrapping up |
95 | 176 |
|
96 |
| -### Sending custom events |
| 177 | +Let's quickly summarize what we have seen so far: |
97 | 178 |
|
98 |
| -The Go Collector, be it instantiated explicitly or implicitly through the tracer, provides a simple wrapper API to send events to Instana as described in [its documentation](https://www.ibm.com/docs/en/obi/current?topic=integrations-sdks-apis). |
| 179 | +1. We learned how to install, import and initialize the Instana Go Tracer. |
| 180 | +1. Once the tracer is initialized, application metrics are collected out of the box. |
| 181 | +1. Application profiling can be enabled via the `EnableAutoProfile` option. |
| 182 | +1. Tracing incoming HTTP requests by wrapping the Go standard library `http.Handler` with `instana.TracingHandlerFunc`. |
99 | 183 |
|
100 |
| -To learn more, see the [Events API](./EventAPI.md) document in this repository. |
| 184 | +With this knowledge it's already possible to make your Go application traceable by our SDK. |
| 185 | +But there is much more you can do to enhance tracing for your application. |
101 | 186 |
|
102 |
| -## Examples |
| 187 | +The basic functionality covers tracing for the following standard Go features: |
103 | 188 |
|
104 |
| -Following examples are included in the `example` folder: |
| 189 | +1. HTTP incoming requests |
| 190 | +1. HTTP outgoing requests |
| 191 | +1. SQL drivers |
105 | 192 |
|
106 |
| -* [Greeter](./example/http-database-greeter) - an instrumented HTTP server that queries a database |
107 |
| -* [Doubler](./example/kafka-producer-consumer) - an instrumented Kafka processor, that consumes and produces messages |
108 |
| -* [Event](./example/event) - Demonstrates usage of the Events API |
109 |
| -* [Autoprofile](./example/autoprofile) - Demonstrates usage of the AutoProfile™ |
110 |
| -* [OpenTracing](./example/opentracing) - an example of usage of Instana tracer in an app instrumented with OpenTracing |
111 |
| -* [gRPC](./example/grpc-client-server) - an example of usage of Instana tracer in an app instrumented with gRPC |
112 |
| -* [Gin](./example/gin) - an example of usage of Instana tracer instrumenting a [`Gin`](github.com/gin-gonic/gin) application |
113 |
| -* [httprouter](./example/httprouter) - an example of usage of Instana tracer instrumenting a [`github.com/julienschmidt/httprouter`](https://github.com/julienschmidt/httprouter) router |
| 193 | +As we already covered HTTP incoming requests, we suggest that you understand how to collect data from HTTP outgoing requests and SQL driver databases. |
114 | 194 |
|
115 |
| -For more examples please consult the [godoc][godoc] and the [Go Collector How To page](https://www.ibm.com/docs/en/obi/current?topic=go-collector-common-operations). |
| 195 | +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. |
116 | 196 |
|
117 |
| -## Filing Issues |
| 197 | +## What's Next |
118 | 198 |
|
119 |
| -If something is not working as expected or you have a question, instead of opening an issue in this repository, please open a ticket at [Instana Support portal](https://support.instana.com/hc/requests/new) instead. |
| 199 | +1. [Tracer Options](docs/options.md) |
| 200 | +1. [Tracing HTTP Outgoing Requests](docs/roundtripper.md) |
| 201 | +1. [Tracing SQL Driver Databases](docs/sql.md) |
| 202 | +1. [Tracing Other Go Packages](docs/other_packages.md) |
| 203 | +1. [Instrumenting Code Manually](docs/manual_instrumentation.md) |
120 | 204 |
|
121 | 205 | <!-- Links section -->
|
122 | 206 |
|
|
0 commit comments