Skip to content

perf: cache SDK version for span origin context - #782

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
nathan-mialabs:fix/cache-span-origin-sdk-version
Sep 19, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 1 commit into
braintrustdata:mainfrom
nathan-mialabs:fix/cache-span-origin-sdk-version

Conversation

@nathan-mialabs

@nathan-mialabs Nathan (nathan-mialabs) commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

cache installed Braintrust SDK version for the lifetime of the process
add regression test proving repeated span-origin merges resolve package metadata only once

why:

merge_span_origin_context() runs for every span, but currently resolves the installed Braintrust version through importlib.metadata each time. That triggers synchronous os.stat() calls. We observed this with AIOCOP, an asyncio event-loop monitoring tool: these synchronous calls appeared in stalls lasting 2.45–3.18 seconds under load.

Since the installed version cannot change during a process's lifetime, caching it removes repeated blocking I/O while preserving behavior.

Example stack trace captured in SigNoz:

importlib_metadata/__init__.py:838:mtime
<- importlib_metadata/__init__.py:833:search
<- importlib_metadata/__init__.py:974:<genexpr>
<- importlib.metadata:397:from_name
<- importlib.metadata:862:distribution
<- importlib.metadata:889:version
<- braintrust/span_origin.py:103:_sdk_version
<- braintrust/span_origin.py:92:merge_span_origin_context
<- braintrust/logger.py:4619:__init__
<- braintrust/logger.py:4761:start_span
<- braintrust/logger.py:2981:start_span

timing:

Local timing on macOS with Python 3.12 using python -m timeit:

  • uncached importlib.metadata.version("braintrust"): 113 µs per call
  • cached _sdk_version(): 10.3 ns per call
  • approximately 11,000x lower hot-path overhead after the first lookup

Avoid repeated synchronous package metadata scans while creating spans; the installed SDK version is immutable for the process lifetime.

Co-authored-by: Cursor <cursoragent@cursor.com>
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit c484573 into braintrustdata:main Sep 19, 2026
83 checks passed
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