Add concurrency tests for meter/instrument methods#4962
Open
chimchim89 wants to merge 4 commits intoopen-telemetry:mainfrom
Open
Add concurrency tests for meter/instrument methods#4962chimchim89 wants to merge 4 commits intoopen-telemetry:mainfrom
chimchim89 wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
herin049
reviewed
Mar 10, 2026
| def test_counter_add_concurrent(self): | ||
| """Test that Counter.add can be called concurrently safely.""" | ||
| counter = NoOpCounter("name") | ||
| self._run_concurrently(lambda: counter.add(1)) |
Contributor
There was a problem hiding this comment.
We should probably add some logic here to assert that once all of the threads are completed, the value of the counter is valid (e.g. the counter is 1000 with 10 threads and 100 calls/thread)
| ) | ||
|
|
||
|
|
||
| class TestConcurrency(TestCase): |
Contributor
There was a problem hiding this comment.
You should be able to use ConcurrencyTestBase from opentelemetry-test-utils here.
|
|
||
| self.assertEqual([], errors) | ||
|
|
||
| def test_create_counter_concurrent(self): |
Contributor
There was a problem hiding this comment.
Again, similar to the other test cases above, we should do at least some validation after the workers have completed (e.g. verify that the counter exists)
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
Addresses FIXME comments in the metrics API test files that indicated concurrency tests were needed to verify that meter and instrument methods can be called concurrently safely. No production code was modified.
Added a
TestConcurrencyclass using Python'sthreadingmodule to each of the three affected test files:test_instruments.py: concurrent tests forCounter.add,UpDownCounter.add,Histogram.record, andGauge.settest_meter.py: concurrent tests forMeter.create_counter,Meter.create_up_down_counter,Meter.create_observable_counter,Meter.create_histogram,Meter.create_gauge,Meter.create_observable_gauge,Meter.create_observable_up_down_countertest_meter_provider.py: concurrent tests forNoOpMeterProvider.get_meterand_ProxyMeterProvider.get_meterType of change
This is a test-only change that adds missing concurrency tests to address existing FIXME comments. No production code was modified.
How Has This Been Tested?
pytest opentelemetry-api/tests/metrics
76 passed in 0.62s
Does This PR Require a Contrib Repo Change?
Checklist: