Installation
Wire the official Go OpenTelemetry SDK to export traces and logs over OTLP, and configure it with environment variables to send telemetry to Sophonz.
This wires up the upstream go.opentelemetry.io/otel SDK — not a Sophonz package. There's no auto-instrumentation agent for Go, so this is a short setup function you write once; the OTLP exporters it uses already read OTEL_EXPORTER_OTLP_* environment variables on their own.
Quick start
Enter the collector URL, project, app name, version and app key below and pick a tab (net/http · Gin) to generate the initialization code from those values. Advanced options are behind the "Advanced options" toggle.
// otel.go — call setupOTel() once at startup, before serving traffic
package main
import (
"context"
"log"
"net/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/log/global"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
func setupOTel(ctx context.Context) (shutdown func(context.Context) error, err error) {
res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithAttributes(
semconv.ServiceName("next-sample"),
semconv.ServiceVersion("1.0.0"),
semconv.ServiceNamespace("sophonz"),
attribute.String("service.key", "sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz"),
),
)
if err != nil {
return nil, err
}
traceExporter, err := otlptracehttp.New(ctx,
otlptracehttp.WithEndpointURL("https://in.sophonz.ai"),
otlptracehttp.WithHeaders(map[string]string{"authorization": "sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz"}),
)
if err != nil {
return nil, err
}
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(traceExporter),
sdktrace.WithResource(res),
)
otel.SetTracerProvider(tracerProvider)
logExporter, err := otlploghttp.New(ctx,
otlploghttp.WithEndpointURL("https://in.sophonz.ai"),
otlploghttp.WithHeaders(map[string]string{"authorization": "sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz"}),
)
if err != nil {
return nil, err
}
loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithProcessor(sdklog.NewBatchProcessor(logExporter)),
sdklog.WithResource(res),
)
global.SetLoggerProvider(loggerProvider)
return func(ctx context.Context) error {
_ = tracerProvider.Shutdown(ctx)
return loggerProvider.Shutdown(ctx)
}, nil
}
func main() {
ctx := context.Background()
shutdown, err := setupOTel(ctx)
if err != nil {
log.Fatal(err)
}
defer shutdown(ctx)
handler := otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}), "root")
http.ListenAndServe(":8080", handler)
}
// These can also come from the environment instead of being passed
// programmatically above:
// OTEL_SERVICE_NAME service.name (appName)
// OTEL_EXPORTER_OTLP_ENDPOINT collector base URL (collectorUrl)
// OTEL_EXPORTER_OTLP_HEADERS authorization=<appKey>
// OTEL_RESOURCE_ATTRIBUTES service.key=<appKey>,service.namespace=<project>Prerequisites
- Go 1.22 or later.
- A service registered in the Sophonz console, and the
sk_...API key issued for it.
Install the packages
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/otlplog/otlploghttp
go get go.opentelemetry.io/otel/log
go get go.opentelemetry.io/otel/sdk/log
go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttpNOTE — Logs API stability
go.opentelemetry.io/otel/log and go.opentelemetry.io/otel/sdk/log are still pre-1.0 (v0.2x), unlike the trace API which is stable at v1.x. The API can change between minor versions — pin a specific version in go.mod and check the release notes before upgrading. Traces work the same either way.
TIP — Get your API key
Log in to the Sophonz console, register the service, and copy the issued sk_... key. It carries your tenant identity — see service.key below.
Set up the SDK
Call this once at startup, before you start serving traffic:
package main
import (
"context"
"log"
"net/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/log/global"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// setupOTel wires the SDK to export over OTLP/HTTP. The exporters read
// OTEL_EXPORTER_OTLP_ENDPOINT / _PROTOCOL / _HEADERS on their own; the
// resource picks up OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES.
func setupOTel(ctx context.Context) (shutdown func(context.Context) error, err error) {
res, err := resource.New(ctx, resource.WithFromEnv())
if err != nil {
return nil, err
}
traceExporter, err := otlptracehttp.New(ctx)
if err != nil {
return nil, err
}
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(traceExporter),
sdktrace.WithResource(res),
)
otel.SetTracerProvider(tracerProvider)
logExporter, err := otlploghttp.New(ctx)
if err != nil {
return nil, err
}
loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithProcessor(sdklog.NewBatchProcessor(logExporter)),
sdklog.WithResource(res),
)
global.SetLoggerProvider(loggerProvider)
return func(ctx context.Context) error {
_ = tracerProvider.Shutdown(ctx)
return loggerProvider.Shutdown(ctx)
}, nil
}
func main() {
ctx := context.Background()
shutdown, err := setupOTel(ctx)
if err != nil {
log.Fatal(err)
}
defer shutdown(ctx)
handler := otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}), "root")
http.ListenAndServe(":8080", handler)
}otelhttp.NewHandler starts a span for every request that reaches it and propagates trace context to outgoing calls made with an otelhttp.NewTransport-wrapped client. Call shutdown(ctx) on graceful termination so the batch exporters flush before the process exits.
Configure environment variables
OTEL_SERVICE_NAME=my-go-service
OTEL_EXPORTER_OTLP_ENDPOINT=https://in.sophonz.ai
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_HEADERS=authorization=sk_your_api_key
OTEL_RESOURCE_ATTRIBUTES=service.key=sk_your_api_key,service.namespace=my-project,deployment.environment.name=production| Variable | Value | Notes |
|---|---|---|
OTEL_SERVICE_NAME | your service name | Read by resource.WithFromEnv(). Must match the service name registered in the console — the portal joins telemetry to the registered service on (project, service.name). |
OTEL_EXPORTER_OTLP_ENDPOINT | https://in.sophonz.ai | Base OTLP endpoint. otlptracehttp.New and otlploghttp.New derive /v1/traces and /v1/logs from it. |
OTEL_EXPORTER_OTLP_PROTOCOL | http/protobuf | Read automatically by both exporters. |
OTEL_EXPORTER_OTLP_HEADERS | authorization=sk_your_api_key | Sent as an HTTP header on every export request. |
OTEL_RESOURCE_ATTRIBUTES | service.key=sk_your_api_key,service.namespace=...,deployment.environment.name=... | service.key is the tenant identity the collector checks — required. service.namespace groups services into a project; deployment.environment.name labels the environment. |
CAUTION — service.key is required
The collector runs in enforce mode: telemetry without a resolvable service.key resource attribute is dropped silently. Your app keeps running, no error is returned from the exporter, and nothing shows up in Sophonz. Always include service.key in OTEL_RESOURCE_ATTRIBUTES, and send the same key as the authorization header via OTEL_EXPORTER_OTLP_HEADERS.
Framework notes
otelhttp covers plain net/http. For a router, add its dedicated middleware — each one wraps request handling to start a span and record route, method, and status code:
| Framework | Package |
|---|---|
net/http | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp |
| Gin | go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin — router.Use(otelgin.Middleware("my-go-service")) |
| Echo | go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho — e.Use(otelecho.Middleware("my-go-service")) |
For outgoing HTTP calls, wrap your client's Transport with otelhttp.NewTransport(http.DefaultTransport) so trace context propagates downstream and the call shows up as a child span.
Verify
Run your app with the environment variables set:
OTEL_SERVICE_NAME=my-go-service \
OTEL_EXPORTER_OTLP_ENDPOINT=https://in.sophonz.ai \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_EXPORTER_OTLP_HEADERS=authorization=sk_your_api_key \
OTEL_RESOURCE_ATTRIBUTES=service.key=sk_your_api_key,service.namespace=my-project,deployment.environment.name=production \
go run .Since export failures are handled internally by the batch processor (logged through the SDK's error handler, not returned to your request), a running app with no errors doesn't guarantee telemetry arrived — an invalid or missing service.key fails silently. Generate some traffic against your handlers, then check the Sophonz console for the service you registered.
TIP — This is just the beginning
Now ship your app. Sophonz surfaces the problems real users hit — so your app evolves on its own.