Go SDK
Go SDK
Section titled “Go SDK”Instrument your Go applications to send traces and logs 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/otlptracehttpBasic Setup
Section titled “Basic Setup”package main
import ( "context" "os"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.17.0")
func initTracer() (*trace.TracerProvider, error) { ctx := context.Background()
exporter, err := otlptracehttp.New(ctx, otlptracehttp.WithEndpoint("otlp.kopai.app"), otlptracehttp.WithHeaders(map[string]string{ "Authorization": "Bearer " + os.Getenv("KOPAI_TOKEN"), }), ) if err != nil { return nil, err }
res := resource.NewWithAttributes( semconv.SchemaURL, semconv.ServiceName(os.Getenv("OTEL_SERVICE_NAME")), )
tp := trace.NewTracerProvider( trace.WithBatcher(exporter), trace.WithResource(res), ) otel.SetTracerProvider(tp) return tp, nil}Running Your Application
Section titled “Running Your Application”export KOPAI_TOKEN="your-token-here"export OTEL_SERVICE_NAME="my-service"go run main.goManual Instrumentation
Section titled “Manual Instrumentation”package main
import ( "context" "go.opentelemetry.io/otel")
var tracer = otel.Tracer("my-service")
func processOrder(ctx context.Context, orderID string) error { ctx, span := tracer.Start(ctx, "processOrder") defer span.End()
span.SetAttributes(attribute.String("order.id", orderID))
// Your logic here return nil}Auto-Instrumentation
Section titled “Auto-Instrumentation”Go requires explicit instrumentation for libraries. Common instrumentations:
go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttpgo get go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc