Skip to content

Commit

Permalink
only set token metrics if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
ncybul committed Feb 4, 2025
1 parent 8416367 commit 7baffdf
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 40 deletions.
11 changes: 7 additions & 4 deletions ddtrace/_trace/trace_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,10 @@ def _on_botocore_patched_bedrock_api_call_success(ctx, reqid, latency, input_tok
span = ctx.span
span.set_tag_str("bedrock.response.id", reqid)
span.set_tag_str("bedrock.response.duration", latency)
span.set_metric("bedrock.usage.prompt_tokens", int(input_token_count))
span.set_metric("bedrock.usage.completion_tokens", int(output_token_count))
if input_token_count:
span.set_metric("bedrock.usage.prompt_tokens", int(input_token_count))
if output_token_count:
span.set_metric("bedrock.usage.completion_tokens", int(output_token_count))


def _propagate_context(ctx, headers):
Expand Down Expand Up @@ -641,8 +643,9 @@ def _on_botocore_bedrock_process_response(
if metadata is not None:
for k, v in metadata.items():
if k in ["usage.completion_tokens", "usage.prompt_tokens"]:
v = int(v)
span.set_metric("bedrock.{}".format(k), v)
span.set_metric("bedrock.{}".format(k), int(v))
else:
span.set_tag_str("bedrock.{}".format(k), str(v))
if "embed" in model_name:
span.set_metric("bedrock.response.embedding_length", len(formatted_response["text"][0]))
span.finish()
Expand Down
1 change: 1 addition & 0 deletions tests/contrib/botocore/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ def test_span_finishes_after_generator_exit(bedrock_client, request_vcr, mock_tr
if i >= 6:
raise GeneratorExit
i += 1
breakpoint()
span = mock_tracer.pop_traces()[0][0]
assert span is not None
assert span.name == "bedrock-runtime.command"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "10",
"bedrock.usage.prompt_tokens": "10",
"bedrock.usage.completion_tokens": 10,
"bedrock.usage.prompt_tokens": 10,
"process_id": 7458
},
"duration": 2112000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.response.embedding_length": 1536,
"bedrock.usage.completion_tokens": "",
"bedrock.usage.prompt_tokens": "3",
"bedrock.usage.prompt_tokens": 3,
"process_id": 60939
},
"duration": 6739000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "50",
"bedrock.usage.prompt_tokens": "18",
"bedrock.usage.completion_tokens": 50,
"bedrock.usage.prompt_tokens": 18,
"process_id": 14088
},
"duration": 2147082000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "51",
"bedrock.usage.prompt_tokens": "18",
"bedrock.usage.completion_tokens": 51,
"bedrock.usage.prompt_tokens": 18,
"process_id": 14088
},
"duration": 2185710000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "32",
"bedrock.usage.prompt_tokens": "23",
"bedrock.usage.completion_tokens": 32,
"bedrock.usage.prompt_tokens": 23,
"process_id": 7272
},
"duration": 2434000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "4",
"bedrock.usage.prompt_tokens": "25",
"bedrock.usage.completion_tokens": 4,
"bedrock.usage.prompt_tokens": 25,
"process_id": 13707
},
"duration": 624710000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "22",
"bedrock.usage.prompt_tokens": "21",
"bedrock.usage.completion_tokens": 22,
"bedrock.usage.prompt_tokens": 21,
"process_id": 40705
},
"duration": 2160000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "22",
"bedrock.usage.prompt_tokens": "21",
"bedrock.usage.completion_tokens": 22,
"bedrock.usage.prompt_tokens": 21,
"process_id": 40896
},
"duration": 2950000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.response.embedding_length": 1024,
"bedrock.usage.completion_tokens": "",
"bedrock.usage.prompt_tokens": "7",
"bedrock.usage.prompt_tokens": 7,
"process_id": 61336
},
"duration": 630192000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "20",
"bedrock.usage.prompt_tokens": "40",
"bedrock.usage.completion_tokens": 20,
"bedrock.usage.prompt_tokens": 40,
"process_id": 3568
},
"duration": 810213000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "10",
"bedrock.usage.prompt_tokens": "20",
"bedrock.usage.completion_tokens": 10,
"bedrock.usage.prompt_tokens": 20,
"process_id": 13549
},
"duration": 659370000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "20",
"bedrock.usage.prompt_tokens": "40",
"bedrock.usage.completion_tokens": 20,
"bedrock.usage.prompt_tokens": 40,
"process_id": 21816
},
"duration": 980170000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "10",
"bedrock.usage.prompt_tokens": "20",
"bedrock.usage.completion_tokens": 10,
"bedrock.usage.prompt_tokens": 20,
"process_id": 21816
},
"duration": 630536000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "50",
"bedrock.usage.prompt_tokens": "18",
"bedrock.usage.completion_tokens": 50,
"bedrock.usage.prompt_tokens": 18,
"process_id": 96028
},
"duration": 2318000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "60",
"bedrock.usage.prompt_tokens": "10",
"bedrock.usage.completion_tokens": 60,
"bedrock.usage.prompt_tokens": 10,
"process_id": 10703
},
"duration": 2120703000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "60",
"bedrock.usage.prompt_tokens": "10",
"bedrock.usage.completion_tokens": 60,
"bedrock.usage.prompt_tokens": 10,
"process_id": 2664
},
"duration": 3795000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "60",
"bedrock.usage.prompt_tokens": "10",
"bedrock.usage.completion_tokens": 60,
"bedrock.usage.prompt_tokens": 10,
"process_id": 42139
},
"duration": 2505000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"bedrock.usage.completion_tokens": "60",
"bedrock.usage.prompt_tokens": "10",
"bedrock.usage.completion_tokens": 60,
"bedrock.usage.prompt_tokens": 10,
"process_id": 42139
},
"duration": 3064000,
Expand Down

0 comments on commit 7baffdf

Please sign in to comment.