Skip to content

opentelemetry-sdk: reduce lock contention in attributes#5298

Draft
codeboten wants to merge 1 commit into
open-telemetry:mainfrom
codeboten:codeboten/reduce-lock-contention
Draft

opentelemetry-sdk: reduce lock contention in attributes#5298
codeboten wants to merge 1 commit into
open-telemetry:mainfrom
codeboten:codeboten/reduce-lock-contention

Conversation

@codeboten

Copy link
Copy Markdown
Contributor

Description

There are a few improvements here:

  • set_attributes was double locking (once in set_attributes and once in __setitem__. This change updates the call to a private _setitem_locked for set_attributes once the lock has been obtained
  • __iter__ doesn't use a lock if the BoundedAttributes's instantiated as immutable
  • _worker_awaken was being set even when it's already set, adding a check in emit to avoid unnecessary calls

Type of change

Please delete options that are not relevant.

  • Small performance improvements

How Has This Been Tested?

Ran unit tests to validate functionality was still passing and ran benchmarks including a new test to try and capture GIL contention. See the results below:

Test Before OPS After OPS OPS Δ Before (ns) After (ns) Latency Δ Memory before→after
test_bounded_attribute_iterator[10] 6,900,599 7,044,875 +2.1% 144.2 144.6 +0.3% 208 → 208 (+0.0%)
test_bounded_attribute_iterator[128] 2,425,554 2,458,973 +1.4% 412.5 410.7 -0.4% 1152 → 1152 (+0.0%)
test_bounded_attribute_iterator[1] 8,008,301 8,068,857 +0.8% 124.6 125.0 +0.3% 144 → 144 (+0.0%)
test_bounded_attribute_iterator[50] 4,452,807 4,474,884 +0.5% 223.7 223.7 +0.0% 528 → 528 (+0.0%)
test_gil_contention_batch_processor[1] 392 525 +33.9% 2,319,250 1,696,584 -26.8% n/a
test_gil_contention_batch_processor[2] 379 506 +33.4% 2,343,875 1,722,792 -26.5% n/a
test_gil_contention_batch_processor[4] 333 435 +30.7% 2,589,709 1,818,813 -29.8% n/a
test_gil_contention_batch_processor[8] 297 458 +54.2% 2,720,563 2,113,458 -22.3% n/a
test_read_events[1] 158,702 165,482 +4.3% 6,250 5,792 -7.3% 964 → 964 (+0.0%)
test_read_events[10] 59,338 59,196 -0.2% 16,833 15,666 -6.9% 964 → 964 (+0.0%)
test_read_events[50] 16,001 16,134 +0.8% 62,625 60,417 -3.5% 1140 → 1140 (+0.0%)
test_read_events[128] 6,425 6,418 -0.1% 154,750 146,417 -5.4% 2372 → 2372 (+0.0%)
test_read_links[1] 176,111 177,023 +0.5% 5,541 5,375 -3.0% 964 → 964 (+0.0%)
test_read_links[10] 79,587 79,777 +0.2% 11,917 12,500 +4.9% 964 → 964 (+0.0%)
test_read_links[50] 23,526 24,213 +2.9% 41,708 41,583 -0.3% 1140 → 1140 (+0.0%)
test_read_links[128] 9,917 10,148 +2.3% 100,083 98,667 -1.4% 2372 → 2372 (+0.0%)
test_start_span_with_attributes[0] 224,978 227,978 +1.3% 4,167 4,208 +1.0% n/a
test_start_span_with_attributes[1] 199,233 213,340 +7.1% 4,875 4,500 -7.7% n/a
test_start_span_with_attributes[10] 140,692 144,839 +2.9% 6,792 6,625 -2.5% n/a
test_start_span_with_attributes[50] 58,161 59,936 +3.1% 17,125 15,958 -6.8% n/a
test_start_span_with_attributes[128] 27,675 27,575 -0.4% 36,458 35,833 -1.7% n/a

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

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

There are a few improvements here:
- set_attributes was double locking (once in set_attributes and once in __setitem__. This change updates the call to a private _setitem_locked for set_attributes once the lock has been obtained
- __iter__ doesn't use a lock if the BoundedAttributes's instantiated as immutable
- _worker_awaken was being set even when it's already set, adding a check in emit to avoid unnecessary calls

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
@codeboten codeboten requested a review from a team as a code owner June 12, 2026 20:25
if value is None:
return
def _setitem_locked(self, key: str, value: types.AnyValue) -> None:
if self.maxlen is not None and self.maxlen == 0:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't lines 284-295 be moved out of the critical section?

@codeboten codeboten marked this pull request as draft June 12, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants