|
17 | 17 | Tests for utility functions in opentelemetry.instrumentation.agentscope.utils |
18 | 18 | """ |
19 | 19 |
|
| 20 | +from types import SimpleNamespace |
| 21 | + |
20 | 22 | from agentscope.message import Msg, ToolResultBlock |
21 | 23 | from agentscope.tracing._converter import ( |
22 | 24 | _convert_block_to_part as _convert_block_to_part_framework, |
23 | 25 | ) |
24 | 26 |
|
| 27 | +from opentelemetry import baggage |
| 28 | +from opentelemetry import context as otel_context |
| 29 | +from opentelemetry.instrumentation.agentscope import utils as utils_module |
25 | 30 | from opentelemetry.instrumentation.agentscope.utils import ( |
26 | 31 | _convert_block_to_part as _convert_block_to_part_local, |
27 | 32 | ) |
28 | 33 | from opentelemetry.instrumentation.agentscope.utils import ( |
| 34 | + apply_entry_baggage_identity, |
29 | 35 | convert_agentscope_messages_to_genai_format, |
| 36 | + create_agent_invocation, |
| 37 | + create_embedding_invocation, |
| 38 | + create_llm_invocation, |
| 39 | + entry_baggage_identity_attributes, |
| 40 | +) |
| 41 | +from opentelemetry.util.genai.extended_semconv.gen_ai_extended_attributes import ( |
| 42 | + GEN_AI_SESSION_ID, |
| 43 | + GEN_AI_USER_ID, |
30 | 44 | ) |
| 45 | +from opentelemetry.util.genai.extended_types import ReactStepInvocation |
31 | 46 | from opentelemetry.util.genai.types import ToolCallResponse |
32 | 47 |
|
33 | 48 |
|
@@ -103,3 +118,106 @@ def test_convert_with_framework_converter(self): |
103 | 118 | part_obj = converted[0].parts[0] |
104 | 119 | assert isinstance(part_obj, ToolCallResponse) |
105 | 120 | assert part_obj.response == "framework output" |
| 121 | + |
| 122 | + def test_create_agent_invocation_prefers_entry_baggage_identity( |
| 123 | + self, monkeypatch |
| 124 | + ): |
| 125 | + monkeypatch.setattr( |
| 126 | + utils_module._config, "run_id", "agentscope-run-id" |
| 127 | + ) |
| 128 | + ctx = baggage.set_baggage(GEN_AI_SESSION_ID, "entry-session") |
| 129 | + ctx = baggage.set_baggage(GEN_AI_USER_ID, "entry-user", ctx) |
| 130 | + token = otel_context.attach(ctx) |
| 131 | + try: |
| 132 | + invocation = create_agent_invocation( |
| 133 | + SimpleNamespace( |
| 134 | + model=None, |
| 135 | + name="TestAgent", |
| 136 | + id="agent-id", |
| 137 | + sys_prompt=None, |
| 138 | + ), |
| 139 | + tuple(), |
| 140 | + {}, |
| 141 | + ) |
| 142 | + finally: |
| 143 | + otel_context.detach(token) |
| 144 | + |
| 145 | + assert invocation.conversation_id == "entry-session" |
| 146 | + assert invocation.attributes[GEN_AI_SESSION_ID] == "entry-session" |
| 147 | + assert invocation.attributes[GEN_AI_USER_ID] == "entry-user" |
| 148 | + |
| 149 | + def test_entry_baggage_identity_attributes(self): |
| 150 | + ctx = baggage.set_baggage(GEN_AI_SESSION_ID, "entry-session") |
| 151 | + ctx = baggage.set_baggage(GEN_AI_USER_ID, "entry-user", ctx) |
| 152 | + token = otel_context.attach(ctx) |
| 153 | + try: |
| 154 | + attributes = entry_baggage_identity_attributes() |
| 155 | + finally: |
| 156 | + otel_context.detach(token) |
| 157 | + |
| 158 | + assert attributes == { |
| 159 | + GEN_AI_SESSION_ID: "entry-session", |
| 160 | + GEN_AI_USER_ID: "entry-user", |
| 161 | + } |
| 162 | + |
| 163 | + def test_create_agent_invocation_falls_back_to_agentscope_run_id( |
| 164 | + self, monkeypatch |
| 165 | + ): |
| 166 | + monkeypatch.setattr( |
| 167 | + utils_module._config, "run_id", "agentscope-run-id" |
| 168 | + ) |
| 169 | + |
| 170 | + invocation = create_agent_invocation( |
| 171 | + SimpleNamespace( |
| 172 | + model=None, |
| 173 | + name="TestAgent", |
| 174 | + id="agent-id", |
| 175 | + sys_prompt=None, |
| 176 | + ), |
| 177 | + tuple(), |
| 178 | + {}, |
| 179 | + ) |
| 180 | + |
| 181 | + assert invocation.conversation_id == "agentscope-run-id" |
| 182 | + assert GEN_AI_SESSION_ID not in invocation.attributes |
| 183 | + assert GEN_AI_USER_ID not in invocation.attributes |
| 184 | + |
| 185 | + def test_model_invocations_copy_entry_baggage_identity(self): |
| 186 | + ctx = baggage.set_baggage(GEN_AI_SESSION_ID, "entry-session") |
| 187 | + ctx = baggage.set_baggage(GEN_AI_USER_ID, "entry-user", ctx) |
| 188 | + token = otel_context.attach(ctx) |
| 189 | + try: |
| 190 | + llm_invocation = create_llm_invocation( |
| 191 | + SimpleNamespace(model_name="qwen-max"), |
| 192 | + tuple(), |
| 193 | + {}, |
| 194 | + ) |
| 195 | + embedding_invocation = create_embedding_invocation( |
| 196 | + SimpleNamespace(model_name="text-embedding-v4"), |
| 197 | + tuple(), |
| 198 | + {}, |
| 199 | + ) |
| 200 | + finally: |
| 201 | + otel_context.detach(token) |
| 202 | + |
| 203 | + assert llm_invocation.conversation_id == "entry-session" |
| 204 | + assert llm_invocation.attributes[GEN_AI_SESSION_ID] == "entry-session" |
| 205 | + assert llm_invocation.attributes[GEN_AI_USER_ID] == "entry-user" |
| 206 | + assert ( |
| 207 | + embedding_invocation.attributes[GEN_AI_SESSION_ID] |
| 208 | + == "entry-session" |
| 209 | + ) |
| 210 | + assert embedding_invocation.attributes[GEN_AI_USER_ID] == "entry-user" |
| 211 | + |
| 212 | + def test_react_step_invocation_copies_entry_baggage_identity(self): |
| 213 | + ctx = baggage.set_baggage(GEN_AI_SESSION_ID, "entry-session") |
| 214 | + ctx = baggage.set_baggage(GEN_AI_USER_ID, "entry-user", ctx) |
| 215 | + token = otel_context.attach(ctx) |
| 216 | + try: |
| 217 | + invocation = ReactStepInvocation(round=1) |
| 218 | + apply_entry_baggage_identity(invocation) |
| 219 | + finally: |
| 220 | + otel_context.detach(token) |
| 221 | + |
| 222 | + assert invocation.attributes[GEN_AI_SESSION_ID] == "entry-session" |
| 223 | + assert invocation.attributes[GEN_AI_USER_ID] == "entry-user" |
0 commit comments