Skip to content

Commit cc444e4

Browse files
committed
Assert error types in duration metrics in error paths
1 parent 2eb9a2d commit cc444e4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

instrumentation/opentelemetry-instrumentation-botocore/tests/bedrock_utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
from opentelemetry.semconv._incubating.attributes import (
3232
gen_ai_attributes as GenAIAttributes,
3333
)
34+
from opentelemetry.semconv._incubating.attributes.error_attributes import (
35+
ERROR_TYPE,
36+
)
3437
from opentelemetry.semconv._incubating.metrics.gen_ai_metrics import (
3538
GEN_AI_CLIENT_OPERATION_DURATION,
3639
GEN_AI_CLIENT_TOKEN_USAGE,
@@ -270,7 +273,9 @@ def assert_message_in_logs(log, event_name, expected_content, parent_span):
270273
assert_log_parent(log, parent_span)
271274

272275

273-
def assert_all_metric_attributes(data_point, operation_name, model):
276+
def assert_all_metric_attributes(
277+
data_point, operation_name: str, model: str, error_type: str | None = None
278+
):
274279
assert GenAIAttributes.GEN_AI_OPERATION_NAME in data_point.attributes
275280
assert (
276281
data_point.attributes[GenAIAttributes.GEN_AI_OPERATION_NAME]
@@ -284,13 +289,20 @@ def assert_all_metric_attributes(data_point, operation_name, model):
284289
assert GenAIAttributes.GEN_AI_REQUEST_MODEL in data_point.attributes
285290
assert data_point.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] == model
286291

292+
if error_type is not None:
293+
assert ERROR_TYPE in data_point.attributes
294+
assert data_point.attributes[ERROR_TYPE] == error_type
295+
else:
296+
assert ERROR_TYPE not in data_point.attributes
297+
287298

288299
def assert_metrics(
289300
resource_metrics: ResourceMetrics,
290301
operation_name: str,
291302
model: str,
292303
input_tokens: float | None = None,
293304
output_tokens: float | None = None,
305+
error_type: str | None = None,
294306
):
295307
assert len(resource_metrics) == 1
296308

@@ -309,7 +321,9 @@ def assert_metrics(
309321

310322
duration_point = duration_metric.data.data_points[0]
311323
assert duration_point.sum > 0
312-
assert_all_metric_attributes(duration_point, operation_name, model)
324+
assert_all_metric_attributes(
325+
duration_point, operation_name, model, error_type
326+
)
313327
assert duration_point.explicit_bounds == tuple(
314328
_GEN_AI_CLIENT_OPERATION_DURATION_BUCKETS
315329
)

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_bedrock.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ def test_converse_with_invalid_model(
499499
assert_message_in_logs(logs[0], "gen_ai.user.message", user_content, span)
500500

501501
metrics = metric_reader.get_metrics_data().resource_metrics
502-
assert_metrics(metrics, "chat", llm_model_value)
502+
assert_metrics(
503+
metrics, "chat", llm_model_value, error_type="ValidationException"
504+
)
503505

504506

505507
@pytest.mark.skipif(
@@ -1073,7 +1075,9 @@ def test_converse_stream_handles_event_stream_error(
10731075
assert_message_in_logs(logs[0], "gen_ai.user.message", user_content, span)
10741076

10751077
metrics = metric_reader.get_metrics_data().resource_metrics
1076-
assert_metrics(metrics, "chat", llm_model_value)
1078+
assert_metrics(
1079+
metrics, "chat", llm_model_value, error_type="EventStreamError"
1080+
)
10771081

10781082

10791083
@pytest.mark.skipif(

0 commit comments

Comments
 (0)