@cloudbase/agent-observability 1.0.1-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +231 -0
- package/dist/chunk-NFEGQTCC.mjs +27 -0
- package/dist/chunk-NFEGQTCC.mjs.map +1 -0
- package/dist/chunk-ZGEMAYS4.mjs +716 -0
- package/dist/chunk-ZGEMAYS4.mjs.map +1 -0
- package/dist/esm-PGEDANAI.mjs +1030 -0
- package/dist/esm-PGEDANAI.mjs.map +1 -0
- package/dist/index.d.mts +728 -0
- package/dist/index.d.ts +728 -0
- package/dist/index.js +732 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +52 -0
- package/dist/index.mjs.map +1 -0
- package/dist/langchain.d.mts +108 -0
- package/dist/langchain.d.ts +108 -0
- package/dist/langchain.js +1237 -0
- package/dist/langchain.js.map +1 -0
- package/dist/langchain.mjs +535 -0
- package/dist/langchain.mjs.map +1 -0
- package/dist/server.d.mts +163 -0
- package/dist/server.d.ts +163 -0
- package/dist/server.js +1530 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +177 -0
- package/dist/server.mjs.map +1 -0
- package/package.json +91 -0
- package/src/core/attributes.ts +233 -0
- package/src/core/constants.ts +75 -0
- package/src/core/spanWrapper.ts +417 -0
- package/src/core/tracerProvider.ts +136 -0
- package/src/index.ts +775 -0
- package/src/langchain/CallbackHandler.ts +893 -0
- package/src/langchain/index.ts +7 -0
- package/src/server/config.ts +160 -0
- package/src/server/index.ts +21 -0
- package/src/server/setup.ts +349 -0
- package/src/types.ts +254 -0
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__esm,
|
|
3
|
+
__export,
|
|
4
|
+
__toCommonJS
|
|
5
|
+
} from "./chunk-NFEGQTCC.mjs";
|
|
6
|
+
|
|
7
|
+
// src/core/constants.ts
|
|
8
|
+
import {
|
|
9
|
+
SemanticConventions,
|
|
10
|
+
OpenInferenceSpanKind
|
|
11
|
+
} from "@arizeai/openinference-semantic-conventions";
|
|
12
|
+
var OtelSpanAttributes;
|
|
13
|
+
var init_constants = __esm({
|
|
14
|
+
"src/core/constants.ts"() {
|
|
15
|
+
"use strict";
|
|
16
|
+
OtelSpanAttributes = {
|
|
17
|
+
// OpenInference - re-export all standard conventions
|
|
18
|
+
...SemanticConventions,
|
|
19
|
+
// AG-Kit Trace attributes (non-standard)
|
|
20
|
+
TRACE_NAME: "trace.name",
|
|
21
|
+
TRACE_TAGS: "trace.tags",
|
|
22
|
+
TRACE_PUBLIC: "trace.public",
|
|
23
|
+
TRACE_METADATA: "trace.metadata",
|
|
24
|
+
TRACE_INPUT: "trace.input",
|
|
25
|
+
TRACE_OUTPUT: "trace.output",
|
|
26
|
+
// AG-Kit Observation attributes (non-standard)
|
|
27
|
+
OBSERVATION_TYPE: "observation.type",
|
|
28
|
+
OBSERVATION_LEVEL: "observation.level",
|
|
29
|
+
OBSERVATION_STATUS_MESSAGE: "observation.status_message",
|
|
30
|
+
OBSERVATION_INPUT: "observation.input",
|
|
31
|
+
OBSERVATION_OUTPUT: "observation.output",
|
|
32
|
+
OBSERVATION_METADATA: "observation.metadata",
|
|
33
|
+
// AG-Kit LLM-specific (non-standard)
|
|
34
|
+
LLM_COMPLETION_START_TIME: "llm.completion_start_time",
|
|
35
|
+
LLM_MODEL_PARAMETERS: "llm.model_parameters",
|
|
36
|
+
LLM_USAGE_DETAILS: "llm.usage_details",
|
|
37
|
+
LLM_COST_DETAILS: "llm.cost_details",
|
|
38
|
+
// AG-Kit Retriever-specific (non-standard)
|
|
39
|
+
RETRIEVER_NAME: "retriever.name",
|
|
40
|
+
RETRIEVER_QUERY: "retriever.query",
|
|
41
|
+
RETRIEVER_INDEX_ID: "retriever.index_id",
|
|
42
|
+
RETRIEVER_TOP_K: "retriever.top_k",
|
|
43
|
+
// AG-Kit General (non-standard)
|
|
44
|
+
ENVIRONMENT: "environment",
|
|
45
|
+
RELEASE: "release",
|
|
46
|
+
VERSION: "version"
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// src/core/attributes.ts
|
|
52
|
+
import { SemanticConventions as SemanticConventions2 } from "@arizeai/openinference-semantic-conventions";
|
|
53
|
+
function createTraceAttributes({
|
|
54
|
+
name,
|
|
55
|
+
userId,
|
|
56
|
+
sessionId,
|
|
57
|
+
version,
|
|
58
|
+
release,
|
|
59
|
+
input,
|
|
60
|
+
output,
|
|
61
|
+
metadata,
|
|
62
|
+
tags,
|
|
63
|
+
environment,
|
|
64
|
+
public: isPublic
|
|
65
|
+
} = {}) {
|
|
66
|
+
const attributes = {
|
|
67
|
+
[OtelSpanAttributes.TRACE_NAME]: name,
|
|
68
|
+
// Use OpenInference standard attributes for user and session
|
|
69
|
+
[OtelSpanAttributes.USER_ID]: userId,
|
|
70
|
+
[OtelSpanAttributes.SESSION_ID]: sessionId,
|
|
71
|
+
[OtelSpanAttributes.VERSION]: version,
|
|
72
|
+
[OtelSpanAttributes.RELEASE]: release,
|
|
73
|
+
[OtelSpanAttributes.TRACE_INPUT]: _serialize(input),
|
|
74
|
+
[OtelSpanAttributes.TRACE_OUTPUT]: _serialize(output),
|
|
75
|
+
[OtelSpanAttributes.TRACE_TAGS]: tags,
|
|
76
|
+
[OtelSpanAttributes.ENVIRONMENT]: environment,
|
|
77
|
+
[OtelSpanAttributes.TRACE_PUBLIC]: isPublic,
|
|
78
|
+
..._flattenAndSerializeMetadata(metadata, OtelSpanAttributes.TRACE_METADATA)
|
|
79
|
+
};
|
|
80
|
+
return Object.fromEntries(
|
|
81
|
+
Object.entries(attributes).filter(([_, v]) => v != null)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function createObservationAttributes(type, attributes) {
|
|
85
|
+
const {
|
|
86
|
+
metadata,
|
|
87
|
+
input,
|
|
88
|
+
output,
|
|
89
|
+
level,
|
|
90
|
+
statusMessage,
|
|
91
|
+
version,
|
|
92
|
+
completionStartTime,
|
|
93
|
+
model,
|
|
94
|
+
modelParameters,
|
|
95
|
+
usageDetails
|
|
96
|
+
} = attributes;
|
|
97
|
+
const otelAttributes = {
|
|
98
|
+
[SemanticConventions2.OPENINFERENCE_SPAN_KIND]: type.toUpperCase(),
|
|
99
|
+
[OtelSpanAttributes.OBSERVATION_TYPE]: type,
|
|
100
|
+
[OtelSpanAttributes.OBSERVATION_LEVEL]: level,
|
|
101
|
+
[OtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
|
|
102
|
+
[OtelSpanAttributes.VERSION]: version,
|
|
103
|
+
// Use OpenInference input.value convention
|
|
104
|
+
[SemanticConventions2.INPUT_VALUE]: _serialize(input),
|
|
105
|
+
// Also set legacy agkit.observation.input for compatibility
|
|
106
|
+
[OtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
|
|
107
|
+
// Use OpenInference output.value convention
|
|
108
|
+
[SemanticConventions2.OUTPUT_VALUE]: _serialize(output),
|
|
109
|
+
// Also set legacy agkit.observation.output for compatibility
|
|
110
|
+
[OtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output)
|
|
111
|
+
};
|
|
112
|
+
if (type === "llm") {
|
|
113
|
+
if (model) {
|
|
114
|
+
otelAttributes[SemanticConventions2.LLM_MODEL_NAME] = model;
|
|
115
|
+
}
|
|
116
|
+
if (modelParameters) {
|
|
117
|
+
otelAttributes[SemanticConventions2.LLM_INVOCATION_PARAMETERS] = _serialize(modelParameters);
|
|
118
|
+
otelAttributes[OtelSpanAttributes.LLM_MODEL_PARAMETERS] = _serialize(modelParameters);
|
|
119
|
+
}
|
|
120
|
+
if (usageDetails) {
|
|
121
|
+
if (typeof usageDetails === "object") {
|
|
122
|
+
const usage = usageDetails;
|
|
123
|
+
if (usage.promptTokens !== void 0) {
|
|
124
|
+
otelAttributes[SemanticConventions2.LLM_TOKEN_COUNT_PROMPT] = usage.promptTokens;
|
|
125
|
+
}
|
|
126
|
+
if (usage.completionTokens !== void 0) {
|
|
127
|
+
otelAttributes[SemanticConventions2.LLM_TOKEN_COUNT_COMPLETION] = usage.completionTokens;
|
|
128
|
+
}
|
|
129
|
+
if (usage.totalTokens !== void 0) {
|
|
130
|
+
otelAttributes[SemanticConventions2.LLM_TOKEN_COUNT_TOTAL] = usage.totalTokens;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
otelAttributes[OtelSpanAttributes.LLM_USAGE_DETAILS] = _serialize(usageDetails);
|
|
134
|
+
}
|
|
135
|
+
if (completionStartTime) {
|
|
136
|
+
otelAttributes[OtelSpanAttributes.LLM_COMPLETION_START_TIME] = _serialize(completionStartTime);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (type === "embedding") {
|
|
140
|
+
if (model) {
|
|
141
|
+
otelAttributes[SemanticConventions2.EMBEDDING_MODEL_NAME] = model;
|
|
142
|
+
}
|
|
143
|
+
if (modelParameters) {
|
|
144
|
+
otelAttributes[SemanticConventions2.LLM_INVOCATION_PARAMETERS] = _serialize(modelParameters);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const metadataAttrs = _flattenAndSerializeMetadata(
|
|
148
|
+
metadata,
|
|
149
|
+
SemanticConventions2.METADATA
|
|
150
|
+
);
|
|
151
|
+
Object.assign(otelAttributes, metadataAttrs);
|
|
152
|
+
const obsetvabilityMetadataAttrs = _flattenAndSerializeMetadata(
|
|
153
|
+
metadata,
|
|
154
|
+
OtelSpanAttributes.OBSERVATION_METADATA
|
|
155
|
+
);
|
|
156
|
+
Object.assign(otelAttributes, obsetvabilityMetadataAttrs);
|
|
157
|
+
return Object.fromEntries(
|
|
158
|
+
Object.entries(otelAttributes).filter(([_, v]) => v != null)
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
function _serialize(obj) {
|
|
162
|
+
try {
|
|
163
|
+
if (typeof obj === "string") return obj;
|
|
164
|
+
if (obj instanceof Date) return obj.toISOString();
|
|
165
|
+
return obj != null ? JSON.stringify(obj) : void 0;
|
|
166
|
+
} catch {
|
|
167
|
+
return "<failed to serialize>";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function _flattenAndSerializeMetadata(metadata, prefix) {
|
|
171
|
+
const metadataAttributes = {};
|
|
172
|
+
if (metadata === void 0 || metadata === null) {
|
|
173
|
+
return metadataAttributes;
|
|
174
|
+
}
|
|
175
|
+
if (typeof metadata !== "object" || Array.isArray(metadata)) {
|
|
176
|
+
const serialized = _serialize(metadata);
|
|
177
|
+
if (serialized) {
|
|
178
|
+
metadataAttributes[prefix] = serialized;
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
182
|
+
const serialized = typeof value === "string" ? value : _serialize(value);
|
|
183
|
+
if (serialized) {
|
|
184
|
+
metadataAttributes[`${prefix}.${key}`] = serialized;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return metadataAttributes;
|
|
189
|
+
}
|
|
190
|
+
var init_attributes = __esm({
|
|
191
|
+
"src/core/attributes.ts"() {
|
|
192
|
+
"use strict";
|
|
193
|
+
init_constants();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// src/core/tracerProvider.ts
|
|
198
|
+
import { trace } from "@opentelemetry/api";
|
|
199
|
+
function createState() {
|
|
200
|
+
return {
|
|
201
|
+
isolatedTracerProvider: null
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function getObservabilityGlobalState() {
|
|
205
|
+
const initialState = createState();
|
|
206
|
+
try {
|
|
207
|
+
const g = globalThis;
|
|
208
|
+
if (typeof g !== "object" || g === null) {
|
|
209
|
+
console.warn(
|
|
210
|
+
"[Observability] globalThis is not available, using fallback state"
|
|
211
|
+
);
|
|
212
|
+
return initialState;
|
|
213
|
+
}
|
|
214
|
+
if (!g[OBSERVABILITY_GLOBAL_SYMBOL]) {
|
|
215
|
+
Object.defineProperty(g, OBSERVABILITY_GLOBAL_SYMBOL, {
|
|
216
|
+
value: initialState,
|
|
217
|
+
writable: false,
|
|
218
|
+
configurable: false,
|
|
219
|
+
enumerable: false
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return g[OBSERVABILITY_GLOBAL_SYMBOL];
|
|
223
|
+
} catch (err) {
|
|
224
|
+
console.error(
|
|
225
|
+
`[Observability] Failed to access global state: ${err instanceof Error ? err.message : String(err)}`
|
|
226
|
+
);
|
|
227
|
+
return initialState;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function setTracerProvider(provider) {
|
|
231
|
+
getObservabilityGlobalState().isolatedTracerProvider = provider;
|
|
232
|
+
}
|
|
233
|
+
function getTracerProvider() {
|
|
234
|
+
const { isolatedTracerProvider } = getObservabilityGlobalState();
|
|
235
|
+
if (isolatedTracerProvider) return isolatedTracerProvider;
|
|
236
|
+
return trace.getTracerProvider();
|
|
237
|
+
}
|
|
238
|
+
function getTracer() {
|
|
239
|
+
return getTracerProvider().getTracer(
|
|
240
|
+
OBSERVABILITY_SDK_NAME,
|
|
241
|
+
OBSERVABILITY_SDK_VERSION
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
var OBSERVABILITY_GLOBAL_SYMBOL, OBSERVABILITY_SDK_NAME, OBSERVABILITY_SDK_VERSION;
|
|
245
|
+
var init_tracerProvider = __esm({
|
|
246
|
+
"src/core/tracerProvider.ts"() {
|
|
247
|
+
"use strict";
|
|
248
|
+
OBSERVABILITY_GLOBAL_SYMBOL = /* @__PURE__ */ Symbol.for("observability");
|
|
249
|
+
OBSERVABILITY_SDK_NAME = "ag-kit-observability";
|
|
250
|
+
OBSERVABILITY_SDK_VERSION = "0.1.0";
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// src/core/spanWrapper.ts
|
|
255
|
+
var BaseObservation, ObservationSpan, ObservationLLM, ObservationEmbedding, ObservationAgent, ObservationTool, ObservationChain, ObservationRetriever, ObservationReranker, ObservationEvaluator, ObservationGuardrail;
|
|
256
|
+
var init_spanWrapper = __esm({
|
|
257
|
+
"src/core/spanWrapper.ts"() {
|
|
258
|
+
"use strict";
|
|
259
|
+
init_attributes();
|
|
260
|
+
init_tracerProvider();
|
|
261
|
+
BaseObservation = class {
|
|
262
|
+
/** The underlying OpenTelemetry span */
|
|
263
|
+
otelSpan;
|
|
264
|
+
/** The observation type */
|
|
265
|
+
type;
|
|
266
|
+
/** The span ID from the OpenTelemetry span context */
|
|
267
|
+
id;
|
|
268
|
+
/** The trace ID from the OpenTelemetry span context */
|
|
269
|
+
traceId;
|
|
270
|
+
constructor(params) {
|
|
271
|
+
this.otelSpan = params.otelSpan;
|
|
272
|
+
this.id = params.otelSpan.spanContext().spanId;
|
|
273
|
+
this.traceId = params.otelSpan.spanContext().traceId;
|
|
274
|
+
this.type = params.type;
|
|
275
|
+
if (params.attributes) {
|
|
276
|
+
this.otelSpan.setAttributes(
|
|
277
|
+
createObservationAttributes(params.type, params.attributes)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/** Gets the AG-Kit OpenTelemetry tracer instance */
|
|
282
|
+
get tracer() {
|
|
283
|
+
return getTracer();
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Ends the observation, marking it as complete.
|
|
287
|
+
*
|
|
288
|
+
* @param endTime - Optional end time, defaults to current time
|
|
289
|
+
*/
|
|
290
|
+
end(endTime) {
|
|
291
|
+
this.otelSpan.end(endTime);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Updates the OTEL span attributes.
|
|
295
|
+
*
|
|
296
|
+
* @param attributes - Attributes to update
|
|
297
|
+
* @internal
|
|
298
|
+
*/
|
|
299
|
+
updateOtelSpanAttributes(attributes) {
|
|
300
|
+
this.otelSpan.setAttributes(
|
|
301
|
+
createObservationAttributes(this.type, attributes)
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Updates the parent trace with new attributes.
|
|
306
|
+
*
|
|
307
|
+
* @param attributes - Trace attributes to set
|
|
308
|
+
* @returns This observation for method chaining
|
|
309
|
+
*/
|
|
310
|
+
updateTrace(attributes) {
|
|
311
|
+
this.otelSpan.setAttributes(createTraceAttributes(attributes));
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
startObservation(name, attributes, options) {
|
|
315
|
+
const { startObservation: startObs } = (init_src(), __toCommonJS(src_exports));
|
|
316
|
+
const { asType = "span" } = options || {};
|
|
317
|
+
return startObs(name, attributes, {
|
|
318
|
+
asType,
|
|
319
|
+
parentSpanContext: this.otelSpan.spanContext()
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
ObservationSpan = class extends BaseObservation {
|
|
324
|
+
constructor(params) {
|
|
325
|
+
super({ ...params, type: "span" });
|
|
326
|
+
}
|
|
327
|
+
update(attributes) {
|
|
328
|
+
super.updateOtelSpanAttributes(attributes);
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
ObservationLLM = class extends BaseObservation {
|
|
333
|
+
constructor(params) {
|
|
334
|
+
super({ ...params, type: "llm" });
|
|
335
|
+
}
|
|
336
|
+
update(attributes) {
|
|
337
|
+
super.updateOtelSpanAttributes(attributes);
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
ObservationEmbedding = class extends BaseObservation {
|
|
342
|
+
constructor(params) {
|
|
343
|
+
super({ ...params, type: "embedding" });
|
|
344
|
+
}
|
|
345
|
+
update(attributes) {
|
|
346
|
+
super.updateOtelSpanAttributes(attributes);
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
ObservationAgent = class extends BaseObservation {
|
|
351
|
+
constructor(params) {
|
|
352
|
+
super({ ...params, type: "agent" });
|
|
353
|
+
}
|
|
354
|
+
update(attributes) {
|
|
355
|
+
super.updateOtelSpanAttributes(attributes);
|
|
356
|
+
return this;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
ObservationTool = class extends BaseObservation {
|
|
360
|
+
constructor(params) {
|
|
361
|
+
super({ ...params, type: "tool" });
|
|
362
|
+
}
|
|
363
|
+
update(attributes) {
|
|
364
|
+
super.updateOtelSpanAttributes(attributes);
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
ObservationChain = class extends BaseObservation {
|
|
369
|
+
constructor(params) {
|
|
370
|
+
super({ ...params, type: "chain" });
|
|
371
|
+
}
|
|
372
|
+
update(attributes) {
|
|
373
|
+
super.updateOtelSpanAttributes(attributes);
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
ObservationRetriever = class extends BaseObservation {
|
|
378
|
+
constructor(params) {
|
|
379
|
+
super({ ...params, type: "retriever" });
|
|
380
|
+
}
|
|
381
|
+
update(attributes) {
|
|
382
|
+
super.updateOtelSpanAttributes(attributes);
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
ObservationReranker = class extends BaseObservation {
|
|
387
|
+
constructor(params) {
|
|
388
|
+
super({ ...params, type: "reranker" });
|
|
389
|
+
}
|
|
390
|
+
update(attributes) {
|
|
391
|
+
super.updateOtelSpanAttributes(attributes);
|
|
392
|
+
return this;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
ObservationEvaluator = class extends BaseObservation {
|
|
396
|
+
constructor(params) {
|
|
397
|
+
super({ ...params, type: "evaluator" });
|
|
398
|
+
}
|
|
399
|
+
update(attributes) {
|
|
400
|
+
super.updateOtelSpanAttributes(attributes);
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
ObservationGuardrail = class extends BaseObservation {
|
|
405
|
+
constructor(params) {
|
|
406
|
+
super({ ...params, type: "guardrail" });
|
|
407
|
+
}
|
|
408
|
+
update(attributes) {
|
|
409
|
+
super.updateOtelSpanAttributes(attributes);
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
// src/index.ts
|
|
417
|
+
var src_exports = {};
|
|
418
|
+
__export(src_exports, {
|
|
419
|
+
ObservationAgent: () => ObservationAgent,
|
|
420
|
+
ObservationChain: () => ObservationChain,
|
|
421
|
+
ObservationEmbedding: () => ObservationEmbedding,
|
|
422
|
+
ObservationEvaluator: () => ObservationEvaluator,
|
|
423
|
+
ObservationGuardrail: () => ObservationGuardrail,
|
|
424
|
+
ObservationLLM: () => ObservationLLM,
|
|
425
|
+
ObservationReranker: () => ObservationReranker,
|
|
426
|
+
ObservationRetriever: () => ObservationRetriever,
|
|
427
|
+
ObservationSpan: () => ObservationSpan,
|
|
428
|
+
ObservationTool: () => ObservationTool,
|
|
429
|
+
createObservationAttributes: () => createObservationAttributes,
|
|
430
|
+
createTraceAttributes: () => createTraceAttributes,
|
|
431
|
+
getActiveSpanId: () => getActiveSpanId,
|
|
432
|
+
getActiveTraceId: () => getActiveTraceId,
|
|
433
|
+
getTracer: () => getTracer,
|
|
434
|
+
getTracerProvider: () => getTracerProvider,
|
|
435
|
+
observe: () => observe,
|
|
436
|
+
setTracerProvider: () => setTracerProvider,
|
|
437
|
+
startActiveObservation: () => startActiveObservation,
|
|
438
|
+
startObservation: () => startObservation,
|
|
439
|
+
updateActiveObservation: () => updateActiveObservation,
|
|
440
|
+
updateActiveTrace: () => updateActiveTrace
|
|
441
|
+
});
|
|
442
|
+
import { trace as trace2, context as context2, SpanStatusCode } from "@opentelemetry/api";
|
|
443
|
+
function createOtelSpan(params) {
|
|
444
|
+
return getTracer().startSpan(
|
|
445
|
+
params.name,
|
|
446
|
+
{ startTime: params.startTime },
|
|
447
|
+
createParentContext(params.parentSpanContext)
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
function createParentContext(parentSpanContext) {
|
|
451
|
+
if (!parentSpanContext) return;
|
|
452
|
+
return trace2.setSpanContext(context2.active(), parentSpanContext);
|
|
453
|
+
}
|
|
454
|
+
function startObservation(name, attributes, options) {
|
|
455
|
+
const { asType = "span", ...observationOptions } = options || {};
|
|
456
|
+
const otelSpan = createOtelSpan({
|
|
457
|
+
name,
|
|
458
|
+
...observationOptions
|
|
459
|
+
});
|
|
460
|
+
switch (asType) {
|
|
461
|
+
case "llm":
|
|
462
|
+
return new ObservationLLM({
|
|
463
|
+
otelSpan,
|
|
464
|
+
attributes
|
|
465
|
+
});
|
|
466
|
+
case "embedding":
|
|
467
|
+
return new ObservationEmbedding({
|
|
468
|
+
otelSpan,
|
|
469
|
+
attributes
|
|
470
|
+
});
|
|
471
|
+
case "agent":
|
|
472
|
+
return new ObservationAgent({
|
|
473
|
+
otelSpan,
|
|
474
|
+
attributes
|
|
475
|
+
});
|
|
476
|
+
case "tool":
|
|
477
|
+
return new ObservationTool({
|
|
478
|
+
otelSpan,
|
|
479
|
+
attributes
|
|
480
|
+
});
|
|
481
|
+
case "chain":
|
|
482
|
+
return new ObservationChain({
|
|
483
|
+
otelSpan,
|
|
484
|
+
attributes
|
|
485
|
+
});
|
|
486
|
+
case "retriever":
|
|
487
|
+
return new ObservationRetriever({
|
|
488
|
+
otelSpan,
|
|
489
|
+
attributes
|
|
490
|
+
});
|
|
491
|
+
case "reranker":
|
|
492
|
+
return new ObservationReranker({
|
|
493
|
+
otelSpan,
|
|
494
|
+
attributes
|
|
495
|
+
});
|
|
496
|
+
case "evaluator":
|
|
497
|
+
return new ObservationEvaluator({
|
|
498
|
+
otelSpan,
|
|
499
|
+
attributes
|
|
500
|
+
});
|
|
501
|
+
case "guardrail":
|
|
502
|
+
return new ObservationGuardrail({
|
|
503
|
+
otelSpan,
|
|
504
|
+
attributes
|
|
505
|
+
});
|
|
506
|
+
case "span":
|
|
507
|
+
default:
|
|
508
|
+
return new ObservationSpan({
|
|
509
|
+
otelSpan,
|
|
510
|
+
attributes
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function updateActiveTrace(attributes) {
|
|
515
|
+
const span = trace2.getActiveSpan();
|
|
516
|
+
if (!span) {
|
|
517
|
+
console.warn(
|
|
518
|
+
"[Observability] No active OTEL span in context. Skipping trace update."
|
|
519
|
+
);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
span.setAttributes(createTraceAttributes(attributes));
|
|
523
|
+
}
|
|
524
|
+
function getActiveTraceId() {
|
|
525
|
+
return trace2.getActiveSpan()?.spanContext().traceId;
|
|
526
|
+
}
|
|
527
|
+
function getActiveSpanId() {
|
|
528
|
+
return trace2.getActiveSpan()?.spanContext().spanId;
|
|
529
|
+
}
|
|
530
|
+
function wrapPromise(promise, span, endOnExit) {
|
|
531
|
+
return promise.then(
|
|
532
|
+
(value) => {
|
|
533
|
+
if (endOnExit !== false) {
|
|
534
|
+
span.end();
|
|
535
|
+
}
|
|
536
|
+
return value;
|
|
537
|
+
},
|
|
538
|
+
(err) => {
|
|
539
|
+
span.setStatus({
|
|
540
|
+
code: SpanStatusCode.ERROR,
|
|
541
|
+
message: err instanceof Error ? err.message : "Unknown error"
|
|
542
|
+
});
|
|
543
|
+
if (endOnExit !== false) {
|
|
544
|
+
span.end();
|
|
545
|
+
}
|
|
546
|
+
throw err;
|
|
547
|
+
}
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
function startActiveObservation(name, fn, options) {
|
|
551
|
+
const { asType = "span", endOnExit, ...observationOptions } = options || {};
|
|
552
|
+
return getTracer().startActiveSpan(
|
|
553
|
+
name,
|
|
554
|
+
{ startTime: observationOptions?.startTime },
|
|
555
|
+
createParentContext(observationOptions?.parentSpanContext) ?? context2.active(),
|
|
556
|
+
(span) => {
|
|
557
|
+
try {
|
|
558
|
+
let observation;
|
|
559
|
+
switch (asType) {
|
|
560
|
+
case "llm":
|
|
561
|
+
observation = new ObservationLLM({ otelSpan: span });
|
|
562
|
+
break;
|
|
563
|
+
case "embedding":
|
|
564
|
+
observation = new ObservationEmbedding({ otelSpan: span });
|
|
565
|
+
break;
|
|
566
|
+
case "agent":
|
|
567
|
+
observation = new ObservationAgent({ otelSpan: span });
|
|
568
|
+
break;
|
|
569
|
+
case "tool":
|
|
570
|
+
observation = new ObservationTool({ otelSpan: span });
|
|
571
|
+
break;
|
|
572
|
+
case "chain":
|
|
573
|
+
observation = new ObservationChain({ otelSpan: span });
|
|
574
|
+
break;
|
|
575
|
+
case "retriever":
|
|
576
|
+
observation = new ObservationRetriever({ otelSpan: span });
|
|
577
|
+
break;
|
|
578
|
+
case "reranker":
|
|
579
|
+
observation = new ObservationReranker({ otelSpan: span });
|
|
580
|
+
break;
|
|
581
|
+
case "evaluator":
|
|
582
|
+
observation = new ObservationEvaluator({ otelSpan: span });
|
|
583
|
+
break;
|
|
584
|
+
case "guardrail":
|
|
585
|
+
observation = new ObservationGuardrail({ otelSpan: span });
|
|
586
|
+
break;
|
|
587
|
+
case "span":
|
|
588
|
+
default:
|
|
589
|
+
observation = new ObservationSpan({ otelSpan: span });
|
|
590
|
+
}
|
|
591
|
+
const result = fn(observation);
|
|
592
|
+
if (result instanceof Promise) {
|
|
593
|
+
return wrapPromise(
|
|
594
|
+
result,
|
|
595
|
+
span,
|
|
596
|
+
endOnExit
|
|
597
|
+
);
|
|
598
|
+
} else {
|
|
599
|
+
if (endOnExit !== false) {
|
|
600
|
+
span.end();
|
|
601
|
+
}
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
} catch (err) {
|
|
605
|
+
span.setStatus({
|
|
606
|
+
code: SpanStatusCode.ERROR,
|
|
607
|
+
message: err instanceof Error ? err.message : "Unknown error"
|
|
608
|
+
});
|
|
609
|
+
if (endOnExit !== false) {
|
|
610
|
+
span.end();
|
|
611
|
+
}
|
|
612
|
+
throw err;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
function updateActiveObservation(attributes) {
|
|
618
|
+
const span = trace2.getActiveSpan();
|
|
619
|
+
if (!span) {
|
|
620
|
+
console.warn(
|
|
621
|
+
"[Observability] No active OTEL span in context. Skipping observation update."
|
|
622
|
+
);
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
span.setAttributes(createObservationAttributes("span", attributes));
|
|
626
|
+
}
|
|
627
|
+
function _captureArguments(args) {
|
|
628
|
+
if (args.length === 0) return {};
|
|
629
|
+
if (args.length === 1) return { arg: args[0] };
|
|
630
|
+
return { args };
|
|
631
|
+
}
|
|
632
|
+
function observe(fn, options = {}) {
|
|
633
|
+
const {
|
|
634
|
+
asType = "span",
|
|
635
|
+
captureInput = true,
|
|
636
|
+
captureOutput = true,
|
|
637
|
+
...observationOptions
|
|
638
|
+
} = options;
|
|
639
|
+
const wrappedFunction = function(...args) {
|
|
640
|
+
const name = fn.name || "anonymous-function";
|
|
641
|
+
const inputData = captureInput ? _captureArguments(args) : void 0;
|
|
642
|
+
const observation = startObservation(
|
|
643
|
+
name,
|
|
644
|
+
inputData ? { input: inputData } : {},
|
|
645
|
+
{
|
|
646
|
+
...observationOptions,
|
|
647
|
+
asType
|
|
648
|
+
}
|
|
649
|
+
);
|
|
650
|
+
const activeContext = trace2.setSpan(context2.active(), observation.otelSpan);
|
|
651
|
+
const result = context2.with(activeContext, () => fn.apply(this, args));
|
|
652
|
+
if (result instanceof Promise) {
|
|
653
|
+
return result.then(
|
|
654
|
+
(value) => {
|
|
655
|
+
if (captureOutput) {
|
|
656
|
+
observation.update({ output: value });
|
|
657
|
+
}
|
|
658
|
+
observation.end();
|
|
659
|
+
return value;
|
|
660
|
+
},
|
|
661
|
+
(err) => {
|
|
662
|
+
observation.update({
|
|
663
|
+
level: "ERROR",
|
|
664
|
+
statusMessage: err instanceof Error ? err.message : "Unknown error"
|
|
665
|
+
});
|
|
666
|
+
observation.end();
|
|
667
|
+
throw err;
|
|
668
|
+
}
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
if (captureOutput) {
|
|
672
|
+
observation.update({ output: result });
|
|
673
|
+
}
|
|
674
|
+
observation.end();
|
|
675
|
+
return result;
|
|
676
|
+
};
|
|
677
|
+
Object.defineProperty(wrappedFunction, "name", { value: fn.name });
|
|
678
|
+
Object.defineProperty(wrappedFunction, "length", { value: fn.length });
|
|
679
|
+
return wrappedFunction;
|
|
680
|
+
}
|
|
681
|
+
var init_src = __esm({
|
|
682
|
+
"src/index.ts"() {
|
|
683
|
+
init_attributes();
|
|
684
|
+
init_spanWrapper();
|
|
685
|
+
init_tracerProvider();
|
|
686
|
+
init_attributes();
|
|
687
|
+
init_tracerProvider();
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
export {
|
|
692
|
+
createTraceAttributes,
|
|
693
|
+
createObservationAttributes,
|
|
694
|
+
setTracerProvider,
|
|
695
|
+
getTracerProvider,
|
|
696
|
+
getTracer,
|
|
697
|
+
ObservationSpan,
|
|
698
|
+
ObservationLLM,
|
|
699
|
+
ObservationEmbedding,
|
|
700
|
+
ObservationAgent,
|
|
701
|
+
ObservationTool,
|
|
702
|
+
ObservationChain,
|
|
703
|
+
ObservationRetriever,
|
|
704
|
+
ObservationReranker,
|
|
705
|
+
ObservationEvaluator,
|
|
706
|
+
ObservationGuardrail,
|
|
707
|
+
startObservation,
|
|
708
|
+
updateActiveTrace,
|
|
709
|
+
getActiveTraceId,
|
|
710
|
+
getActiveSpanId,
|
|
711
|
+
startActiveObservation,
|
|
712
|
+
updateActiveObservation,
|
|
713
|
+
observe,
|
|
714
|
+
init_src
|
|
715
|
+
};
|
|
716
|
+
//# sourceMappingURL=chunk-ZGEMAYS4.mjs.map
|