Repository navigation
Description
Thanks for shipping the gen_ai.client.operation.duration histogram in the Bedrock instrumentation; comparing GPT-6 Sol, Luna and Astra latency with it showed this.
Which component is this bug for?
Bedrock Instrumentation
📜 Description
metric_params.start_time is one shared value, set when the instrumentor starts (__init__.py:107) and reset only in _wrap when a bedrock-runtime client is created (__init__.py:183). _record_usage_to_span records time.time() - metric_params.start_time (span_utils.py:804). So each recorded duration is the client's age, not the call's latency, and it grows with the client's lifetime. Model-agnostic, all four call paths.
👟 Reproduction steps
BedrockInstrumentor().instrument() # with an InMemoryMetricReader
c = boto3.client("bedrock-runtime", region_name="us-east-1")
msg = [{"role": "user", "content": [{"text": "Reply with just: pong"}]}]
time.sleep(8) # client sits idle, as in any service
for mid in ("us.openai.gpt-6-sol", "us.openai.gpt-6-luna"):
c.converse(modelId=mid, messages=msg)
for _ in c.converse_stream(modelId=mid, messages=msg)["stream"]:
pass👍 Expected behavior
The histogram sum is close to the 4 calls' wall time.
👎 Actual Behavior with Screenshots
Real calls on main d13e721:
converse us.openai.gpt-6-sol: wall=2.21s
converse_stream us.openai.gpt-6-sol: wall=0.74s
converse us.openai.gpt-6-luna: wall=0.54s
converse_stream us.openai.gpt-6-luna: wall=0.68s
total wall for 4 calls = 4.16s
spans=4 span durations: 2.21, 0.74, 0.54, 0.68
METRIC gen_ai.client.operation.duration count=4 sum=44.97s max=12.20s
| Wall time | Recorded today | |
|---|---|---|
| Sol + Luna, 4 calls, sum | 4.16 s | ❌ 44.97 s |
| slowest single call | 2.21 s | ❌ max 12.20 s |
Astra, same steps with us. and global.openai.gpt-6-astra, sum |
4.54 s | ❌ 44.06 s |
🤖 Python Version
3.12
📃 Provide any additional context for the Bug.
Option A (ready, with a test): the span's own start time in _record_usage_to_span; spans without one (sampled out, where only converse records the metric) keep today's value. Option B: a per-call start_time in each wrapper, like the anthropic and groq packages, passed down; it also covers sampled-out spans but touches every helper in between. Which do you prefer?
Proposed patch (option A) and result, same calls
if metric_params.duration_histogram:
- duration = time.time() - metric_params.start_time
+ # metric_params.start_time is shared and only reset when a client is
+ # created; the span's own start time (ns) is per call.
+ span_start = getattr(span, "start_time", None)
+ if isinstance(span_start, int):
+ duration = (time.time_ns() - span_start) / 1e9
+ else:
+ duration = time.time() - metric_params.start_timetotal wall for 4 calls = 3.07s
spans=4 span durations: 1.06, 0.75, 0.60, 0.67
METRIC gen_ai.client.operation.duration count=4 sum=3.07s max=1.06s
| client idle, then | main: wall / recorded | patch: wall / recorded |
|---|---|---|
Astra us. + global., converse + converse_stream (8 s) |
4.54 s / 44.06 s | 4.81 s / 4.81 s |
Claude Haiku 4.5, invoke_model + invoke_model_with_response_stream (5 s) |
1.69 s / 12.81 s | 1.93 s / 1.93 s |
every span sampled out, Sol converse (5 s; converse_stream records nothing then) |
1.05 s / 6.10 s | 1.21 s / 6.24 s (fallback, unchanged) |
New test (tests/test_operation_duration.py) fails on main with assert 100.00045800209045 < 3 and passes with the patch; full suite 378 passed, ruff clean.
👀 Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Issue/PR search (operation.duration bedrock, bedrock duration, bedrock start_time) and the open Bedrock PR diffs: nothing matching.
Are you willing to submit PR?
Yes I am willing to submit a PR!
Activity
CodeAlex52 commentedon Sep 24, 2026
Taking this one. Plan: capture the call start time in each instrumented wrapper and pass per-call state down to the usage recording, instead of reading the shared
metric_params.start_time(which is the client-creation timestamp). Will follow up with a PR and tests undertests/metrics/.fix(bedrock): record operation duration per call instead of client age