Skip to content

Commit fe94b12

Browse files
committed
add context attributes
1 parent 60ee275 commit fe94b12

File tree

1 file changed

+46
-0
lines changed
  • python/openinference-instrumentation/src/openinference/instrumentation

1 file changed

+46
-0
lines changed

python/openinference-instrumentation/src/openinference/instrumentation/_attributes.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Any,
1010
Dict,
1111
Iterator,
12+
List,
1213
Literal,
1314
Optional,
1415
Tuple,
@@ -40,6 +41,7 @@
4041
TokenCount,
4142
Tool,
4243
)
44+
from .helpers import safe_json_dumps
4345

4446
pydantic: Optional[ModuleType]
4547
try:
@@ -51,6 +53,46 @@
5153
from _typeshed import DataclassInstance
5254

5355

56+
def get_context_attributes(
57+
*,
58+
session_id: Optional[str] = None,
59+
user_id: Optional[str] = None,
60+
metadata: Optional[Union[str, Dict[str, Any]]] = None,
61+
tags: Optional[List[str]] = None,
62+
) -> Dict[str, AttributeValue]:
63+
context_attributes: Dict[str, AttributeValue] = {}
64+
if session_id is not None:
65+
context_attributes.update(get_session_attributes(session_id=session_id))
66+
if user_id is not None:
67+
context_attributes.update(get_user_id(user_id=user_id))
68+
if metadata is not None:
69+
context_attributes.update(get_metadata_attributes(metadata=metadata))
70+
if tags is not None:
71+
context_attributes.update(get_tag_attributes(tags=tags))
72+
return context_attributes
73+
74+
75+
def get_session_attributes(*, session_id: str) -> Dict[str, AttributeValue]:
76+
return {SESSION_ID: session_id}
77+
78+
79+
def get_tag_attributes(*, tags: List[str]) -> Dict[str, AttributeValue]:
80+
return {TAG_TAGS: tags}
81+
82+
83+
def get_metadata_attributes(*, metadata: Union[str, Dict[str, Any]]) -> Dict[str, AttributeValue]:
84+
serialized_metadata: str
85+
if isinstance(metadata, str):
86+
serialized_metadata = metadata
87+
else:
88+
serialized_metadata = safe_json_dumps(metadata)
89+
return {METADATA: serialized_metadata}
90+
91+
92+
def get_user_id(*, user_id: str) -> Dict[str, AttributeValue]:
93+
return {USER_ID: user_id}
94+
95+
5496
def get_span_kind_attributes(kind: "OpenInferenceSpanKind", /) -> Dict[str, AttributeValue]:
5597
normalized_kind = _normalize_openinference_span_kind(kind)
5698
return {
@@ -402,12 +444,16 @@ def get_llm_tool_attributes(
402444
LLM_TOKEN_COUNT_PROMPT = SpanAttributes.LLM_TOKEN_COUNT_PROMPT
403445
LLM_TOKEN_COUNT_TOTAL = SpanAttributes.LLM_TOKEN_COUNT_TOTAL
404446
LLM_TOOLS = SpanAttributes.LLM_TOOLS
447+
METADATA = SpanAttributes.METADATA
405448
OPENINFERENCE_SPAN_KIND = SpanAttributes.OPENINFERENCE_SPAN_KIND
406449
OUTPUT_MIME_TYPE = SpanAttributes.OUTPUT_MIME_TYPE
407450
OUTPUT_VALUE = SpanAttributes.OUTPUT_VALUE
451+
SESSION_ID = SpanAttributes.SESSION_ID
452+
TAG_TAGS = SpanAttributes.TAG_TAGS
408453
TOOL_DESCRIPTION = SpanAttributes.TOOL_DESCRIPTION
409454
TOOL_NAME = SpanAttributes.TOOL_NAME
410455
TOOL_PARAMETERS = SpanAttributes.TOOL_PARAMETERS
456+
USER_ID = SpanAttributes.USER_ID
411457

412458

413459
# tool attributes

0 commit comments

Comments
 (0)