@uselemma/tracing 2.8.0 → 2.9.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.
@@ -5,8 +5,9 @@ export type TraceContext = {
5
5
  /** Unique identifier for this agent run. */
6
6
  runId: string;
7
7
  /**
8
- * Signal the run is complete. When `autoEndRoot` is disabled, this
9
- * ends the top-level span and returns `true`.
8
+ * Signal the run is complete. When `autoEndRoot` is enabled (the default),
9
+ * this is a no-op and returns `false` — the span is ended automatically.
10
+ * When `autoEndRoot` is disabled, this ends the top-level span and returns `true`.
10
11
  */
11
12
  onComplete: (result: unknown) => boolean;
12
13
  /** Record an error on the span. Marks the span as errored. */
@@ -16,8 +17,9 @@ export type WrapAgentOptions = {
16
17
  /** Mark this run as an experiment in Lemma. */
17
18
  isExperiment?: boolean;
18
19
  /**
19
- * If `true`, the run processor will automatically end the top-level span
20
- * when all direct child spans have ended.
20
+ * If `true` (the default), the top-level span is automatically ended when
21
+ * the wrapped function returns or throws. Set to `false` to manage span
22
+ * lifetime manually via `ctx.onComplete`.
21
23
  */
22
24
  autoEndRoot?: boolean;
23
25
  };
@@ -42,7 +44,7 @@ export type WrapAgentOptions = {
42
44
  * @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
43
45
  * @param options - Configuration for the agent trace.
44
46
  * @param options.isExperiment - Mark this run as an experiment in Lemma.
45
- * @param options.autoEndRoot - Enable processor-driven automatic ending of the top-level span after direct children have ended.
47
+ * @param options.autoEndRoot - Automatically end the top-level span when the wrapped function returns or throws (default: `true`). Set to `false` to end manually via `ctx.onComplete`.
46
48
  * @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
47
49
  */
48
50
  export declare function wrapAgent<Input = unknown>(agentName: string, fn: (traceContext: TraceContext, input: Input) => any, options?: WrapAgentOptions): (this: any, input: Input) => Promise<{
@@ -1 +1 @@
1
- {"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAIxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,8DAA8D;IAC9D,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;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CAAC,KAAK,GAAG,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,KAAK,GAAG,EAAE,OAAO,CAAC,EAAE,gBAAgB,UAC/F,GAAG,SAAS,KAAK;;;;GAgDhE"}
1
+ {"version":3,"file":"trace-wrapper.d.ts","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAIxE,MAAM,MAAM,YAAY,GAAG;IACzB,wDAAwD;IACxD,IAAI,EAAE,IAAI,CAAC;IACX,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,8DAA8D;IAC9D,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;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CAAC,KAAK,GAAG,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,KAAK,GAAG,EAAE,OAAO,CAAC,EAAE,gBAAgB,UAC/F,GAAG,SAAS,KAAK;;;;GA4DhE"}
@@ -25,7 +25,7 @@ const experiment_mode_1 = require("./experiment-mode");
25
25
  * @param fn - The agent function to wrap. Receives a {@link TraceContext} as its first argument and the call-time input as its second.
26
26
  * @param options - Configuration for the agent trace.
27
27
  * @param options.isExperiment - Mark this run as an experiment in Lemma.
28
- * @param options.autoEndRoot - Enable processor-driven automatic ending of the top-level span after direct children have ended.
28
+ * @param options.autoEndRoot - Automatically end the top-level span when the wrapped function returns or throws (default: `true`). Set to `false` to end manually via `ctx.onComplete`.
29
29
  * @returns An async function that accepts an `input`, executes `fn` inside a traced context, and returns `{ result, runId, span }`.
30
30
  */
31
31
  function wrapAgent(agentName, fn, options) {
@@ -34,39 +34,49 @@ function wrapAgent(agentName, fn, options) {
34
34
  const tracer = api_1.trace.getTracer("lemma");
35
35
  // Generate a unique run ID and open a new span for this agent invocation
36
36
  const runId = (0, uuid_1.v4)();
37
+ const autoEndRoot = options?.autoEndRoot !== false; // default true
37
38
  const span = tracer.startSpan("ai.agent.run", {
38
39
  attributes: {
39
40
  "ai.agent.name": agentName,
40
41
  "lemma.run_id": runId,
41
42
  "lemma.is_experiment": (0, experiment_mode_1.isExperimentModeEnabled)() || options?.isExperiment === true,
42
- "lemma.auto_end_root": options?.autoEndRoot === true,
43
+ "lemma.auto_end_root": autoEndRoot,
43
44
  },
44
45
  }, api_1.ROOT_CONTEXT);
45
46
  // Propagate the span as the active context so child spans are nested correctly
46
47
  const ctx = api_1.trace.setSpan(api_1.ROOT_CONTEXT, span);
47
- const autoEndRoot = options?.autoEndRoot === true;
48
48
  let rootEnded = false;
49
49
  try {
50
50
  return await api_1.context.with(ctx, async () => {
51
51
  const onComplete = (_result) => {
52
- if (autoEndRoot || rootEnded)
53
- return false;
54
- rootEnded = true;
55
- span.end();
56
- return true;
52
+ if (!autoEndRoot && !rootEnded) {
53
+ rootEnded = true;
54
+ span.end();
55
+ return true;
56
+ }
57
+ return false;
57
58
  };
58
59
  const recordError = (error) => {
59
60
  span.recordException(error instanceof Error ? error : new Error(String(error)));
60
61
  span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
61
62
  };
62
63
  const result = await fn.call(this, { span, runId, onComplete, recordError }, input);
64
+ // Auto-end the span if autoEndRoot is enabled and onComplete hasn't ended it yet
65
+ if (autoEndRoot && !rootEnded) {
66
+ rootEnded = true;
67
+ span.end();
68
+ }
63
69
  return { result, runId, span };
64
70
  });
65
71
  }
66
72
  catch (err) {
67
- // Record the exception on the span and mark it as errored
73
+ // Record the exception on the span, mark it as errored, and end it
68
74
  span.recordException(err);
69
75
  span.setStatus({ code: 2 }); // SpanStatusCode.ERROR
76
+ if (!rootEnded) {
77
+ rootEnded = true;
78
+ span.end();
79
+ }
70
80
  throw err;
71
81
  }
72
82
  };
@@ -1 +1 @@
1
- {"version":3,"file":"trace-wrapper.js","sourceRoot":"","sources":["../src/trace-wrapper.ts"],"names":[],"mappings":";;AAoDA,8BAiDC;AArGD,4CAAwE;AACxE,+BAAoC;AACpC,uDAA4D;AA0B5D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,SAAgB,SAAS,CAAkB,SAAiB,EAAE,EAAqD,EAAE,OAA0B;IAC7I,MAAM,eAAe,GAAG,KAAK,WAAsB,KAAY;QAC7D,wDAAwD;QACxD,MAAM,MAAM,GAAG,WAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAExC,yEAAyE;QACzE,MAAM,KAAK,GAAG,IAAA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C,SAAS,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAuB;YACpD,IAAI,CAAC,SAAS,EAAE,CAAC;gBACf,SAAS,GAAG,IAAI,CAAC;gBACjB,IAAI,CAAC,GAAG,EAAE,CAAC;YACb,CAAC;YAED,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,eAAe,CAAC;AACzB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uselemma/tracing",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "OpenTelemetry-based tracing module for Lemma",
5
5
  "license": "MIT",
6
6
  "author": "Lemma",
@@ -41,15 +41,21 @@
41
41
  "uuid": "^13.0.0"
42
42
  },
43
43
  "devDependencies": {
44
+ "@arizeai/openinference-instrumentation-openai": "^4.0.5",
45
+ "@opentelemetry/instrumentation": "^0.213.0",
44
46
  "@trivago/prettier-plugin-sort-imports": "^5.2.2",
45
47
  "@types/node": "^22.18.8",
48
+ "@vitest/coverage-v8": "^3.2.4",
49
+ "openai": "^6.26.0",
46
50
  "prettier": "^3.6.2",
47
51
  "prettier-plugin-tailwindcss": "^0.6.14",
48
- "typescript": "^5.9.3"
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^3.2.4"
49
54
  },
50
55
  "scripts": {
51
56
  "type-check": "tsc --noEmit",
52
57
  "build": "tsc",
53
- "clean": "rm -rf dist"
58
+ "clean": "rm -rf dist",
59
+ "test": "vitest run"
54
60
  }
55
61
  }