# Go Send Go traces to Kopai with the OpenTelemetry SDK and its OTLP HTTP exporter. Let your coding agent wire it up for you, or follow the manual steps. Instrument your Go applications to send traces, logs, and metrics to Kopai. ## Installation ```bash go get go.opentelemetry.io/otel go get go.opentelemetry.io/otel/sdk go get go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp go get go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp ``` ## Basic Setup Initialize OpenTelemetry in your application: ```go import ( "context" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.24.0" ) func initTracer(ctx context.Context) (*sdktrace.TracerProvider, error) { exporter, err := otlptracehttp.New(ctx) if err != nil { return nil, err } res, _ := resource.New(ctx, resource.WithAttributes(semconv.ServiceName("my-service")), ) tp := sdktrace.NewTracerProvider( sdktrace.WithBatcher(exporter), sdktrace.WithResource(res), ) otel.SetTracerProvider(tp) return tp, nil } ``` The SDK reads the OTLP endpoint from standard OpenTelemetry environment variables. ## Running Your Application Set the required environment variables and run: ```bash export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-http.kopai.app" export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_TOKEN" export OTEL_SERVICE_NAME="my-service" go run main.go ``` | Variable | Description | |----------|-------------| | `OTEL_EXPORTER_OTLP_ENDPOINT` | Kopai OTLP endpoint | | `OTEL_EXPORTER_OTLP_HEADERS` | Auth header with your backend token | | `OTEL_SERVICE_NAME` | Name shown in Kopai UI | ## Graceful Shutdown Always shut down the tracer provider to flush remaining spans: ```go defer func() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() tp.Shutdown(ctx) }() ``` ## Working Example For a complete working example: **[Go Example](https://github.com/kopai-app/kopai-integration-examples/tree/main/go)**