opentelemetry-sdk: Add ability to refresh process sensitive Resource attributes#5280
Open
herin049 wants to merge 9 commits into
Open
opentelemetry-sdk: Add ability to refresh process sensitive Resource attributes#5280herin049 wants to merge 9 commits into
herin049 wants to merge 9 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds fork-safety for process-sensitive SDK Resource attributes (e.g., process.pid) by re-running process-sensitive resource detectors in child processes and propagating refreshed resources into trace, metrics, and logs providers (including already-created tracers/loggers). It also introduces provider-level update_resource() APIs and adds subprocess-based fork tests.
Changes:
- Add
ResourceDetector.is_process_sensitive()and_get_process_sensitive_resource()to selectively re-run only process-sensitive detectors. - Add
update_resource()toTracerProvider,MeterProvider, andLoggerProvider, and wireos.register_at_fork(after_in_child=...)to refresh resources in child processes. - Add fork-based subprocess tests plus direct
update_resource()unit tests for traces, metrics, and logs.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| opentelemetry-sdk/src/opentelemetry/sdk/resources/init.py | Adds process-sensitivity marker on detectors and helper to aggregate only process-sensitive resources. |
| opentelemetry-sdk/src/opentelemetry/sdk/trace/init.py | Adds tracer/provider resource refresh on fork and public update_resource() that updates existing tracers. |
| opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/init.py | Adds meter provider resource refresh on fork and update_resource() support. |
| opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/init.py | Adds logger provider resource refresh on fork, update_resource(), and propagation to active loggers. |
| opentelemetry-sdk/tests/resources/test_resources.py | Adds unit tests for process-sensitivity defaults and _get_process_sensitive_resource(). |
| opentelemetry-sdk/tests/trace/test_trace.py | Adds TracerProvider.update_resource() test and fork subprocess test. |
| opentelemetry-sdk/tests/trace/scripts/tracer_provider_resource_after_fork.py | New subprocess script validating trace resource PID refresh after fork. |
| opentelemetry-sdk/tests/metrics/test_metrics.py | Adds MeterProvider.update_resource() test and fork subprocess test. |
| opentelemetry-sdk/tests/metrics/scripts/meter_provider_resource_after_fork.py | New subprocess script validating metric resource PID refresh after fork. |
| opentelemetry-sdk/tests/logs/test_logs.py | Adds LoggerProvider.update_resource() test and fork subprocess test. |
| opentelemetry-sdk/tests/logs/scripts/logger_provider_resource_after_fork.py | New subprocess script validating log resource PID refresh after fork. |
| .changelog/5280.added | Changelog entry for the new fork/resource refresh capability. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Description
This PR adds support for refreshing process sensitive SDK resources after
os.fork(). It introduces provider level resource updates for traces, metrics, and logs, marks process resource detection as process sensitive and usesos.register_at_fork()to re-run any process sensitive resource detectors in child processes. This ensures attributes such asprocess.pidare updated for newly emitted spans, metrics, and logs after a fork.Changes
update_resource()support for SDK tracer, meter and logger providers.os.fork()usingos.register_at_fork().Open Questions
Should
update_resource()be part of the public SDK provider API or should resource refresh remain an internal implementation detail for fork handling? An argument for making it public is if users want to have the ability to update the resource on providers in reaction to other changes.Fixes #5279
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Does This PR Require a Contrib Repo Change?
Checklist: