|
1 | 1 | package datadog.trace.instrumentation.openai_java; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertNull; |
6 | 7 |
|
|
15 | 16 | import com.sun.net.httpserver.HttpServer; |
16 | 17 | import datadog.context.ContextScope; |
17 | 18 | import datadog.trace.agent.test.AbstractInstrumentationTest; |
| 19 | +import datadog.trace.api.Config; |
18 | 20 | import datadog.trace.api.llmobs.GenAiApmTags; |
19 | 21 | import datadog.trace.api.llmobs.LLMObsContext; |
20 | 22 | import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
@@ -267,6 +269,105 @@ void openAiRequestSpanComputesItsOwnSamplingDecisionWhenNoLlmObsContext() throws |
267 | 269 | assertEquals("1", openAiSpan.getTag("_ml_obs_tag.sample_rate")); |
268 | 270 | } |
269 | 271 |
|
| 272 | + @Test |
| 273 | + void openAiRequestSpanStaysInTheParentsAdoptedLlmObsTrace() throws Exception { |
| 274 | + // A parent that continued a trace from another service carries that service's LLMObs trace id, |
| 275 | + // which is not the local APM trace id. The LLM span must report the parent's id, or it lands |
| 276 | + // in a different LLMObs trace from its parent. |
| 277 | + String adoptedTraceId = "6ab41f6e00000000fea4f6b7295bd5e9"; |
| 278 | + |
| 279 | + AgentSpan parentSpan = AgentTracer.startSpan("test", "parent"); |
| 280 | + try (ContextScope ignored1 = AgentTracer.activateSpan(parentSpan)) { |
| 281 | + try (ContextScope ignored2 = |
| 282 | + LLMObsContext.attach( |
| 283 | + parentSpan.spanContext(), |
| 284 | + adoptedTraceId, |
| 285 | + null, |
| 286 | + null, |
| 287 | + null, |
| 288 | + "1", |
| 289 | + LLMObsContext.SAMPLING_DECISION_SAMPLED, |
| 290 | + null, |
| 291 | + null)) { |
| 292 | + openAiClient.chat().completions().create(buildMinimalChatParams()); |
| 293 | + } |
| 294 | + } finally { |
| 295 | + parentSpan.finish(); |
| 296 | + } |
| 297 | + |
| 298 | + writer.waitForTraces(1); |
| 299 | + DDSpan openAiSpan = findSpanByOperationName(writer, "openai.request"); |
| 300 | + assertNotNull(openAiSpan, "openai.request span should have been created"); |
| 301 | + assertNotEquals(adoptedTraceId, openAiSpan.getTraceId().toHexString()); |
| 302 | + assertEquals(adoptedTraceId, openAiSpan.getTag("_ml_obs_tag.trace_id")); |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + void openAiRequestSpanInheritsMlAppFromActiveContext() throws Exception { |
| 307 | + String expectedMlApp = "upstream-app"; |
| 308 | + |
| 309 | + AgentSpan parentSpan = AgentTracer.startSpan("test", "parent"); |
| 310 | + try (ContextScope ignored1 = AgentTracer.activateSpan(parentSpan)) { |
| 311 | + try (ContextScope ignored2 = |
| 312 | + LLMObsContext.attach( |
| 313 | + parentSpan.spanContext(), |
| 314 | + null, |
| 315 | + expectedMlApp, |
| 316 | + null, |
| 317 | + null, |
| 318 | + "1", |
| 319 | + LLMObsContext.SAMPLING_DECISION_SAMPLED, |
| 320 | + null, |
| 321 | + null)) { |
| 322 | + openAiClient.chat().completions().create(buildMinimalChatParams()); |
| 323 | + } |
| 324 | + } finally { |
| 325 | + parentSpan.finish(); |
| 326 | + } |
| 327 | + |
| 328 | + writer.waitForTraces(1); |
| 329 | + DDSpan openAiSpan = findSpanByOperationName(writer, "openai.request"); |
| 330 | + assertNotNull(openAiSpan, "openai.request span should have been created"); |
| 331 | + assertEquals(expectedMlApp, openAiSpan.getTag("_ml_obs_tag.ml_app")); |
| 332 | + } |
| 333 | + |
| 334 | + @Test |
| 335 | + void openAiRequestSpanUsesServiceDefaultsWithoutLlmObsContext() throws Exception { |
| 336 | + openAiClient.chat().completions().create(buildMinimalChatParams()); |
| 337 | + |
| 338 | + // No parent: no trace_id tag, so the mapper seeds it from the APM trace id, and ml_app is the |
| 339 | + // configured default. |
| 340 | + writer.waitForTraces(1); |
| 341 | + DDSpan openAiSpan = findSpanByOperationName(writer, "openai.request"); |
| 342 | + assertNotNull(openAiSpan, "openai.request span should have been created"); |
| 343 | + assertNull(openAiSpan.getTag("_ml_obs_tag.trace_id")); |
| 344 | + assertEquals(Config.get().getLlmObsMlApp(), openAiSpan.getTag("_ml_obs_tag.ml_app")); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + void openAiRequestSpanRecordsNoSamplingDecisionWhenItsParentHasNone() throws Exception { |
| 349 | + // The parent continued a trace whose caller sent no verdict, so it has none. The LLM span must |
| 350 | + // not roll its own: below a rate of 1 that could drop the span while its parent is kept. |
| 351 | + AgentSpan parentSpan = AgentTracer.startSpan("test", "parent"); |
| 352 | + try (ContextScope ignored1 = AgentTracer.activateSpan(parentSpan)) { |
| 353 | + try (ContextScope ignored2 = |
| 354 | + LLMObsContext.attach( |
| 355 | + parentSpan.spanContext(), null, null, null, null, null, null, null, null)) { |
| 356 | + openAiClient.chat().completions().create(buildMinimalChatParams()); |
| 357 | + } |
| 358 | + } finally { |
| 359 | + parentSpan.finish(); |
| 360 | + } |
| 361 | + |
| 362 | + writer.waitForTraces(1); |
| 363 | + DDSpan openAiSpan = findSpanByOperationName(writer, "openai.request"); |
| 364 | + assertNotNull(openAiSpan, "openai.request span should have been created"); |
| 365 | + assertEquals( |
| 366 | + String.valueOf(parentSpan.getSpanId()), openAiSpan.getTag("_ml_obs_tag.parent_id")); |
| 367 | + assertNull(openAiSpan.getTag("_ml_obs_tag.sampling_decision")); |
| 368 | + assertNull(openAiSpan.getTag("_ml_obs_tag.sample_rate")); |
| 369 | + } |
| 370 | + |
270 | 371 | @Test |
271 | 372 | void openAiRequestSpanInheritsNothingFromStaleCrossTraceContext() throws Exception { |
272 | 373 | // Simulates a stale LLMObsContext leaked across an async boundary: the context is attached, |
|
0 commit comments