Skip to content

perf(sdk-trace-base): avoid Object.entries in Span.setAttributes#6514

Merged
dyladan merged 2 commits intoopen-telemetry:mainfrom
daniellockyer:bench/span-sdk-trace-base
Mar 23, 2026
Merged

perf(sdk-trace-base): avoid Object.entries in Span.setAttributes#6514
dyladan merged 2 commits intoopen-telemetry:mainfrom
daniellockyer:bench/span-sdk-trace-base

Conversation

@daniellockyer
Copy link
Copy Markdown
Contributor

@daniellockyer daniellockyer commented Mar 21, 2026

Which problem is this PR solving?

Whilst looking at the performance overhead of OTel, I noticed setAttributes in unusually heavy in comparison to setAttribute. Turns out, we're using Object.entries for the loop here, which is much heavier than a standard for-in loop. This change has been made several other times in this repo, such as in #5866 and #6287.

Part of the flamegraph showing the setAttributes call has overhead in comparison to setAttribute:
CleanShot 2026-03-21 at 10 54 31@2x

Many instrumentations use setAttributes, including http, so we should see strong improvements across the board.

Before:

create spans (10 attributes) x 1,040,461 ops/sec ±1.81% (97 runs sampled)
create spans (10 attributes w/ setAttributes) x 842,071 ops/sec ±1.78% (94 runs sampled)

After:

create spans (10 attributes) x 1,013,509 ops/sec ±1.60% (96 runs sampled)
create spans (10 attributes w/ setAttributes) x 1,025,856 ops/sec ±1.42% (99 runs sampled)

You can see span creation with setAttributes is now on par with the performance of the benchmark using 10x setAttribute thanks to the 22% performance improvement 🚀

refs #6100

Short description of the changes

Replace Object.entries with a for-in loop for better performance.

Also adds a benchmark test for span creation using setAttributes.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Tests still pass, and see benchmark results from above.

Checklist:

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

@daniellockyer daniellockyer requested a review from a team as a code owner March 21, 2026 09:47
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Mar 21, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.75%. Comparing base (2f73776) to head (81f8a82).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6514   +/-   ##
=======================================
  Coverage   95.75%   95.75%           
=======================================
  Files         364      364           
  Lines       12095    12096    +1     
  Branches     2884     2885    +1     
=======================================
+ Hits        11581    11582    +1     
  Misses        514      514           
Files with missing lines Coverage Δ
packages/opentelemetry-sdk-trace-base/src/Span.ts 99.12% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dyladan dyladan added this pull request to the merge queue Mar 23, 2026
Merged via the queue into open-telemetry:main with commit e9831e7 Mar 23, 2026
27 checks passed
@otelbot
Copy link
Copy Markdown
Contributor

otelbot Bot commented Mar 23, 2026

Thank you for your contribution @daniellockyer! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@daniellockyer daniellockyer deleted the bench/span-sdk-trace-base branch March 23, 2026 13:35
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.

3 participants