@uselemma/tracing 3.0.2 → 4.0.0
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 +189 -45
- package/dist/client.d.ts +237 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +660 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -17
- package/dist/index.js.map +1 -1
- package/dist/vercel-ai.d.ts +117 -0
- package/dist/vercel-ai.d.ts.map +1 -0
- package/dist/vercel-ai.js +230 -0
- package/dist/vercel-ai.js.map +1 -0
- package/examples/callback-tracing.ts +55 -0
- package/examples/record-by-id.ts +47 -0
- package/examples/trace-handles.ts +59 -0
- package/examples/vercel-ai-v6.ts +27 -0
- package/examples/vercel-ai-v7.ts +27 -0
- package/package.json +5 -13
- package/dist/register.d.ts +0 -61
- package/dist/register.d.ts.map +0 -1
- package/dist/register.js +0 -76
- package/dist/register.js.map +0 -1
- package/dist/run-batch-span-processor.d.ts +0 -25
- package/dist/run-batch-span-processor.d.ts.map +0 -1
- package/dist/run-batch-span-processor.js +0 -139
- package/dist/run-batch-span-processor.js.map +0 -1
- package/dist/span-helpers.d.ts +0 -50
- package/dist/span-helpers.d.ts.map +0 -1
- package/dist/span-helpers.js +0 -95
- package/dist/span-helpers.js.map +0 -1
- package/dist/trace-wrapper.d.ts +0 -107
- package/dist/trace-wrapper.d.ts.map +0 -1
- package/dist/trace-wrapper.js +0 -133
- package/dist/trace-wrapper.js.map +0 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { generateText } from "ai";
|
|
2
|
+
import { Lemma, vercelAI } from "@uselemma/tracing";
|
|
3
|
+
|
|
4
|
+
const lemma = new Lemma({
|
|
5
|
+
apiKey: process.env.LEMMA_API_KEY,
|
|
6
|
+
projectId: process.env.LEMMA_PROJECT_ID,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export async function runVercelAIV6(
|
|
10
|
+
model: Parameters<typeof generateText>[0]["model"],
|
|
11
|
+
userMessage: string,
|
|
12
|
+
) {
|
|
13
|
+
const trace = lemma.trace({
|
|
14
|
+
name: "support-agent",
|
|
15
|
+
input: userMessage,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const result = await generateText({
|
|
19
|
+
model,
|
|
20
|
+
prompt: userMessage,
|
|
21
|
+
experimental_telemetry: {
|
|
22
|
+
integrations: [vercelAI({ trace })],
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return result.text;
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { generateText } from "ai";
|
|
2
|
+
import { Lemma, vercelAI } from "@uselemma/tracing";
|
|
3
|
+
|
|
4
|
+
const lemma = new Lemma({
|
|
5
|
+
apiKey: process.env.LEMMA_API_KEY,
|
|
6
|
+
projectId: process.env.LEMMA_PROJECT_ID,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export async function runVercelAIV7(
|
|
10
|
+
model: Parameters<typeof generateText>[0]["model"],
|
|
11
|
+
userMessage: string,
|
|
12
|
+
) {
|
|
13
|
+
const trace = lemma.trace({
|
|
14
|
+
name: "support-agent",
|
|
15
|
+
input: userMessage,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const result = await generateText({
|
|
19
|
+
model,
|
|
20
|
+
prompt: userMessage,
|
|
21
|
+
telemetry: {
|
|
22
|
+
integrations: [vercelAI({ trace })],
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return result.text;
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uselemma/tracing",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "HTTP tracing SDK for Lemma",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Lemma",
|
|
7
7
|
"repository": {
|
|
@@ -13,12 +13,11 @@
|
|
|
13
13
|
"url": "https://github.com/uselemma/sdk/issues"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
16
|
-
"opentelemetry",
|
|
17
16
|
"tracing",
|
|
18
17
|
"observability",
|
|
19
18
|
"monitoring",
|
|
20
19
|
"llm",
|
|
21
|
-
"
|
|
20
|
+
"sdk"
|
|
22
21
|
],
|
|
23
22
|
"main": "./dist/index.js",
|
|
24
23
|
"types": "./dist/index.d.ts",
|
|
@@ -30,19 +29,12 @@
|
|
|
30
29
|
},
|
|
31
30
|
"files": [
|
|
32
31
|
"dist",
|
|
32
|
+
"examples",
|
|
33
33
|
"README.md",
|
|
34
34
|
"LICENSE"
|
|
35
35
|
],
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"@opentelemetry/api": "^1.9.0",
|
|
38
|
-
"@opentelemetry/exporter-trace-otlp-proto": "^0.211.0",
|
|
39
|
-
"@opentelemetry/sdk-trace-base": "^2.2.0",
|
|
40
|
-
"@opentelemetry/sdk-trace-node": "^2.5.0",
|
|
41
|
-
"uuid": "^13.0.0"
|
|
42
|
-
},
|
|
36
|
+
"dependencies": {},
|
|
43
37
|
"devDependencies": {
|
|
44
|
-
"@arizeai/openinference-instrumentation-openai": "^4.0.5",
|
|
45
|
-
"@opentelemetry/instrumentation": "^0.213.0",
|
|
46
38
|
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
|
|
47
39
|
"@types/node": "^22.18.8",
|
|
48
40
|
"@vitest/coverage-v8": "^3.2.4",
|
package/dist/register.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
2
|
-
import { type SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
3
|
-
export type LemmaOTelOptions = {
|
|
4
|
-
/** Lemma API key. Defaults to LEMMA_API_KEY environment variable. */
|
|
5
|
-
apiKey?: string;
|
|
6
|
-
/** Lemma project ID. Defaults to LEMMA_PROJECT_ID environment variable. */
|
|
7
|
-
projectId?: string;
|
|
8
|
-
/** Base URL for the Lemma API. Defaults to https://api.uselemma.ai */
|
|
9
|
-
baseUrl?: string;
|
|
10
|
-
};
|
|
11
|
-
/** @deprecated Use {@link LemmaOTelOptions} */
|
|
12
|
-
export type RegisterOTelOptions = LemmaOTelOptions;
|
|
13
|
-
/** @deprecated Use {@link LemmaOTelOptions} */
|
|
14
|
-
export type CreateLemmaSpanProcessorOptions = LemmaOTelOptions;
|
|
15
|
-
/**
|
|
16
|
-
* Creates a `BatchSpanProcessor` pre-configured to export traces to Lemma.
|
|
17
|
-
*
|
|
18
|
-
* Use this when you need to add Lemma as one of several span processors on an
|
|
19
|
-
* existing `NodeTracerProvider` (e.g. for dual-export to Lemma and another
|
|
20
|
-
* backend simultaneously).
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* // Dual export: Lemma + your own OTLP collector
|
|
24
|
-
* import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
25
|
-
* import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
26
|
-
* import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
27
|
-
* import { createLemmaSpanProcessor } from "@uselemma/tracing";
|
|
28
|
-
*
|
|
29
|
-
* const provider = new NodeTracerProvider({
|
|
30
|
-
* spanProcessors: [
|
|
31
|
-
* createLemmaSpanProcessor(),
|
|
32
|
-
* new BatchSpanProcessor(new OTLPTraceExporter({ url: "http://localhost:4318/v1/traces" })),
|
|
33
|
-
* ],
|
|
34
|
-
* });
|
|
35
|
-
* provider.register();
|
|
36
|
-
*/
|
|
37
|
-
export declare function createLemmaSpanProcessor(options?: LemmaOTelOptions): SpanProcessor;
|
|
38
|
-
/**
|
|
39
|
-
* Registers an OpenTelemetry tracer provider configured to send traces to Lemma.
|
|
40
|
-
*
|
|
41
|
-
* This is a convenience wrapper that sets up `NodeTracerProvider` with a
|
|
42
|
-
* `BatchSpanProcessor` and `OTLPTraceExporter` pointing at the Lemma ingest endpoint.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* // instrumentation.ts (Next.js)
|
|
46
|
-
* export async function register() {
|
|
47
|
-
* if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
48
|
-
* const { registerOTel } = await import('@uselemma/tracing');
|
|
49
|
-
* registerOTel();
|
|
50
|
-
* }
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* // With explicit options
|
|
55
|
-
* registerOTel({
|
|
56
|
-
* apiKey: 'lma_...',
|
|
57
|
-
* projectId: 'proj_...',
|
|
58
|
-
* });
|
|
59
|
-
*/
|
|
60
|
-
export declare function registerOTel(options?: LemmaOTelOptions): NodeTracerProvider;
|
|
61
|
-
//# sourceMappingURL=register.d.ts.map
|
package/dist/register.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAInE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sEAAsE;IACtE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,+CAA+C;AAC/C,MAAM,MAAM,mBAAmB,GAAG,gBAAgB,CAAC;AACnD,+CAA+C;AAC/C,MAAM,MAAM,+BAA+B,GAAG,gBAAgB,CAAC;AAG/D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,gBAAqB,GAC7B,aAAa,CAoBf;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,gBAAqB,sBAU1D"}
|
package/dist/register.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLemmaSpanProcessor = createLemmaSpanProcessor;
|
|
4
|
-
exports.registerOTel = registerOTel;
|
|
5
|
-
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
6
|
-
const exporter_trace_otlp_proto_1 = require("@opentelemetry/exporter-trace-otlp-proto");
|
|
7
|
-
const run_batch_span_processor_1 = require("./run-batch-span-processor");
|
|
8
|
-
/**
|
|
9
|
-
* Creates a `BatchSpanProcessor` pre-configured to export traces to Lemma.
|
|
10
|
-
*
|
|
11
|
-
* Use this when you need to add Lemma as one of several span processors on an
|
|
12
|
-
* existing `NodeTracerProvider` (e.g. for dual-export to Lemma and another
|
|
13
|
-
* backend simultaneously).
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* // Dual export: Lemma + your own OTLP collector
|
|
17
|
-
* import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
|
|
18
|
-
* import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
19
|
-
* import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
|
|
20
|
-
* import { createLemmaSpanProcessor } from "@uselemma/tracing";
|
|
21
|
-
*
|
|
22
|
-
* const provider = new NodeTracerProvider({
|
|
23
|
-
* spanProcessors: [
|
|
24
|
-
* createLemmaSpanProcessor(),
|
|
25
|
-
* new BatchSpanProcessor(new OTLPTraceExporter({ url: "http://localhost:4318/v1/traces" })),
|
|
26
|
-
* ],
|
|
27
|
-
* });
|
|
28
|
-
* provider.register();
|
|
29
|
-
*/
|
|
30
|
-
function createLemmaSpanProcessor(options = {}) {
|
|
31
|
-
const apiKey = options.apiKey ?? process.env.LEMMA_API_KEY;
|
|
32
|
-
const projectId = options.projectId ?? process.env.LEMMA_PROJECT_ID;
|
|
33
|
-
const baseUrl = options.baseUrl ?? "https://api.uselemma.ai";
|
|
34
|
-
if (!apiKey || !projectId) {
|
|
35
|
-
throw new Error("@uselemma/tracing: Missing API key and/or project ID. Set the LEMMA_API_KEY and LEMMA_PROJECT_ID environment variables or pass them to createLemmaSpanProcessor().");
|
|
36
|
-
}
|
|
37
|
-
return new run_batch_span_processor_1.RunBatchSpanProcessor(new exporter_trace_otlp_proto_1.OTLPTraceExporter({
|
|
38
|
-
url: `${baseUrl}/otel/v1/traces`,
|
|
39
|
-
headers: {
|
|
40
|
-
Authorization: `Bearer ${apiKey}`,
|
|
41
|
-
"X-Lemma-Project-ID": projectId,
|
|
42
|
-
},
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Registers an OpenTelemetry tracer provider configured to send traces to Lemma.
|
|
47
|
-
*
|
|
48
|
-
* This is a convenience wrapper that sets up `NodeTracerProvider` with a
|
|
49
|
-
* `BatchSpanProcessor` and `OTLPTraceExporter` pointing at the Lemma ingest endpoint.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* // instrumentation.ts (Next.js)
|
|
53
|
-
* export async function register() {
|
|
54
|
-
* if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
55
|
-
* const { registerOTel } = await import('@uselemma/tracing');
|
|
56
|
-
* registerOTel();
|
|
57
|
-
* }
|
|
58
|
-
* }
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* // With explicit options
|
|
62
|
-
* registerOTel({
|
|
63
|
-
* apiKey: 'lma_...',
|
|
64
|
-
* projectId: 'proj_...',
|
|
65
|
-
* });
|
|
66
|
-
*/
|
|
67
|
-
function registerOTel(options = {}) {
|
|
68
|
-
const tracerProvider = new sdk_trace_node_1.NodeTracerProvider({
|
|
69
|
-
spanProcessors: [createLemmaSpanProcessor(options)],
|
|
70
|
-
});
|
|
71
|
-
// Register this provider as the global tracer provider so all
|
|
72
|
-
// subsequent `trace.getTracer()` calls use it
|
|
73
|
-
tracerProvider.register();
|
|
74
|
-
return tracerProvider;
|
|
75
|
-
}
|
|
76
|
-
//# sourceMappingURL=register.js.map
|
package/dist/register.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":";;AA0CA,4DAsBC;AAwBD,oCAUC;AAlGD,kEAAmE;AAEnE,wFAA6E;AAC7E,yEAAmE;AAiBnE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,wBAAwB,CACtC,UAA4B,EAAE;IAE9B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;IAC3D,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IACpE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,yBAAyB,CAAC;IAE7D,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CACb,oKAAoK,CACrK,CAAC;IACJ,CAAC;IAED,OAAO,IAAI,gDAAqB,CAC9B,IAAI,6CAAiB,CAAC;QACpB,GAAG,EAAE,GAAG,OAAO,iBAAiB;QAChC,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,MAAM,EAAE;YACjC,oBAAoB,EAAE,SAAS;SAChC;KACF,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,YAAY,CAAC,UAA4B,EAAE;IACzD,MAAM,cAAc,GAAG,IAAI,mCAAkB,CAAC;QAC5C,cAAc,EAAE,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC;KACpD,CAAC,CAAC;IAEH,8DAA8D;IAC9D,8CAA8C;IAC9C,cAAc,CAAC,QAAQ,EAAE,CAAC;IAE1B,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Context } from "@opentelemetry/api";
|
|
2
|
-
import { type ReadableSpan, type Span, type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
3
|
-
export declare class RunBatchSpanProcessor implements SpanProcessor {
|
|
4
|
-
private isShutdown;
|
|
5
|
-
private spanIdToRunId;
|
|
6
|
-
private topLevelSpanIdByRunId;
|
|
7
|
-
private directChildCountByRunId;
|
|
8
|
-
private directChildSpanIdToRunId;
|
|
9
|
-
private batches;
|
|
10
|
-
private endedRuns;
|
|
11
|
-
private readonly exporter;
|
|
12
|
-
constructor(exporter: SpanExporter);
|
|
13
|
-
onStart(span: Span, _parentContext: Context): void;
|
|
14
|
-
onEnd(span: ReadableSpan): void;
|
|
15
|
-
forceFlush(): Promise<void>;
|
|
16
|
-
shutdown(): Promise<void>;
|
|
17
|
-
private isTopLevelRun;
|
|
18
|
-
private getRunIdFromSpan;
|
|
19
|
-
private getInstrumentationScopeName;
|
|
20
|
-
private shouldSkipExport;
|
|
21
|
-
private exportRunBatch;
|
|
22
|
-
private hasNoOpenDirectChildren;
|
|
23
|
-
private clearRunMapping;
|
|
24
|
-
}
|
|
25
|
-
//# sourceMappingURL=run-batch-span-processor.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-batch-span-processor.d.ts","sourceRoot":"","sources":["../src/run-batch-span-processor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,IAAI,EACT,KAAK,YAAY,EACjB,KAAK,aAAa,EACnB,MAAM,+BAA+B,CAAC;AAMvC,qBAAa,qBAAsB,YAAW,aAAa;IACzD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,aAAa,CAA4B;IACjD,OAAO,CAAC,qBAAqB,CAA4B;IACzD,OAAO,CAAC,uBAAuB,CAA4B;IAC3D,OAAO,CAAC,wBAAwB,CAA4B;IAC5D,OAAO,CAAC,OAAO,CAAoC;IACnD,OAAO,CAAC,SAAS,CAAoB;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;gBAE5B,QAAQ,EAAE,YAAY;IAIlC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,GAAG,IAAI;IA6BlD,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,IAAI;IAoCzB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAS3B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAS/B,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,gBAAgB;IAOxB,OAAO,CAAC,2BAA2B;IAQnC,OAAO,CAAC,gBAAgB;YAIV,cAAc;IAmB5B,OAAO,CAAC,uBAAuB;IAI/B,OAAO,CAAC,eAAe;CAcxB"}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunBatchSpanProcessor = void 0;
|
|
4
|
-
const crypto_1 = require("crypto");
|
|
5
|
-
const debug_mode_1 = require("./debug-mode");
|
|
6
|
-
class RunBatchSpanProcessor {
|
|
7
|
-
isShutdown = false;
|
|
8
|
-
spanIdToRunId = new Map();
|
|
9
|
-
topLevelSpanIdByRunId = new Map();
|
|
10
|
-
directChildCountByRunId = new Map();
|
|
11
|
-
directChildSpanIdToRunId = new Map();
|
|
12
|
-
batches = new Map();
|
|
13
|
-
endedRuns = new Set();
|
|
14
|
-
exporter;
|
|
15
|
-
constructor(exporter) {
|
|
16
|
-
this.exporter = exporter;
|
|
17
|
-
}
|
|
18
|
-
onStart(span, _parentContext) {
|
|
19
|
-
const spanId = span.spanContext().spanId;
|
|
20
|
-
if (this.isTopLevelRun(span)) {
|
|
21
|
-
const runId = this.getRunIdFromSpan(span) ?? (0, crypto_1.randomUUID)();
|
|
22
|
-
span.setAttribute("lemma.run_id", runId);
|
|
23
|
-
this.spanIdToRunId.set(spanId, runId);
|
|
24
|
-
this.topLevelSpanIdByRunId.set(runId, spanId);
|
|
25
|
-
this.directChildCountByRunId.set(runId, 0);
|
|
26
|
-
(0, debug_mode_1.lemmaDebug)("processor", "onStart: top-level run span", { spanId, runId });
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const parentSpanId = span.parentSpanContext?.spanId;
|
|
30
|
-
if (!parentSpanId)
|
|
31
|
-
return;
|
|
32
|
-
const runId = this.spanIdToRunId.get(parentSpanId);
|
|
33
|
-
if (!runId)
|
|
34
|
-
return;
|
|
35
|
-
if (this.topLevelSpanIdByRunId.get(runId) === parentSpanId) {
|
|
36
|
-
this.directChildSpanIdToRunId.set(spanId, runId);
|
|
37
|
-
this.directChildCountByRunId.set(runId, (this.directChildCountByRunId.get(runId) ?? 0) + 1);
|
|
38
|
-
}
|
|
39
|
-
this.spanIdToRunId.set(spanId, runId);
|
|
40
|
-
span.setAttribute("lemma.run_id", runId);
|
|
41
|
-
(0, debug_mode_1.lemmaDebug)("processor", "onStart: child span", { spanName: span.name, spanId, runId });
|
|
42
|
-
}
|
|
43
|
-
onEnd(span) {
|
|
44
|
-
const spanId = span.spanContext().spanId;
|
|
45
|
-
const runId = this.spanIdToRunId.get(spanId) ??
|
|
46
|
-
(span.parentSpanContext?.spanId
|
|
47
|
-
? this.spanIdToRunId.get(span.parentSpanContext.spanId)
|
|
48
|
-
: undefined);
|
|
49
|
-
if (!runId)
|
|
50
|
-
return;
|
|
51
|
-
const isTopLevelRun = this.isTopLevelRun(span);
|
|
52
|
-
const shouldSkipExport = this.shouldSkipExport(span);
|
|
53
|
-
const directChildRunId = this.directChildSpanIdToRunId.get(spanId);
|
|
54
|
-
if (directChildRunId) {
|
|
55
|
-
this.directChildSpanIdToRunId.delete(spanId);
|
|
56
|
-
const currentCount = this.directChildCountByRunId.get(directChildRunId) ?? 0;
|
|
57
|
-
const nextCount = Math.max(0, currentCount - 1);
|
|
58
|
-
this.directChildCountByRunId.set(directChildRunId, nextCount);
|
|
59
|
-
(0, debug_mode_1.lemmaDebug)("processor", "onEnd: direct child ended", { spanName: span.name, spanId, runId: directChildRunId, remainingChildren: nextCount });
|
|
60
|
-
}
|
|
61
|
-
(0, debug_mode_1.lemmaDebug)("processor", "onEnd: span ended", { spanName: span.name, spanId, runId, isTopLevelRun, skipped: shouldSkipExport });
|
|
62
|
-
if (!shouldSkipExport) {
|
|
63
|
-
const batch = this.batches.get(runId);
|
|
64
|
-
if (batch)
|
|
65
|
-
batch.push(span);
|
|
66
|
-
else
|
|
67
|
-
this.batches.set(runId, [span]);
|
|
68
|
-
}
|
|
69
|
-
if (isTopLevelRun) {
|
|
70
|
-
this.endedRuns.add(runId);
|
|
71
|
-
}
|
|
72
|
-
void this.exportRunBatch(runId, false);
|
|
73
|
-
}
|
|
74
|
-
async forceFlush() {
|
|
75
|
-
(0, debug_mode_1.lemmaDebug)("processor", "forceFlush called");
|
|
76
|
-
const runIds = [...this.batches.keys()];
|
|
77
|
-
await Promise.all(runIds.map((runId) => this.exportRunBatch(runId, true)));
|
|
78
|
-
if (this.exporter.forceFlush) {
|
|
79
|
-
await this.exporter.forceFlush();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
async shutdown() {
|
|
83
|
-
if (this.isShutdown)
|
|
84
|
-
return;
|
|
85
|
-
(0, debug_mode_1.lemmaDebug)("processor", "shutdown called");
|
|
86
|
-
this.isShutdown = true;
|
|
87
|
-
await this.forceFlush();
|
|
88
|
-
this.exporter.shutdown();
|
|
89
|
-
}
|
|
90
|
-
isTopLevelRun(span) {
|
|
91
|
-
return span.name === "ai.agent.run" && !span.parentSpanContext;
|
|
92
|
-
}
|
|
93
|
-
getRunIdFromSpan(span) {
|
|
94
|
-
const attributes = span
|
|
95
|
-
.attributes;
|
|
96
|
-
const runId = attributes?.["lemma.run_id"];
|
|
97
|
-
return typeof runId === "string" && runId.length > 0 ? runId : undefined;
|
|
98
|
-
}
|
|
99
|
-
getInstrumentationScopeName(span) {
|
|
100
|
-
return span.instrumentationScope?.name;
|
|
101
|
-
}
|
|
102
|
-
shouldSkipExport(span) {
|
|
103
|
-
return this.getInstrumentationScopeName(span) === "next.js";
|
|
104
|
-
}
|
|
105
|
-
async exportRunBatch(runId, force) {
|
|
106
|
-
if (!force && (!this.endedRuns.has(runId) || !this.hasNoOpenDirectChildren(runId))) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
const batch = this.batches.get(runId);
|
|
110
|
-
this.batches.delete(runId);
|
|
111
|
-
this.endedRuns.delete(runId);
|
|
112
|
-
this.clearRunMapping(runId);
|
|
113
|
-
if (!batch || batch.length === 0)
|
|
114
|
-
return;
|
|
115
|
-
(0, debug_mode_1.lemmaDebug)("processor", "exporting batch", { runId, spanCount: batch.length, force });
|
|
116
|
-
await new Promise((resolve) => {
|
|
117
|
-
this.exporter.export(batch, () => resolve());
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
hasNoOpenDirectChildren(runId) {
|
|
121
|
-
return (this.directChildCountByRunId.get(runId) ?? 0) === 0;
|
|
122
|
-
}
|
|
123
|
-
clearRunMapping(runId) {
|
|
124
|
-
for (const [spanId, mappedRunId] of this.spanIdToRunId.entries()) {
|
|
125
|
-
if (mappedRunId === runId) {
|
|
126
|
-
this.spanIdToRunId.delete(spanId);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
for (const [spanId, mappedRunId] of this.directChildSpanIdToRunId.entries()) {
|
|
130
|
-
if (mappedRunId === runId) {
|
|
131
|
-
this.directChildSpanIdToRunId.delete(spanId);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
this.topLevelSpanIdByRunId.delete(runId);
|
|
135
|
-
this.directChildCountByRunId.delete(runId);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
exports.RunBatchSpanProcessor = RunBatchSpanProcessor;
|
|
139
|
-
//# sourceMappingURL=run-batch-span-processor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run-batch-span-processor.js","sourceRoot":"","sources":["../src/run-batch-span-processor.ts"],"names":[],"mappings":";;;AAAA,mCAAoC;AAQpC,6CAA0C;AAK1C,MAAa,qBAAqB;IACxB,UAAU,GAAG,KAAK,CAAC;IACnB,aAAa,GAAG,IAAI,GAAG,EAAiB,CAAC;IACzC,qBAAqB,GAAG,IAAI,GAAG,EAAiB,CAAC;IACjD,uBAAuB,GAAG,IAAI,GAAG,EAAiB,CAAC;IACnD,wBAAwB,GAAG,IAAI,GAAG,EAAiB,CAAC;IACpD,OAAO,GAAG,IAAI,GAAG,EAAyB,CAAC;IAC3C,SAAS,GAAG,IAAI,GAAG,EAAS,CAAC;IACpB,QAAQ,CAAe;IAExC,YAAY,QAAsB;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,OAAO,CAAC,IAAU,EAAE,cAAuB;QACzC,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,MAAM,CAAC;QAEzC,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7B,MAAM,KAAK,GAAG,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,IAAA,mBAAU,GAAE,CAAC;YAC1D,IAAI,CAAC,YAAY,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;YACzC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;YACtC,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAC9C,IAAI,CAAC,uBAAuB,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAC3C,IAAA,uBAAU,EAAC,WAAW,EAAE,6BAA6B,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CA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AAC,CAAC,KAAK,CAAC,CAAC;IAC9D,CAAC;IAEO,eAAe,CAAC,KAAY;QAClC,KAAK,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC;YACjE,IAAI,WAAW,KAAK,KAAK,EAAE,CAAC;gBAC1B,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,IAAI,IAAI,CAAC,wBAAwB,CAAC,OAAO,EAAE,EAAE,CAAC;YAC5E,IAAI,WAAW,KAAK,KAAK,EAAE,CAAC;gBAC1B,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QACD,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzC,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7C,CAAC;CACF;AA7JD,sDA6JC"}
|
package/dist/span-helpers.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wraps a function with a child span under the current active context.
|
|
3
|
-
*
|
|
4
|
-
* Use this for any internal function you want to appear in the trace — tool
|
|
5
|
-
* implementations, retrieval calls, preprocessing steps, etc.
|
|
6
|
-
*
|
|
7
|
-
* @param name - Span name shown in the Lemma trace view.
|
|
8
|
-
* @param fn - Function to wrap. Receives the same input as the returned wrapper.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* const formatOutput = trace("format-output", async (raw: string) => {
|
|
12
|
-
* return raw.trim().toLowerCase();
|
|
13
|
-
* });
|
|
14
|
-
*
|
|
15
|
-
* // Inside an agent() handler:
|
|
16
|
-
* const formatted = await formatOutput(rawText);
|
|
17
|
-
*/
|
|
18
|
-
export declare function trace<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
19
|
-
/**
|
|
20
|
-
* Wraps a tool function with a child span. Sets `span.type = "tool"`.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const lookupOrder = tool("lookup-order", async (orderId: string) => {
|
|
24
|
-
* return db.orders.findById(orderId);
|
|
25
|
-
* });
|
|
26
|
-
*/
|
|
27
|
-
export declare function tool<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
28
|
-
/**
|
|
29
|
-
* Wraps an LLM call with a child span. Sets `span.type = "generation"`.
|
|
30
|
-
*
|
|
31
|
-
* Prefer provider instrumentation (OpenInference) for automatic LLM spans
|
|
32
|
-
* with prompt/completion/token attributes. Use this helper for custom or
|
|
33
|
-
* lightly-instrumented models.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* const generate = llm("gpt-4o", async (prompt: string) => {
|
|
37
|
-
* return openai.chat.completions.create({ ... });
|
|
38
|
-
* });
|
|
39
|
-
*/
|
|
40
|
-
export declare function llm<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
41
|
-
/**
|
|
42
|
-
* Wraps a retrieval function with a child span. Sets `span.type = "retriever"`.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* const search = retrieval("vector-search", async (query: string) => {
|
|
46
|
-
* return vectorDB.search(query, { topK: 5 });
|
|
47
|
-
* });
|
|
48
|
-
*/
|
|
49
|
-
export declare function retrieval<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
50
|
-
//# sourceMappingURL=span-helpers.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"span-helpers.d.ts","sourceRoot":"","sources":["../src/span-helpers.ts"],"names":[],"mappings":"AAqCA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EACrD,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC7C,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAEnC;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EACpD,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC7C,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAEnC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,GAAG,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EACnD,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC7C,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAEnC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EACzD,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC7C,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,CAEnC"}
|
package/dist/span-helpers.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.trace = trace;
|
|
4
|
-
exports.tool = tool;
|
|
5
|
-
exports.llm = llm;
|
|
6
|
-
exports.retrieval = retrieval;
|
|
7
|
-
const api_1 = require("@opentelemetry/api");
|
|
8
|
-
/**
|
|
9
|
-
* Core helper: wraps `fn` so every call runs inside a child span named
|
|
10
|
-
* `spanName` under the currently active context. Unlike `wrapAgent`, this
|
|
11
|
-
* does NOT create a new trace root — it adds a child span to whatever span
|
|
12
|
-
* is currently active.
|
|
13
|
-
*
|
|
14
|
-
* The span ends when the function returns (or throws). Return values are
|
|
15
|
-
* passed through unchanged.
|
|
16
|
-
*/
|
|
17
|
-
function spanHelper(spanName, fn, spanType) {
|
|
18
|
-
return async function (input) {
|
|
19
|
-
const tracer = api_1.trace.getTracer("lemma");
|
|
20
|
-
return tracer.startActiveSpan(spanName, async (span) => {
|
|
21
|
-
if (spanType) {
|
|
22
|
-
span.setAttribute("span.type", spanType);
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
const result = await fn(input);
|
|
26
|
-
span.end();
|
|
27
|
-
return result;
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
span.recordException(err);
|
|
31
|
-
span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
|
|
32
|
-
span.end();
|
|
33
|
-
throw err;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Wraps a function with a child span under the current active context.
|
|
40
|
-
*
|
|
41
|
-
* Use this for any internal function you want to appear in the trace — tool
|
|
42
|
-
* implementations, retrieval calls, preprocessing steps, etc.
|
|
43
|
-
*
|
|
44
|
-
* @param name - Span name shown in the Lemma trace view.
|
|
45
|
-
* @param fn - Function to wrap. Receives the same input as the returned wrapper.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* const formatOutput = trace("format-output", async (raw: string) => {
|
|
49
|
-
* return raw.trim().toLowerCase();
|
|
50
|
-
* });
|
|
51
|
-
*
|
|
52
|
-
* // Inside an agent() handler:
|
|
53
|
-
* const formatted = await formatOutput(rawText);
|
|
54
|
-
*/
|
|
55
|
-
function trace(name, fn) {
|
|
56
|
-
return spanHelper(name, fn);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Wraps a tool function with a child span. Sets `span.type = "tool"`.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* const lookupOrder = tool("lookup-order", async (orderId: string) => {
|
|
63
|
-
* return db.orders.findById(orderId);
|
|
64
|
-
* });
|
|
65
|
-
*/
|
|
66
|
-
function tool(name, fn) {
|
|
67
|
-
return spanHelper(name, fn, "tool");
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Wraps an LLM call with a child span. Sets `span.type = "generation"`.
|
|
71
|
-
*
|
|
72
|
-
* Prefer provider instrumentation (OpenInference) for automatic LLM spans
|
|
73
|
-
* with prompt/completion/token attributes. Use this helper for custom or
|
|
74
|
-
* lightly-instrumented models.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* const generate = llm("gpt-4o", async (prompt: string) => {
|
|
78
|
-
* return openai.chat.completions.create({ ... });
|
|
79
|
-
* });
|
|
80
|
-
*/
|
|
81
|
-
function llm(name, fn) {
|
|
82
|
-
return spanHelper(name, fn, "generation");
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Wraps a retrieval function with a child span. Sets `span.type = "retriever"`.
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* const search = retrieval("vector-search", async (query: string) => {
|
|
89
|
-
* return vectorDB.search(query, { topK: 5 });
|
|
90
|
-
* });
|
|
91
|
-
*/
|
|
92
|
-
function retrieval(name, fn) {
|
|
93
|
-
return spanHelper(name, fn, "retriever");
|
|
94
|
-
}
|
|
95
|
-
//# sourceMappingURL=span-helpers.js.map
|
package/dist/span-helpers.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"span-helpers.js","sourceRoot":"","sources":["../src/span-helpers.ts"],"names":[],"mappings":";;AAsDA,sBAKC;AAUD,oBAKC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