fix: shut down TracerProvider in OpenTelemetryInstrument.teardown#90
Merged
Conversation
The instrument's bootstrap() created a TracerProvider, registered span processors against it, and called set_tracer_provider — but never stored a reference. teardown() only uninstrumented the instrumentors; the provider was never shut down. Spans buffered in BatchSpanProcessor were lost on graceful shutdown. Stash the provider on the instrument via object.__setattr__ (mirroring the LoggingInstrument._logger_factory pattern for runtime state on a frozen dataclass), shut it down after the uninstrument loop, reset the field to None so a subsequent bootstrap starts clean. Regression test asserts shutdown is called exactly once on the stored provider and the field is reset. Closes CRIT-2, TEST-2 from the audit.
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Summary
OpenTelemetryInstrument.bootstrap()now stashes theTracerProviderit created on the instance (viaobject.__setattr__, mirroringLoggingInstrument._logger_factory).teardown()callsself._tracer_provider.shutdown()after the existing instrumentor uninstrument loop, then resets the field toNone. Spans buffered inBatchSpanProcessornow flush on graceful shutdown.test_opentelemetry_instrument_teardown_shuts_down_tracer_providerpatchesshutdownon the stashed provider and asserts it's invoked once and the field resets. Test fails onmain, passes on this branch.Closes CRIT-2 and TEST-2 from an internal audit of the codebase.
Test plan
just test -- tests/instruments/test_opentelemetry_instrument.py -v— three tests pass.just test— full suite 80/80.just lint— clean (eof-fixer, ruff format, ruff check, ty check).object.__setattr__usage match theLoggingInstrument._logger_factoryprecedent.Known follow-up
If
shutdown()raises,_tracer_provideris left non-None — same shape asLoggingInstrument.teardown(also a pre-existing pattern). The follow-up idempotency work (a separate PR addressing CRIT-3 from the audit) should wrap both teardowns intry/finallyso a failed shutdown still resets the cached reference.🤖 Generated with Claude Code