# OpenTelemetry Collector

Export traces from a Collector over OTLP/HTTP and set the customer on spans with a processor.

AI Spendbook documentation, Your app's data. Web page: https://www.aispendbook.com/docs/quickstart-otel-collector

If your services already send traces to an OpenTelemetry Collector, add an `otlphttp` exporter that sends them to the [OTLP receiver](https://www.aispendbook.com/docs/opentelemetry) at `https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces`, with an ingestion key in the `Authorization` header. The receiver has not been run against a live collector.

```yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318

processors:
  # Copy your own tenant attribute onto the names AI Spendbook reads.
  transform/spendbook:
    trace_statements:
      - context: span
        statements:
          - set(attributes["spendbook.customer_id"], attributes["tenant.id"]) where attributes["tenant.id"] != nil
          - set(attributes["spendbook.feature"], resource.attributes["service.name"]) where attributes["spendbook.feature"] == nil
  # At most 500 usage spans are accepted per request.
  batch:
    send_batch_size: 200
    send_batch_max_size: 500

exporters:
  otlphttp/spendbook:
    traces_endpoint: https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces
    headers:
      Authorization: "Bearer ${env:SPENDBOOK_API_KEY}"
    compression: gzip   # or none
    encoding: proto     # or json

service:
  pipelines:
    traces/spendbook:
      receivers: [otlp]
      processors: [transform/spendbook, batch]
      exporters: [otlphttp/spendbook]
```

## The exporter

| Setting | Value |
| --- | --- |
| traces_endpoint | https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces. With endpoint instead, set https://www.aispendbook.com/api/v1/ingest/otlp and the exporter adds /v1/traces. |
| headers.Authorization | Bearer followed by an ingestion key (afk_). Keep the key in an environment variable or your secret store. |
| compression | gzip or none. Other compressions are answered with 415. |
| encoding | proto or json. The response comes back in the same encoding. |
| batch | At most 500 usage spans per request. The body limit is 5 MB, also after gzip is decompressed. |

## Setting the customer on spans

- The receiver reads `spendbook.customer_id` and `spendbook.feature` from each span's attributes, then from the resource's attributes. A span with neither arrives unattributed.
- The `transform` processor above copies a `tenant.id` attribute your code already sets onto `spendbook.customer_id`, and uses the service name as the feature where none is set. Replace `tenant.id` with your own attribute name.
- The `transform` and `attributes` processors ship in the Collector's contrib distribution (`otelcol-contrib`).

```yaml
processors:
  # The same with the attributes processor: one fixed customer for everything this collector sends.
  attributes/spendbook:
    actions:
      - key: spendbook.customer_id
        value: acme
        action: upsert
```

## What the receiver keeps

- Spans with model usage attributes (`gen_ai.*`, the OpenLLMetry names or `llm.*`) become usage events. Other spans are ignored, so you can send a whole pipeline. Agent, workflow, chain and tool wrapper spans are ignored too, so usage is not counted twice.
- A span sent again is a duplicate, never a second event. The event id is built from the trace id and span id, so collector retries are safe.
- The response carries `X-Spans-Accepted`, `X-Spans-Duplicates`, `X-Spans-Ignored` and `X-Spans-Rejected` headers, and rejected spans are listed in `partialSuccess`. The full mapping is on the [OpenTelemetry traces](https://www.aispendbook.com/docs/opentelemetry) page.
- One collector can send the same traces to AI Spendbook and to Datadog or Honeycomb; the fan-out config is on [Vercel AI SDK, LangChain and agents](https://www.aispendbook.com/docs/agent-frameworks).

## Check it works

1. In the app, open **Integrations**. Under **Add a source**, find **SDK / HTTP ingestion** and choose **Set up**.
2. Go to the **Test event** step. Use the key you just created, or paste the one your service uses, and choose **Send test event**.
3. Wait for the **Test event received** notice. It means the key and the ingestion API work. If you see **Accepted, but not visible yet**, choose **Send again** after a minute. The test event has zero tokens, so it adds no cost, and it carries the feature `spendbook-connection-test` so you can filter it out.
4. The test event checks the key. Then send one traced model call through the collector and open **Usage Explorer**: it should appear with its model, token counts and the customer the processor set. The collector's own logs show export errors, such as 401 for a wrong key or 415 for an encoding the receiver doesn't accept.
