Skip to content

Commit 9390cb5

Browse files
committed
expose just a json util
1 parent 910f8a6 commit 9390cb5

File tree

3 files changed

+21
-17
lines changed
  • instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai
  • util/opentelemetry-util-genai/src/opentelemetry/util/genai

3 files changed

+21
-17
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/patch.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from __future__ import annotations
1616

17-
import json
1817
from contextlib import contextmanager
1918
from dataclasses import asdict
2019
from typing import (
@@ -54,7 +53,7 @@
5453
InputMessage,
5554
OutputMessage,
5655
)
57-
from opentelemetry.util.genai.utils import Base64JsonEncoder
56+
from opentelemetry.util.genai.utils import gen_ai_json_dumps
5857

5958
if TYPE_CHECKING:
6059
from google.cloud.aiplatform_v1.services.prediction_service import client
@@ -224,11 +223,7 @@ def handle_response(
224223
):
225224
span.set_attributes(
226225
{
227-
k: json.dumps(
228-
v,
229-
cls=Base64JsonEncoder,
230-
separators=(",", ":"),
231-
)
226+
k: gen_ai_json_dumps(v)
232227
for k, v in content_attributes.items()
233228
}
234229
)

util/opentelemetry-util-genai/src/opentelemetry/util/genai/_upload/completion_hook.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
from __future__ import annotations
1717

18-
import json
1918
import logging
2019
import posixpath
2120
import threading
@@ -38,7 +37,7 @@
3837
from opentelemetry.util.genai.environment_variables import (
3938
OTEL_INSTRUMENTATION_GENAI_UPLOAD_FORMAT,
4039
)
41-
from opentelemetry.util.genai.utils import Base64JsonEncoder
40+
from opentelemetry.util.genai.utils import gen_ai_json_dump
4241

4342
GEN_AI_INPUT_MESSAGES_REF: Final = (
4443
gen_ai_attributes.GEN_AI_INPUT_MESSAGES + "_ref"
@@ -192,12 +191,7 @@ def _do_upload(
192191

193192
with self._fs.open(path, "w", content_type=content_type) as file:
194193
for message in message_lines:
195-
json.dump(
196-
message,
197-
file,
198-
separators=(",", ":"),
199-
cls=Base64JsonEncoder,
200-
)
194+
gen_ai_json_dump(message, file)
201195
file.write("\n")
202196

203197
def on_completion(

util/opentelemetry-util-genai/src/opentelemetry/util/genai/utils.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logging
1717
import os
1818
from base64 import b64encode
19+
from functools import partial
1920
from typing import Any
2021

2122
from opentelemetry.instrumentation._semconv import (
@@ -59,10 +60,24 @@ def get_content_capturing_mode() -> ContentCapturingMode:
5960
return ContentCapturingMode.NO_CONTENT
6061

6162

62-
class Base64JsonEncoder(json.JSONEncoder):
63-
"""Should be used to serialize python objects to json that may contain bytes."""
63+
class _GenAiJsonEncoder(json.JSONEncoder):
64+
"""Should be used by GenAI instrumentations when serializing objects that may contain
65+
bytes, datetimes, etc. for GenAI observability."""
6466

6567
def default(self, o: Any) -> Any:
6668
if isinstance(o, bytes):
6769
return b64encode(o).decode()
6870
return super().default(o)
71+
72+
73+
gen_ai_json_dump = partial(
74+
json.dump, separators=(",", ":"), cls=_GenAiJsonEncoder
75+
)
76+
"""Should be used by GenAI instrumentations when serializing objects that may contain
77+
bytes, datetimes, etc. for GenAI observability."""
78+
79+
gen_ai_json_dumps = partial(
80+
json.dumps, separators=(",", ":"), cls=_GenAiJsonEncoder
81+
)
82+
"""Should be used by GenAI instrumentations when serializing objects that may contain
83+
bytes, datetimes, etc. for GenAI observability."""

0 commit comments

Comments
 (0)