Skip to content

Commit ea81f26

Browse files
committed
factor get gen ai event
1 parent b6bc347 commit ea81f26

File tree

1 file changed

+35
-80
lines changed
  • aws-opentelemetry-distro/src/amazon/opentelemetry/distro

1 file changed

+35
-80
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/llo_handler.py

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
from typing import Any, Dict, List, Sequence
55

6-
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter
7-
86
from opentelemetry.attributes import BoundedAttributes
97
from opentelemetry._events import Event
108
from opentelemetry.sdk._logs import LoggerProvider
11-
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
129
from opentelemetry.sdk._events import EventLoggerProvider
1310
from opentelemetry.sdk.trace import ReadableSpan
1411

12+
GEN_AI_SYSTEM_MESSAGE = "gen_ai.system.message"
13+
GEN_AI_USER_MESSAGE = "gen_ai.user.message"
14+
GEN_AI_ASSISTANT_MESSAGE = "gen_ai.assistant.message"
15+
1516
_logger = logging.getLogger(__name__)
1617

1718
class LLOHandler:
@@ -134,103 +135,57 @@ def _extract_gen_ai_prompt_events(self, span: ReadableSpan, attributes: Dict[str
134135

135136
event = None
136137
if role == "system":
137-
event = self._get_gen_ai_system_message_event(
138-
span_ctx,
139-
prompt_timestamp,
140-
event_attributes,
141-
body
138+
event = self._get_gen_ai_event(
139+
name=GEN_AI_SYSTEM_MESSAGE,
140+
span_ctx=span_ctx,
141+
timestamp=prompt_timestamp,
142+
attributes=event_attributes,
143+
body=body
142144
)
143145
elif role == "user":
144-
event = self._get_gen_ai_user_message_event(
145-
span_ctx,
146-
prompt_timestamp,
147-
event_attributes,
148-
body
146+
event = self._get_gen_ai_event(
147+
name=GEN_AI_USER_MESSAGE,
148+
span_ctx=span_ctx,
149+
timestamp=prompt_timestamp,
150+
attributes=event_attributes,
151+
body=body
149152
)
150153
elif role == "assistant":
151-
event = self._get_gen_ai_assistant_message_event(
152-
span_ctx,
153-
prompt_timestamp,
154-
event_attributes,
155-
body
154+
event = self._get_gen_ai_event(
155+
name=GEN_AI_ASSISTANT_MESSAGE,
156+
span_ctx=span_ctx,
157+
timestamp=prompt_timestamp,
158+
attributes=event_attributes,
159+
body=body
156160
)
157161
elif role in ["function", "unknown"]:
158-
# TODO: Need to define a custom event and emit
159-
pass
162+
event = self._get_gen_ai_event(
163+
name=f"gen_ai.{gen_ai_system}.message",
164+
span_ctx=span_ctx,
165+
timestamp=prompt_timestamp,
166+
attributes=event_attributes,
167+
body=body
168+
)
160169

161170
if event:
162171
events.append(event)
163172

164173
return events
165174

166-
def _get_gen_ai_system_message_event(
167-
self,
168-
span_ctx,
169-
timestamp,
170-
event_attributes,
171-
body
172-
):
173-
"""
174-
Create and return a `gen_ai.system.message` Event.
175-
"""
176-
return Event(
177-
name="gen_ai.system.message",
178-
timestamp=timestamp,
179-
attributes=event_attributes,
180-
body=body,
181-
trace_id=span_ctx.trace_id,
182-
span_id=span_ctx.span_id,
183-
trace_flags=span_ctx.trace_flags,
184-
)
185-
186-
def _get_gen_ai_user_message_event(
187-
self,
188-
span_ctx,
189-
timestamp,
190-
event_attributes,
191-
body
192-
):
193-
"""
194-
Create and return a `gen_ai.user.message` Event.
195-
"""
196-
return Event(
197-
name="gen_ai.user.message",
198-
timestamp=timestamp,
199-
attributes=event_attributes,
200-
body=body,
201-
trace_id=span_ctx.trace_id,
202-
span_id=span_ctx.span_id,
203-
trace_flags=span_ctx.trace_flags,
204-
)
205-
206-
def _get_gen_ai_assistant_message_event(
175+
def _get_gen_ai_event(
207176
self,
177+
name,
208178
span_ctx,
209179
timestamp,
210-
event_attributes,
180+
attributes,
211181
body
212182
):
213-
"""
214-
Create and return a `gen_ai.assistant.message` Event.
215-
216-
According to the OTel spec, assistant message events may contain tool_calls,
217-
if available. In our implementation, tool call information is not available
218-
directly in the span attributes we're processing - it exists in separate
219-
related spans.
220-
221-
Thus without implementing complex span correlation, we cannot reliable extract
222-
tool_calls for assistant messages. This limitation is acceptable per the OTel
223-
spec since tool_calls are only required when available. However, this will
224-
lead to reduction in data quality.
225-
226-
ref: https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-events/#event-gen_aiassistantmessage
227-
"""
228183
return Event(
229-
name="gen_ai.assistant.message",
184+
name=name,
230185
timestamp=timestamp,
231-
attributes=event_attributes,
186+
attributes=attributes,
232187
body=body,
233188
trace_id=span_ctx.trace_id,
234189
span_id=span_ctx.span_id,
235-
trace_flags=span_ctx.trace_flags,
190+
trace_flags=span_ctx.trace_flags
236191
)

0 commit comments

Comments
 (0)