-
Notifications
You must be signed in to change notification settings - Fork 823
fix(sdk): regenerate service.instance.id post-fork in MeterProvider and TracerProvider #5000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sterchelen
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
sterchelen:fix/post-fork-service-instance-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
126 changes: 126 additions & 0 deletions
126
opentelemetry-sdk/tests/metrics/test_meter_provider_fork.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=protected-access | ||
|
|
||
| import multiprocessing | ||
| import os | ||
| import unittest | ||
| from platform import system | ||
|
|
||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.resources import Resource | ||
|
|
||
| _fork_ctx = multiprocessing.get_context("fork") if system() != "Windows" else None | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| hasattr(os, "fork"), | ||
| "needs *nix", | ||
| ) | ||
| class TestMeterProviderFork(unittest.TestCase): | ||
| def test_at_fork_reinit_changes_service_instance_id(self): | ||
| """_at_fork_reinit should assign a new service.instance.id.""" | ||
| resource = Resource({"service.instance.id": "original-id"}) | ||
| provider = MeterProvider(resource=resource) | ||
|
|
||
| original_id = provider._sdk_config.resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| self.assertEqual(original_id, "original-id") | ||
|
|
||
| provider._at_fork_reinit() | ||
|
|
||
| new_id = provider._sdk_config.resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| self.assertNotEqual(new_id, "original-id") | ||
| self.assertIsNotNone(new_id) | ||
|
|
||
| def test_at_fork_reinit_preserves_other_resource_attributes(self): | ||
| """_at_fork_reinit should not affect other resource attributes.""" | ||
| resource = Resource( | ||
| { | ||
| "service.name": "my-service", | ||
| "service.instance.id": "original-id", | ||
| "deployment.environment": "production", | ||
| } | ||
| ) | ||
| provider = MeterProvider(resource=resource) | ||
|
|
||
| provider._at_fork_reinit() | ||
|
|
||
| attrs = provider._sdk_config.resource.attributes | ||
| self.assertEqual(attrs.get("service.name"), "my-service") | ||
| self.assertEqual( | ||
| attrs.get("deployment.environment"), "production" | ||
| ) | ||
|
|
||
| def test_fork_produces_unique_service_instance_ids(self): | ||
| """Each forked worker should get a distinct service.instance.id.""" | ||
| provider = MeterProvider() | ||
|
|
||
| parent_id = provider._sdk_config.resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
|
|
||
| def child(conn): | ||
| child_id = provider._sdk_config.resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| conn.send(child_id) | ||
| conn.close() | ||
|
|
||
| parent_conn, child_conn = _fork_ctx.Pipe() | ||
| process = _fork_ctx.Process(target=child, args=(child_conn,)) | ||
| process.start() | ||
| child_id = parent_conn.recv() | ||
| process.join() | ||
|
|
||
| # Child should have a different service.instance.id than parent | ||
| self.assertNotEqual(parent_id, child_id) | ||
| self.assertIsNotNone(child_id) | ||
|
|
||
| def test_multiple_forks_produce_unique_service_instance_ids(self): | ||
| """Each of N forked workers should have a distinct service.instance.id.""" | ||
| provider = MeterProvider() | ||
|
|
||
| def child(conn): | ||
| child_id = provider._sdk_config.resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| conn.send(child_id) | ||
| conn.close() | ||
|
|
||
| ids = set() | ||
| processes = [] | ||
| conns = [] | ||
|
|
||
| for _ in range(4): | ||
| parent_conn, child_conn = _fork_ctx.Pipe() | ||
| process = _fork_ctx.Process( | ||
| target=child, args=(child_conn,) | ||
| ) | ||
| processes.append(process) | ||
| conns.append(parent_conn) | ||
| process.start() | ||
|
|
||
| for conn in conns: | ||
| ids.add(conn.recv()) | ||
|
|
||
| for process in processes: | ||
| process.join() | ||
|
|
||
| # All 4 workers should have distinct IDs | ||
| self.assertEqual(len(ids), 4) |
116 changes: 116 additions & 0 deletions
116
opentelemetry-sdk/tests/trace/test_tracer_provider_fork.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # pylint: disable=protected-access | ||
|
|
||
| import multiprocessing | ||
| import os | ||
| import unittest | ||
| from platform import system | ||
|
|
||
| from opentelemetry.sdk.resources import Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
|
|
||
| _fork_ctx = multiprocessing.get_context("fork") if system() != "Windows" else None | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| hasattr(os, "fork"), | ||
| "needs *nix", | ||
| ) | ||
| class TestTracerProviderFork(unittest.TestCase): | ||
| def test_at_fork_reinit_changes_service_instance_id(self): | ||
| """_at_fork_reinit should assign a new service.instance.id.""" | ||
| resource = Resource({"service.instance.id": "original-id"}) | ||
| provider = TracerProvider(resource=resource) | ||
|
|
||
| original_id = provider._resource.attributes.get("service.instance.id") | ||
| self.assertEqual(original_id, "original-id") | ||
|
|
||
| provider._at_fork_reinit() | ||
|
|
||
| new_id = provider._resource.attributes.get("service.instance.id") | ||
| self.assertNotEqual(new_id, "original-id") | ||
| self.assertIsNotNone(new_id) | ||
|
|
||
| def test_at_fork_reinit_preserves_other_resource_attributes(self): | ||
| """_at_fork_reinit should not affect other resource attributes.""" | ||
| resource = Resource( | ||
| { | ||
| "service.name": "my-service", | ||
| "service.instance.id": "original-id", | ||
| "deployment.environment": "production", | ||
| } | ||
| ) | ||
| provider = TracerProvider(resource=resource) | ||
|
|
||
| provider._at_fork_reinit() | ||
|
|
||
| attrs = provider._resource.attributes | ||
| self.assertEqual(attrs.get("service.name"), "my-service") | ||
| self.assertEqual(attrs.get("deployment.environment"), "production") | ||
|
|
||
| def test_fork_produces_unique_service_instance_ids(self): | ||
| """Each forked worker should get a distinct service.instance.id.""" | ||
| provider = TracerProvider() | ||
|
|
||
| parent_id = provider._resource.attributes.get("service.instance.id") | ||
|
|
||
| def child(conn): | ||
| child_id = provider._resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| conn.send(child_id) | ||
| conn.close() | ||
|
|
||
| parent_conn, child_conn = _fork_ctx.Pipe() | ||
| process = _fork_ctx.Process(target=child, args=(child_conn,)) | ||
| process.start() | ||
| child_id = parent_conn.recv() | ||
| process.join() | ||
|
|
||
| self.assertNotEqual(parent_id, child_id) | ||
| self.assertIsNotNone(child_id) | ||
|
|
||
| def test_multiple_forks_produce_unique_service_instance_ids(self): | ||
| """Each of N forked workers should have a distinct service.instance.id.""" | ||
| provider = TracerProvider() | ||
|
|
||
| def child(conn): | ||
| child_id = provider._resource.attributes.get( | ||
| "service.instance.id" | ||
| ) | ||
| conn.send(child_id) | ||
| conn.close() | ||
|
|
||
| ids = set() | ||
| processes = [] | ||
| conns = [] | ||
|
|
||
| for _ in range(4): | ||
| parent_conn, child_conn = _fork_ctx.Pipe() | ||
| process = _fork_ctx.Process( | ||
| target=child, args=(child_conn,) | ||
| ) | ||
| processes.append(process) | ||
| conns.append(parent_conn) | ||
| process.start() | ||
|
|
||
| for conn in conns: | ||
| ids.add(conn.recv()) | ||
|
|
||
| for process in processes: | ||
| process.join() | ||
|
|
||
| self.assertEqual(len(ids), 4) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not setting this in the resource attributes, also not sure the issue is metrics specific.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure to get your point but if the user didn't set it, workers would be completely indistinguishable in the backend after fork, which is worse than having no ID at all. By always generating one post-fork we ensure every worker has a distinct identity regardless of whether the user configured it upfront.
On the second point: agreed, the problem affects both metrics and traces 👍🏼
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is that if we have a solution that work fine after forks() since the semantic conventions is now stable we can add this to the default resource detector. This way we have the very same attributes before and after fork.