-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathDDLLMObsSpan.java
295 lines (263 loc) · 8.11 KB
/
DDLLMObsSpan.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package datadog.trace.llmobs.domain;
import datadog.context.ContextScope;
import datadog.trace.api.DDSpanTypes;
import datadog.trace.api.llmobs.LLMObs;
import datadog.trace.api.llmobs.LLMObsSpan;
import datadog.trace.api.llmobs.LLMObsTags;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DDLLMObsSpan implements LLMObsSpan {
private static final String LLM_MESSAGE_UNKNOWN_ROLE = "unknown";
// Well known tags for LLM obs will be prefixed with _ml_obs_(tags|metrics).
// Prefix for tags
private static final String LLMOBS_TAG_PREFIX = "_ml_obs_tag.";
// Prefix for metrics
private static final String LLMOBS_METRIC_PREFIX = "_ml_obs_metric.";
// internal tags to be prefixed
private static final String INPUT = LLMOBS_TAG_PREFIX + "input";
private static final String OUTPUT = LLMOBS_TAG_PREFIX + "output";
private static final String SPAN_KIND = LLMOBS_TAG_PREFIX + Tags.SPAN_KIND;
private static final String METADATA = LLMOBS_TAG_PREFIX + LLMObsTags.METADATA;
private static final String PARENT_ID_TAG_INTERNAL = "parent_id";
private static final String LLM_OBS_INSTRUMENTATION_NAME = "llmobs";
private static final Logger LOGGER = LoggerFactory.getLogger(DDLLMObsSpan.class);
private final AgentSpan span;
private final String spanKind;
private final ContextScope scope;
private boolean finished = false;
public DDLLMObsSpan(
@Nonnull String kind,
String spanName,
@Nonnull String mlApp,
String sessionID,
@Nonnull String serviceName) {
if (null == spanName || spanName.isEmpty()) {
spanName = kind;
}
AgentTracer.SpanBuilder spanBuilder =
AgentTracer.get()
.buildSpan(LLM_OBS_INSTRUMENTATION_NAME, spanName)
.withServiceName(serviceName)
.withSpanType(DDSpanTypes.LLMOBS);
this.span = spanBuilder.start();
this.span.setTag(SPAN_KIND, kind);
this.spanKind = kind;
this.span.setTag(LLMOBS_TAG_PREFIX + LLMObsTags.ML_APP, mlApp);
if (sessionID != null && !sessionID.isEmpty()) {
this.span.setTag(LLMOBS_TAG_PREFIX + LLMObsTags.SESSION_ID, sessionID);
}
AgentSpanContext parent = LLMObsState.getLLMObsParentContext();
String parentSpanID = LLMObsState.ROOT_SPAN_ID;
if (null != parent) {
if (parent.getTraceId() != this.span.getTraceId()) {
LOGGER.error("trace ID mismatch, retrieved parent from context trace_id={}, span_id={}, started span trace_id={}, span_id={}", parent.getTraceId(), parent.getSpanId(), this.span.getTraceId(), this.span.getSpanId());
} else {
parentSpanID = String.valueOf(parent.getSpanId());
}
}
this.span.setTag(
LLMOBS_TAG_PREFIX + PARENT_ID_TAG_INTERNAL, parentSpanID);
this.scope = LLMObsState.attach();
LLMObsState.setLLMObsParentContext(this.span.context());
}
@Override
public String toString() {
return super.toString()
+ ", trace_id="
+ this.span.context().getTraceId()
+ ", span_id="
+ this.span.context().getSpanId()
+ ", ml_app="
+ this.span.getTag(LLMObsTags.ML_APP)
+ ", service="
+ this.span.getServiceName()
+ ", span_kind="
+ this.span.getTag(SPAN_KIND);
}
@Override
public void annotateIO(List<LLMObs.LLMMessage> inputData, List<LLMObs.LLMMessage> outputData) {
if (finished) {
return;
}
if (inputData != null && !inputData.isEmpty()) {
this.span.setTag(INPUT, inputData);
}
if (outputData != null && !outputData.isEmpty()) {
this.span.setTag(OUTPUT, outputData);
}
}
@Override
public void annotateIO(String inputData, String outputData) {
if (finished) {
return;
}
boolean wrongSpanKind = false;
if (inputData != null && !inputData.isEmpty()) {
if (Tags.LLMOBS_LLM_SPAN_KIND.equals(this.spanKind)) {
wrongSpanKind = true;
annotateIO(
Collections.singletonList(LLMObs.LLMMessage.from(LLM_MESSAGE_UNKNOWN_ROLE, inputData)),
null);
} else {
this.span.setTag(INPUT, inputData);
}
}
if (outputData != null && !outputData.isEmpty()) {
if (Tags.LLMOBS_LLM_SPAN_KIND.equals(this.spanKind)) {
wrongSpanKind = true;
annotateIO(
null,
Collections.singletonList(
LLMObs.LLMMessage.from(LLM_MESSAGE_UNKNOWN_ROLE, outputData)));
} else {
this.span.setTag(OUTPUT, outputData);
}
}
if (wrongSpanKind) {
LOGGER.warn(
"the span being annotated is an LLM span, it is recommended to use the overload with List<LLMObs.LLMMessage> as arguments");
}
}
@Override
public void setMetadata(Map<String, Object> metadata) {
if (finished) {
return;
}
Object value = span.getTag(METADATA);
if (value == null) {
this.span.setTag(METADATA, new HashMap<>(metadata));
return;
}
if (value instanceof Map) {
((Map) value).putAll(metadata);
} else {
LOGGER.debug(
"unexpected instance type for metadata {}, overwriting for now",
value.getClass().getName());
this.span.setTag(METADATA, new HashMap<>(metadata));
}
}
@Override
public void setMetrics(Map<String, Number> metrics) {
if (finished) {
return;
}
for (Map.Entry<String, Number> entry : metrics.entrySet()) {
this.span.setMetric(LLMOBS_METRIC_PREFIX + entry.getKey(), entry.getValue().doubleValue());
}
}
@Override
public void setMetric(CharSequence key, int value) {
if (finished) {
return;
}
this.span.setMetric(LLMOBS_METRIC_PREFIX + key, value);
}
@Override
public void setMetric(CharSequence key, long value) {
if (finished) {
return;
}
this.span.setMetric(LLMOBS_METRIC_PREFIX + key, value);
}
@Override
public void setMetric(CharSequence key, double value) {
if (finished) {
return;
}
this.span.setMetric(LLMOBS_METRIC_PREFIX + key, value);
}
@Override
public void setTags(Map<String, Object> tags) {
if (finished) {
return;
}
if (tags != null && !tags.isEmpty()) {
for (Map.Entry<String, Object> entry : tags.entrySet()) {
this.span.setTag(LLMOBS_TAG_PREFIX + entry.getKey(), entry.getValue());
}
}
}
@Override
public void setTag(String key, String value) {
if (finished) {
return;
}
this.span.setTag(LLMOBS_TAG_PREFIX + key, value);
}
@Override
public void setTag(String key, boolean value) {
if (finished) {
return;
}
this.span.setTag(LLMOBS_TAG_PREFIX + key, value);
}
@Override
public void setTag(String key, int value) {
if (finished) {
return;
}
this.span.setTag(LLMOBS_TAG_PREFIX + key, value);
}
@Override
public void setTag(String key, long value) {
if (finished) {
return;
}
this.span.setTag(LLMOBS_TAG_PREFIX + key, value);
}
@Override
public void setTag(String key, double value) {
if (finished) {
return;
}
this.span.setTag(LLMOBS_TAG_PREFIX + key, value);
}
@Override
public void setError(boolean error) {
if (finished) {
return;
}
this.span.setError(error);
}
@Override
public void setErrorMessage(String errorMessage) {
if (finished) {
return;
}
if (errorMessage == null || errorMessage.isEmpty()) {
return;
}
this.span.setError(true);
this.span.setErrorMessage(errorMessage);
}
@Override
public void addThrowable(Throwable throwable) {
if (finished) {
return;
}
if (throwable == null) {
return;
}
this.span.setError(true);
this.span.addThrowable(throwable);
}
@Override
public void finish() {
if (finished) {
return;
}
this.span.finish();
this.scope.close();
this.finished = true;
}
}