-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathLLMObsSystem.java
More file actions
104 lines (85 loc) · 3.28 KB
/
LLMObsSystem.java
File metadata and controls
104 lines (85 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package datadog.trace.llmobs;
import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.trace.api.Config;
import datadog.trace.api.llmobs.LLMObs;
import datadog.trace.api.llmobs.LLMObsSpan;
import datadog.trace.api.llmobs.LLMObsTags;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.llmobs.domain.DDLLMObsSpan;
import datadog.trace.llmobs.domain.LLMObsInternal;
import java.lang.instrument.Instrumentation;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LLMObsSystem {
private static final Logger LOGGER = LoggerFactory.getLogger(LLMObsSystem.class);
private static final String CUSTOM_MODEL_VAL = "custom";
public static void start(Instrumentation inst, SharedCommunicationObjects sco) {
Config config = Config.get();
if (!config.isLlmObsEnabled()) {
LOGGER.debug("LLM Observability is disabled");
return;
}
sco.createRemaining(config);
LLMObsInternal.setLLMObsSpanFactory(
new LLMObsManualSpanFactory(config.getLlmObsMlApp(), config.getServiceName()));
}
private static class LLMObsManualSpanFactory implements LLMObs.LLMObsSpanFactory {
private final String serviceName;
private final String defaultMLApp;
public LLMObsManualSpanFactory(String defaultMLApp, String serviceName) {
this.defaultMLApp = defaultMLApp;
this.serviceName = serviceName;
}
@Override
public LLMObsSpan startLLMSpan(
String spanName,
String modelName,
String modelProvider,
@Nullable String mlApp,
@Nullable String sessionID) {
DDLLMObsSpan span =
new DDLLMObsSpan(
Tags.LLMOBS_LLM_SPAN_KIND, spanName, getMLApp(mlApp), sessionID, serviceName);
if (modelName == null || modelName.isEmpty()) {
modelName = CUSTOM_MODEL_VAL;
}
span.setTag(LLMObsTags.MODEL_NAME, modelName);
if (modelProvider == null || modelProvider.isEmpty()) {
modelProvider = CUSTOM_MODEL_VAL;
}
span.setTag(LLMObsTags.MODEL_PROVIDER, modelProvider);
return span;
}
@Override
public LLMObsSpan startAgentSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
return new DDLLMObsSpan(
Tags.LLMOBS_AGENT_SPAN_KIND, spanName, getMLApp(mlApp), sessionID, serviceName);
}
@Override
public LLMObsSpan startToolSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
return new DDLLMObsSpan(
Tags.LLMOBS_TOOL_SPAN_KIND, spanName, getMLApp(mlApp), sessionID, serviceName);
}
@Override
public LLMObsSpan startTaskSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
return new DDLLMObsSpan(
Tags.LLMOBS_TASK_SPAN_KIND, spanName, getMLApp(mlApp), sessionID, serviceName);
}
@Override
public LLMObsSpan startWorkflowSpan(
String spanName, @Nullable String mlApp, @Nullable String sessionID) {
return new DDLLMObsSpan(
Tags.LLMOBS_WORKFLOW_SPAN_KIND, spanName, getMLApp(mlApp), sessionID, serviceName);
}
private String getMLApp(String mlApp) {
if (mlApp == null || mlApp.isEmpty()) {
return defaultMLApp;
}
return mlApp;
}
}
}