• Home
  • Extend Dynatrace
  • Send data to Dynatrace with OpenTelemetry
  • OpenTelemetry traces
  • OpenTelemetry instrumentation guide
  • Instrument Go applications with OpenTelemetry

Instrument Go applications with OpenTelemetry

This guide shows how to instrument your Go application with OpenTelemetry and export the traces to Dynatrace. To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.

Prerequisites

  • Dynatrace version 1.222+
  • Send W3C Trace Context HTTP headers is turned on (default setting). To verify this setting
    1. From the Dynatrace menu, go to Settings > Server-side service monitoring > Deep monitoring > Distributed tracing.
    2. Make sure Send W3C Trace Context HTTP headers is turned on.

Overview

To monitor your Go application with OpenTelemetry you need to

  1. Instrument your application
  2. Send the data to Dynatrace
  3. Restart your application and verify the data in Dynatrace

1. Instrument your application

You can use the available instrumentation packages (provided by OpenTelemetry) or instrument manually.

To take advantage of the available instrumentation packages, go to the OpenTelemetry repository, find the package that your app is using, and install it using the following command:

go
go get go.opentelemetry.io/contrib/instrumentation/<IMPORT_PATH>/otel<PACKAGE_NAME>

Where the <IMPORT_PATH> and <PACKAGE_NAME> are the standard Go identifiers for the package being instrumented.

Instrumentation differs for each package, below are the steps to instrument with net/http. For others, please check out the specific package in the OpenTelemetry repository and follow their example.

  • Install the dependency:
go
go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
  • Add the import to your code:
go
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  • Wrap your HTTP handler with otelhttp:
go
handler := http.HandlerFunc(httpHandler) wrappedHandler := otelhttp.NewHandler(handler, "my-span") //TODO Replace with the name of your span //Use the wrappedHandler with your handle http.Handle("/", wrappedHandler)

To instrument manually, install the packages below and add the code snippet below to any Go function you want to monitor. Set names for the tracer and the span, and add attributes as you see fit.

go
go get go.opentelemetry.io/otel@v1.3.0 go get go.opentelemetry.io/otel/sdk@v1.3.0
  • Add the following imports to your code:
go
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" sdktrace "go.opentelemetry.io/otel/sdk/trace"
  • Set the TracerProvider within your main function:
go
otel.SetTracerProvider(sdktrace.NewTracerProvider())
  • And finally, add the span to the function you would like to instrument:
go
_, span := otel.Tracer("my-tracer").Start(context.Background(), "my-span") //TODO Add names for tracer and span span.SetAttributes( attribute.String("my-key-1", "my-value-1"))//TODO Add attributes defer span.End() //TODO your code goes here // Set the status of your span if there is a possible error if err != nil { span.SetStatus(codes.Error, err.Error()) } span.SetStatus(codes.Ok, "successful")

2. Send data to Dynatrace

You have two options for sending data to Dynatrace.

OneAgent can only instrument a tracer implementation of the default OpenTelemetry SDK. If you have not already installed the OpenTelemetry SDK, use the following command to install it:

go
go get go.opentelemetry.io/otel/sdk@v1.3.0

Set OpenTelemetry SDK TracerProvider and TextMapPropagator globally before capturing any spans to enrich distributed traces with OpenTelemetry spans:

go
otel.SetTracerProvider(sdktrace.NewTracerProvider()) otel.SetTextMapPropagator(propagation.TraceContext{})

You will be able to see your traces in the Dynatrace web UI as soon as you redeploy your app.

  • Install the following dependencies:
go
go get go.opentelemetry.io/otel@v1.3.0 go get go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp@v1.3.0 go get go.opentelemetry.io/otel/sdk@v1.3.0
  • Add the following imports to your code and create a function to initialize and configure the Tracer Provider:
go
import ( "context" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.7.0" "log" ) func InitTracerProvider() *sdktrace.TracerProvider { ctx := context.Background() res, err := resource.New(ctx, resource.WithAttributes( semconv.ServiceNameKey.String("go-quickstart"), //TODO Replace with the name of your application semconv.ServiceVersionKey.String("1.0.1"), //TODO Replace with the version of your application ), ) if err != nil { log.Fatalf("Failed to create resource: %v", err) } exporter, err := otlptracehttp.New( ctx, otlptracehttp.WithEndpoint("<URL>"), //TODO Replace <URL> to your SaaS/Managed-URL as mentioned in the next step otlptracehttp.WithURLPath("<URL_PATH>"),//TODO Replace <URL_PATH> to your SaaS/Managed-URL-PATH as mentioned in the next step otlptracehttp.WithHeaders(map[string]string{"Authorization": "Api-Token <TOKEN>"}), //TODO Replace <TOKEN> with your API Token as mentioned in the next step ) if err != nil { log.Fatalf("Failed to create OTLP exporter: %v", err) } tp := sdktrace.NewTracerProvider( sdktrace.WithResource(res), sdktrace.WithSampler(sdktrace.AlwaysSample()), sdktrace.WithBatcher(exporter), ) otel.SetTracerProvider(tp) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) return tp }
  • In your main method, make sure to call the newly created function:
go
func main() { tp := InitTracerProvider() defer func() { if err := tp.Shutdown(context.Background()); err != nil { log.Printf("Error shutting down tracer provider: %v", err) } }() //Your code goes here }

Lastly, you need to define the correct endpoint and token, to make sure your data arrives where it should be.

  • To set the endpoint:

    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Remove the https:// from the URL:
      • Dynatrace SaaS {your-environment-id}.live.dynatrace.com
      • Dynatrace Managed {your-domain}
        • Note: for Dynatrace Managed, /e/{your-environment-id} will be used with your URL path.
    2. Replace <URL> in the code snippet above with your endpoint.
  • To set the URL path:

    1. Use the following pattern:
    • Dynatrace SaaS /api/v2/otlp/v1/traces
    • Dynatrace Managed /e/{your-environment-id}/api/v2/otlp/v1/traces
    1. Replace <URL_PATH> in the code snippet above with your URL path.
  • To create an authentication token:

    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest OpenTelemetry traces and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

Verify that the traces are ingested into Dynatrace

A few minutes after restarting your app, look for your spans:

  • In the Dynatrace menu, go to Distributed traces and select the Ingested traces tab.
  • Your spans will be part of an existing PurePath distributed trace if the root of your call is already monitored by OneAgent.

If your application does not receive any traffic, there will be no traces.

(Optional) Configure data capture to meet privacy requirements

While Dynatrace automatically captures all OpenTelemetry resource and span attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data that's stored.

To view your custom span attributes, you need to allow them in the Dynatrace web UI first:

  • Span attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Span attributes.
  • Resource attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Resource attributes.