Skip to content

Commit 91accd2

Browse files
committed
Workaround broken logs SDK
1 parent 64162c3 commit 91accd2

File tree

1 file changed

+22
-7
lines changed
  • instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai

1 file changed

+22
-7
lines changed

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
# limitations under the License.
1616

1717
from collections.abc import Iterable, Mapping
18+
import inspect
1819
from timeit import default_timer
1920
from typing import TYPE_CHECKING, Optional
2021

21-
from opentelemetry._logs import Logger, LogRecord
22+
from opentelemetry._logs import Logger, LogRecord, SeverityNumber
2223
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
2324
GEN_AI_OPENAI_REQUEST_SERVICE_TIER,
2425
GEN_AI_OPENAI_RESPONSE_SERVICE_TIER,
@@ -291,15 +292,17 @@ def _send_logs_from_messages(logger: Logger, messages, attributes: Attributes, c
291292
if message["role"] == "developer":
292293
body["role"] = message["role"]
293294
# keep compat on the exported attributes with Event
294-
log = LogRecord(
295+
log = _new_log_record(
296+
logger,
295297
event_name=EVENT_GEN_AI_SYSTEM_MESSAGE,
296298
body=body,
297299
attributes={**attributes, "event.name": EVENT_GEN_AI_SYSTEM_MESSAGE},
298300
)
299301
logger.emit(log)
300302
elif message["role"] == "user":
301303
# keep compat on the exported attributes with Event
302-
log = LogRecord(
304+
log = _new_log_record(
305+
logger,
303306
event_name=EVENT_GEN_AI_USER_MESSAGE,
304307
body=body,
305308
attributes={**attributes, "event.name": EVENT_GEN_AI_USER_MESSAGE},
@@ -310,7 +313,8 @@ def _send_logs_from_messages(logger: Logger, messages, attributes: Attributes, c
310313
if tool_calls:
311314
body["tool_calls"] = tool_calls
312315
# keep compat on the exported attributes with Event
313-
log = LogRecord(
316+
log = _new_log_record(
317+
logger,
314318
event_name=EVENT_GEN_AI_ASSISTANT_MESSAGE,
315319
body=body,
316320
attributes={**attributes, "event.name": EVENT_GEN_AI_ASSISTANT_MESSAGE},
@@ -319,7 +323,8 @@ def _send_logs_from_messages(logger: Logger, messages, attributes: Attributes, c
319323
elif message["role"] == "tool":
320324
body["id"] = message["tool_call_id"]
321325
# keep compat on the exported attributes with Event
322-
log = LogRecord(
326+
log = _new_log_record(
327+
logger,
323328
event_name=EVENT_GEN_AI_TOOL_MESSAGE,
324329
body=body,
325330
attributes={**attributes, "event.name": EVENT_GEN_AI_TOOL_MESSAGE},
@@ -337,12 +342,21 @@ def _send_logs_from_choices(logger: Logger, choices, attributes: Attributes, cap
337342
body["message"]["content"] = choice.message.content
338343

339344
# keep compat on the exported attributes with Event
340-
log = LogRecord(
345+
log = _new_log_record(
346+
logger,
341347
event_name=EVENT_GEN_AI_CHOICE, body=body, attributes={**attributes, "event.name": EVENT_GEN_AI_CHOICE}
342348
)
343349
logger.emit(log)
344350

345351

352+
def _new_log_record(logger: Logger, **kwargs):
353+
mod = inspect.getmodule(logger.__class__)
354+
LoggerLogRecord = getattr(mod, "LogRecord", None)
355+
if LoggerLogRecord:
356+
return LoggerLogRecord(**kwargs, severity_number=SeverityNumber.INFO)
357+
return LogRecord(**kwargs)
358+
359+
346360
def _send_logs_from_stream_choices(
347361
logger: Logger, choices, span: Span, attributes: Attributes, capture_message_content: bool
348362
):
@@ -382,7 +396,8 @@ def _send_logs_from_stream_choices(
382396
# StreamWrapper is consumed after start_as_current_span exits, so capture the current span
383397
ctx = span.get_span_context()
384398
# keep compat on the exported attributes with Event
385-
log = LogRecord(
399+
log = _new_log_record(
400+
logger,
386401
event_name=EVENT_GEN_AI_CHOICE,
387402
body=body,
388403
attributes={**attributes, "event.name": EVENT_GEN_AI_CHOICE},

0 commit comments

Comments
 (0)