@telemetry-dev/sdk 0.1.9 → 0.1.11

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/src/span.ts CHANGED
@@ -41,6 +41,7 @@ import {
41
41
  import { prepareCaptureValue } from "./capture.ts";
42
42
  import { type ClientCore, currentClient } from "./client.ts";
43
43
  import { NOOP_SPAN_HANDLE } from "./noop.ts";
44
+ import { promptAttributes } from "./prompts.ts";
44
45
 
45
46
  export type ParentRef = string /* W3C traceparent */ | SpanHandle | SpanContext | Context;
46
47
 
@@ -153,7 +154,7 @@ function resolveParent(parent: ParentRef | undefined): Context {
153
154
  return trace.setSpanContext(base, ref);
154
155
  }
155
156
 
156
- function applyError(span: Span, error: unknown): void {
157
+ function applyError(span: Span, error: unknown, capture: FieldCaptureConfig): void {
157
158
  const err = error instanceof Error ? error : undefined;
158
159
 
159
160
  // Prefer the specific subclass name (Python parity: type(error).__name__) — many SDK
@@ -165,15 +166,21 @@ function applyError(span: Span, error: unknown): void {
165
166
  ? err.name
166
167
  : (err.constructor?.name ?? "Error") || "Error";
167
168
 
168
- const message = err?.message ?? String(error);
169
169
  span.setStatus({ code: SpanStatusCode.ERROR });
170
170
  span.setAttribute("error.type", errorType);
171
+ // Exceptions can echo either inputs or outputs; both must allow content capture.
172
+ const captureContent = capture.captureInput && capture.captureOutput;
171
173
  span.addEvent(
172
174
  "exception",
173
175
  omitUndefined({
174
176
  "exception.type": errorType,
175
- "exception.message": message,
176
- "exception.stacktrace": err?.stack,
177
+ "exception.message": captureContent
178
+ ? prepareCaptureValue("exception.message", err?.message ?? String(error), capture)
179
+ : undefined,
180
+ "exception.stacktrace":
181
+ captureContent && err?.stack !== undefined
182
+ ? prepareCaptureValue("exception.stacktrace", err.stack, capture)
183
+ : undefined,
177
184
  "log.severity_number": SEVERITY_ERROR,
178
185
  }),
179
186
  );
@@ -185,7 +192,7 @@ function applyFields(span: Span, meta: SpanMeta, fields: SpanFields): void {
185
192
 
186
193
  if (Object.keys(attrs).length > 0) span.setAttributes(attrs);
187
194
 
188
- if (fields.error !== undefined) applyError(span, fields.error);
195
+ if (fields.error !== undefined) applyError(span, fields.error, meta.capture);
189
196
  }
190
197
 
191
198
  function metaFor(core: ClientCore, options?: StartSpanOptions): SpanMeta {
@@ -223,6 +230,7 @@ export function createSpanHandle(
223
230
 
224
231
  const parentCtx = withSessionParent(sessionContext, sessionId, core.config.apiKey);
225
232
  const attrs = fieldsToAttributes(options, meta.type, meta.capture);
233
+ const operation = attrs["gen_ai.operation.name"] ?? SPAN_TYPE_OPERATIONS[meta.type];
226
234
 
227
235
  const span = core.tracer.startSpan(
228
236
  name,
@@ -233,6 +241,7 @@ export function createSpanHandle(
233
241
  attributes: {
234
242
  "gen_ai.operation.name": SPAN_TYPE_OPERATIONS[meta.type],
235
243
  ...propagatedFromContext(parentCtx),
244
+ ...promptAttributes(parentCtx, operation, meta.capture),
236
245
  ...attrs,
237
246
  },
238
247
  },
@@ -246,7 +255,7 @@ export function createSpanHandle(
246
255
 
247
256
  if (options.name) span.updateName(options.name);
248
257
 
249
- if (options.error !== undefined) applyError(span, options.error);
258
+ if (options.error !== undefined) applyError(span, options.error, meta.capture);
250
259
 
251
260
  const spanContext = span.spanContext();
252
261
  let previousOutputChunkTime: number | undefined;