@kortyx/otel 0.4.9 → 0.5.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/CHANGELOG.md +19 -0
- package/dist/index.js +17 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.0](https://github.com/kortyx-io/kortyx/compare/otel-v0.4.9...otel-v0.5.0) (2026-09-14)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* **errors:** public failure messages and default HTTP statuses change; malformed streams fail explicitly, provider error classes are shared, and retry helpers exclude control flow. Existing calls and legacy records remain supported. See the error-contract migration and release notes.
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* **errors:** harden failure contracts across execution and recovery ([#175](https://github.com/kortyx-io/kortyx/issues/175)) ([d231e36](https://github.com/kortyx-io/kortyx/commit/d231e364259a4f23fc3675d8783fde5ba338416e))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Dependencies
|
|
16
|
+
|
|
17
|
+
* The following workspace dependencies were updated
|
|
18
|
+
* dependencies
|
|
19
|
+
* @kortyx/hooks bumped to 0.25.0
|
|
20
|
+
* @kortyx/core bumped to 0.9.0
|
|
21
|
+
|
|
3
22
|
## [0.4.9](https://github.com/kortyx-io/kortyx/compare/otel-v0.4.8...otel-v0.4.9) (2026-09-11)
|
|
4
23
|
|
|
5
24
|
|
package/dist/index.js
CHANGED
|
@@ -200,6 +200,7 @@ var applyAttributeMapping = (name, attributes, options, args) => {
|
|
|
200
200
|
};
|
|
201
201
|
|
|
202
202
|
// src/span-wrapper.ts
|
|
203
|
+
var import_errors = require("@kortyx/core/errors");
|
|
203
204
|
var import_api = require("@opentelemetry/api");
|
|
204
205
|
var createSpanWrapper = (span, name, options) => {
|
|
205
206
|
const wrapper = {
|
|
@@ -239,8 +240,14 @@ var createSpanWrapper = (span, name, options) => {
|
|
|
239
240
|
});
|
|
240
241
|
},
|
|
241
242
|
fail: (error, args) => {
|
|
242
|
-
|
|
243
|
-
|
|
243
|
+
if ((0, import_errors.isControlFlowError)(error)) {
|
|
244
|
+
span.setAttribute("kortyx.control_flow", true);
|
|
245
|
+
wrapper.end?.(args);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const failure = (0, import_errors.serializeFailure)(error);
|
|
249
|
+
const message = failure.message;
|
|
250
|
+
span.recordException({ name: failure.code, message });
|
|
244
251
|
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message });
|
|
245
252
|
const attributes = spanErrorAttributes(error, args);
|
|
246
253
|
span.setAttributes(
|
|
@@ -264,10 +271,12 @@ var spanEndAttributes = (args, options) => ({
|
|
|
264
271
|
...telemetryAttributes(args?.telemetry, options, "output")
|
|
265
272
|
});
|
|
266
273
|
var spanErrorAttributes = (error, args) => {
|
|
267
|
-
const
|
|
274
|
+
const failure = (0, import_errors.serializeFailure)(error);
|
|
275
|
+
const message = failure.message;
|
|
268
276
|
return {
|
|
269
277
|
...args?.attributes ?? {},
|
|
270
|
-
"error.type":
|
|
278
|
+
"error.type": failure.code,
|
|
279
|
+
"kortyx.error.category": failure.category,
|
|
271
280
|
"error.message": message,
|
|
272
281
|
...usageAttributes(args)
|
|
273
282
|
};
|
|
@@ -322,7 +331,10 @@ function createOpenTelemetryTraceAdapter(options = {}) {
|
|
|
322
331
|
if (!wrapped.ended) wrapped.end?.();
|
|
323
332
|
return result;
|
|
324
333
|
} catch (error) {
|
|
325
|
-
|
|
334
|
+
try {
|
|
335
|
+
wrapped.fail?.(error);
|
|
336
|
+
} catch {
|
|
337
|
+
}
|
|
326
338
|
throw error;
|
|
327
339
|
}
|
|
328
340
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/adapter.ts","../src/content.ts","../src/attributes.ts","../src/span-wrapper.ts"],"sourcesContent":["export { createOpenTelemetryTraceAdapter } from \"./adapter\";\nexport type {\n OpenTelemetryMapAttributesArgs,\n OpenTelemetrySpanEndInfo,\n OpenTelemetrySpanStartInfo,\n OpenTelemetryTraceAdapterOptions,\n OpenTelemetryTracePhase,\n} from \"./types\";\n","import type {\n ReasonTraceAdapter,\n ReasonTraceAttributes,\n ReasonTraceSpan,\n ReasonTraceSpanStartArgs,\n} from \"@kortyx/hooks\";\nimport { context, SpanKind, trace } from \"@opentelemetry/api\";\nimport {\n applyAttributeMapping,\n startAttributes,\n toAttributes,\n} from \"./attributes\";\nimport { createSpanWrapper } from \"./span-wrapper\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport function createOpenTelemetryTraceAdapter(\n options: OpenTelemetryTraceAdapterOptions = {},\n): ReasonTraceAdapter {\n const tracer =\n options.tracer ??\n trace.getTracer(\n options.instrumentationName ?? \"kortyx\",\n options.instrumentationVersion,\n );\n\n const start = (args: ReasonTraceSpanStartArgs): ReasonTraceSpan => {\n const attributes = startAttributes(args, options);\n const span = tracer.startSpan(\n args.name,\n {\n kind: SpanKind.INTERNAL,\n attributes: spanStartOtelAttributes(args, attributes, options),\n },\n context.active(),\n );\n const spanContext = span.spanContext();\n options.onSpanStart?.({\n name: args.name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n attributes,\n });\n return createSpanWrapper(span, args.name, options);\n };\n\n return {\n startSpan: start,\n withSpan: async (args, fn) => {\n const attributes = startAttributes(args, options);\n return tracer.startActiveSpan(\n args.name,\n {\n kind: SpanKind.INTERNAL,\n attributes: spanStartOtelAttributes(args, attributes, options),\n },\n async (span) => {\n const wrapped = createSpanWrapper(span, args.name, options);\n const spanContext = span.spanContext();\n options.onSpanStart?.({\n name: args.name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n attributes,\n });\n try {\n const result = await fn(wrapped);\n if (!wrapped.ended) wrapped.end?.();\n return result;\n } catch (error) {\n wrapped.fail?.(error);\n throw error;\n }\n },\n );\n },\n getActiveContext: () => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const spanContext = span.spanContext();\n return {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n };\n },\n };\n}\n\nconst spanStartOtelAttributes = (\n args: ReasonTraceSpanStartArgs,\n attributes: ReasonTraceAttributes,\n options: OpenTelemetryTraceAdapterOptions,\n) =>\n toAttributes(\n applyAttributeMapping(args.name, attributes, options, {\n phase: \"start\",\n telemetry: args.telemetry,\n }),\n );\n","import type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport type ContentCaptureSide = \"input\" | \"output\";\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n\nexport const shouldCapture = (\n value: OpenTelemetryTraceAdapterOptions[\"captureContent\"],\n side: ContentCaptureSide,\n): boolean => {\n if (value === true) return true;\n if (!isRecord(value)) return false;\n return Boolean(value[side]);\n};\n","import type {\n KortyxTraceMetadata,\n ReasonTraceAttributes,\n ReasonTraceSpanEndArgs,\n ReasonTraceSpanStartArgs,\n} from \"@kortyx/hooks\";\nimport type { Attributes } from \"@opentelemetry/api\";\nimport { type ContentCaptureSide, shouldCapture } from \"./content\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\ntype OpenTelemetryAttributeValue =\n | string\n | number\n | boolean\n | string[]\n | number[]\n | boolean[];\n\nexport const toAttributeValue = (\n value: unknown,\n): OpenTelemetryAttributeValue | undefined => {\n if (\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\"\n ) {\n return value;\n }\n if (Array.isArray(value)) {\n if (value.every((item) => typeof item === \"string\")) {\n return value as string[];\n }\n if (value.every((item) => typeof item === \"number\")) {\n return value as number[];\n }\n if (value.every((item) => typeof item === \"boolean\")) {\n return value as boolean[];\n }\n }\n if (value === null || value === undefined) return undefined;\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n};\n\nexport const toAttributes = (attributes: ReasonTraceAttributes): Attributes => {\n const result: Attributes = {};\n for (const [key, value] of Object.entries(attributes)) {\n const attributeValue = toAttributeValue(value);\n if (attributeValue !== undefined) result[key] = attributeValue;\n }\n return result;\n};\n\nexport const normalizeKnownAttributes = (\n attributes: ReasonTraceAttributes,\n): ReasonTraceAttributes => {\n const normalized: ReasonTraceAttributes = { ...attributes };\n\n if (typeof attributes.providerId === \"string\") {\n normalized[\"gen_ai.provider.name\"] = attributes.providerId;\n normalized[\"kortyx.provider.id\"] = attributes.providerId;\n }\n if (typeof attributes.modelId === \"string\") {\n normalized[\"gen_ai.request.model\"] = attributes.modelId;\n normalized[\"kortyx.model.id\"] = attributes.modelId;\n }\n if (typeof attributes.stream === \"boolean\") {\n normalized[\"gen_ai.request.stream\"] = attributes.stream;\n }\n if (typeof attributes.operation === \"string\") {\n normalized[\"gen_ai.operation.name\"] = attributes.operation;\n normalized[\"kortyx.operation.name\"] = attributes.operation;\n }\n if (typeof attributes.nodeId === \"string\") {\n normalized[\"kortyx.node.id\"] = attributes.nodeId;\n }\n if (typeof attributes.workflowId === \"string\") {\n normalized[\"kortyx.workflow.id\"] = attributes.workflowId;\n }\n if (typeof attributes.runId === \"string\") {\n normalized[\"kortyx.run.id\"] = attributes.runId;\n }\n if (typeof attributes.sessionId === \"string\") {\n normalized[\"session.id\"] = attributes.sessionId;\n normalized[\"gen_ai.conversation.id\"] = attributes.sessionId;\n normalized[\"kortyx.session.id\"] = attributes.sessionId;\n }\n if (typeof attributes.userId === \"string\") {\n normalized[\"user.id\"] = attributes.userId;\n }\n if (typeof attributes.tenantId === \"string\") {\n normalized[\"kortyx.tenant.id\"] = attributes.tenantId;\n }\n if (typeof attributes.promptName === \"string\") {\n normalized[\"gen_ai.prompt.name\"] = attributes.promptName;\n normalized[\"kortyx.prompt.name\"] = attributes.promptName;\n }\n if (attributes.promptVersion !== undefined) {\n normalized[\"gen_ai.prompt.version\"] = attributes.promptVersion;\n normalized[\"kortyx.prompt.version\"] = attributes.promptVersion;\n }\n if (typeof attributes.promptType === \"string\") {\n normalized[\"gen_ai.prompt.type\"] = attributes.promptType;\n normalized[\"kortyx.prompt.type\"] = attributes.promptType;\n }\n if (typeof attributes.toolCallCount === \"number\") {\n normalized[\"kortyx.tool.call.count\"] = attributes.toolCallCount;\n }\n if (typeof attributes.toolResultCount === \"number\") {\n normalized[\"kortyx.tool.result.count\"] = attributes.toolResultCount;\n }\n if (typeof attributes.toolStepCount === \"number\") {\n normalized[\"kortyx.tool.step.count\"] = attributes.toolStepCount;\n }\n if (typeof attributes.tool === \"string\") {\n normalized[\"gen_ai.tool.name\"] = attributes.tool;\n normalized[\"kortyx.tool.name\"] = attributes.tool;\n }\n if (typeof attributes.toolCallId === \"string\") {\n normalized[\"kortyx.tool.call.id\"] = attributes.toolCallId;\n }\n\n return normalized;\n};\n\nexport const telemetryAttributes = (\n telemetry: KortyxTraceMetadata | undefined,\n options: OpenTelemetryTraceAdapterOptions,\n side?: ContentCaptureSide,\n): ReasonTraceAttributes => {\n const attributes: ReasonTraceAttributes = {};\n if (!telemetry) return attributes;\n\n if (telemetry.operation) {\n attributes.operation = telemetry.operation;\n attributes[\"gen_ai.operation.name\"] = telemetry.operation;\n }\n\n if (telemetry.prompt) {\n const prompt = telemetry.prompt;\n if (prompt.name) attributes[\"gen_ai.prompt.name\"] = prompt.name;\n if (prompt.name) attributes[\"kortyx.prompt.name\"] = prompt.name;\n if (prompt.version !== undefined) {\n attributes[\"gen_ai.prompt.version\"] = prompt.version;\n attributes[\"kortyx.prompt.version\"] = prompt.version;\n }\n if (prompt.type) attributes[\"gen_ai.prompt.type\"] = prompt.type;\n if (prompt.type) attributes[\"kortyx.prompt.type\"] = prompt.type;\n if (prompt.source) attributes[\"kortyx.prompt.source\"] = prompt.source;\n Object.assign(attributes, options.mapPromptMetadata?.(prompt) ?? {});\n }\n\n if (telemetry.tags?.length) {\n attributes[\"kortyx.trace.tags\"] = telemetry.tags;\n }\n\n if (telemetry.metadata) {\n for (const [key, value] of Object.entries(telemetry.metadata)) {\n attributes[`kortyx.trace.metadata.${key}`] = value;\n }\n }\n\n if (\n side === \"input\" &&\n shouldCapture(telemetry.captureContent ?? options.captureContent, \"input\")\n ) {\n attributes[\"gen_ai.prompt\"] = telemetry.input;\n attributes[\"kortyx.trace.input\"] = telemetry.input;\n }\n if (\n side === \"output\" &&\n shouldCapture(telemetry.captureContent ?? options.captureContent, \"output\")\n ) {\n attributes[\"gen_ai.completion\"] = telemetry.output;\n attributes[\"kortyx.trace.output\"] = telemetry.output;\n }\n\n return attributes;\n};\n\nexport const usageAttributes = (\n args: ReasonTraceSpanEndArgs | undefined,\n): ReasonTraceAttributes => {\n const usage = args?.usage;\n if (!usage) return {};\n return {\n ...(usage.input !== undefined\n ? { \"gen_ai.usage.input_tokens\": usage.input }\n : {}),\n ...(usage.output !== undefined\n ? { \"gen_ai.usage.output_tokens\": usage.output }\n : {}),\n ...(usage.total !== undefined\n ? { \"gen_ai.usage.total_tokens\": usage.total }\n : {}),\n ...(usage.reasoning !== undefined\n ? { \"gen_ai.usage.reasoning.output_tokens\": usage.reasoning }\n : {}),\n ...(usage.cacheRead !== undefined\n ? { \"gen_ai.usage.cache_read.input_tokens\": usage.cacheRead }\n : {}),\n ...(usage.cacheWrite !== undefined\n ? { \"gen_ai.usage.cache_creation.input_tokens\": usage.cacheWrite }\n : {}),\n ...(args.finishReason?.unified\n ? { \"gen_ai.response.finish_reasons\": [args.finishReason.unified] }\n : {}),\n ...(args.finishReason?.raw\n ? { \"kortyx.finish_reason.raw\": args.finishReason.raw }\n : {}),\n ...(args.warnings?.length\n ? { \"kortyx.warning.count\": args.warnings.length }\n : {}),\n };\n};\n\nexport const startAttributes = (\n args: ReasonTraceSpanStartArgs,\n options: OpenTelemetryTraceAdapterOptions,\n): ReasonTraceAttributes => ({\n ...(options.defaultAttributes ?? {}),\n ...(args.attributes ?? {}),\n ...telemetryAttributes(args.telemetry, options, \"input\"),\n});\n\nexport const applyAttributeMapping = (\n name: string,\n attributes: ReasonTraceAttributes,\n options: OpenTelemetryTraceAdapterOptions,\n args: Omit<\n Parameters<\n NonNullable<OpenTelemetryTraceAdapterOptions[\"mapAttributes\"]>\n >[0],\n \"name\" | \"attributes\"\n >,\n): ReasonTraceAttributes => {\n const mapped =\n options.mapAttributes?.({\n ...args,\n name,\n attributes,\n }) ?? {};\n return normalizeKnownAttributes({ ...attributes, ...mapped });\n};\n","import type { ReasonTraceSpan, ReasonTraceSpanEndArgs } from \"@kortyx/hooks\";\nimport { type Span, SpanStatusCode } from \"@opentelemetry/api\";\nimport {\n applyAttributeMapping,\n normalizeKnownAttributes,\n telemetryAttributes,\n toAttributes,\n usageAttributes,\n} from \"./attributes\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport type OpenTelemetryReasonSpan = ReasonTraceSpan & { ended: boolean };\n\nexport const createSpanWrapper = (\n span: Span,\n name: string,\n options: OpenTelemetryTraceAdapterOptions,\n): OpenTelemetryReasonSpan => {\n const wrapper: OpenTelemetryReasonSpan = {\n ended: false,\n setAttributes: (attributes) => {\n span.setAttributes(toAttributes(normalizeKnownAttributes(attributes)));\n },\n addEvent: (eventName, attributes) => {\n span.addEvent(\n eventName,\n toAttributes(\n applyAttributeMapping(name, attributes ?? {}, options, {\n phase: \"event\",\n }),\n ),\n );\n },\n end: (args) => {\n if (wrapper.ended) return;\n const attributes = spanEndAttributes(args, options);\n span.setAttributes(\n toAttributes(\n applyAttributeMapping(name, attributes, options, {\n phase: \"end\",\n telemetry: args?.telemetry,\n end: args,\n }),\n ),\n );\n wrapper.ended = true;\n span.end();\n const spanContext = span.spanContext();\n options.onSpanEnd?.({\n name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n });\n },\n fail: (error, args) => {\n const message = error instanceof Error ? error.message : String(error);\n span.recordException(error instanceof Error ? error : new Error(message));\n span.setStatus({ code: SpanStatusCode.ERROR, message });\n const attributes = spanErrorAttributes(error, args);\n span.setAttributes(\n toAttributes(\n applyAttributeMapping(name, attributes, options, {\n phase: \"error\",\n telemetry: args?.telemetry,\n end: args,\n error,\n }),\n ),\n );\n wrapper.end?.(args);\n },\n };\n return wrapper;\n};\n\nconst spanEndAttributes = (\n args: ReasonTraceSpanEndArgs | undefined,\n options: OpenTelemetryTraceAdapterOptions,\n) => ({\n ...(args?.attributes ?? {}),\n ...usageAttributes(args),\n ...telemetryAttributes(args?.telemetry, options, \"output\"),\n});\n\nconst spanErrorAttributes = (\n error: unknown,\n args: ReasonTraceSpanEndArgs | undefined,\n) => {\n const message = error instanceof Error ? error.message : String(error);\n return {\n ...(args?.attributes ?? {}),\n \"error.type\": error instanceof Error ? error.name : typeof error,\n \"error.message\": message,\n ...usageAttributes(args),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,cAAyC;;;ACFzC,IAAM,WAAW,CAAC,UAChB,QAAQ,KAAK,KAAK,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK;AAE9D,IAAM,gBAAgB,CAC3B,OACA,SACY;AACZ,MAAI,UAAU,KAAM,QAAO;AAC3B,MAAI,CAAC,SAAS,KAAK,EAAG,QAAO;AAC7B,SAAO,QAAQ,MAAM,IAAI,CAAC;AAC5B;;;ACIO,IAAM,mBAAmB,CAC9B,UAC4C;AAC5C,MACE,OAAO,UAAU,YACjB,OAAO,UAAU,YACjB,OAAO,UAAU,WACjB;AACA,WAAO;AAAA,EACT;AACA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AACnD,aAAO;AAAA,IACT;AACA,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AACnD,aAAO;AAAA,IACT;AACA,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,SAAS,GAA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Y;AAAA,YACrB,QAAQ,YAAY;AAAA,YACpB;AAAA,UACF,CAAC;AACD,cAAI;AACF,kBAAM,SAAS,MAAM,GAAG,OAAO;AAC/B,gBAAI,CAAC,QAAQ,MAAO,SAAQ,MAAM;AAClC,mBAAO;AAAA,UACT,SAAS,OAAO;AACd,oBAAQ,OAAO,KAAK;AACpB,kBAAM;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,kBAAkB,MAAM;AACtB,YAAM,OAAO,kBAAM,cAAc;AACjC,UAAI,CAAC,KAAM,QAAO;AAClB,YAAM,cAAc,KAAK,YAAY;AACrC,aAAO;AAAA,QACL,SAAS,YAAY;AAAA,QACrB,QAAQ,YAAY;AAAA,MACtB;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,0BAA0B,CAC9B,MACA,YACA,YAEA;AAAA,EACE,sBAAsB,KAAK,MAAM,YAAY,SAAS;AAAA,IACpD,OAAO;AAAA,IACP,WAAW,KAAK;AAAA,EAClB,CAAC;AACH;","names":["import_api"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/adapter.ts","../src/content.ts","../src/attributes.ts","../src/span-wrapper.ts"],"sourcesContent":["export { createOpenTelemetryTraceAdapter } from \"./adapter\";\nexport type {\n OpenTelemetryMapAttributesArgs,\n OpenTelemetrySpanEndInfo,\n OpenTelemetrySpanStartInfo,\n OpenTelemetryTraceAdapterOptions,\n OpenTelemetryTracePhase,\n} from \"./types\";\n","import type {\n ReasonTraceAdapter,\n ReasonTraceAttributes,\n ReasonTraceSpan,\n ReasonTraceSpanStartArgs,\n} from \"@kortyx/hooks\";\nimport { context, SpanKind, trace } from \"@opentelemetry/api\";\nimport {\n applyAttributeMapping,\n startAttributes,\n toAttributes,\n} from \"./attributes\";\nimport { createSpanWrapper } from \"./span-wrapper\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport function createOpenTelemetryTraceAdapter(\n options: OpenTelemetryTraceAdapterOptions = {},\n): ReasonTraceAdapter {\n const tracer =\n options.tracer ??\n trace.getTracer(\n options.instrumentationName ?? \"kortyx\",\n options.instrumentationVersion,\n );\n\n const start = (args: ReasonTraceSpanStartArgs): ReasonTraceSpan => {\n const attributes = startAttributes(args, options);\n const span = tracer.startSpan(\n args.name,\n {\n kind: SpanKind.INTERNAL,\n attributes: spanStartOtelAttributes(args, attributes, options),\n },\n context.active(),\n );\n const spanContext = span.spanContext();\n options.onSpanStart?.({\n name: args.name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n attributes,\n });\n return createSpanWrapper(span, args.name, options);\n };\n\n return {\n startSpan: start,\n withSpan: async (args, fn) => {\n const attributes = startAttributes(args, options);\n return tracer.startActiveSpan(\n args.name,\n {\n kind: SpanKind.INTERNAL,\n attributes: spanStartOtelAttributes(args, attributes, options),\n },\n async (span) => {\n const wrapped = createSpanWrapper(span, args.name, options);\n const spanContext = span.spanContext();\n options.onSpanStart?.({\n name: args.name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n attributes,\n });\n try {\n const result = await fn(wrapped);\n if (!wrapped.ended) wrapped.end?.();\n return result;\n } catch (error) {\n try {\n wrapped.fail?.(error);\n } catch {\n /* Preserve the callback failure. */\n }\n throw error;\n }\n },\n );\n },\n getActiveContext: () => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const spanContext = span.spanContext();\n return {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n };\n },\n };\n}\n\nconst spanStartOtelAttributes = (\n args: ReasonTraceSpanStartArgs,\n attributes: ReasonTraceAttributes,\n options: OpenTelemetryTraceAdapterOptions,\n) =>\n toAttributes(\n applyAttributeMapping(args.name, attributes, options, {\n phase: \"start\",\n telemetry: args.telemetry,\n }),\n );\n","import type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport type ContentCaptureSide = \"input\" | \"output\";\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n\nexport const shouldCapture = (\n value: OpenTelemetryTraceAdapterOptions[\"captureContent\"],\n side: ContentCaptureSide,\n): boolean => {\n if (value === true) return true;\n if (!isRecord(value)) return false;\n return Boolean(value[side]);\n};\n","import type {\n KortyxTraceMetadata,\n ReasonTraceAttributes,\n ReasonTraceSpanEndArgs,\n ReasonTraceSpanStartArgs,\n} from \"@kortyx/hooks\";\nimport type { Attributes } from \"@opentelemetry/api\";\nimport { type ContentCaptureSide, shouldCapture } from \"./content\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\ntype OpenTelemetryAttributeValue =\n | string\n | number\n | boolean\n | string[]\n | number[]\n | boolean[];\n\nexport const toAttributeValue = (\n value: unknown,\n): OpenTelemetryAttributeValue | undefined => {\n if (\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\"\n ) {\n return value;\n }\n if (Array.isArray(value)) {\n if (value.every((item) => typeof item === \"string\")) {\n return value as string[];\n }\n if (value.every((item) => typeof item === \"number\")) {\n return value as number[];\n }\n if (value.every((item) => typeof item === \"boolean\")) {\n return value as boolean[];\n }\n }\n if (value === null || value === undefined) return undefined;\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n};\n\nexport const toAttributes = (attributes: ReasonTraceAttributes): Attributes => {\n const result: Attributes = {};\n for (const [key, value] of Object.entries(attributes)) {\n const attributeValue = toAttributeValue(value);\n if (attributeValue !== undefined) result[key] = attributeValue;\n }\n return result;\n};\n\nexport const normalizeKnownAttributes = (\n attributes: ReasonTraceAttributes,\n): ReasonTraceAttributes => {\n const normalized: ReasonTraceAttributes = { ...attributes };\n\n if (typeof attributes.providerId === \"string\") {\n normalized[\"gen_ai.provider.name\"] = attributes.providerId;\n normalized[\"kortyx.provider.id\"] = attributes.providerId;\n }\n if (typeof attributes.modelId === \"string\") {\n normalized[\"gen_ai.request.model\"] = attributes.modelId;\n normalized[\"kortyx.model.id\"] = attributes.modelId;\n }\n if (typeof attributes.stream === \"boolean\") {\n normalized[\"gen_ai.request.stream\"] = attributes.stream;\n }\n if (typeof attributes.operation === \"string\") {\n normalized[\"gen_ai.operation.name\"] = attributes.operation;\n normalized[\"kortyx.operation.name\"] = attributes.operation;\n }\n if (typeof attributes.nodeId === \"string\") {\n normalized[\"kortyx.node.id\"] = attributes.nodeId;\n }\n if (typeof attributes.workflowId === \"string\") {\n normalized[\"kortyx.workflow.id\"] = attributes.workflowId;\n }\n if (typeof attributes.runId === \"string\") {\n normalized[\"kortyx.run.id\"] = attributes.runId;\n }\n if (typeof attributes.sessionId === \"string\") {\n normalized[\"session.id\"] = attributes.sessionId;\n normalized[\"gen_ai.conversation.id\"] = attributes.sessionId;\n normalized[\"kortyx.session.id\"] = attributes.sessionId;\n }\n if (typeof attributes.userId === \"string\") {\n normalized[\"user.id\"] = attributes.userId;\n }\n if (typeof attributes.tenantId === \"string\") {\n normalized[\"kortyx.tenant.id\"] = attributes.tenantId;\n }\n if (typeof attributes.promptName === \"string\") {\n normalized[\"gen_ai.prompt.name\"] = attributes.promptName;\n normalized[\"kortyx.prompt.name\"] = attributes.promptName;\n }\n if (attributes.promptVersion !== undefined) {\n normalized[\"gen_ai.prompt.version\"] = attributes.promptVersion;\n normalized[\"kortyx.prompt.version\"] = attributes.promptVersion;\n }\n if (typeof attributes.promptType === \"string\") {\n normalized[\"gen_ai.prompt.type\"] = attributes.promptType;\n normalized[\"kortyx.prompt.type\"] = attributes.promptType;\n }\n if (typeof attributes.toolCallCount === \"number\") {\n normalized[\"kortyx.tool.call.count\"] = attributes.toolCallCount;\n }\n if (typeof attributes.toolResultCount === \"number\") {\n normalized[\"kortyx.tool.result.count\"] = attributes.toolResultCount;\n }\n if (typeof attributes.toolStepCount === \"number\") {\n normalized[\"kortyx.tool.step.count\"] = attributes.toolStepCount;\n }\n if (typeof attributes.tool === \"string\") {\n normalized[\"gen_ai.tool.name\"] = attributes.tool;\n normalized[\"kortyx.tool.name\"] = attributes.tool;\n }\n if (typeof attributes.toolCallId === \"string\") {\n normalized[\"kortyx.tool.call.id\"] = attributes.toolCallId;\n }\n\n return normalized;\n};\n\nexport const telemetryAttributes = (\n telemetry: KortyxTraceMetadata | undefined,\n options: OpenTelemetryTraceAdapterOptions,\n side?: ContentCaptureSide,\n): ReasonTraceAttributes => {\n const attributes: ReasonTraceAttributes = {};\n if (!telemetry) return attributes;\n\n if (telemetry.operation) {\n attributes.operation = telemetry.operation;\n attributes[\"gen_ai.operation.name\"] = telemetry.operation;\n }\n\n if (telemetry.prompt) {\n const prompt = telemetry.prompt;\n if (prompt.name) attributes[\"gen_ai.prompt.name\"] = prompt.name;\n if (prompt.name) attributes[\"kortyx.prompt.name\"] = prompt.name;\n if (prompt.version !== undefined) {\n attributes[\"gen_ai.prompt.version\"] = prompt.version;\n attributes[\"kortyx.prompt.version\"] = prompt.version;\n }\n if (prompt.type) attributes[\"gen_ai.prompt.type\"] = prompt.type;\n if (prompt.type) attributes[\"kortyx.prompt.type\"] = prompt.type;\n if (prompt.source) attributes[\"kortyx.prompt.source\"] = prompt.source;\n Object.assign(attributes, options.mapPromptMetadata?.(prompt) ?? {});\n }\n\n if (telemetry.tags?.length) {\n attributes[\"kortyx.trace.tags\"] = telemetry.tags;\n }\n\n if (telemetry.metadata) {\n for (const [key, value] of Object.entries(telemetry.metadata)) {\n attributes[`kortyx.trace.metadata.${key}`] = value;\n }\n }\n\n if (\n side === \"input\" &&\n shouldCapture(telemetry.captureContent ?? options.captureContent, \"input\")\n ) {\n attributes[\"gen_ai.prompt\"] = telemetry.input;\n attributes[\"kortyx.trace.input\"] = telemetry.input;\n }\n if (\n side === \"output\" &&\n shouldCapture(telemetry.captureContent ?? options.captureContent, \"output\")\n ) {\n attributes[\"gen_ai.completion\"] = telemetry.output;\n attributes[\"kortyx.trace.output\"] = telemetry.output;\n }\n\n return attributes;\n};\n\nexport const usageAttributes = (\n args: ReasonTraceSpanEndArgs | undefined,\n): ReasonTraceAttributes => {\n const usage = args?.usage;\n if (!usage) return {};\n return {\n ...(usage.input !== undefined\n ? { \"gen_ai.usage.input_tokens\": usage.input }\n : {}),\n ...(usage.output !== undefined\n ? { \"gen_ai.usage.output_tokens\": usage.output }\n : {}),\n ...(usage.total !== undefined\n ? { \"gen_ai.usage.total_tokens\": usage.total }\n : {}),\n ...(usage.reasoning !== undefined\n ? { \"gen_ai.usage.reasoning.output_tokens\": usage.reasoning }\n : {}),\n ...(usage.cacheRead !== undefined\n ? { \"gen_ai.usage.cache_read.input_tokens\": usage.cacheRead }\n : {}),\n ...(usage.cacheWrite !== undefined\n ? { \"gen_ai.usage.cache_creation.input_tokens\": usage.cacheWrite }\n : {}),\n ...(args.finishReason?.unified\n ? { \"gen_ai.response.finish_reasons\": [args.finishReason.unified] }\n : {}),\n ...(args.finishReason?.raw\n ? { \"kortyx.finish_reason.raw\": args.finishReason.raw }\n : {}),\n ...(args.warnings?.length\n ? { \"kortyx.warning.count\": args.warnings.length }\n : {}),\n };\n};\n\nexport const startAttributes = (\n args: ReasonTraceSpanStartArgs,\n options: OpenTelemetryTraceAdapterOptions,\n): ReasonTraceAttributes => ({\n ...(options.defaultAttributes ?? {}),\n ...(args.attributes ?? {}),\n ...telemetryAttributes(args.telemetry, options, \"input\"),\n});\n\nexport const applyAttributeMapping = (\n name: string,\n attributes: ReasonTraceAttributes,\n options: OpenTelemetryTraceAdapterOptions,\n args: Omit<\n Parameters<\n NonNullable<OpenTelemetryTraceAdapterOptions[\"mapAttributes\"]>\n >[0],\n \"name\" | \"attributes\"\n >,\n): ReasonTraceAttributes => {\n const mapped =\n options.mapAttributes?.({\n ...args,\n name,\n attributes,\n }) ?? {};\n return normalizeKnownAttributes({ ...attributes, ...mapped });\n};\n","import { isControlFlowError, serializeFailure } from \"@kortyx/core/errors\";\nimport type { ReasonTraceSpan, ReasonTraceSpanEndArgs } from \"@kortyx/hooks\";\nimport { type Span, SpanStatusCode } from \"@opentelemetry/api\";\nimport {\n applyAttributeMapping,\n normalizeKnownAttributes,\n telemetryAttributes,\n toAttributes,\n usageAttributes,\n} from \"./attributes\";\nimport type { OpenTelemetryTraceAdapterOptions } from \"./types\";\n\nexport type OpenTelemetryReasonSpan = ReasonTraceSpan & { ended: boolean };\n\nexport const createSpanWrapper = (\n span: Span,\n name: string,\n options: OpenTelemetryTraceAdapterOptions,\n): OpenTelemetryReasonSpan => {\n const wrapper: OpenTelemetryReasonSpan = {\n ended: false,\n setAttributes: (attributes) => {\n span.setAttributes(toAttributes(normalizeKnownAttributes(attributes)));\n },\n addEvent: (eventName, attributes) => {\n span.addEvent(\n eventName,\n toAttributes(\n applyAttributeMapping(name, attributes ?? {}, options, {\n phase: \"event\",\n }),\n ),\n );\n },\n end: (args) => {\n if (wrapper.ended) return;\n const attributes = spanEndAttributes(args, options);\n span.setAttributes(\n toAttributes(\n applyAttributeMapping(name, attributes, options, {\n phase: \"end\",\n telemetry: args?.telemetry,\n end: args,\n }),\n ),\n );\n wrapper.ended = true;\n span.end();\n const spanContext = span.spanContext();\n options.onSpanEnd?.({\n name,\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n });\n },\n fail: (error, args) => {\n if (isControlFlowError(error)) {\n span.setAttribute(\"kortyx.control_flow\", true);\n wrapper.end?.(args);\n return;\n }\n const failure = serializeFailure(error);\n const message = failure.message;\n span.recordException({ name: failure.code, message });\n span.setStatus({ code: SpanStatusCode.ERROR, message });\n const attributes = spanErrorAttributes(error, args);\n span.setAttributes(\n toAttributes(\n applyAttributeMapping(name, attributes, options, {\n phase: \"error\",\n telemetry: args?.telemetry,\n end: args,\n error,\n }),\n ),\n );\n wrapper.end?.(args);\n },\n };\n return wrapper;\n};\n\nconst spanEndAttributes = (\n args: ReasonTraceSpanEndArgs | undefined,\n options: OpenTelemetryTraceAdapterOptions,\n) => ({\n ...(args?.attributes ?? {}),\n ...usageAttributes(args),\n ...telemetryAttributes(args?.telemetry, options, \"output\"),\n});\n\nconst spanErrorAttributes = (\n error: unknown,\n args: ReasonTraceSpanEndArgs | undefined,\n) => {\n const failure = serializeFailure(error);\n const message = failure.message;\n return {\n ...(args?.attributes ?? {}),\n \"error.type\": failure.code,\n \"kortyx.error.category\": failure.category,\n \"error.message\": message,\n ...usageAttributes(args),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,cAAyC;;;ACFzC,IAAM,WAAW,CAAC,UAChB,QAAQ,KAAK,KAAK,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK;AAE9D,IAAM,gBAAgB,CAC3B,OACA,SACY;AACZ,MAAI,UAAU,KAAM,QAAO;AAC3B,MAAI,CAAC,SAAS,KAAK,EAAG,QAAO;AAC7B,SAAO,QAAQ,MAAM,IAAI,CAAC;AAC5B;;;ACIO,IAAM,mBAAmB,CAC9B,UAC4C;AAC5C,MACE,OAAO,UAAU,YACjB,OAAO,UAAU,YACjB,OAAO,UAAU,WACjB;AACA,WAAO;AAAA,EACT;AACA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AACnD,aAAO;AAAA,IACT;AACA,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AACnD,aAAO;AAAA,IACT;AACA,QAAI,MAAM,MAAM,CAAC,SAAS,OAAO,SAAS,SAAS,GAAG;AACpD,aAAO;AAAA,IACT;AAAA,EACF;AACA,MAAI,UAAU,QAAQ,UAAU,OAAW,QAAO;AAClD,MAAI;AACF,WAAO,KAAK,UAAU,KAAK;AAAA,EAC7B,QAAQ;AACN,WAAO,OAAO,KAAK;AAAA,EACrB;AACF;AAEO,IAAM,eAAe,CAAC,eAAkD;AAC7E,QAAM,SAAqB,CAAC;AAC5B,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,UAAM,iBAAiB,iBAAiB,KAAK;AAC7C,QAAI,mBAAmB,OAAW,QAAO,GAAG,IAAI;AAAA,EAClD;A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AAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,IACA,kBAAkB,MAAM;AACtB,YAAM,OAAO,kBAAM,cAAc;AACjC,UAAI,CAAC,KAAM,QAAO;AAClB,YAAM,cAAc,KAAK,YAAY;AACrC,aAAO;AAAA,QACL,SAAS,YAAY;AAAA,QACrB,QAAQ,YAAY;AAAA,MACtB;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAM,0BAA0B,CAC9B,MACA,YACA,YAEA;AAAA,EACE,sBAAsB,KAAK,MAAM,YAAY,SAAS;AAAA,IACpD,OAAO;AAAA,IACP,WAAW,KAAK;AAAA,EAClB,CAAC;AACH;","names":["import_api"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kortyx/otel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "OpenTelemetry adapter for Kortyx tracing.",
|
|
6
6
|
"keywords": [
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"CHANGELOG.md"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@kortyx/hooks": "0.
|
|
35
|
+
"@kortyx/hooks": "0.25.0",
|
|
36
|
+
"@kortyx/core": "0.9.0"
|
|
36
37
|
},
|
|
37
38
|
"peerDependencies": {
|
|
38
39
|
"@opentelemetry/api": "^1.9.0"
|