Skip to content

Fix the duplicate chat spans issues and http spans not propagating in langchain#147

Merged
rads-1996 merged 4 commits into
microsoft:mainfrom
rads-1996:http-spans-propagat-langchain
May 18, 2026
Merged

Fix the duplicate chat spans issues and http spans not propagating in langchain#147
rads-1996 merged 4 commits into
microsoft:mainfrom
rads-1996:http-spans-propagat-langchain

Conversation

@rads-1996
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings May 15, 2026 17:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses LangChain tracing issues by ensuring HTTP/OpenAI child spans inherit the correct parent context (via runtime context attach/detach) and by preventing duplicate OpenAI v2 spans when overlapping GenAI instrumentations are present.

Changes:

  • Attach/detach LangChain run spans to the OpenTelemetry runtime context to improve downstream HTTP span parenting, and force LangChain tracer callbacks to run inline.
  • Add distro logic to auto-disable openai (v2) instrumentation when overlapping libraries are detected, while preserving explicit user configuration.
  • Extend/adjust tests to validate the new behavior (context attach/detach, openai auto-disable rules, and langchain instrumentor no-op behavior).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/test_distro.py Updates end-to-end expectations and adds unit tests for openai v2 auto-disable behavior.
tests/langchain/test_tracer.py Adds tests covering runtime context attach/detach and run_inline behavior.
tests/langchain/test_tracer_instrumentor.py Adjusts expectations for instrumentor behavior when langchain is unavailable.
src/microsoft/opentelemetry/_utils.py Introduces _disable_openai_v2_instrumentation() helper to auto-disable openai v2 when overlaps exist.
src/microsoft/opentelemetry/_genai/_langchain/_tracer.py Attaches LangChain spans to runtime context and detaches on end to fix child-span parenting.
src/microsoft/opentelemetry/_distro.py Calls _disable_openai_v2_instrumentation() during instrumentation setup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_distro.py
Comment thread tests/test_distro.py Outdated
Comment thread src/microsoft/opentelemetry/_utils.py
Comment thread src/microsoft/opentelemetry/_utils.py Outdated
Comment thread src/microsoft/opentelemetry/_genai/_langchain/_tracer.py Outdated
@rads-1996 rads-1996 requested a review from Copilot May 15, 2026 18:13
@rads-1996
Copy link
Copy Markdown
Member Author

HTTP spans disabled -

Screenshot 2026-05-15 083014

HTTP spans enabled -
Screenshot 2026-05-15 105858

@rads-1996
Copy link
Copy Markdown
Member Author

Before -

image

After -
image

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread src/microsoft/opentelemetry/_genai/_langchain/_tracer.py Outdated
Comment thread tests/test_distro.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/microsoft/opentelemetry/_utils.py:171

  • This auto-disables openai whenever langchain_core/agent_framework is importable, even if the user has explicitly disabled the conflicting instrumentation via instrumentation_options (e.g. {'langchain': {'enabled': False}}). That can unexpectedly turn off OpenAI tracing in apps that merely have LangChain installed. Consider also checking whether langchain / agent_framework instrumentation is enabled before forcing openai.enabled=False (consistent with _is_instrumentation_enabled in _distro.py).
    overlapping_present = any(
        find_spec(import_name) is not None for import_name in ("langchain_core", "agent_framework")
    )

    if not overlapping_present:
        return

Comment thread tests/test_distro.py
Comment thread src/microsoft/opentelemetry/_utils.py
@rads-1996 rads-1996 force-pushed the http-spans-propagat-langchain branch from 4d0f46c to e63caad Compare May 18, 2026 19:06
@rads-1996 rads-1996 merged commit 222284e into microsoft:main May 18, 2026
9 checks passed
@rads-1996 rads-1996 mentioned this pull request May 18, 2026
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.

3 participants