Skip to content

OpenTelemetry metrics support#846

Open
KirillKurdyukov wants to merge 34 commits into
mainfrom
opentelemetry-metrics-support
Open

OpenTelemetry metrics support#846
KirillKurdyukov wants to merge 34 commits into
mainfrom
opentelemetry-metrics-support

Conversation

@KirillKurdyukov

Copy link
Copy Markdown
Contributor

Reopens #824 for main.

The original PR was merged into release_v3; this branch is rebased onto the current main.

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

The SDK supports OpenTelemetry tracing, but it does not expose client-side OpenTelemetry metrics for YDB operations, retries, or query session pools.

What is the new behavior?

This PR adds OpenTelemetry metrics support for the YDB Python SDK.

Added metrics include:

  • db.client.operation.duration for client operation latency
  • ydb.client.operation.failed for failed client operations
  • ydb.query.session.create_time for query session creation latency
  • ydb.query.session.pending_requests for session pool wait queue size
  • ydb.query.session.timeouts for session acquisition timeouts
  • ydb.query.session.count for current session pool state by idle / used
  • ydb.query.session.max for configured session pool size
  • ydb.client.retry.duration for total retry operation duration
  • ydb.client.retry.attempts for number of retry attempts per logical operation

The public OpenTelemetry API now includes:

  • enable_metrics(meter_provider=None)
  • disable_metrics()

Metrics are independent from tracing, but both can be enabled together. YDB operation telemetry now uses a composite operation object so tracing spans and metrics can be recorded for the same SDK operation without adding an extra span level.

This PR also updates the OpenTelemetry example to export both traces and metrics through OTLP, and documents the metrics setup, emitted instruments, and metric attributes.

Other information

The OpenTelemetry tests were moved from tests/tracing to tests/opentelemetry, and metrics tests were added using OpenTelemetry SDK in-memory metric readers.

Test plan

  • CI passes on rebased branch
  • tox -e py -- tests/opentelemetry -v

Made with Cursor

@codecov-commenter

codecov-commenter commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.79412% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.89%. Comparing base (a7bf659) to head (d904f9b).

Files with missing lines Patch % Lines
ydb/opentelemetry/metrics_plugin.py 97.43% 0 Missing and 2 partials ⚠️
ydb/retries.py 93.33% 2 Missing ⚠️
ydb/aio/query/session.py 75.00% 0 Missing and 1 partial ⚠️
ydb/opentelemetry/_endpoint.py 94.11% 0 Missing and 1 partial ⚠️
ydb/opentelemetry/metrics.py 99.39% 0 Missing and 1 partial ⚠️
ydb/opentelemetry/tracing_plugin.py 88.88% 0 Missing and 1 partial ⚠️
ydb/query/session.py 92.85% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #846      +/-   ##
==========================================
+ Coverage   81.38%   81.89%   +0.51%     
==========================================
  Files          94       97       +3     
  Lines       12197    12550     +353     
  Branches     1204     1223      +19     
==========================================
+ Hits         9926    10278     +352     
+ Misses       1807     1805       -2     
- Partials      464      467       +3     
Flag Coverage Δ
integration 79.76% <97.54%> (+0.61%) ⬆️
unit 47.15% <40.93%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
ydb/aio/query/pool.py 94.96% <100.00%> (+0.68%) ⬆️
ydb/opentelemetry/__init__.py 84.00% <100.00%> (+14.76%) ⬆️
ydb/opentelemetry/tracing.py 93.96% <100.00%> (+4.64%) ⬆️
ydb/query/pool.py 92.85% <100.00%> (+0.85%) ⬆️
ydb/aio/query/session.py 87.95% <75.00%> (-0.66%) ⬇️
ydb/opentelemetry/_endpoint.py 94.11% <94.11%> (ø)
ydb/opentelemetry/metrics.py 99.39% <99.39%> (ø)
ydb/opentelemetry/tracing_plugin.py 95.58% <88.88%> (ø)
ydb/query/session.py 91.66% <92.85%> (+0.04%) ⬆️
ydb/opentelemetry/metrics_plugin.py 97.43% <97.43%> (ø)
... and 1 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tewbo and others added 2 commits July 8, 2026 17:58
@KirillKurdyukov KirillKurdyukov force-pushed the opentelemetry-metrics-support branch from ec6ea96 to 8da40a3 Compare July 8, 2026 14:58
KirillKurdyukov and others added 3 commits July 8, 2026 18:14
Cover pool/session instrumentation, public API edge cases, tracing facade,
and noop metrics paths to satisfy codecov patch requirements.

Co-authored-by: Cursor <cursoragent@cursor.com>
Save the real __import__ before monkeypatching builtins, matching the
enable_metrics import-error test and avoiding infinite recursion when
pytest imports stdlib modules during error reporting.

Co-authored-by: Cursor <cursoragent@cursor.com>
Apply black formatting and remove an unused import flagged by flake8.

Co-authored-by: Cursor <cursoragent@cursor.com>
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