Skip to content

Commit 253e9b2

Browse files
committed
Move choice event creation to _Choice and add types
1 parent c50ba8d commit 253e9b2

File tree

2 files changed

+27
-22
lines changed
  • instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions

2 files changed

+27
-22
lines changed

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/bedrock.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
_Choice,
3333
genai_capture_message_content,
3434
message_to_event,
35-
to_choice_event,
3635
)
3736
from opentelemetry.instrumentation.botocore.extensions.types import (
3837
_AttributeMapT,
@@ -293,8 +292,7 @@ def _converse_on_success(
293292
# context so need to add the span context manually
294293
span_ctx = span.get_span_context()
295294
event_logger.emit(
296-
to_choice_event(
297-
choice,
295+
choice.to_choice_event(
298296
trace_id=span_ctx.trace_id,
299297
span_id=span_ctx.span_id,
300298
trace_flags=span_ctx.trace_flags,
@@ -444,7 +442,7 @@ def _handle_amazon_titan_response(
444442
choice = _Choice.from_invoke_amazon_titan(
445443
response_body, capture_content
446444
)
447-
event_logger.emit(to_choice_event(choice))
445+
event_logger.emit(choice.to_choice_event())
448446

449447
# pylint: disable=no-self-use
450448
def _handle_amazon_nova_response(
@@ -471,7 +469,7 @@ def _handle_amazon_nova_response(
471469

472470
event_logger = instrumentor_context.event_logger
473471
choice = _Choice.from_converse(response_body, capture_content)
474-
event_logger.emit(to_choice_event(choice))
472+
event_logger.emit(choice.to_choice_event())
475473

476474
# pylint: disable=no-self-use
477475
def _handle_anthropic_claude_response(
@@ -499,7 +497,7 @@ def _handle_anthropic_claude_response(
499497
choice = _Choice.from_invoke_anthropic_claude(
500498
response_body, capture_content
501499
)
502-
event_logger.emit(to_choice_event(choice))
500+
event_logger.emit(choice.to_choice_event())
503501

504502
def on_error(
505503
self,

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import json
1818
from os import environ
19-
from typing import Callable, Dict, Union
19+
from typing import Any, Callable, Dict, Union
2020

2121
from botocore.eventstream import EventStream, EventStreamError
2222
from wrapt import ObjectProxy
@@ -297,7 +297,7 @@ def genai_capture_message_content() -> bool:
297297
return capture_content.lower() == "true"
298298

299299

300-
def message_to_event(message, capture_content):
300+
def message_to_event(message: dict[str, Any], capture_content: bool) -> Event:
301301
attributes = {GEN_AI_SYSTEM: GenAiSystemValues.AWS_BEDROCK.value}
302302
role = message.get("role")
303303
content = message.get("content")
@@ -314,13 +314,17 @@ def message_to_event(message, capture_content):
314314

315315

316316
class _Choice:
317-
def __init__(self, message, finish_reason, index):
317+
def __init__(
318+
self, message: dict[str, Any], finish_reason: str, index: int
319+
):
318320
self.message = message
319321
self.finish_reason = finish_reason
320322
self.index = index
321323

322324
@classmethod
323-
def from_converse(cls, response, capture_content):
325+
def from_converse(
326+
cls, response: dict[str, Any], capture_content: bool
327+
) -> _Choice:
324328
orig_message = response["output"]["message"]
325329
if role := orig_message.get("role"):
326330
message = {"role": role}
@@ -332,7 +336,9 @@ def from_converse(cls, response, capture_content):
332336
return cls(message, response["stopReason"], index=0)
333337

334338
@classmethod
335-
def from_invoke_amazon_titan(cls, response, capture_content):
339+
def from_invoke_amazon_titan(
340+
cls, response: dict[str, Any], capture_content: bool
341+
) -> _Choice:
336342
result = response["results"][0]
337343
if capture_content:
338344
message = {"content": result["outputText"]}
@@ -341,7 +347,9 @@ def from_invoke_amazon_titan(cls, response, capture_content):
341347
return cls(message, result["completionReason"], index=0)
342348

343349
@classmethod
344-
def from_invoke_anthropic_claude(cls, response, capture_content):
350+
def from_invoke_anthropic_claude(
351+
cls, response: dict[str, Any], capture_content: bool
352+
) -> _Choice:
345353
if capture_content:
346354
message = {
347355
"content": response["content"],
@@ -352,19 +360,18 @@ def from_invoke_anthropic_claude(cls, response, capture_content):
352360

353361
return cls(message, response["stop_reason"], index=0)
354362

355-
def to_body_dict(self):
363+
def _to_body_dict(self) -> dict[str, Any]:
356364
return {
357365
"finish_reason": self.finish_reason,
358366
"index": self.index,
359367
"message": self.message,
360368
}
361369

362-
363-
def to_choice_event(choice: _Choice, **event_kwargs):
364-
attributes = {GEN_AI_SYSTEM: GenAiSystemValues.AWS_BEDROCK.value}
365-
return Event(
366-
name="gen_ai.choice",
367-
attributes=attributes,
368-
body=choice.to_body_dict(),
369-
**event_kwargs,
370-
)
370+
def to_choice_event(self, **event_kwargs) -> Event:
371+
attributes = {GEN_AI_SYSTEM: GenAiSystemValues.AWS_BEDROCK.value}
372+
return Event(
373+
name="gen_ai.choice",
374+
attributes=attributes,
375+
body=self._to_body_dict(),
376+
**event_kwargs,
377+
)

0 commit comments

Comments
 (0)