Skip to content

Reuse no-op metric instruments - #1732

Merged
fractalwrench merged 1 commit into
open-telemetry:mainfrom
benjibunbun:fix/noop-metric-singletons
Nov 27, 2026
Merged

fractalwrench merged 1 commit into
open-telemetry:mainfrom
benjibunbun:fix/noop-metric-singletons

Conversation

@benjibunbun

@benjibunbun benjibunbun commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Goal

Return one shared instance per no-op metric type instead of allocating on each factory call. Use an empty name and null unit/description, and document these placeholders.

Fixes #1727.

Testing

  • ./gradlew verify assembleAndroidTest passed: 8,843 test executions across JVM, Node.js and iOS, plus static analysis, ABI and documentation checks.
  • Instance reuse, metadata and concurrent-sharing checks fail before the fix and pass afterward. Additional tests cover inert callbacks and shared lifetimes.

Android device tests and the separate CI consumer matrix were not run locally.

Codex assisted with implementation, tests, and review.

Return shared instances with placeholder metadata and cover reuse,
inert callbacks, and shared lifecycle behavior.

Fixes open-telemetry#1727

Assisted-by: Codex (GPT-6)
@benjibunbun
benjibunbun requested a review from a team as a code owner November 27, 2026 02:56
@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: benjibunbun / name: ben (b986eb1)

Copilot AI 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.

🟢 Approval recommended

No active review issues block the change to reuse shared no-op metric instruments.

No open issues 🎉

What changed in this PR

Reduces allocations in the Kotlin Multiplatform no-op metrics implementation by returning shared instruments, addressing #1727.

Changes:

  • Converts ten no-op instrument types to singletons and updates NoopMeter factories to reuse them.
  • Documents placeholder metadata: an empty name and null unit/description.
  • Adds tests for instance reuse, concurrent access, ignored callbacks, and shared lifetimes; updates existing metadata assertions.
File Description
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​NoopTests.kt Updates up-down counter metadata expectations.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableLongUpDownCounterTest.kt Expects placeholder metadata.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableLongGaugeTest.kt Expects placeholder metadata.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableDoubleUpDownCounterTest.kt Expects placeholder metadata.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableDoubleGaugeTest.kt Expects placeholder metadata.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopMeterTest.kt Adds reuse, concurrency, callback, and lifetime tests.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopLongGaugeTest.kt Expects placeholder metadata.
noop/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopDoubleGaugeTest.kt Expects placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableLongUpDownCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableLongGauge.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableLongCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableDoubleUpDownCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableDoubleGauge.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopObservableDoubleCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopMeter.kt Returns shared instruments instead of constructing instances.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopLongUpDownCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopLongGauge.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopDoubleUpDownCounter.kt Converts to a singleton with placeholder metadata.
noop/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​NoopDoubleGauge.kt Converts to a singleton with placeholder metadata.
noop/​README.md Documents sharing, placeholders, and no-op behavior.
implementation/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​UpDownCounterImplTest.kt Updates invalid-input fallback metadata assertions.
implementation/​src/​commonTest/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​MeterProviderImplTest.kt Updates no-op instrument metadata assertions.
docs/​configuration.md Explains shared instruments and metadata when disabled.
api/​src/​commonMain/​kotlin/​io/​opentelemetry/​kotlin/​metrics/​Instrument.kt Clarifies no-op metadata exceptions in API documentation.

Review effort: Balanced

@fractalwrench fractalwrench left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM - thanks!

@fractalwrench
fractalwrench enabled auto-merge (squash) November 27, 2026 09:35

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing Jamie! Glad I could help.

@fractalwrench
fractalwrench merged commit fd845b7 into open-telemetry:main Nov 27, 2026
9 checks passed
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.24%. Comparing base (7553812) to head (b986eb1).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1732   +/-   ##
=======================================
  Coverage   91.24%   91.24%           
=======================================
  Files         578      578           
  Lines        9226     9218    -8     
  Branches     1684     1684           
=======================================
- Hits         8418     8411    -7     
  Misses        407      407           
+ Partials      401      400    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Avoid allocations in noop metrics implementation

3 participants