Skip to content

Create OTel Exporter for LangChain tracing - #1568

Merged
Angus Jelinek (angus-langchain) merged 22 commits into
mainfrom
angus/otel-poc
Mar 19, 2025
Merged

Angus Jelinek (angus-langchain) merged 22 commits into
mainfrom
angus/otel-poc

Conversation

@angus-langchain

@angus-langchain Angus Jelinek (angus-langchain) commented Mar 11, 2025 •

Copy link
Copy Markdown
Contributor

Add OpenTelemetry Export Support to LangSmith

Purpose

Folks want to natively trace LangChain and LangGraph through our sdk. The implementation follows OpenTelemetry GenAI Semantic Conventions

Usage

The OTEL integration can be enabled by:

Installing the OTEL dependencies: pip install langsmith[otel]
Setting environment variable: LANGSMITH_OTEL_ENABLED=true

By default, the exporter will send data to the LangSmith API OTEL endpoint, but this can be customized by setting standard OTEL environment variables:

OTEL_EXPORTER_OTLP_ENDPOINT: Override the endpoint URL
OTEL_EXPORTER_OTLP_HEADERS: Add custom headers (API keys are added automatically)
OTEL_SERVICE_NAME: Set a custom service name (defaults to "langsmith")

Depends on https://github.com/langchain-ai/langchainplus/pull/8582 (including integration test passing)

Comment thread python/pyproject.toml Outdated
[tool.poetry]
name = "langsmith"
version = "0.3.13"
version = "0.3.14rc1"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will rebase to current version before merging

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not up to speed with the gen ai OTEL semantic conventions but looks reasonable (cc Alex Kira (@akira) to review more)

Comment thread python/tests/integration_tests/test_client.py Outdated
Comment thread python/langsmith/_internal/_background_thread.py Outdated
if client.otel_exporter is not None:
_otel_tracing_thread_handle_batch(client, tracing_queue, next_batch)
else:
_tracing_thread_handle_batch(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to have both OTEL and regular LangSmith tracing or would only go to one or the other?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one or the other, but ls accepts the otel messages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack on "one or the other", but the latter point "accepts the otel messages" is probably not that useful unless I missed something.

If you run it with opentelemetry-instrument to bootstrap the otel exporters, both langsmith and other instrumentation will export to the same collector. That's true.

However, they will be in different traces as the tracer implementation here is an export only-concern. Meaning it cannot propagate trace context to normal otel instrumentation. For that reason, they'd be in different traces, and likely less useful as a result.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adrian Cole (@codefromthecrypt) I was referring to LangSmith if a user wanted to fan out on receiver to multiple OTel backends. ack that this implementation is export only, but it should still be possible to inject headers into LangSmith OTel tracing. Would be happy to work with you as a design partner to discuss future features for distributed tracing with you though!

# Set GenAI attributes according to OTEL semantic conventions
# Set gen_ai.operation.name
operation_name = run_info.get("run_type", "chain")
span.set_attribute(GEN_AI_OPERATION_NAME, operation_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the mappings for this, would it come through as "chat", etc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapping in _get_operation_name


# Additional GenAI attributes
GEN_AI_REQUEST_STREAMING = "gen_ai.request.streaming"
GEN_AI_REQUEST_HEADERS = "gen_ai.request.headers"

@akira Alex Kira (akira) Mar 18, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these genai semconv or should they be langsmith prefixed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

langsmith prefixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants