otlpHttpExporter to create an onExport callback and pass it to Tracer — every finished trace is serialised to the OTLP JSON format and POSTed to your collector in the background.
How export works
When aTracer is constructed with an onExport callback, that callback fires once per trace, immediately after the agent run ends and traceHandle.end() is called. The otlpHttpExporter factory returns exactly such a callback: it converts the Trace object into an OTLP resourceSpans payload and sends it via fetch. Export is fire-and-forget — failures are logged to console.error but never propagate to the request, so a collector outage cannot break your API.
Setting up the exporter
tracer to defineAgent as usual. Every completed trace is now forwarded to your collector alongside being stored locally.
OtlpExporterOptions
The OTLP/HTTP endpoint to POST traces to. This is the
/v1/traces path on your collector. Common values:| Backend | Endpoint |
|---|---|
| Datadog | https://http-intake.logs.datadoghq.com/api/v2/otlp |
| Grafana Tempo | https://<your-stack>.grafana.net/otlp/v1/traces |
| Langfuse | https://cloud.langfuse.com/api/public/otel/v1/traces |
| Braintrust | https://api.braintrustdata.com/otel/v1/traces |
| Local collector | http://localhost:4318/v1/traces |
HTTP headers added to every export request. Use this to pass API keys, tokens, or dataset identifiers required by your backend.
The
service.name resource attribute included in every exported payload. Set this to identify your application in the backend’s UI.Converting traces manually with traceToOtelSpans
If you need to control the HTTP transport yourself, usetraceToOtelSpans directly to obtain the OTLP payload and send it with your own client:
traceToOtelSpans(trace, serviceName?) returns an OtlpResourceSpans object ready to be nested under { resourceSpans: [...] } in the OTLP JSON body.
GenAI semantic convention attributes
Anvil maps span fields to the OpenTelemetry GenAI semantic conventions. The following attributes are emitted onmodel spans:
| OTel attribute | Source |
|---|---|
gen_ai.system | span.attributes.provider |
gen_ai.request.model | span.attributes.model |
gen_ai.response.model | span.attributes.model |
gen_ai.usage.input_tokens | span.attributes.usage.inputTokens |
gen_ai.usage.output_tokens | span.attributes.usage.outputTokens |
gen_ai.usage.cost | span.attributes.costUsd |
gen_ai.operation.name | span.kind |
gen_ai.tool.name from span.attributes.name.
Sending to multiple backends
Compose multiple exporters inside a singleonExport callback:
Both exporters run concurrently and independently. A failure in one does not affect the other, and neither can throw back into the request path because export errors are caught and logged internally.
OtlpResourceSpans type reference
For advanced use cases, the full OTLP type shapes are exported fromanvil/trace: