@elasticdash/tracing 0.0.1 → 0.0.3
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/LICENSE +24 -0
- package/README.md +2 -2
- package/dist/index.cjs +800 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2090 -0
- package/dist/index.d.ts +2090 -0
- package/dist/index.mjs +761 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +11 -11
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { getGlobalLogger as getGlobalLogger2, LangfuseOtelSpanAttributes as LangfuseOtelSpanAttributes2 } from "@elasticdash/core";
|
|
3
|
+
import {
|
|
4
|
+
trace as trace2,
|
|
5
|
+
context,
|
|
6
|
+
SpanStatusCode
|
|
7
|
+
} from "@opentelemetry/api";
|
|
8
|
+
|
|
9
|
+
// src/attributes.ts
|
|
10
|
+
import { LangfuseOtelSpanAttributes } from "@elasticdash/core";
|
|
11
|
+
function createTraceAttributes({
|
|
12
|
+
name,
|
|
13
|
+
userId,
|
|
14
|
+
sessionId,
|
|
15
|
+
version,
|
|
16
|
+
release,
|
|
17
|
+
input,
|
|
18
|
+
output,
|
|
19
|
+
metadata,
|
|
20
|
+
tags,
|
|
21
|
+
environment,
|
|
22
|
+
public: isPublic
|
|
23
|
+
} = {}) {
|
|
24
|
+
const attributes = {
|
|
25
|
+
[LangfuseOtelSpanAttributes.TRACE_NAME]: name,
|
|
26
|
+
[LangfuseOtelSpanAttributes.TRACE_USER_ID]: userId,
|
|
27
|
+
[LangfuseOtelSpanAttributes.TRACE_SESSION_ID]: sessionId,
|
|
28
|
+
[LangfuseOtelSpanAttributes.VERSION]: version,
|
|
29
|
+
[LangfuseOtelSpanAttributes.RELEASE]: release,
|
|
30
|
+
[LangfuseOtelSpanAttributes.TRACE_INPUT]: _serialize(input),
|
|
31
|
+
[LangfuseOtelSpanAttributes.TRACE_OUTPUT]: _serialize(output),
|
|
32
|
+
[LangfuseOtelSpanAttributes.TRACE_TAGS]: tags,
|
|
33
|
+
[LangfuseOtelSpanAttributes.ENVIRONMENT]: environment,
|
|
34
|
+
[LangfuseOtelSpanAttributes.TRACE_PUBLIC]: isPublic,
|
|
35
|
+
..._flattenAndSerializeMetadata(metadata, "trace")
|
|
36
|
+
};
|
|
37
|
+
return Object.fromEntries(
|
|
38
|
+
Object.entries(attributes).filter(([_, v]) => v != null)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function createObservationAttributes(type, attributes) {
|
|
42
|
+
const {
|
|
43
|
+
metadata,
|
|
44
|
+
input,
|
|
45
|
+
output,
|
|
46
|
+
level,
|
|
47
|
+
statusMessage,
|
|
48
|
+
version,
|
|
49
|
+
completionStartTime,
|
|
50
|
+
model,
|
|
51
|
+
modelParameters,
|
|
52
|
+
usageDetails,
|
|
53
|
+
costDetails,
|
|
54
|
+
prompt
|
|
55
|
+
} = attributes;
|
|
56
|
+
let otelAttributes = {
|
|
57
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: type,
|
|
58
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
|
|
59
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
|
|
60
|
+
[LangfuseOtelSpanAttributes.VERSION]: version,
|
|
61
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
|
|
62
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output),
|
|
63
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_MODEL]: model,
|
|
64
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_USAGE_DETAILS]: _serialize(usageDetails),
|
|
65
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_COST_DETAILS]: _serialize(costDetails),
|
|
66
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_COMPLETION_START_TIME]: _serialize(completionStartTime),
|
|
67
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS]: _serialize(modelParameters),
|
|
68
|
+
...prompt && !prompt.isFallback ? {
|
|
69
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_NAME]: prompt.name,
|
|
70
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_PROMPT_VERSION]: prompt.version
|
|
71
|
+
} : {},
|
|
72
|
+
..._flattenAndSerializeMetadata(metadata, "observation")
|
|
73
|
+
};
|
|
74
|
+
return Object.fromEntries(
|
|
75
|
+
Object.entries(otelAttributes).filter(([_, v]) => v != null)
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
function _serialize(obj) {
|
|
79
|
+
try {
|
|
80
|
+
if (typeof obj === "string") return obj;
|
|
81
|
+
return obj != null ? JSON.stringify(obj) : void 0;
|
|
82
|
+
} catch {
|
|
83
|
+
return "<failed to serialize>";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function _flattenAndSerializeMetadata(metadata, type) {
|
|
87
|
+
const prefix = type === "observation" ? LangfuseOtelSpanAttributes.OBSERVATION_METADATA : LangfuseOtelSpanAttributes.TRACE_METADATA;
|
|
88
|
+
const metadataAttributes = {};
|
|
89
|
+
if (metadata === void 0 || metadata === null) {
|
|
90
|
+
return metadataAttributes;
|
|
91
|
+
}
|
|
92
|
+
if (typeof metadata !== "object" || Array.isArray(metadata)) {
|
|
93
|
+
const serialized = _serialize(metadata);
|
|
94
|
+
if (serialized) {
|
|
95
|
+
metadataAttributes[prefix] = serialized;
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
99
|
+
const serialized = typeof value === "string" ? value : _serialize(value);
|
|
100
|
+
if (serialized) {
|
|
101
|
+
metadataAttributes[`${prefix}.${key}`] = serialized;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return metadataAttributes;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/tracerProvider.ts
|
|
109
|
+
import {
|
|
110
|
+
getGlobalLogger,
|
|
111
|
+
LANGFUSE_SDK_VERSION,
|
|
112
|
+
LANGFUSE_TRACER_NAME
|
|
113
|
+
} from "@elasticdash/core";
|
|
114
|
+
import { trace } from "@opentelemetry/api";
|
|
115
|
+
var LANGFUSE_GLOBAL_SYMBOL = Symbol.for("langfuse");
|
|
116
|
+
function createState() {
|
|
117
|
+
return {
|
|
118
|
+
isolatedTracerProvider: null
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function getGlobalState() {
|
|
122
|
+
const initialState = createState();
|
|
123
|
+
try {
|
|
124
|
+
const g = globalThis;
|
|
125
|
+
if (typeof g !== "object" || g === null) {
|
|
126
|
+
getGlobalLogger().warn(
|
|
127
|
+
"globalThis is not available, using fallback state"
|
|
128
|
+
);
|
|
129
|
+
return initialState;
|
|
130
|
+
}
|
|
131
|
+
if (!g[LANGFUSE_GLOBAL_SYMBOL]) {
|
|
132
|
+
Object.defineProperty(g, LANGFUSE_GLOBAL_SYMBOL, {
|
|
133
|
+
value: initialState,
|
|
134
|
+
writable: false,
|
|
135
|
+
// lock the slot (not the contents)
|
|
136
|
+
configurable: false,
|
|
137
|
+
enumerable: false
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return g[LANGFUSE_GLOBAL_SYMBOL];
|
|
141
|
+
} catch (err) {
|
|
142
|
+
if (err instanceof Error) {
|
|
143
|
+
getGlobalLogger().error(`Failed to access global state: ${err.message}`);
|
|
144
|
+
} else {
|
|
145
|
+
getGlobalLogger().error(`Failed to access global state: ${String(err)}`);
|
|
146
|
+
}
|
|
147
|
+
return initialState;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function setLangfuseTracerProvider(provider) {
|
|
151
|
+
getGlobalState().isolatedTracerProvider = provider;
|
|
152
|
+
}
|
|
153
|
+
function getLangfuseTracerProvider() {
|
|
154
|
+
const { isolatedTracerProvider } = getGlobalState();
|
|
155
|
+
if (isolatedTracerProvider) return isolatedTracerProvider;
|
|
156
|
+
return trace.getTracerProvider();
|
|
157
|
+
}
|
|
158
|
+
function getLangfuseTracer() {
|
|
159
|
+
return getLangfuseTracerProvider().getTracer(
|
|
160
|
+
LANGFUSE_TRACER_NAME,
|
|
161
|
+
LANGFUSE_SDK_VERSION
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/spanWrapper.ts
|
|
166
|
+
var LangfuseBaseObservation = class {
|
|
167
|
+
constructor(params) {
|
|
168
|
+
this.otelSpan = params.otelSpan;
|
|
169
|
+
this.id = params.otelSpan.spanContext().spanId;
|
|
170
|
+
this.traceId = params.otelSpan.spanContext().traceId;
|
|
171
|
+
this.type = params.type;
|
|
172
|
+
if (params.attributes) {
|
|
173
|
+
this.otelSpan.setAttributes(
|
|
174
|
+
createObservationAttributes(params.type, params.attributes)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Gets the Langfuse OpenTelemetry tracer instance */
|
|
179
|
+
get tracer() {
|
|
180
|
+
return getLangfuseTracer();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Ends the observation, marking it as complete.
|
|
184
|
+
*
|
|
185
|
+
* @param endTime - Optional end time, defaults to current time
|
|
186
|
+
*/
|
|
187
|
+
end(endTime) {
|
|
188
|
+
this.otelSpan.end(endTime);
|
|
189
|
+
}
|
|
190
|
+
updateOtelSpanAttributes(attributes) {
|
|
191
|
+
this.otelSpan.setAttributes(
|
|
192
|
+
createObservationAttributes(this.type, attributes)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Updates the parent trace with new attributes.
|
|
197
|
+
*/
|
|
198
|
+
updateTrace(attributes) {
|
|
199
|
+
this.otelSpan.setAttributes(createTraceAttributes(attributes));
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
startObservation(name, attributes, options) {
|
|
203
|
+
const { asType = "span" } = options || {};
|
|
204
|
+
return startObservation(name, attributes, {
|
|
205
|
+
asType,
|
|
206
|
+
// typecast necessary as ts cannot narrow the type correctly
|
|
207
|
+
parentSpanContext: this.otelSpan.spanContext()
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var LangfuseSpan = class extends LangfuseBaseObservation {
|
|
212
|
+
constructor(params) {
|
|
213
|
+
super({ ...params, type: "span" });
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Updates this span with new attributes.
|
|
217
|
+
*
|
|
218
|
+
* @param attributes - Span attributes to set
|
|
219
|
+
* @returns This span for method chaining
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```typescript
|
|
223
|
+
* span.update({
|
|
224
|
+
* output: { result: 'success' },
|
|
225
|
+
* level: 'DEFAULT',
|
|
226
|
+
* metadata: { duration: 150 }
|
|
227
|
+
* });
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
update(attributes) {
|
|
231
|
+
super.updateOtelSpanAttributes(attributes);
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var LangfuseAgent = class extends LangfuseBaseObservation {
|
|
236
|
+
constructor(params) {
|
|
237
|
+
super({ ...params, type: "agent" });
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Updates this agent observation with new attributes.
|
|
241
|
+
*
|
|
242
|
+
* @param attributes - Agent attributes to set
|
|
243
|
+
* @returns This agent for method chaining
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* agent.update({
|
|
248
|
+
* output: {
|
|
249
|
+
* taskCompleted: true,
|
|
250
|
+
* iterationsUsed: 5,
|
|
251
|
+
* toolsInvoked: ['web-search', 'calculator', 'summarizer'],
|
|
252
|
+
* finalResult: 'Research completed with high confidence'
|
|
253
|
+
* },
|
|
254
|
+
* metadata: {
|
|
255
|
+
* efficiency: 0.85,
|
|
256
|
+
* qualityScore: 0.92,
|
|
257
|
+
* resourcesConsumed: { tokens: 15000, apiCalls: 12 }
|
|
258
|
+
* }
|
|
259
|
+
* });
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
update(attributes) {
|
|
263
|
+
super.updateOtelSpanAttributes(attributes);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
var LangfuseTool = class extends LangfuseBaseObservation {
|
|
268
|
+
constructor(params) {
|
|
269
|
+
super({ ...params, type: "tool" });
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Updates this tool observation with new attributes.
|
|
273
|
+
*
|
|
274
|
+
* @param attributes - Tool attributes to set
|
|
275
|
+
* @returns This tool for method chaining
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```typescript
|
|
279
|
+
* tool.update({
|
|
280
|
+
* output: {
|
|
281
|
+
* result: searchResults,
|
|
282
|
+
* count: searchResults.length,
|
|
283
|
+
* relevanceScore: 0.89,
|
|
284
|
+
* executionTime: 1250
|
|
285
|
+
* },
|
|
286
|
+
* metadata: {
|
|
287
|
+
* cacheHit: false,
|
|
288
|
+
* apiCost: 0.025,
|
|
289
|
+
* rateLimitRemaining: 950
|
|
290
|
+
* }
|
|
291
|
+
* });
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
update(attributes) {
|
|
295
|
+
super.updateOtelSpanAttributes(attributes);
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
var LangfuseChain = class extends LangfuseBaseObservation {
|
|
300
|
+
constructor(params) {
|
|
301
|
+
super({ ...params, type: "chain" });
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Updates this chain observation with new attributes.
|
|
305
|
+
*
|
|
306
|
+
* @param attributes - Chain attributes to set
|
|
307
|
+
* @returns This chain for method chaining
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* chain.update({
|
|
312
|
+
* output: {
|
|
313
|
+
* stepsCompleted: 5,
|
|
314
|
+
* stepsSuccessful: 4,
|
|
315
|
+
* finalResult: processedData,
|
|
316
|
+
* pipelineEfficiency: 0.87
|
|
317
|
+
* },
|
|
318
|
+
* metadata: {
|
|
319
|
+
* bottleneckStep: 'data-validation',
|
|
320
|
+
* parallelizationOpportunities: ['step-2', 'step-3'],
|
|
321
|
+
* optimizationSuggestions: ['cache-intermediate-results']
|
|
322
|
+
* }
|
|
323
|
+
* });
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
update(attributes) {
|
|
327
|
+
super.updateOtelSpanAttributes(attributes);
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
var LangfuseRetriever = class extends LangfuseBaseObservation {
|
|
332
|
+
constructor(params) {
|
|
333
|
+
super({ ...params, type: "retriever" });
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Updates this retriever observation with new attributes.
|
|
337
|
+
*
|
|
338
|
+
* @param attributes - Retriever attributes to set
|
|
339
|
+
* @returns This retriever for method chaining
|
|
340
|
+
*/
|
|
341
|
+
update(attributes) {
|
|
342
|
+
super.updateOtelSpanAttributes(attributes);
|
|
343
|
+
return this;
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
var LangfuseEvaluator = class extends LangfuseBaseObservation {
|
|
347
|
+
constructor(params) {
|
|
348
|
+
super({ ...params, type: "evaluator" });
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Updates this evaluator observation with new attributes.
|
|
352
|
+
*
|
|
353
|
+
* @param attributes - Evaluator attributes to set
|
|
354
|
+
* @returns This evaluator for method chaining
|
|
355
|
+
*/
|
|
356
|
+
update(attributes) {
|
|
357
|
+
super.updateOtelSpanAttributes(attributes);
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
var LangfuseGuardrail = class extends LangfuseBaseObservation {
|
|
362
|
+
constructor(params) {
|
|
363
|
+
super({ ...params, type: "guardrail" });
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Updates this guardrail observation with new attributes.
|
|
367
|
+
*
|
|
368
|
+
* @param attributes - Guardrail attributes to set
|
|
369
|
+
* @returns This guardrail for method chaining
|
|
370
|
+
*/
|
|
371
|
+
update(attributes) {
|
|
372
|
+
super.updateOtelSpanAttributes(attributes);
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
var LangfuseGeneration = class extends LangfuseBaseObservation {
|
|
377
|
+
constructor(params) {
|
|
378
|
+
super({ ...params, type: "generation" });
|
|
379
|
+
}
|
|
380
|
+
update(attributes) {
|
|
381
|
+
this.updateOtelSpanAttributes(attributes);
|
|
382
|
+
return this;
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
var LangfuseEmbedding = class extends LangfuseBaseObservation {
|
|
386
|
+
constructor(params) {
|
|
387
|
+
super({ ...params, type: "embedding" });
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Updates this embedding observation with new attributes.
|
|
391
|
+
*
|
|
392
|
+
* @param attributes - Embedding attributes to set
|
|
393
|
+
* @returns This embedding for method chaining
|
|
394
|
+
*/
|
|
395
|
+
update(attributes) {
|
|
396
|
+
this.updateOtelSpanAttributes(attributes);
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
var LangfuseEvent = class extends LangfuseBaseObservation {
|
|
401
|
+
constructor(params) {
|
|
402
|
+
super({ ...params, type: "event" });
|
|
403
|
+
this.otelSpan.end(params.timestamp);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
// src/index.ts
|
|
408
|
+
import {
|
|
409
|
+
propagateAttributes
|
|
410
|
+
} from "@elasticdash/core";
|
|
411
|
+
import { LangfuseOtelSpanAttributes as LangfuseOtelSpanAttributes3 } from "@elasticdash/core";
|
|
412
|
+
function createOtelSpan(params) {
|
|
413
|
+
return getLangfuseTracer().startSpan(
|
|
414
|
+
params.name,
|
|
415
|
+
{ startTime: params.startTime },
|
|
416
|
+
createParentContext(params.parentSpanContext)
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
function createParentContext(parentSpanContext) {
|
|
420
|
+
if (!parentSpanContext) return;
|
|
421
|
+
return trace2.setSpanContext(context.active(), parentSpanContext);
|
|
422
|
+
}
|
|
423
|
+
function wrapPromise(promise, span, endOnExit) {
|
|
424
|
+
return promise.then(
|
|
425
|
+
(value) => {
|
|
426
|
+
if (endOnExit !== false) {
|
|
427
|
+
span.end();
|
|
428
|
+
}
|
|
429
|
+
return value;
|
|
430
|
+
},
|
|
431
|
+
(err) => {
|
|
432
|
+
span.setStatus({
|
|
433
|
+
code: SpanStatusCode.ERROR,
|
|
434
|
+
message: err instanceof Error ? err.message : "Unknown error"
|
|
435
|
+
});
|
|
436
|
+
if (endOnExit !== false) {
|
|
437
|
+
span.end();
|
|
438
|
+
}
|
|
439
|
+
throw err;
|
|
440
|
+
}
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
function startObservation(name, attributes, options) {
|
|
444
|
+
var _a;
|
|
445
|
+
const { asType = "span", ...observationOptions } = options || {};
|
|
446
|
+
const otelSpan = createOtelSpan({
|
|
447
|
+
name,
|
|
448
|
+
...observationOptions
|
|
449
|
+
});
|
|
450
|
+
switch (asType) {
|
|
451
|
+
case "generation":
|
|
452
|
+
return new LangfuseGeneration({
|
|
453
|
+
otelSpan,
|
|
454
|
+
attributes
|
|
455
|
+
});
|
|
456
|
+
case "embedding":
|
|
457
|
+
return new LangfuseEmbedding({
|
|
458
|
+
otelSpan,
|
|
459
|
+
attributes
|
|
460
|
+
});
|
|
461
|
+
case "agent":
|
|
462
|
+
return new LangfuseAgent({
|
|
463
|
+
otelSpan,
|
|
464
|
+
attributes
|
|
465
|
+
});
|
|
466
|
+
case "tool":
|
|
467
|
+
return new LangfuseTool({
|
|
468
|
+
otelSpan,
|
|
469
|
+
attributes
|
|
470
|
+
});
|
|
471
|
+
case "chain":
|
|
472
|
+
return new LangfuseChain({
|
|
473
|
+
otelSpan,
|
|
474
|
+
attributes
|
|
475
|
+
});
|
|
476
|
+
case "retriever":
|
|
477
|
+
return new LangfuseRetriever({
|
|
478
|
+
otelSpan,
|
|
479
|
+
attributes
|
|
480
|
+
});
|
|
481
|
+
case "evaluator":
|
|
482
|
+
return new LangfuseEvaluator({
|
|
483
|
+
otelSpan,
|
|
484
|
+
attributes
|
|
485
|
+
});
|
|
486
|
+
case "guardrail":
|
|
487
|
+
return new LangfuseGuardrail({
|
|
488
|
+
otelSpan,
|
|
489
|
+
attributes
|
|
490
|
+
});
|
|
491
|
+
case "event": {
|
|
492
|
+
const timestamp = (_a = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a : /* @__PURE__ */ new Date();
|
|
493
|
+
return new LangfuseEvent({
|
|
494
|
+
otelSpan,
|
|
495
|
+
attributes,
|
|
496
|
+
timestamp
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
case "span":
|
|
500
|
+
default:
|
|
501
|
+
return new LangfuseSpan({
|
|
502
|
+
otelSpan,
|
|
503
|
+
attributes
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
function startActiveObservation(name, fn, options) {
|
|
508
|
+
var _a;
|
|
509
|
+
const { asType = "span", ...observationOptions } = options || {};
|
|
510
|
+
return getLangfuseTracer().startActiveSpan(
|
|
511
|
+
name,
|
|
512
|
+
{ startTime: observationOptions == null ? void 0 : observationOptions.startTime },
|
|
513
|
+
(_a = createParentContext(observationOptions == null ? void 0 : observationOptions.parentSpanContext)) != null ? _a : context.active(),
|
|
514
|
+
(span) => {
|
|
515
|
+
var _a2;
|
|
516
|
+
try {
|
|
517
|
+
let observation;
|
|
518
|
+
switch (asType) {
|
|
519
|
+
case "generation":
|
|
520
|
+
observation = new LangfuseGeneration({
|
|
521
|
+
otelSpan: span
|
|
522
|
+
});
|
|
523
|
+
break;
|
|
524
|
+
case "embedding":
|
|
525
|
+
observation = new LangfuseEmbedding({
|
|
526
|
+
otelSpan: span
|
|
527
|
+
});
|
|
528
|
+
break;
|
|
529
|
+
case "agent":
|
|
530
|
+
observation = new LangfuseAgent({
|
|
531
|
+
otelSpan: span
|
|
532
|
+
});
|
|
533
|
+
break;
|
|
534
|
+
case "tool":
|
|
535
|
+
observation = new LangfuseTool({
|
|
536
|
+
otelSpan: span
|
|
537
|
+
});
|
|
538
|
+
break;
|
|
539
|
+
case "chain":
|
|
540
|
+
observation = new LangfuseChain({
|
|
541
|
+
otelSpan: span
|
|
542
|
+
});
|
|
543
|
+
break;
|
|
544
|
+
case "retriever":
|
|
545
|
+
observation = new LangfuseRetriever({
|
|
546
|
+
otelSpan: span
|
|
547
|
+
});
|
|
548
|
+
break;
|
|
549
|
+
case "evaluator":
|
|
550
|
+
observation = new LangfuseEvaluator({
|
|
551
|
+
otelSpan: span
|
|
552
|
+
});
|
|
553
|
+
break;
|
|
554
|
+
case "guardrail":
|
|
555
|
+
observation = new LangfuseGuardrail({
|
|
556
|
+
otelSpan: span
|
|
557
|
+
});
|
|
558
|
+
break;
|
|
559
|
+
case "event": {
|
|
560
|
+
const timestamp = (_a2 = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a2 : /* @__PURE__ */ new Date();
|
|
561
|
+
observation = new LangfuseEvent({
|
|
562
|
+
otelSpan: span,
|
|
563
|
+
timestamp
|
|
564
|
+
});
|
|
565
|
+
break;
|
|
566
|
+
}
|
|
567
|
+
case "span":
|
|
568
|
+
default:
|
|
569
|
+
observation = new LangfuseSpan({
|
|
570
|
+
otelSpan: span
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
const result = fn(observation);
|
|
574
|
+
if (result instanceof Promise) {
|
|
575
|
+
return wrapPromise(
|
|
576
|
+
result,
|
|
577
|
+
span,
|
|
578
|
+
observationOptions == null ? void 0 : observationOptions.endOnExit
|
|
579
|
+
);
|
|
580
|
+
} else {
|
|
581
|
+
if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
|
|
582
|
+
span.end();
|
|
583
|
+
}
|
|
584
|
+
return result;
|
|
585
|
+
}
|
|
586
|
+
} catch (err) {
|
|
587
|
+
span.setStatus({
|
|
588
|
+
code: SpanStatusCode.ERROR,
|
|
589
|
+
message: err instanceof Error ? err.message : "Unknown error"
|
|
590
|
+
});
|
|
591
|
+
if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
|
|
592
|
+
span.end();
|
|
593
|
+
}
|
|
594
|
+
throw err;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
function updateActiveTrace(attributes) {
|
|
600
|
+
const span = trace2.getActiveSpan();
|
|
601
|
+
if (!span) {
|
|
602
|
+
getGlobalLogger2().warn(
|
|
603
|
+
"No active OTEL span in context. Skipping trace update."
|
|
604
|
+
);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
span.setAttributes(createTraceAttributes(attributes));
|
|
608
|
+
}
|
|
609
|
+
function updateActiveObservation(attributes, options) {
|
|
610
|
+
var _a;
|
|
611
|
+
const span = trace2.getActiveSpan();
|
|
612
|
+
if (!span) {
|
|
613
|
+
getGlobalLogger2().warn(
|
|
614
|
+
"No active OTEL span in context. Skipping span update."
|
|
615
|
+
);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
const otelAttributes = createObservationAttributes(
|
|
619
|
+
(_a = options == null ? void 0 : options.asType) != null ? _a : "span",
|
|
620
|
+
attributes
|
|
621
|
+
);
|
|
622
|
+
if (!(options == null ? void 0 : options.asType)) {
|
|
623
|
+
otelAttributes[LangfuseOtelSpanAttributes2.OBSERVATION_TYPE] = void 0;
|
|
624
|
+
}
|
|
625
|
+
span.setAttributes(otelAttributes);
|
|
626
|
+
}
|
|
627
|
+
function observe(fn, options = {}) {
|
|
628
|
+
const {
|
|
629
|
+
name = fn.name || "anonymous-function",
|
|
630
|
+
asType = "span",
|
|
631
|
+
captureInput = true,
|
|
632
|
+
captureOutput = true,
|
|
633
|
+
parentSpanContext = void 0
|
|
634
|
+
} = options;
|
|
635
|
+
const wrappedFunction = function(...args) {
|
|
636
|
+
const inputData = captureInput ? _captureArguments(args) : void 0;
|
|
637
|
+
const observation = startObservation(
|
|
638
|
+
name,
|
|
639
|
+
inputData ? { input: inputData } : {},
|
|
640
|
+
{
|
|
641
|
+
asType,
|
|
642
|
+
// typecast necessary as ts cannot narrow down type
|
|
643
|
+
parentSpanContext
|
|
644
|
+
}
|
|
645
|
+
);
|
|
646
|
+
const activeContext = trace2.setSpan(context.active(), observation.otelSpan);
|
|
647
|
+
try {
|
|
648
|
+
const result = context.with(activeContext, () => fn.apply(this, args));
|
|
649
|
+
if (result instanceof Promise) {
|
|
650
|
+
return result.then(
|
|
651
|
+
(value) => {
|
|
652
|
+
if (captureOutput) {
|
|
653
|
+
observation.update({ output: _captureOutput(value) });
|
|
654
|
+
}
|
|
655
|
+
if ((options == null ? void 0 : options.endOnExit) !== false) {
|
|
656
|
+
observation.end();
|
|
657
|
+
}
|
|
658
|
+
return value;
|
|
659
|
+
},
|
|
660
|
+
(error) => {
|
|
661
|
+
observation.update({
|
|
662
|
+
level: "ERROR",
|
|
663
|
+
statusMessage: (error instanceof Error ? error.message : String(error)) || "Function threw an error",
|
|
664
|
+
output: captureOutput ? { error: String(error) } : void 0
|
|
665
|
+
});
|
|
666
|
+
if ((options == null ? void 0 : options.endOnExit) !== false) {
|
|
667
|
+
observation.end();
|
|
668
|
+
}
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
671
|
+
);
|
|
672
|
+
} else {
|
|
673
|
+
if (captureOutput) {
|
|
674
|
+
observation.update({ output: _captureOutput(result) });
|
|
675
|
+
}
|
|
676
|
+
if ((options == null ? void 0 : options.endOnExit) !== false) {
|
|
677
|
+
observation.end();
|
|
678
|
+
}
|
|
679
|
+
return result;
|
|
680
|
+
}
|
|
681
|
+
} catch (error) {
|
|
682
|
+
observation.update({
|
|
683
|
+
level: "ERROR",
|
|
684
|
+
statusMessage: (error instanceof Error ? error.message : String(error)) || "Function threw an error",
|
|
685
|
+
output: captureOutput ? { error: String(error) } : void 0
|
|
686
|
+
});
|
|
687
|
+
if ((options == null ? void 0 : options.endOnExit) !== false) {
|
|
688
|
+
observation.end();
|
|
689
|
+
}
|
|
690
|
+
throw error;
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
return wrappedFunction;
|
|
694
|
+
}
|
|
695
|
+
function _captureArguments(args) {
|
|
696
|
+
try {
|
|
697
|
+
if (args.length === 0) return void 0;
|
|
698
|
+
if (args.length === 1) return args[0];
|
|
699
|
+
return args;
|
|
700
|
+
} catch {
|
|
701
|
+
return "<failed to capture arguments>";
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
function _captureOutput(value) {
|
|
705
|
+
try {
|
|
706
|
+
if (value === void 0 || value === null) return value;
|
|
707
|
+
if (typeof value !== "object") return value;
|
|
708
|
+
return value;
|
|
709
|
+
} catch {
|
|
710
|
+
return "<failed to capture output>";
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
async function createTraceId(seed) {
|
|
714
|
+
if (seed) {
|
|
715
|
+
const data = new TextEncoder().encode(seed);
|
|
716
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
717
|
+
const hashArray = new Uint8Array(hashBuffer);
|
|
718
|
+
return uint8ArrayToHex(hashArray).slice(0, 32);
|
|
719
|
+
}
|
|
720
|
+
const randomValues = crypto.getRandomValues(new Uint8Array(16));
|
|
721
|
+
return uint8ArrayToHex(randomValues);
|
|
722
|
+
}
|
|
723
|
+
function uint8ArrayToHex(array) {
|
|
724
|
+
return Array.from(array).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
725
|
+
}
|
|
726
|
+
function getActiveTraceId() {
|
|
727
|
+
var _a;
|
|
728
|
+
return (_a = trace2.getActiveSpan()) == null ? void 0 : _a.spanContext().traceId;
|
|
729
|
+
}
|
|
730
|
+
function getActiveSpanId() {
|
|
731
|
+
var _a;
|
|
732
|
+
return (_a = trace2.getActiveSpan()) == null ? void 0 : _a.spanContext().spanId;
|
|
733
|
+
}
|
|
734
|
+
export {
|
|
735
|
+
LangfuseAgent,
|
|
736
|
+
LangfuseChain,
|
|
737
|
+
LangfuseEmbedding,
|
|
738
|
+
LangfuseEvaluator,
|
|
739
|
+
LangfuseEvent,
|
|
740
|
+
LangfuseGeneration,
|
|
741
|
+
LangfuseGuardrail,
|
|
742
|
+
LangfuseOtelSpanAttributes3 as LangfuseOtelSpanAttributes,
|
|
743
|
+
LangfuseRetriever,
|
|
744
|
+
LangfuseSpan,
|
|
745
|
+
LangfuseTool,
|
|
746
|
+
createObservationAttributes,
|
|
747
|
+
createTraceAttributes,
|
|
748
|
+
createTraceId,
|
|
749
|
+
getActiveSpanId,
|
|
750
|
+
getActiveTraceId,
|
|
751
|
+
getLangfuseTracer,
|
|
752
|
+
getLangfuseTracerProvider,
|
|
753
|
+
observe,
|
|
754
|
+
propagateAttributes,
|
|
755
|
+
setLangfuseTracerProvider,
|
|
756
|
+
startActiveObservation,
|
|
757
|
+
startObservation,
|
|
758
|
+
updateActiveObservation,
|
|
759
|
+
updateActiveTrace
|
|
760
|
+
};
|
|
761
|
+
//# sourceMappingURL=index.mjs.map
|