Skip to content

Commit 15ca97a

Browse files
fix: avoid duplicate exception recording and drop str(e) from status
For start_as_current_span blocks (bedrock invoke/converse, sagemaker endpoint invoke), add record_exception=False, set_status_on_exception=False so the manual try/except is the sole exception handler without duplication. Drop str(e) from all Status(StatusCode.ERROR) calls to prevent sensitive request data from leaking into span status descriptions. Addresses coderabbitai review feedback on #4005
1 parent 7bc681e commit 15ca97a

File tree

5 files changed

+21
-12
lines changed
  • packages
    • opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock
    • opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama
    • opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate
    • opentelemetry-instrumentation-sagemaker/opentelemetry/instrumentation/sagemaker
    • opentelemetry-instrumentation-together/opentelemetry/instrumentation/together

5 files changed

+21
-12
lines changed

packages/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,17 @@ def with_instrumentation(*args, **kwargs):
219219
GenAIAttributes.GEN_AI_REQUEST_MODEL: _model,
220220
}
221221
with tracer.start_as_current_span(
222-
_span_name(operation_name, _model), kind=SpanKind.CLIENT, attributes=span_attributes
222+
_span_name(operation_name, _model),
223+
kind=SpanKind.CLIENT,
224+
attributes=span_attributes,
225+
record_exception=False,
226+
set_status_on_exception=False,
223227
) as span:
224228
try:
225229
response = fn(*args, **kwargs)
226230
except Exception as e:
227231
span.record_exception(e)
228-
span.set_status(Status(StatusCode.ERROR, str(e)))
232+
span.set_status(Status(StatusCode.ERROR))
229233
raise
230234
_handle_call(span, kwargs, response, metric_params, event_logger)
231235
return response
@@ -258,7 +262,7 @@ def with_instrumentation(*args, **kwargs):
258262
response = fn(*args, **kwargs)
259263
except Exception as e:
260264
span.record_exception(e)
261-
span.set_status(Status(StatusCode.ERROR, str(e)))
265+
span.set_status(Status(StatusCode.ERROR))
262266
span.end()
263267
raise
264268
_handle_stream_call(span, kwargs, response, metric_params, event_logger)
@@ -287,12 +291,14 @@ def with_instrumentation(*args, **kwargs):
287291
_span_name(GenAiOperationNameValues.CHAT.value, _model),
288292
kind=SpanKind.CLIENT,
289293
attributes=span_attributes,
294+
record_exception=False,
295+
set_status_on_exception=False,
290296
) as span:
291297
try:
292298
response = fn(*args, **kwargs)
293299
except Exception as e:
294300
span.record_exception(e)
295-
span.set_status(Status(StatusCode.ERROR, str(e)))
301+
span.set_status(Status(StatusCode.ERROR))
296302
raise
297303
_handle_converse(span, kwargs, response, metric_params, event_logger)
298304

@@ -322,7 +328,7 @@ def with_instrumentation(*args, **kwargs):
322328
response = fn(*args, **kwargs)
323329
except Exception as e:
324330
span.record_exception(e)
325-
span.set_status(Status(StatusCode.ERROR, str(e)))
331+
span.set_status(Status(StatusCode.ERROR))
326332
span.end()
327333
raise
328334
if span.is_recording():

packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def _wrap(
313313
response = wrapped(*args, **kwargs)
314314
except Exception as e:
315315
span.record_exception(e)
316-
span.set_status(Status(StatusCode.ERROR, str(e)))
316+
span.set_status(Status(StatusCode.ERROR))
317317
span.end()
318318
raise
319319
end_time = time.perf_counter()
@@ -394,7 +394,7 @@ async def _awrap(
394394
response = await wrapped(*args, **kwargs)
395395
except Exception as e:
396396
span.record_exception(e)
397-
span.set_status(Status(StatusCode.ERROR, str(e)))
397+
span.set_status(Status(StatusCode.ERROR))
398398
span.end()
399399
raise
400400
end_time = time.perf_counter()

packages/opentelemetry-instrumentation-replicate/opentelemetry/instrumentation/replicate/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _wrap(
137137
response = wrapped(*args, **kwargs)
138138
except Exception as e:
139139
span.record_exception(e)
140-
span.set_status(Status(StatusCode.ERROR, str(e)))
140+
span.set_status(Status(StatusCode.ERROR))
141141
span.end()
142142
raise
143143

packages/opentelemetry-instrumentation-sagemaker/opentelemetry/instrumentation/sagemaker/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ def with_instrumentation(*args, **kwargs):
9595
return fn(*args, **kwargs)
9696

9797
with tracer.start_as_current_span(
98-
"sagemaker.completion", kind=SpanKind.CLIENT
98+
"sagemaker.completion",
99+
kind=SpanKind.CLIENT,
100+
record_exception=False,
101+
set_status_on_exception=False,
99102
) as span:
100103
try:
101104
response = fn(*args, **kwargs)
102105
except Exception as e:
103106
span.record_exception(e)
104-
span.set_status(Status(StatusCode.ERROR, str(e)))
107+
span.set_status(Status(StatusCode.ERROR))
105108
raise
106109
_handle_call(span, event_logger, kwargs, response)
107110

@@ -122,7 +125,7 @@ def with_instrumentation(*args, **kwargs):
122125
response = fn(*args, **kwargs)
123126
except Exception as e:
124127
span.record_exception(e)
125-
span.set_status(Status(StatusCode.ERROR, str(e)))
128+
span.set_status(Status(StatusCode.ERROR))
126129
span.end()
127130
raise
128131

packages/opentelemetry-instrumentation-together/opentelemetry/instrumentation/together/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _wrap(
126126
response = wrapped(*args, **kwargs)
127127
except Exception as e:
128128
span.record_exception(e)
129-
span.set_status(Status(StatusCode.ERROR, str(e)))
129+
span.set_status(Status(StatusCode.ERROR))
130130
span.end()
131131
raise
132132

0 commit comments

Comments
 (0)