Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from base64 import b64encode
from functools import partial
from typing import Any
from collections.abc import Sequence

from opentelemetry.instrumentation._semconv import (
_OpenTelemetrySemanticConventionStability,
Expand Down Expand Up @@ -111,6 +112,12 @@ def should_capture_content_on_spans_in_experimental_mode() -> bool:
return False
return True

# Use this to serialize an Any type to something that can be put in a span attribute.
# Span attributes must be one of str, bytes, int, float or bool.
def serialize_any_to_span_attribute(item: Any) -> str | bytes | int | float | bool:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not need it in the long term - any is a valid span attribute type, we just don't support it yet in python. should we keep this method in the libs that need it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well anyone that uses this will need it until that change goes through.. is there an open bug or something that I can put here to say we will eventually remove this ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not used today by existing instrumentations - it's done internally via

removing public api in the future is costry, so if there is 1-2 spots where it's actually needed copy-paste is better than future breaking changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right that code serializes everything to a string -- that's the same thing I'm doing here -- it sounds like you are saying we should not be doing that IIUC ? but instead do it in the instrumentation libraries so people don't get the wrong idea ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying that most instrumentation libs don't need this method to be public in the utils. And those who do migh be better with having this logic internal as well so that they won't suffer from utils removing this method in the future.

My main worry is utils becoming a pile of disjoint helpers (what usually happens with utils) which is impossible to clean up or consolidate because it's breaking everyone. If things are internal it's much of a smaller problem to solve.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that's fair.. Can you take a look at the updates i've made ? The issue is I need to serialize an Any. For now I just ended up using the existing utility to do it.

if isinstance(item, (str, bytes, int, float, bool)):
return item
return gen_ai_json_dumps(item)

class _GenAiJsonEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
Expand Down
Loading