Send LangChain and LangGraph traces to Zerobus Ingest
The agent is instrumented with the OpenInference LangChain instrumentor. The token handling comes from the oauth2client extension in the OpenTelemetry Collector Contrib distribution. For the endpoints, the table contract and the grants, see Configure OpenTelemetry (OTLP) clients and the OpenTelemetry table reference.
Maintained by: the Arize OpenInference project, in the Arize-ai/openinference repository.
Your LangChain or LangGraph agent sends its traces over OTLP to a Collector on the same host. The Collector's oauth2client extension gets an OAuth token for a Databricks service principal, refreshes it before it expires, and writes the spans to a Delta table in Zerobus Ingest. The agent needs no token code, and you don't need to run a message bus between the agent and the lakehouse.
When to use OpenInference
- Your agent runs on LangChain or LangGraph, and you want each run's spans in a Delta table.
- You want the OpenInference span kinds (
AGENT,CHAIN,LLMandTOOL) in the table, so you can query the agent's model calls and tool calls with SQL. - The agent runs for longer than one hour, so a static token would expire during the run.
This page covers traces only. OpenInference doesn't emit logs or metrics. To send logs and metrics from the same process, see Send data to Zerobus Ingest via the OpenTelemetry Collector.
How it works
The OpenInference instrumentor hooks into LangChain's callbacks, so each run of a chain, agent, model or tool produces a span. The application exports the spans over OTLP/HTTP to the Collector on 127.0.0.1:4318.
The Collector has one traces pipeline. It exports the spans over gRPC to Zerobus Ingest through an oauth2client extension, which gets a token scoped to the spans table. The x-databricks-zerobus-table-name header names that table.
The Collector holds accepted spans in memory until it exports them. If the Collector stops before it exports them, those spans are lost.
Zerobus Ingest delivers each record at least one time when the client retries records that it hasn't received an acknowledgment for, so a retry can write a row more than one time. See Delivery guarantees.
Requirements
- A workspace in a region that supports Zerobus Ingest. See Availability.
- Target tables that meet the Table requirements. The table in Create the table and grant access is a managed Delta table. For the endpoint and the workspace URL, see Use Zerobus Ingest.
- Permission to create tables in the schema, and access to Databricks SQL to run the table statement.
- A Databricks service principal with an OAuth secret and the Workspace access entitlement. See Configure authentication. It needs
USE CATALOGon the catalog,USE SCHEMAon the schema, andMODIFYandSELECTon the spans table.ALL PRIVILEGESisn't enough. See Create a service principal and grant permissions. - Databricks Runtime 17.3 or above to create and query the table. The table definition in Create the table and grant access turns on variant shredding. See Use variant shredding.
- The OpenTelemetry Collector Contrib distribution. The
oauth2clientextension is in Contrib, not in the core distribution. - Python 3.12, with
pip, in the agent's environment. - An agent built with LangChain 1.5.0 or LangGraph 1.2.22, the versions tested. The instrumentor hooks into
langchain-core, which both use.
Send LangChain traces to Zerobus Ingest
Create the table and grant access
Replace <catalog>.<schema>.<prefix> with your catalog, schema, and table prefix, then run the statement in Databricks SQL. The table is named <prefix>_otel_spans, the same name as in Send data to Zerobus Ingest via the OpenTelemetry Collector.
CREATE TABLE <catalog>.<schema>.<prefix>_otel_spans (
record_id STRING,
time TIMESTAMP,
date DATE,
service_name STRING,
trace_id STRING,
span_id STRING,
trace_state STRING,
parent_span_id STRING,
flags INT,
name STRING,
kind STRING,
start_time_unix_nano LONG,
end_time_unix_nano LONG,
attributes VARIANT,
dropped_attributes_count INT,
events ARRAY<STRUCT<
time_unix_nano: LONG,
name: STRING,
attributes: VARIANT,
dropped_attributes_count: INT
>>,
dropped_events_count INT,
links ARRAY<STRUCT<
trace_id: STRING,
span_id: STRING,
trace_state: STRING,
attributes: VARIANT,
dropped_attributes_count: INT,
flags: INT
>>,
dropped_links_count INT,
status STRUCT<
message: STRING,
code: STRING
>,
resource STRUCT<
attributes: VARIANT,
dropped_attributes_count: INT
>,
resource_schema_url STRING,
instrumentation_scope STRUCT<
name: STRING,
version: STRING,
attributes: VARIANT,
dropped_attributes_count: INT
>,
span_schema_url STRING
) USING DELTA
CLUSTER BY (time, service_name, trace_id)
TBLPROPERTIES (
'otel.schemaVersion' = 'v2',
'delta.checkpointPolicy' = 'classic',
'delta.enableVariantShredding' = 'true', -- optional
'delta.feature.variantShredding' = 'supported', -- optional
'delta.feature.variantType' = 'supported' -- optional
);
Then grant the service principal USE CATALOG on the catalog, USE SCHEMA on the schema, and MODIFY and SELECT on the table. Replace <service-principal-uuid> with the service principal's application ID.
GRANT USE CATALOG ON CATALOG <catalog> TO `<service-principal-uuid>`;
GRANT USE SCHEMA ON SCHEMA <catalog>.<schema> TO `<service-principal-uuid>`;
GRANT MODIFY, SELECT ON TABLE <catalog>.<schema>.<prefix>_otel_spans TO `<service-principal-uuid>`;
Install the Collector Contrib
Download the Contrib archive for your platform from the OpenTelemetry Collector releases page, along with the .sha256 file published next to it. This page uses version 0.165.0 for Linux on x86-64, so the archive is otelcol-contrib_0.165.0_linux_amd64.tar.gz. Before you extract the archive, compare its SHA-256 hash with the hash in that file. Then extract the archive and check that the binary has the extension:
tar -xzf otelcol-contrib_0.165.0_linux_amd64.tar.gz
./otelcol-contrib --version
./otelcol-contrib components | grep oauth2client
Create the Collector configuration
Set these variables in the shell where you start the Collector:
Variable | Value |
|---|---|
| The Databricks service principal's application ID ( |
| The Databricks service principal's OAuth secret |
| Your workspace URL without |
| Your workspace ID ( |
| Your Zerobus Ingest endpoint host, without |
| Your catalog, schema, and table prefix ( |
To find your values, see Get your workspace URL and Zerobus Ingest endpoint.
Your endpoint host is <workspace-id>.zerobus.<region>.gcp.databricks.com.
Save this as collector.yaml. The file reads the endpoint, workspace, credentials and table names from the variables that you set, so you set those variables for your workspace and the file itself stays the same. The receiver listens on 127.0.0.1 only, on ports 4317 (gRPC) and 4318 (HTTP). The agent in Instrument the agent uses the HTTP port.
extensions:
oauth2client/spans:
client_id: ${env:DATABRICKS_CLIENT_ID}
client_secret: ${env:DATABRICKS_CLIENT_SECRET}
token_url: https://${env:WORKSPACE_URL}/oidc/v1/token
scopes: ['all-apis']
endpoint_params:
resource: 'api://databricks/workspaces/${env:WORKSPACE_ID}/zerobusDirectWriteApi'
authorization_details:
- '[{"type":"unity_catalog_privileges","privileges":["USE CATALOG"],"object_type":"CATALOG","object_full_path":"${env:CATALOG}"},{"type":"unity_catalog_privileges","privileges":["USE SCHEMA"],"object_type":"SCHEMA","object_full_path":"${env:CATALOG}.${env:SCHEMA}"},{"type":"unity_catalog_privileges","privileges":["SELECT","MODIFY"],"object_type":"TABLE","object_full_path":"${env:CATALOG}.${env:SCHEMA}.${env:TABLE_PREFIX}_otel_spans"}]'
exporters:
otlp_grpc/spans:
endpoint: ${env:ZEROBUS_ENDPOINT}:443
auth:
authenticator: oauth2client/spans
headers:
x-databricks-zerobus-table-name: '${env:CATALOG}.${env:SCHEMA}.${env:TABLE_PREFIX}_otel_spans'
receivers:
otlp:
protocols:
grpc:
endpoint: 127.0.0.1:4317
http:
endpoint: 127.0.0.1:4318
processors:
batch:
timeout: 5s
send_batch_size: 10
service:
extensions: [oauth2client/spans]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp_grpc/spans]
Start the Collector
./otelcol-contrib --config collector.yaml
When the log shows Everything is ready. Begin running and processing data., the receiver accepts OTLP on 127.0.0.1:4317 and 127.0.0.1:4318. The loopback address means only applications on the same host can send to the receiver. A receiver that other hosts must reach needs TLS and an authenticator on the otlp receiver, plus a network policy that limits access. See the OpenTelemetry Collector configuration reference.
Instrument the agent
Install the packages in the agent's environment, after its own dependencies:
pip install openinference-instrumentation-langchain==0.1.87 opentelemetry-sdk==1.45.2 opentelemetry-exporter-otlp-proto-http==1.45.2
Add a module that sets up the tracer provider and the OpenInference instrumentor. The OTLP/HTTP exporter reads OTEL_EXPORTER_OTLP_ENDPOINT unless OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is set (see Run the agent), so the agent needs no endpoint in its code.
import os
from openinference.instrumentation.langchain import LangChainInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor
def build_provider(service_name: str | None = None) -> trace_sdk.TracerProvider:
"""Return a tracer provider that exports every span to the Collector."""
name = service_name or os.environ.get("OTEL_SERVICE_NAME", "langchain-agent")
provider = trace_sdk.TracerProvider(resource=Resource.create({"service.name": name}))
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
return provider
def instrument(provider: trace_sdk.TracerProvider) -> None:
"""Turn on OpenInference tracing for LangChain and LangGraph runs."""
LangChainInstrumentor().instrument(tracer_provider=provider)
def setup_tracing(service_name: str | None = None) -> trace_sdk.TracerProvider:
provider = build_provider(service_name)
trace_api.set_tracer_provider(provider)
instrument(provider)
return provider
Call setup_tracing() one time, before you create the agent or the graph. Every LangChain and LangGraph run after that call produces spans. Call provider.shutdown() before the process exits, so the last batch is exported.
Run the agent
Point the agent at the Collector's HTTP port, then start the agent the way you normally do:
export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318
export OTEL_SERVICE_NAME=<service-name>
python <your-agent>.py
If your environment sets OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, unset it before you run the agent. When that variable is set, the trace exporter in opentelemetry-exporter-otlp-proto-http 1.45.2 sends the spans to the endpoint it names and ignores OTEL_EXPORTER_OTLP_ENDPOINT.
Each run sends one trace. In testing, a LangChain agent with one tool produced seven spans per run: two LLM spans, two model CHAIN spans, one TOOL span, one tools CHAIN span and one AGENT span. The count depends on how many model and tool calls a run makes.
To leave the input and output values and messages out of the spans, set OPENINFERENCE_HIDE_INPUTS=true and OPENINFERENCE_HIDE_OUTPUTS=true before you start the agent. OpenInference writes them by default. OPENINFERENCE_HIDE_INPUTS hides the input value, the input messages, the prompt strings (llm.prompts), the tool definitions (llm.tools), and the input images and documents. OPENINFERENCE_HIDE_OUTPUTS hides the output value, the output messages, and the output images and documents.
These flags don't hide a LangChain prompt template. When the agent uses a prompt template, OpenInference writes the template text to llm.prompt_template.template and the values filled into it to llm.prompt_template.variables, and no environment variable turns those off. To keep them out of the spans, pass an instance of a TraceConfig subclass as config to LangChainInstrumentor().instrument(). Give the subclass a mask(key, value) method that returns None for those two attributes and super().mask(key, value) for every other attribute, so OPENINFERENCE_HIDE_INPUTS and OPENINFERENCE_HIDE_OUTPUTS still apply. The module in Instrument the agent passes no config, so it doesn't mask these attributes.
Query the table
Run this in Databricks SQL. It counts the spans for each span name and OpenInference span kind:
SELECT
name,
kind,
variant_get(attributes, '$["openinference.span.kind"]', 'STRING') AS openinference_kind,
count(*) AS spans
FROM <catalog>.<schema>.<prefix>_otel_spans
GROUP BY name, kind, openinference_kind
ORDER BY spans DESC;
The name column holds the span name, such as the agent's name or a tool's name. The openinference.span.kind attribute is in the attributes column, which is a VARIANT. The result has one row for each span name and OpenInference kind. The agent's run appears as an AGENT row, and each tool call appears as a TOOL row.
Troubleshooting
Symptom | Cause | Fix |
|---|---|---|
The agent logs | The Collector isn't running, or | Check that the Collector is running, and that the endpoint is |
The Collector fails to start with | A second Collector on the same host uses the default port for its own metrics. | Start the Collector with |
The table has no rows for the time the Collector was down, and rows resume after it restarts | The agent's exports fail while the Collector is unreachable, and the agent's SDK drops a batch after its retries run out. | Expect a gap for the time the Collector is down. Restart the Collector as soon as you can. The Create the Collector configuration step doesn't set a persistent queue. |
The table has no rows, and the Collector log shows an authentication error | The service principal is missing one of the grants in Create the table and grant access, or its secret is wrong. | Check the grants on the table, as in Create the table and grant access. Then restart the Collector, as in Start the Collector, with the corrected |
Additional resources
- Send data to Zerobus Ingest via the OpenTelemetry Collector: the Collector setup for logs, metrics and traces from an OpenTelemetry SDK, with one table for each signal.
- Configure OpenTelemetry (OTLP) clients: the endpoints, the headers, and the static-token quick start.
- OpenTelemetry table reference: the columns in each table.
- OpenInference LangChain instrumentation: the instrumentor's README and its configuration options.