|
| 1 | +--- |
| 2 | +title: Custom Instrumentation of Go Applications with the OpenTelemetry API |
| 3 | +kind: documentation |
| 4 | +description: 'Instrument your Go application with OTel API to send traces to Datadog' |
| 5 | +code_lang: go |
| 6 | +type: multi-code-lang |
| 7 | +code_lang_weight: 30 |
| 8 | +further_reading: |
| 9 | + - link: 'tracing/glossary/' |
| 10 | + tag: 'Documentation' |
| 11 | + text: 'Explore your services, resources, and traces' |
| 12 | +--- |
| 13 | + |
| 14 | +{{% otel-custom-instrumentation %}} |
| 15 | + |
| 16 | +## Requirements and limitations |
| 17 | + |
| 18 | +- Datadog Go tracing library `dd-trace-go` version 1.5.0 or greater. |
| 19 | +- Go version 1.8 or greater. |
| 20 | +- The Datadog OTel API implementation is dependent on upstream [OpenTelemetry Go][6]. |
| 21 | + |
| 22 | +The following OTel features are implemented in the Datadog library as noted: |
| 23 | + |
| 24 | +| Feature | Support notes | |
| 25 | +|---------------------------------------|------------------------------------| |
| 26 | +| [OTel Context propagation][1] | [Datadog distributed header format][9] is used instead. | |
| 27 | +| [Span processors][2] | Unsupported | |
| 28 | +| [Span Exporters][3] | Unsupported | |
| 29 | +| Trace/span [ID generators][4] | ID generation is performed by `ddtrace`. | |
| 30 | + |
| 31 | + |
| 32 | +## Configuring OTel to use the Datadog trace provider |
| 33 | + |
| 34 | +1. Add your desired manual OTel instrumentation to your Go code following the [OTel Go Manual Instrumentation documentation][5]. |
| 35 | + |
| 36 | +2. Install the OpenTelemetry package `go.opentelemetry.io/otel` using the command: |
| 37 | + |
| 38 | + ```shell |
| 39 | + go get go.opentelemetry.io/otel |
| 40 | + ``` |
| 41 | + |
| 42 | +3. Install the Datadog OpenTelemetry wrapper package `gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentelemetry` using the command: |
| 43 | + |
| 44 | + ```shell |
| 45 | + go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentelemetry |
| 46 | + ``` |
| 47 | + |
| 48 | +4. Import packages in the code: |
| 49 | + |
| 50 | + ```go |
| 51 | + import ( |
| 52 | + "go.opentelemetry.io/otel" |
| 53 | + ddotel "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentelemetry" |
| 54 | + ) |
| 55 | + ``` |
| 56 | + |
| 57 | +5. Create a TracerProvider, optionally providing a set of options, that are specific to Datadog APM, and defer the Shutdown method, which stops the tracer: |
| 58 | + |
| 59 | + ```go |
| 60 | + provider := ddotel.NewTracerProvider() |
| 61 | + defer provider.Shutdown() |
| 62 | + ``` |
| 63 | + |
| 64 | +6. Use the Tracer Provider instance with the OpenTelemetry API to set the global TracerProvider: |
| 65 | + |
| 66 | + ```go |
| 67 | + otel.SetTracerProvider(provider) |
| 68 | + ``` |
| 69 | + |
| 70 | +7. Run your application. |
| 71 | + |
| 72 | +Datadog combines these OpenTelemetry spans with other Datadog APM spans into a single trace of your application. |
| 73 | + |
| 74 | + |
| 75 | +[1]: https://opentelemetry.io/docs/instrumentation/go/manual/#propagators-and-context |
| 76 | +[2]: https://opentelemetry.io/docs/reference/specification/trace/sdk/#span-processor |
| 77 | +[3]: https://opentelemetry.io/docs/reference/specification/trace/sdk/#span-exporter |
| 78 | +[4]: https://opentelemetry.io/docs/reference/specification/trace/sdk/#id-generators |
| 79 | +[5]: https://opentelemetry.io/docs/instrumentation/go/manual/ |
| 80 | +[6]: https://opentelemetry.io/docs/instrumentation/go/ |
| 81 | +[9]: /tracing/trace_collection/trace_context_propagation/go/ |
0 commit comments