refactor(telemetry): collapse 6 OpenTelemetry no-op stubs into one proxy#286
Merged
Conversation
When opentelemetry-api is not installed, telemetry fell back to six hand-written no-op stand-ins (_NoOpSpan / _NoOpTracer / _NoOpStatus / _NoOpStatusCode / _NoOpHistogram / _NoOpMeter). Replace them with a single _NoOp proxy that returns itself for every attribute access and call, covering span/tracer/meter/histogram usage plus Status(StatusCode.ERROR, ...) construction and the StatusCode.ERROR attribute. _noop_use_span stays separate: it must yield the passed-in span, which a self-returning proxy would not. Add test_telemetry_noop.py exercising every call shape the telemetry module uses against the proxy; the import-fallback path previously had no test coverage. No behavior change. The real-OpenTelemetry path is untouched.
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
When
opentelemetry-apiis not installed,telemetry.pyfalls back to six hand-written no-op stand-ins —_NoOpSpan,_NoOpTracer,_NoOpStatus,_NoOpStatusCode,_NoOpHistogram,_NoOpMeter. They're near-identical discard-everything boilerplate.This replaces all six with a single
_NoOpproxy that returns itself for every attribute access and call:Net −36 lines (
telemetry.py: +24 / −60).Why it's safe
Every call site the telemetry module makes against these objects is covered by the proxy, and all of them discard return values:
with tracer.start_as_current_span(...) as span:→__getattr__→__call__→__enter__all return self;span.set_attribute / set_attributes / add_event / record_exception / set_status / end(...)resolve through__getattr__/__call__.Status(StatusCode.ERROR, str(exc))→StatusCode.ERRORis the real class attr"ERROR";Status(...)instantiates_NoOpand discards args.meter.create_histogram(...).record(...)chains through the proxy.__enter__,__call__) are looked up on the type, so__getattr__never shadows them. No==/identity checks are made onStatusobjects._noop_use_spanis kept as a separate function — it must yield the passed-in span, which a self-returning proxy would not. The real-OpenTelemetry path is untouched. No behavior change.Tests
Adds
tests/unit_tests/test_telemetry_noop.pydriving the_NoOpproxy through every call shape the module uses. The import-fallback path previously had no coverage (the suite always runs withopentelemetryinstalled).make cigreen: 676 passed, coverage 81.76%, ruff + format + mypy + bandit clean.Context
This is the single surviving cleanup from a repo-wide over-engineering audit. The audit's other candidates — including a "protocol client dedup" — were investigated and deliberately not done: the
chatcompletions/openresponsesprotocols are a template-driven extension point and only ~14% identical, so merging them would fight the design.