OpenTelemetry for Go

Trace a Go service on net/http, Gin, chi or echo, and send its requests, queries, logs and panics to Flowhawk.

Go has no agent that instruments a binary from outside, so you add a few lines at start-up. Plain OpenTelemetry works. The Flowhawk Go SDK does the same in fewer lines and adds database spans, slog logs and crash reports.

What you get

  • Every request as a trace, with its database calls and outgoing HTTP calls, in Traces.
  • Requests matched to your business flows by service and route on our side, so you see "checkout is failing at place order" rather than a list of endpoints.
  • Exceptions grouped into Issues, slow and N+1 database queries on each database's page, and deploys found from service.version.
  • Alerts, incidents and the on-call agent, which reads these traces when something breaks.

Set it up with the Flowhawk SDK

1. Install

go get github.com/flowhawk-dev/flowhawk-go     # Go 1.26 or later

2. Start it first in main

import flowhawk "github.com/flowhawk-dev/flowhawk-go"

func main() {
	shutdown, err := flowhawk.Init(flowhawk.Options{Service: "orders"})
	if err != nil {
		log.Fatal(err)
	}
	defer shutdown(context.Background())

	mux := http.NewServeMux()
	mux.HandleFunc("POST /orders/{id}/pay", pay)
	http.ListenAndServe(":8080", flowhawk.Middleware(mux))
}

Requests are named after their ServeMux pattern (POST /orders/{id}/pay). For database calls use flowhawk.OpenDB("pgx", dsn) instead of sql.Open, for outgoing calls &http.Client{Transport: flowhawk.Transport(nil)}, and for logs flowhawk.Logger(), a log/slog logger. Panics, in a handler or anywhere else, show in Issues with their stack.

3. Point it at Flowhawk

First create an ingest key: in the app, open Settings > API keys and pick Services. In the examples below, KEY stands for that key. Keep it out of your repository, like any other secret.

FLOWHAWK_API_KEY=KEY
FLOWHAWK_CONTROL_URL=https://api.flowhawk.dev
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.flowhawk.dev
FLOWHAWK_ENVIRONMENT=production

With Gin, chi or echo

Wrap the router the same way, and tell the SDK the route from a middleware, so a request is named GET /orders/:id rather than its raw path:

r := gin.New()
r.Use(func(c *gin.Context) {
	flowhawk.SetRoute(c.Request.Context(), c.FullPath())
	c.Next()
})
r.POST("/orders/:id/pay", pay)
http.ListenAndServe(":8080", flowhawk.Middleware(r))

With chi it is chi.RouteContext(r.Context()).RoutePattern(), with echo c.Path().

Or with plain OpenTelemetry

go get go.opentelemetry.io/otel go.opentelemetry.io/otel/sdk \
  go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp \
  go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
exp, err := otlptracehttp.New(ctx) // reads the OTEL_ variables below
if err != nil {
	log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exp))
defer tp.Shutdown(ctx)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})

http.ListenAndServe(":8080", otelhttp.NewHandler(mux, "server"))
OTEL_SERVICE_NAME=orders
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.flowhawk.dev
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer%20KEY
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production,service.version=1.4.2

The space in the header is written %20. Set service.version to your release, so a deploy shows as a change next to the flows it made better or worse.

With Gin, use r.Use(otelgin.Middleware("orders")) from go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin.

Check it arrived

Open Setup in the app. It lists each service that sent data in the last 15 minutes. If yours isn't there, Help and troubleshooting says why telemetry can be refused (a wrong key, an empty balance) and how to see it.