Skip to content

Add native OpenTelemetry client telemetry#1052

Open
Kludex wants to merge 11 commits into
mainfrom
codex/native-opentelemetry
Open

Add native OpenTelemetry client telemetry#1052
Kludex wants to merge 11 commits into
mainfrom
codex/native-opentelemetry

Conversation

@Kludex

@Kludex Kludex commented Jun 27, 2026

Copy link
Copy Markdown
Member

Summary

Adds native OpenTelemetry client telemetry for httpx2 without adding a runtime dependency unless users install the opentelemetry extra.

  • Adds a private OpenTelemetry adapter that lazily imports opentelemetry-api and emits current stable HTTP client spans and http.client.request.duration metrics.
  • Wires sync and async client request paths through the adapter while keeping the disabled path cheap.
  • Adds httpx2[opentelemetry] with opentelemetry-api only.
  • Adds Logfire-backed tests for emitted spans, metrics, propagation, suppression, exceptions, and opt-in header capture/sanitization.

Validation

  • scripts/check
  • uv run pytest tests/httpx2 -q
  • uv run pytest tests/httpx2/test_opentelemetry.py -q

Review in cubic

@github-actions

Copy link
Copy Markdown

Docs preview:

@Kludex Kludex force-pushed the codex/native-opentelemetry branch from aa1ba0f to 653b450 Compare June 27, 2026 08:40
@codspeed-hq

codspeed-hq Bot commented Jun 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing codex/native-opentelemetry (508b0bf) with main (70fac53)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aa1ba0fc4b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpx2/httpx2/_client.py Outdated
Comment thread src/httpx2/httpx2/_opentelemetry.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

1 issue found across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_opentelemetry.py Outdated
Comment thread tests/httpx2/test_opentelemetry.py Outdated
@Kludex Kludex force-pushed the codex/native-opentelemetry branch from d493ddb to 4f5477d Compare June 27, 2026 10:00
@Kludex Kludex force-pushed the codex/native-opentelemetry branch from 4f5477d to 28cde97 Compare June 27, 2026 11:16
Comment thread src/httpx2/httpx2/_client.py Outdated
Comment on lines +1019 to +1037
otel = get_opentelemetry()
if otel is None or not otel.is_enabled(request):
response = transport.handle_request(request)
else:
trace = otel.start_request(request)
try:
response = transport.handle_request(request)
trace.set_response(response)
except BaseException as exc:
trace.set_exception(exc)
trace.detach_current(type(exc), exc, exc.__traceback__)
trace.close()
raise
trace.detach_current()
if response.is_closed:
trace.close()
else:
assert isinstance(response.stream, SyncByteStream)
response.stream = trace.wrap_sync_stream(response.stream)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This seems extremely complex. We are not merging this PR with this. I'll see what to do here.

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

1 issue found across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_client.py Outdated
Comment thread pyproject.toml Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

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.

1 issue found across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_opentelemetry.py Outdated
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.

1 participant