Go
Integration
Section titled “Integration”Instrument your Go applications to send traces, logs, and metrics to Kopai.
Installation
Section titled “Installation”go get go.opentelemetry.io/otelgo get go.opentelemetry.io/otel/sdkgo get go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttpgo get go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttpBasic Setup
Section titled “Basic Setup”Initialize OpenTelemetry in your application:
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
Section titled “Running Your Application”Set the required environment variables and run:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.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
Section titled “Graceful Shutdown”Always shut down the tracer provider to flush remaining spans:
defer func() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() tp.Shutdown(ctx)}()Working Example
Section titled “Working Example”For a complete working example: