# Trace a Go app

Go has no stable zero-code agent yet. Set up the official OpenTelemetry SDK once in `main`, then wrap your handlers with `otelhttp`. The exporter uses OTLP over HTTP; there is no gRPC endpoint.

Time to first span: **about 10 min**. API key: [app.epok.cloud, then Settings, then API Keys](https://app.epok.cloud).

## 1. Set the OTLP endpoint and key

Set these in the environment that starts your service. Use the base URL; the exporter appends `/v1/traces`. The header name must be `x-api-key`.

```
export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.epok.cloud"
export OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"   # not gRPC
export OTEL_EXPORTER_OTLP_HEADERS="x-api-key=YOUR_API_KEY"
export OTEL_EXPORTER_OTLP_COMPRESSION="gzip"
export OTEL_SERVICE_NAME="checkout-service"          # becomes service.name
```

## 2. Install the SDK

Add the official OpenTelemetry modules. The HTTP trace exporter is the one that matches the endpoint above.

```
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/contrib/instrumentation/net/http/otelhttp
```

## 3. Create the tracer provider

The SDK reads `OTEL_SERVICE_NAME` from the environment and sets it as `service.name`. Keep the deferred `Shutdown` call or a short-lived process can exit before the first batch is sent.

```
package main

import (
    "context"

    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
    sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
    ctx := context.Background()

    // Reads OTEL_EXPORTER_OTLP_* from the environment.
    exp, err := otlptracehttp.New(ctx)
    if err != nil {
        panic(err)
    }

    tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exp))
    otel.SetTracerProvider(tp)
    defer tp.Shutdown(ctx) // flushes buffered spans before exit

    // Wrap handlers with otelhttp.NewHandler(mux, "server"), then serve.
}
```

## 4. Verify traces in Epok

1. Send a request through your app.
2. Open **Traces** in the app and filter by service name `checkout-service`.
3. A span tree appears within a few seconds. The service summary fills in after a few minutes of traffic.

## Logs

This guide does not use the Go OpenTelemetry logs SDK yet. Send logs with your existing logger to the OTLP collector. See [OpenTelemetry Collector](https://epok.cloud/docs/install/otel-collector).

## Troubleshooting

1. **No spans arrive.** A short-lived process may exit before the batch is sent. Keep `defer tp.Shutdown(ctx)` and give the process time to drain.
2. **Spans show as `unknown_service:<binary>`.** `OTEL_SERVICE_NAME` did not reach the process. Set it in the same environment that starts the service.
3. **401 or 403.** 401 means no key was found; check the header is exactly `x-api-key` in `OTEL_EXPORTER_OTLP_HEADERS`. 403 means the key was read and rejected; check its scope and whether it was rotated.

Next

- [Node.js traces](https://epok.cloud/docs/install/traces/node): zero-code auto-instrumentation.
- [Java traces](https://epok.cloud/docs/install/traces/java): one JVM flag for the official agent.
- [.NET traces](https://epok.cloud/docs/install/traces/dotnet): auto-instrumentation and the logging bridge.
- [Authentication](https://epok.cloud/docs/authentication): every accepted credential header.
