@uselemma/tracing 3.0.3 → 4.0.1
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 +239 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +691 -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 +159 -0
- package/dist/vercel-ai.d.ts.map +1 -0
- package/dist/vercel-ai.js +392 -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 -55
- package/dist/span-helpers.d.ts.map +0 -1
- package/dist/span-helpers.js +0 -103
- 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
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,CAA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AC,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,55 +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
|
-
* Input and output are automatically captured as span attributes.
|
|
8
|
-
*
|
|
9
|
-
* @param name - Span name shown in the Lemma trace view.
|
|
10
|
-
* @param fn - Function to wrap. Receives the same input as the returned wrapper.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* const formatOutput = trace("format-output", async (raw: string) => {
|
|
14
|
-
* return raw.trim().toLowerCase();
|
|
15
|
-
* });
|
|
16
|
-
*
|
|
17
|
-
* // Inside an agent() handler:
|
|
18
|
-
* const formatted = await formatOutput(rawText);
|
|
19
|
-
*/
|
|
20
|
-
export declare function trace<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
21
|
-
/**
|
|
22
|
-
* Wraps a tool function with a child span. Sets `span.type = "tool"`.
|
|
23
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* const lookupOrder = tool("lookup-order", async (orderId: string) => {
|
|
27
|
-
* return db.orders.findById(orderId);
|
|
28
|
-
* });
|
|
29
|
-
*/
|
|
30
|
-
export declare function tool<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
31
|
-
/**
|
|
32
|
-
* Wraps an LLM call with a child span. Sets `span.type = "generation"`.
|
|
33
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
34
|
-
*
|
|
35
|
-
* Prefer provider instrumentation (OpenInference) for automatic LLM spans
|
|
36
|
-
* with prompt/completion/token attributes. Use this helper for custom or
|
|
37
|
-
* lightly-instrumented models.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* const generate = llm("gpt-4o", async (prompt: string) => {
|
|
41
|
-
* return openai.chat.completions.create({ ... });
|
|
42
|
-
* });
|
|
43
|
-
*/
|
|
44
|
-
export declare function llm<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
|
|
45
|
-
/**
|
|
46
|
-
* Wraps a retrieval function with a child span. Sets `span.type = "retriever"`.
|
|
47
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* const search = retrieval("vector-search", async (query: string) => {
|
|
51
|
-
* return vectorDB.search(query, { topK: 5 });
|
|
52
|
-
* });
|
|
53
|
-
*/
|
|
54
|
-
export declare function retrieval<Input = unknown, Output = unknown>(name: string, fn: (input: Input) => Output | Promise<Output>): (input: Input) => Promise<Output>;
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span-helpers.js
DELETED
|
@@ -1,103 +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. Input and output are automatically recorded as
|
|
16
|
-
* `input.value` and `output.value` span attributes.
|
|
17
|
-
*/
|
|
18
|
-
function spanHelper(spanName, fn, spanType) {
|
|
19
|
-
return async function (input) {
|
|
20
|
-
const tracer = api_1.trace.getTracer("lemma");
|
|
21
|
-
return tracer.startActiveSpan(spanName, async (span) => {
|
|
22
|
-
if (spanType) {
|
|
23
|
-
span.setAttribute("span.type", spanType);
|
|
24
|
-
}
|
|
25
|
-
span.setAttribute("input.value", JSON.stringify(input) ?? "null");
|
|
26
|
-
try {
|
|
27
|
-
const result = await fn(input);
|
|
28
|
-
span.setAttribute("output.value", JSON.stringify(result) ?? "null");
|
|
29
|
-
span.end();
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
span.recordException(err);
|
|
34
|
-
span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
|
|
35
|
-
span.end();
|
|
36
|
-
throw err;
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Wraps a function with a child span under the current active context.
|
|
43
|
-
*
|
|
44
|
-
* Use this for any internal function you want to appear in the trace — tool
|
|
45
|
-
* implementations, retrieval calls, preprocessing steps, etc.
|
|
46
|
-
*
|
|
47
|
-
* Input and output are automatically captured as span attributes.
|
|
48
|
-
*
|
|
49
|
-
* @param name - Span name shown in the Lemma trace view.
|
|
50
|
-
* @param fn - Function to wrap. Receives the same input as the returned wrapper.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* const formatOutput = trace("format-output", async (raw: string) => {
|
|
54
|
-
* return raw.trim().toLowerCase();
|
|
55
|
-
* });
|
|
56
|
-
*
|
|
57
|
-
* // Inside an agent() handler:
|
|
58
|
-
* const formatted = await formatOutput(rawText);
|
|
59
|
-
*/
|
|
60
|
-
function trace(name, fn) {
|
|
61
|
-
return spanHelper(name, fn);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Wraps a tool function with a child span. Sets `span.type = "tool"`.
|
|
65
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* const lookupOrder = tool("lookup-order", async (orderId: string) => {
|
|
69
|
-
* return db.orders.findById(orderId);
|
|
70
|
-
* });
|
|
71
|
-
*/
|
|
72
|
-
function tool(name, fn) {
|
|
73
|
-
return spanHelper(name, fn, "tool");
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Wraps an LLM call with a child span. Sets `span.type = "generation"`.
|
|
77
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
78
|
-
*
|
|
79
|
-
* Prefer provider instrumentation (OpenInference) for automatic LLM spans
|
|
80
|
-
* with prompt/completion/token attributes. Use this helper for custom or
|
|
81
|
-
* lightly-instrumented models.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* const generate = llm("gpt-4o", async (prompt: string) => {
|
|
85
|
-
* return openai.chat.completions.create({ ... });
|
|
86
|
-
* });
|
|
87
|
-
*/
|
|
88
|
-
function llm(name, fn) {
|
|
89
|
-
return spanHelper(name, fn, "generation");
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Wraps a retrieval function with a child span. Sets `span.type = "retriever"`.
|
|
93
|
-
* Input and output are automatically captured as `input.value` / `output.value`.
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* const search = retrieval("vector-search", async (query: string) => {
|
|
97
|
-
* return vectorDB.search(query, { topK: 5 });
|
|
98
|
-
* });
|
|
99
|
-
*/
|
|
100
|
-
function retrieval(name, fn) {
|
|
101
|
-
return spanHelper(name, fn, "retriever");
|
|
102
|
-
}
|
|
103
|
-
//# 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":";;AA2DA,sBAKC;AAWD,oBAKC;AAeD,kBAKC;AAWD,8BAKC;AApHD,4CAAwD;AAGxD;;;;;;;;;GASG;AACH,SAAS,UAAU,CACjB,QAAgB,EAChB,EAA8C,EAC9C,QAAiB;IAEjB,OAAO,KAAK,WAAW,KAAY;QACjC,MAAM,MAAM,GAAG,WAAS,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAU,EAAE,EAAE;YAC3D,IAAI,QAAQ,EAAE,CAAC;gBACb,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;YAC3C,CAAC;YACD,IAAI,CAAC,YAAY,CAAC,aAAa,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,CAAC;YAClE,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC;gBAC/B,IAAI,CAAC,YAAY,CAAC,cAAc,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,CAAC;gBACpE,IAAI,CAAC,GAAG,EAAE,CAAC;gBACX,OAAO,MAAM,CAAC;YAChB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,CAAC,eAAe,CAAC,GAAY,CAAC,CAAC;gBACnC,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAuB;gBACpD,IAAI,CAAC,GAAG,EAAE,CAAC;gBACX,MAAM,GAAG,CAAC;YACZ,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,KAAK,CACnB,IAAY,EACZ,EAA8C;IAE9C,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAC9B,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,IAAI,CAClB,IAAY,EACZ,EAA8C;IAE9C,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,GAAG,CACjB,IAAY,EACZ,EAA8C;IAE9C,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,YAAY,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,SAAS,CACvB,IAAY,EACZ,EAA8C;IAE9C,OAAO,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC;AAC3C,CAAC"}
|
package/dist/trace-wrapper.d.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Span } from "@opentelemetry/api";
|
|
2
|
-
export type TraceContext = {
|
|
3
|
-
/** The active OpenTelemetry span for this agent run. */
|
|
4
|
-
span: Span;
|
|
5
|
-
/** Unique identifier for this agent run. */
|
|
6
|
-
runId: string;
|
|
7
|
-
/**
|
|
8
|
-
* Override the run output and close the span immediately.
|
|
9
|
-
*
|
|
10
|
-
* In non-streaming agents, `complete()` is **optional** — the wrapper
|
|
11
|
-
* automatically captures the return value as `ai.agent.output` and closes
|
|
12
|
-
* the span when the function returns. Call it explicitly only when you need
|
|
13
|
-
* to record a different output than the return value, or close the span
|
|
14
|
-
* before the function exits.
|
|
15
|
-
*
|
|
16
|
-
* In streaming agents (`{ streaming: true }`), `complete()` is **required**
|
|
17
|
-
* — call it inside the stream's `onFinish` callback once the full output is
|
|
18
|
-
* assembled.
|
|
19
|
-
*
|
|
20
|
-
* Idempotent: the first call wins, subsequent calls are no-ops.
|
|
21
|
-
*/
|
|
22
|
-
complete: (result?: unknown) => void;
|
|
23
|
-
/**
|
|
24
|
-
* @deprecated Use {@link complete} instead.
|
|
25
|
-
*/
|
|
26
|
-
onComplete: (result: unknown) => void;
|
|
27
|
-
/**
|
|
28
|
-
* Record an error on the span and mark the run as failed.
|
|
29
|
-
* Does not close the span — the wrapper handles closing on return or error.
|
|
30
|
-
*/
|
|
31
|
-
fail: (error: unknown) => void;
|
|
32
|
-
/**
|
|
33
|
-
* @deprecated Use {@link fail} instead.
|
|
34
|
-
*/
|
|
35
|
-
recordError: (error: unknown) => void;
|
|
36
|
-
};
|
|
37
|
-
export type WrapAgentOptions = {
|
|
38
|
-
/** Mark this run as an experiment in Lemma. */
|
|
39
|
-
isExperiment?: boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Set to `true` for agents that return a streaming response before the
|
|
42
|
-
* full output is known.
|
|
43
|
-
*
|
|
44
|
-
* When `true`, the wrapper does **not** auto-close the span on function
|
|
45
|
-
* return. You must call `ctx.complete(output)` inside the stream's
|
|
46
|
-
* `onFinish` callback once the full output is assembled.
|
|
47
|
-
*
|
|
48
|
-
* @default false
|
|
49
|
-
*/
|
|
50
|
-
streaming?: boolean;
|
|
51
|
-
};
|
|
52
|
-
export type WrapRunOptions = {
|
|
53
|
-
/** Optional thread identifier for this invocation. */
|
|
54
|
-
threadId?: string;
|
|
55
|
-
/** Mark this specific invocation as an experiment. */
|
|
56
|
-
isExperiment?: boolean;
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Wraps an agent function with OpenTelemetry tracing, automatically creating
|
|
60
|
-
* a span for the agent run and providing a `TraceContext` to the wrapped function.
|
|
61
|
-
*
|
|
62
|
-
* **Non-streaming (default):** simply return a value from the wrapped function.
|
|
63
|
-
* The wrapper captures it as `ai.agent.output` and closes the span automatically.
|
|
64
|
-
* No call to `ctx.complete()` is needed.
|
|
65
|
-
*
|
|
66
|
-
* **Streaming:** pass `{ streaming: true }` as the third argument. The wrapper
|
|
67
|
-
* will not auto-close the span on return; call `ctx.complete(output)` inside
|
|
68
|
-
* the stream's `onFinish` callback once the full output is assembled.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* // Non-streaming — just return a value
|
|
72
|
-
* const myAgent = agent('my-agent', async (input: string) => {
|
|
73
|
-
* const { text } = await generateText({
|
|
74
|
-
* model: openai('gpt-4o'), prompt: input,
|
|
75
|
-
* experimental_telemetry: { isEnabled: true },
|
|
76
|
-
* });
|
|
77
|
-
* return text; // wrapper auto-captures and closes the span
|
|
78
|
-
* });
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* // Streaming — opt into manual lifecycle
|
|
82
|
-
* const streamingAgent = agent('streaming-agent', async (input: string, ctx) => {
|
|
83
|
-
* const result = await streamText({
|
|
84
|
-
* model: openai('gpt-4o'), prompt: input,
|
|
85
|
-
* experimental_telemetry: { isEnabled: true },
|
|
86
|
-
* onFinish({ text }) { ctx.complete(text); },
|
|
87
|
-
* });
|
|
88
|
-
* return result.toDataStreamResponse();
|
|
89
|
-
* }, { streaming: true });
|
|
90
|
-
*
|
|
91
|
-
* @param agentName - Human-readable agent name recorded as `ai.agent.name`.
|
|
92
|
-
* @param fn - The agent function to wrap. Receives the call-time input as its
|
|
93
|
-
* first argument and an optional {@link TraceContext} as its second.
|
|
94
|
-
* @param options - Configuration for the agent trace.
|
|
95
|
-
* @returns An async function that accepts an `input`, executes `fn` inside a
|
|
96
|
-
* traced context, and returns `{ result, runId, span }`.
|
|
97
|
-
*/
|
|
98
|
-
export declare function agent<Input = unknown>(agentName: string, fn: (input: Input, traceContext: TraceContext) => any, options?: WrapAgentOptions): (this: any, input: Input, runOptions?: WrapRunOptions) => Promise<{
|
|
99
|
-
result: any;
|
|
100
|
-
runId: string;
|
|
101
|
-
span: Span;
|
|
102
|
-
}>;
|
|
103
|
-
/**
|
|
104
|
-
* @deprecated Use {@link agent} instead. `wrapAgent` will be removed in a future major version.
|
|
105
|
-
*/
|
|
106
|
-
export declare const wrapAgent: typeof agent;
|
|
107
|
-
//# sourceMappingURL=trace-wrapper.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAKxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;;;OAcG;IACH,QAAQ,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACrC;;OAEG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC;;;OAGG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IAC/B;;OAEG;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACvC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,+CAA+C;IAC/C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,sDAAsD;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