# 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](https://flowhawk.dev/docs/signals#traces).
- Requests matched to your [business flows](https://flowhawk.dev/docs/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](https://flowhawk.dev/docs/signals#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](https://flowhawk.dev/docs/signals#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](https://flowhawk.dev/docs/help) says why telemetry can be refused (a wrong key, an empty balance) and how to see it.

## Related

[**OpenTelemetry for Rust and axum**Trace a Rust web service with the tracing crate you already use, and send its requests, queries, logs and panics to Flowhawk.](https://flowhawk.dev/integrations/opentelemetry-rust) [**Kubernetes monitoring**Pod logs, node and pod metrics and your services' traces from a Kubernetes cluster, with two Helm charts you may already know.](https://flowhawk.dev/integrations/kubernetes-monitoring) [**OpenTelemetry Collector**Send everything your OpenTelemetry Collector receives to Flowhawk, and collect host and container metrics on the way.](https://flowhawk.dev/integrations/opentelemetry-collector)

[Try it free for 14 days](https://app.flowhawk.dev/signup?trial=1) [Read the getting started guide](https://flowhawk.dev/docs/getting-started)

---

URL: https://flowhawk.dev/integrations/opentelemetry-go
