# OpenTelemetry

Export traces, metrics, and logs to an OpenTelemetry collector endpoint.

You may send Charter telemetry over the OpenTelemetry Protocol (OTLP). When you set an export endpoint, every request gets a span, and Charter records request and database query counts and durations. Leave the endpoint unset and Charter only writes logs to standard error.

> [!INFO]
> OpenTelemetry ships in the free binary. The variable list is also under [Environment variables](/docs/environment-variables/).

## What you need

- A running Charter process (`charter serve` or `charter dev`)
- An OTLP collector that accepts gRPC or HTTP, for example the Grafana OpenTelemetry LGTM stack that `charter setup` writes into `compose.yaml`

Confirm Docker if you use the local stack:

```sh
docker --version
docker compose version
```

## Step 1: Start a collector

### Local project from `charter setup`

`charter setup` writes a Compose service named `lgtm` (Grafana's all-in-one OpenTelemetry image) and sets the endpoint in `.env`:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
```

Start Compose from the project directory (or let `charter dev` start Postgres; start `lgtm` yourself if it is not already up):

```sh
docker compose up -d lgtm
```

Ports from that template:

| Port | Use |
| --- | --- |
| `4317` | OTLP gRPC (what Charter's setup endpoint uses) |
| `4318` | OTLP HTTP |
| `4000` | Grafana UI in the browser |
| `9090` | Prometheus API |

Open [http://localhost:4000](http://localhost:4000) after the container is healthy.

### Your own collector

Of course, you are free to point Charter at any OTLP endpoint your operator team already runs. Use the base URL the SDK expects for that protocol, for example `http://otel-collector:4317` for gRPC inside a private network.

> [!WARNING]
> Charter refuses non-public webhook targets by default, but OTLP export follows whatever URL you set. Only use endpoints you trust on networks you control.

## Step 2: Set the export endpoint

Export is off until this variable is set and non-empty:

```sh
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
```

Put it in `.env` (loaded automatically) or the process environment. Restart `charter serve` or `charter dev` after you change it.

If the exporter fails to start, Charter logs a warning and keeps running with standard error logging instead of exiting.

## Step 3: Confirm data arrives

1. Hit any HTTP route on your Charter instance (for example the docs page or a resource GET).
2. Wait about ten seconds for the periodic metrics export, then open Grafana (or your backend) and look for service name `charter`.
3. Expect HTTP and database spans, plus the metrics listed below.

Charter always sets `service.name` to `charter`, so `OTEL_SERVICE_NAME` has no effect. Optional resource tags still work through `OTEL_RESOURCE_ATTRIBUTES`.

## Metrics Charter emits

Every instrumented process exports these names over OTLP. Durations are milliseconds.

### Always on

| Name | Kind | Attributes |
| --- | --- | --- |
| `charter.http.requests` | counter | `http.method`, `http.route`, `http.status_code` |
| `charter.http.request.duration` | histogram (ms) | `http.method`, `http.route`, `http.status_code` |
| `charter.db.queries` | counter | `db.operation`, `db.sql.table` (when known) |
| `charter.db.query.duration` | histogram (ms) | `db.operation`, `db.sql.table` (when known) |

`db.operation` is one of `select`, `insert`, `update`, `delete`, `count`, `ping`, or `other`.

HTTP metrics come from the request middleware on every matched route. Database metrics come from Charter's Postgres data path whenever a query runs.

### CSV import jobs

When a CSV import job runs, Charter also emits:

| Name | Kind | Attributes |
| --- | --- | --- |
| `csv.import.rows.success` | counter | `resource` |
| `csv.import.rows.failed` | counter | `resource`, `error_kind` |
| `csv.import.job.duration_ms` | histogram (ms) | `resource` |

### Names in Prometheus

On the local `lgtm` stack (and typical Prometheus remote-write backends), dots become underscores, counters gain a `_total` suffix, and histograms split into `_bucket`, `_sum`, and `_count`. The millisecond unit stays in the description, not in the Prom metric name (except where the OTLP name already includes `_ms`, as with CSV job duration):

| OTLP name | Prometheus series |
| --- | --- |
| `charter.http.requests` | `charter_http_requests_total` |
| `charter.http.request.duration` | `charter_http_request_duration_{bucket,sum,count}` |
| `charter.db.queries` | `charter_db_queries_total` |
| `charter.db.query.duration` | `charter_db_query_duration_{bucket,sum,count}` |
| `csv.import.rows.success` | `csv_import_rows_success_total` |
| `csv.import.rows.failed` | `csv_import_rows_failed_total` |
| `csv.import.job.duration_ms` | `csv_import_job_duration_ms_{bucket,sum,count}` |

You may confirm with the Prometheus API on port `9090`:

```sh
curl -s 'http://localhost:9090/api/v1/label/__name__/values' \
  | jq -r '.data[] | select(startswith("charter_") or startswith("csv_"))'
```

## Traces

Each HTTP request opens a server span named `{method} {route}`. Handlers open nested spans named `charter.handler.{action}`, tagged with `charter.resource`, `charter.action`, `charter.user.role`, `charter.auth.method`, and `charter.error.code` when an error is classified. Database work opens spans named `db.{operation}` (for example `db.select`).

Set `CHARTER_OTEL_CAPTURE_SQL` when you want `db.query.text` on those database spans. It is off by default because query text can include values from your rows.

## Optional settings

- `RUST_LOG`: filter what goes to logs (and into the log exporter when OTLP is on). Examples: `info`, `info,charter_server=debug`. A valid value overrides `-v`.
- `CHARTER_OTEL_CAPTURE_SQL`: when truthy, attaches SQL text on database spans.
- Standard `OTEL_EXPORTER_OTLP_*` headers, protocol, timeout, and compression variables from the OpenTelemetry SDK also apply.

Full reference: [Environment variables](/docs/environment-variables/).

## Turn export off

Unset `OTEL_EXPORTER_OTLP_ENDPOINT` or leave it blank, then restart Charter. Telemetry export stops; logs stay on standard error.
