All Products
Search
Document Center

Cloud Monitor:Get trace and span IDs from your Go application code

Last Updated:Jan 04, 2026

Applications that use the Go agent to connect to Application Monitoring can use the OpenTelemetry software development kit (SDK) to retrieve the trace ID and span ID from their code.

Procedure

  1. In the agent settings for your application, enable the OpenTelemetry switch.

  2. Add the code block.

    package main
    
    import (
        "context"
        "go.opentelemetry.io/otel/trace"
     )
     
     func GetTraceIdAndSpanId() (string, string) {
          span := trace.SpanFromContext(context.Background())
          return span.SpanContext().TraceID().String(),
         span.SpanContext().SpanID().String()
     }