Skip to content

Add concurrency tests for meter/instrument methods#4962

Open
chimchim89 wants to merge 4 commits intoopen-telemetry:mainfrom
chimchim89:test-metrics-concurrency
Open

Add concurrency tests for meter/instrument methods#4962
chimchim89 wants to merge 4 commits intoopen-telemetry:mainfrom
chimchim89:test-metrics-concurrency

Conversation

@chimchim89
Copy link
Contributor

@chimchim89 chimchim89 commented Mar 9, 2026

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 TestConcurrency class using Python's threading module to each of the three affected test files:

  • test_instruments.py: concurrent tests for Counter.add, UpDownCounter.add, Histogram.record, and Gauge.set
  • test_meter.py: concurrent tests for Meter.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_counter
  • test_meter_provider.py: concurrent tests for NoOpMeterProvider.get_meter and _ProxyMeterProvider.get_meter

Type 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?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@chimchim89 chimchim89 requested a review from a team as a code owner March 9, 2026 08:14
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use ConcurrencyTestBase from opentelemetry-test-utils here.


self.assertEqual([], errors)

def test_create_counter_concurrent(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants