Go SDK
Add OpenTelemetry monitoring to Go services with HTTP instrumentation, structured logging, error capture, and graceful shutdown.
Install
Requires Go 1.25 or later:
go get github.com/Prilog-ai/prilog-monitoring/golang@v0.1.0
Create a monitoring connection in Prilog and set PRILOG_DSN in your application's environment.
Initialize and instrument HTTP
package main
import (
"context"
"log"
"log/slog"
"net/http"
"time"
prilog "github.com/Prilog-ai/prilog-monitoring/golang"
)
func main() {
monitoring, err := prilog.Init(context.Background(), prilog.Config{
ServiceName: "checkout-api",
Environment: "production",
Release: "1.0.0",
})
if err != nil {
log.Print(err)
return
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = monitoring.Shutdown(ctx)
}()
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
slog.InfoContext(r.Context(), "Health check")
_, _ = w.Write([]byte("ok"))
})
err = http.ListenAndServe(":8080", monitoring.HTTPHandler("http.server", mux))
if err != nil {
monitoring.CaptureError(context.Background(), err)
log.Print(err)
}
}
PRILOG_DSN takes precedence over Config.DSN; OTEL_SERVICE_NAME takes precedence over Config.ServiceName. The service name defaults to application. Without a DSN, initialization returns a disabled monitor. Initialization with a DSN sends a startup log and span.
Integrate shutdown into your application's existing signal and HTTP shutdown handling so it actually runs on termination. Avoid log.Fatal after initialization when you need deferred flushing; it exits without running deferred functions.
SDK reference
Init(ctx, Config)— configure monitoring and return a monitor and error.Config{DSN, ServiceName, Environment, Release}— connection and resource configuration. An optionalLogHandlerpreserves a customslog.Handler.monitoring.HTTPHandler(name, handler)— wrap an HTTP handler for spans and panic recording.monitoring.Run(ctx, name, fn)— run a function accepting a context and returning an error inside a span; returned errors are recorded.monitoring.CaptureError(ctx, err)— record a handled error in the current span, or create an exception span when there is no active span.monitoring.Recover(ctx)— record and re-panic a recovered panic. Defer it at goroutine entry points you own.monitoring.Shutdown(ctx)— flush telemetry and restore logging state.
Logging and context
The SDK connects slog and standard logging to OpenTelemetry. Use slog.InfoContext and slog.ErrorContext with the request context to correlate logs with the current trace.
Keep context propagation across your service boundaries. Use compatible OpenTelemetry client instrumentation for outgoing requests and explicit spans for background work. See OpenTelemetry and OTLP for resource names and transport details.
