# Add OpenTelemetry Tracing to a Go Service

An agent adds OpenTelemetry to Miniflux's server, Postgres and feed fetcher, but database and fetch spans arrive as separate traces, not under the request.

[Run this on Rush](https://cloud.getrush.ai/run/add-opentelemetry/go/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent read `internal/http/server/server.go`, `internal/database/postgresql.go`, `internal/http/client/client.go` and the fetcher's `request_builder.go` in miniflux/v2. It added the OpenTelemetry SDK, otelhttp and `github.com/XSAM/otelsql`, wrapped the server handler in `otelhttp.NewHandler`, opened Postgres through `otelsql.Open`, and wrapped both client transports in `otelhttp.NewTransport`.

A new `internal/telemetry` package reads the standard variables: tracing turns on when `OTEL_EXPORTER_OTLP_ENDPOINT`, its traces variant or `OTEL_TRACES_EXPORTER` is set, and `OTEL_SDK_DISABLED=true` always wins. `internal/cli/cli.go` calls `telemetry.Setup` and flushes on exit. Server spans are named `METHOD path` with numeric segments as `{id}`, and `/healthcheck` and `/metrics` are not traced.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add OpenTelemetry tracing to this service: HTTP server spans, database spans and outbound HTTP spans, configured through the standard OTEL_* environment variables. Run a local collector in the sandbox, exercise the main endpoints, and open a pull request with an export of the traces.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

Tried on miniflux/v2 at faecfa2f: 10 files changed, 251 insertions(+), 14 deletions(-). `go build`, `go vet` and `gofmt -l` were clean, `go test ./...` exited 0, and the new package ended `ok  	miniflux.app/v2/internal/telemetry	0.003s`. Two existing transport tests failed on the first run after the change and were rewritten (see below). With Postgres 16 from an embedded-binaries jar (no Docker in the sandbox) and otelcol-contrib 0.120.0, its last live run exported 6 server spans, 1 outbound `HTTP GET` and 29 database spans to `miniflux-traces.jsonl`, returned to the caller as a session artifact. No pull request was opened.

The traces are not joined yet: database and fetch spans are not children of the request span, so one request does not show up as one trace. The run reports that most of `internal/storage` calls `db.Query` and `db.Exec` without a context, and the fetcher builds requests with `http.NewRequest`. A reviewer decides between root spans for now and a larger change that passes `ctx` through both.

## Run it on every Linear issue labelled observability

An automation with a `linear` trigger on `linear.issue.labeled` and the label `observability` runs the recipe's prompt: do the work the issue describes and open a pull request that links back to it. If you want one trace per request, say so in the issue, so the run threads `ctx` through `internal/storage` instead of stopping at root spans.

## Why run it on Rush?

Spans reaching a collector is not tracing a request. Server, SQL and fetch spans all arrived here, and the run still said plainly that they do not join into one trace, naming the storage and fetcher code that keeps them apart.

## Frequently asked questions

### Why did two existing tests change?

They asserted that `client.Transport` was a `*http.Transport`, or nil, which the tracing wrapper changes. Private-network blocking is still tested by `TestBlockPrivateNetworksBlocksPrivateIP`.

### Which checks did the run not do?

It did not run `make test`, because `-race` needs cgo, which was disabled, or golangci-lint. Database spans were checked only by the live run, since the Postgres-backed tests in `internal/api` skip unless `TEST_MINIFLUX_BASE_URL` is set, and only OTLP over HTTP was exercised.

## More Go jobs

- [Answer Support Tickets from a Go Codebase](https://getrush.ai/run/answer-support-tickets-from-code/go/)
- [Inventory the API Endpoints of a Go Service](https://getrush.ai/run/api-endpoint-inventory/go/)
- [Explain the Architecture of a Go Codebase](https://getrush.ai/run/architecture-overview/go/)

## The same job on other stacks

- [Add OpenTelemetry Tracing to GORM Queries in a Go Service](https://getrush.ai/run/add-opentelemetry/gorm/)
- [Add OpenTelemetry Tracing to a Koa API](https://getrush.ai/run/add-opentelemetry/koa/)
- [Add OpenTelemetry Tracing to a Ktor Server](https://getrush.ai/run/add-opentelemetry/ktor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
