Public documentation

Documentation indexRead as Markdown

Go

Languages · Shared configuration · Verification

This example uses OpenTelemetry Go 1.44.0 and Go 1.25 or later. Upstream traces and metrics are Stable; the logs SDK is Release candidate as checked on 2026-09-28. The program emits one trace and explicitly passes its context to child work.

Install

mkdir corridora-go
cd corridora-go
go mod init example.com/corridora-go
go get go.opentelemetry.io/otel@v1.44.0 \
  go.opentelemetry.io/otel/sdk@v1.44.0 \
  go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp@v1.44.0
export OTEL_EXPORTER_OTLP_ENDPOINT='http://127.0.0.1:4318'
unset OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
unset OTEL_EXPORTER_OTLP_HEADERS OTEL_EXPORTER_OTLP_TRACES_HEADERS

Start the local Collector. Retain go.mod and go.sum; exporters and the SDK should use the same release family.

Initialize and emit

Save as main.go:

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/attribute"
    "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"
)

func main() {
    if err := run(); err != nil { log.Fatal(err) }
}

func run() error {
    exporter, err := otlptracehttp.New(context.Background())
    if err != nil { return err }
    provider := sdktrace.NewTracerProvider(
        sdktrace.WithBatcher(exporter),
        sdktrace.WithResource(resource.NewSchemaless(
            attribute.String("service.name", "corridora-go"),
            attribute.String("service.version", "example-1"),
            attribute.String("deployment.environment.name", "development"),
        )),
    )
    otel.SetTracerProvider(provider)
    otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
        propagation.TraceContext{}, propagation.Baggage{},
    ))
    tracer := otel.Tracer("corridora.examples.go")
    ctx, span := tracer.Start(context.Background(), "example.calculate")
    _, child := tracer.Start(ctx, "example.child")
    time.Sleep(50 * time.Millisecond)
    child.End()
    fmt.Printf("trace_id=%s\n", span.SpanContext().TraceID())
    span.End()

    shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    return provider.Shutdown(shutdownCtx)
}
go run .

The exporter package fixes the protocol to HTTP/protobuf and reads supported OTLP endpoint/header variables. Its generic endpoint is a base URL; a trace-specific endpoint includes the complete signal path.

Frameworks and propagation

For HTTP services, use compatible otelhttp middleware around handlers and its transport around outbound clients. For gRPC, use compatible otelgrpc instrumentation. Instrumentation modules have their own release series; review their SDK requirements before adding them.

The Go SDK does not automatically instrument arbitrary binaries. Separate compile-time or agent instrumentation has its own supported-library and deployment requirements. When another integration owns provider initialization, add custom spans through its API instead of replacing the global provider.

Pass the context returned by tracer.Start into child operations and requests. Starting each operation from context.Background() creates unrelated roots. Custom queues must inject context into message headers and extract it at the consumer. Installing a global propagator alone does not move headers across the network.

Logs, metrics, and shutdown

Use a supported bridge such as otelslog when exporting correlated structured logs, with a logs provider and OTLP log exporter. Give the bridge the current context when logging. log.Print or fmt.Printf remains ordinary output; tracing alone does not export it.

Metrics need sdk/metric, an OTLP metric exporter, and a periodic reader. Register instruments through a meter provider and shut it down as well. Logs packages use a different release series from trace/metric packages; their release-candidate status warrants explicit dependency compatibility checks.

Keep providers for the process lifetime. Shut them down after ending active spans, using a fresh timeout context rather than an already-cancelled request context. Avoid calling log.Fatal before cleanup in application code because it exits immediately; this sample returns the shutdown result before main reports a failure.

Direct export and verification

Follow shared configuration, replace the base endpoint with Corridora's HTTPS endpoint, and set OTEL_EXPORTER_OTLP_HEADERS="X-CORRIDORA-KEY=${CORRIDORA_INGEST_KEY}". The HTTP exporter supports these variables. Explicit WithEndpoint, WithEndpointURL, or WithHeaders options can override them.

Find service corridora-go and the printed trace ID in Corridora. Expect example.calculate with a child example.child. Separate trace IDs for those spans indicate a context mistake. For absent data, check shutdown errors, endpoint overrides, the Collector's accepted/sent counters, and verification.