@cloudbase/agent-observability 1.0.1-alpha.19 → 1.0.1-alpha.23
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 +4 -4
- package/dist/{chunk-MUTOOEDR.mjs → chunk-DUKIGNZF.mjs} +50 -18
- package/dist/chunk-DUKIGNZF.mjs.map +1 -0
- package/dist/{chunk-WMJKH4XE.mjs → chunk-NFEGQTCC.mjs} +1 -8
- package/dist/{esm-M4BB7YBD.mjs → esm-PGEDANAI.mjs} +2 -2
- package/dist/index.d.mts +38 -5
- package/dist/index.d.ts +38 -5
- package/dist/index.js +58 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -2
- package/dist/langchain.d.mts +4 -4
- package/dist/langchain.d.ts +4 -4
- package/dist/langchain.js +56 -31
- package/dist/langchain.js.map +1 -1
- package/dist/langchain.mjs +3 -9
- package/dist/langchain.mjs.map +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +2 -2
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
- package/src/core/attributes.ts +14 -10
- package/src/core/constants.ts +14 -16
- package/src/core/spanWrapper.ts +25 -2
- package/src/core/tracerProvider.ts +1 -4
- package/src/index.ts +14 -2
- package/src/langchain/CallbackHandler.ts +5 -9
- package/src/server/config.ts +1 -1
- package/src/server/index.ts +1 -1
- package/src/server/setup.ts +1 -1
- package/src/types.ts +1 -1
- package/dist/chunk-MUTOOEDR.mjs.map +0 -1
- /package/dist/{chunk-WMJKH4XE.mjs.map → chunk-NFEGQTCC.mjs.map} +0 -0
- /package/dist/{esm-M4BB7YBD.mjs.map → esm-PGEDANAI.mjs.map} +0 -0
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./chunk-
|
|
1
|
+
import "./chunk-NFEGQTCC.mjs";
|
|
2
2
|
|
|
3
3
|
// src/server/SingleLineConsoleSpanExporter.ts
|
|
4
4
|
var SingleLineConsoleSpanExporter = class {
|
|
@@ -159,7 +159,7 @@ async function setupOTLPExporter(config) {
|
|
|
159
159
|
const { trace } = await import("@opentelemetry/api");
|
|
160
160
|
const { resourceFromAttributes } = await import("@opentelemetry/resources");
|
|
161
161
|
const { NodeTracerProvider } = await import("@opentelemetry/sdk-trace-node");
|
|
162
|
-
const { OTLPTraceExporter } = await import("./esm-
|
|
162
|
+
const { OTLPTraceExporter } = await import("./esm-PGEDANAI.mjs");
|
|
163
163
|
const { BatchSpanProcessor } = await import("@opentelemetry/sdk-trace-base");
|
|
164
164
|
const batchConfig = resolveBatchConfig(config.batch);
|
|
165
165
|
let provider = trace.getTracerProvider();
|
package/dist/server.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server/SingleLineConsoleSpanExporter.ts","../src/server/setup.ts","../src/server/config.ts"],"sourcesContent":["/**\n * Custom console exporter that outputs single-line JSON.\n *\n * This exporter outputs spans as single-line JSON for easier parsing\n * with line-based tools (grep, jq, etc.).\n *\n * To switch back to standard multi-line output, use ConsoleSpanExporter instead.\n *\n * @example\n * ```typescript\n * // Single-line (current default)\n * const exporter = new SingleLineConsoleSpanExporter();\n *\n * // Multi-line (standard OTel)\n * import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base';\n * const exporter = new ConsoleSpanExporter();\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';\n\n/**\n * Export result codes.\n */\nexport enum ExportResultCode {\n SUCCESS = 0,\n FAILED = 1,\n}\n\n/**\n * Export result interface.\n */\nexport interface ExportResult {\n code: ExportResultCode;\n error?: Error;\n}\n\n/**\n * Custom console exporter that outputs single-line JSON.\n *\n * This exporter outputs spans as single-line JSON for easier parsing\n * with line-based tools (grep, jq, etc.).\n */\nexport class SingleLineConsoleSpanExporter implements SpanExporter {\n /**\n * Export spans as single-line JSON.\n */\n export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void {\n try {\n for (const span of spans) {\n const spanDict = this.spanToDict(span);\n // Single-line JSON output\n const jsonLine = JSON.stringify(spanDict);\n console.log(jsonLine);\n }\n resultCallback({ code: ExportResultCode.SUCCESS });\n } catch (error) {\n resultCallback({ code: ExportResultCode.FAILED, error: error as Error });\n }\n }\n\n /**\n * Shutdown the exporter.\n */\n shutdown(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Force flush the exporter.\n */\n forceFlush?(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Convert a ReadableSpan to a dictionary for JSON serialization.\n */\n private spanToDict(span: ReadableSpan): Record<string, unknown> {\n const context = span.spanContext();\n\n // Get parent span ID from parentSpanContext if available\n // Some versions use parentSpanId directly, others use parentSpanContext.spanId\n const parentId = (span as any).parentSpanId || span.parentSpanContext?.spanId;\n\n return {\n name: span.name,\n context: {\n trace_id: context.traceId,\n span_id: context.spanId,\n trace_flags: context.traceFlags,\n },\n kind: span.kind,\n parent_id: parentId,\n start_time: span.startTime,\n end_time: span.endTime,\n status: {\n status_code: span.status.code,\n description: span.status.message,\n },\n attributes: span.attributes,\n events: span.events.map((event) => ({\n name: event.name,\n timestamp: event.time,\n attributes: event.attributes,\n })),\n links: span.links.map((link) => ({\n context: {\n trace_id: link.context.traceId,\n span_id: link.context.spanId,\n },\n attributes: link.attributes,\n })),\n resource: {\n attributes: span.resource.attributes,\n },\n };\n }\n}\n\n/**\n * Check if single-line console exporter should be used.\n *\n * Set CONSOLE_EXPORTER_SINGLE_LINE=false to use standard multi-line output.\n */\nexport function isSingleLineConsoleExporterEnabled(): boolean {\n const value = process.env.CONSOLE_EXPORTER_SINGLE_LINE?.toLowerCase() || 'true';\n return ['true', '1', 'yes', 'on'].includes(value);\n}\n","/**\n * Observability setup implementation for AG-Kit Server.\n *\n * Merges configuration from environment variables and parameters,\n * then applies each exporter configuration.\n *\n * @packageDocumentation\n */\n\nimport type {\n BatchConfig,\n ObservabilityConfig,\n ConsoleTraceConfig,\n OTLPTraceConfig,\n CustomTraceConfig,\n} from './config';\nimport {\n SingleLineConsoleSpanExporter,\n isSingleLineConsoleExporterEnabled,\n} from './SingleLineConsoleSpanExporter';\n\nexport type {\n BatchConfig,\n ObservabilityConfig,\n ConsoleTraceConfig,\n OTLPTraceConfig,\n CustomTraceConfig,\n} from './config';\nexport { SingleLineConsoleSpanExporter } from './SingleLineConsoleSpanExporter';\n\n/**\n * Environment variable truthy values.\n * Matches Python SDK implementation for consistency.\n */\nconst TRUTHY_ENV_VALUES = new Set(['true', '1', 'yes', 'on']);\n\n/**\n * Merged configuration result.\n * Console config allows merge (param overrides env),\n * while OTLP and custom configs are arrays (additive).\n */\ninterface MergedConfig {\n console?: ConsoleTraceConfig | null;\n otlp: OTLPTraceConfig[];\n custom: CustomTraceConfig[];\n}\n\n/**\n * Default batch configuration.\n */\nconst DEFAULT_BATCH_CONFIG: Required<BatchConfig> = {\n maxExportBatchSize: 100,\n scheduledDelayMillis: 5000,\n maxQueueSize: 2048,\n exportTimeoutMillis: 30000,\n};\n\n/**\n * Merge environment variable and parameter configurations.\n *\n * - AUTO_TRACES_STDOUT env adds a console config\n * - Parameter configs override/extend env configs\n */\nfunction mergeConfigs(paramConfigs: ObservabilityConfig[]): MergedConfig {\n const result: MergedConfig = {\n console: null,\n otlp: [],\n custom: [],\n };\n\n // 1. Check AUTO_TRACES_STDOUT env\n const autoTracesStdout = process.env.AUTO_TRACES_STDOUT?.toLowerCase() || '';\n if (TRUTHY_ENV_VALUES.has(autoTracesStdout)) {\n result.console = { type: 'console' };\n console.debug(\n `[Observability] AUTO_TRACES_STDOUT=${autoTracesStdout}, console exporter enabled`\n );\n }\n\n // 2. Process parameter configs\n for (const config of paramConfigs) {\n switch (config.type) {\n case 'console':\n // Parameter overrides env (merge batch config)\n result.console = { ...result.console, ...config };\n break;\n\n case 'otlp':\n result.otlp.push(config);\n break;\n\n case 'custom':\n result.custom.push(config);\n break;\n }\n }\n\n return result;\n}\n\n/**\n * Apply batch configuration with defaults.\n */\nfunction resolveBatchConfig(batch?: BatchConfig): Required<BatchConfig> {\n return { ...DEFAULT_BATCH_CONFIG, ...batch };\n}\n\n/**\n * Safe wrapper for exporter setup functions.\n * Ensures individual exporter failures don't crash the entire setup.\n */\nasync function safeSetup(\n name: string,\n setupFn: () => Promise<void>\n): Promise<void> {\n try {\n await setupFn();\n } catch (error) {\n console.warn(\n `[Observability] ${name} setup failed (non-fatal): ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n // Don't rethrow - allow other exporters to continue\n }\n}\n\n/**\n * Setup console exporter.\n *\n * Uses SingleLineConsoleSpanExporter by default for easier parsing with line-based tools.\n * Set CONSOLE_EXPORTER_SINGLE_LINE=false to use standard multi-line output.\n */\nasync function setupConsoleExporter(config: ConsoleTraceConfig): Promise<void> {\n const { trace } = await import('@opentelemetry/api');\n const { resourceFromAttributes } = await import('@opentelemetry/resources');\n const { NodeTracerProvider } = await import('@opentelemetry/sdk-trace-node');\n const { ConsoleSpanExporter, BatchSpanProcessor } = await import(\n '@opentelemetry/sdk-trace-base'\n );\n\n const batchConfig = resolveBatchConfig(config.batch);\n\n // Choose exporter type based on CONSOLE_EXPORTER_SINGLE_LINE env var\n // Single-line: easier for parsing with line-based tools (grep, jq, etc.)\n // Multi-line: more human-readable for debugging\n const useSingleLine = isSingleLineConsoleExporterEnabled();\n const exporter = useSingleLine\n ? new SingleLineConsoleSpanExporter()\n : new ConsoleSpanExporter();\n const exporterType = useSingleLine ? 'single-line' : 'multi-line';\n\n // Check if a real TracerProvider already exists\n let provider = trace.getTracerProvider();\n const isRealProvider = 'addSpanProcessor' in provider;\n\n if (isRealProvider) {\n // Add processor to existing provider\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n (provider as any).addSpanProcessor(processor);\n\n console.info(\n `[Observability] Console exporter configured (${exporterType}, batch=${batchConfig.maxExportBatchSize}, ` +\n `delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n } else {\n // Create new provider with console exporter\n const resource = resourceFromAttributes({\n 'service.name': process.env.OTEL_SERVICE_NAME || 'ag-ui-server',\n 'service.version': '1.0.0',\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n\n const tracerProvider = new NodeTracerProvider({\n resource,\n spanProcessors: [processor],\n });\n\n tracerProvider.register();\n\n console.info(\n `[Observability] Console exporter configured (${exporterType}, batch=${batchConfig.maxExportBatchSize}, ` +\n `delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n }\n}\n\n/**\n * Setup OTLP exporter.\n */\nasync function setupOTLPExporter(config: OTLPTraceConfig): Promise<void> {\n const { trace } = await import('@opentelemetry/api');\n const { resourceFromAttributes } = await import('@opentelemetry/resources');\n const { NodeTracerProvider } = await import('@opentelemetry/sdk-trace-node');\n const { OTLPTraceExporter } = await import('@opentelemetry/exporter-trace-otlp-http');\n const { BatchSpanProcessor } = await import('@opentelemetry/sdk-trace-base');\n\n const batchConfig = resolveBatchConfig(config.batch);\n\n // Check if a real TracerProvider already exists\n let provider = trace.getTracerProvider();\n const isRealProvider = 'addSpanProcessor' in provider;\n\n if (isRealProvider) {\n // Add processor to existing provider\n const exporter = new OTLPTraceExporter({\n url: config.url,\n headers: config.headers,\n timeoutMillis: config.timeout ?? 10000,\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n (provider as any).addSpanProcessor(processor);\n\n console.info(\n `[Observability] OTLP exporter configured (url=${config.url}, ` +\n `batch=${batchConfig.maxExportBatchSize}, delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n } else {\n // Create new provider with OTLP exporter\n const resource = resourceFromAttributes({\n 'service.name': process.env.OTEL_SERVICE_NAME || 'ag-ui-server',\n 'service.version': '1.0.0',\n });\n\n const exporter = new OTLPTraceExporter({\n url: config.url,\n headers: config.headers,\n timeoutMillis: config.timeout ?? 10000,\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n\n const tracerProvider = new NodeTracerProvider({\n resource,\n spanProcessors: [processor],\n });\n\n tracerProvider.register();\n\n console.info(\n `[Observability] OTLP exporter configured (url=${config.url}, ` +\n `batch=${batchConfig.maxExportBatchSize}, delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n }\n}\n\n/**\n * Setup custom exporter.\n */\nasync function setupCustomExporter(config: CustomTraceConfig): Promise<void> {\n await config.setup();\n console.info(`[Observability] Custom exporter setup completed`);\n}\n\n/**\n * Setup observability from merged configuration.\n *\n * @internal\n */\nasync function applyMergedConfigs(merged: MergedConfig): Promise<void> {\n const setupTasks: Promise<void>[] = [];\n\n // Apply console (non-blocking)\n if (merged.console) {\n setupTasks.push(safeSetup('Console exporter', () => setupConsoleExporter(merged.console!)));\n }\n\n // Apply otlp (non-blocking)\n for (const otlp of merged.otlp) {\n setupTasks.push(safeSetup(`OTLP exporter (${otlp.url})`, () => setupOTLPExporter(otlp)));\n }\n\n // Apply custom (non-blocking)\n for (const custom of merged.custom) {\n setupTasks.push(safeSetup('Custom exporter', () => setupCustomExporter(custom)));\n }\n\n // Wait for all exporters to complete (or fail gracefully)\n await Promise.all(setupTasks);\n\n if (merged.console || merged.otlp.length > 0 || merged.custom.length > 0) {\n console.info(`[Observability] Setup completed`);\n }\n}\n\n/**\n * Setup observability from configuration.\n *\n * Merges environment variable (AUTO_TRACES_STDOUT) with parameter configs,\n * then applies each exporter configuration.\n *\n * Environment variables act as presets, parameter configs override or extend.\n *\n * Returns a promise that resolves when setup is complete. This allows callers\n * to await initialization before proceeding, eliminating race conditions.\n *\n * The returned promise is cached - subsequent calls return the same promise\n * to avoid duplicate initialization.\n *\n * @param configs - Observability configuration(s)\n *\n * @example\n * ```typescript\n * // Console only (from env)\n * await setupObservability();\n *\n * // Console + OTLP\n * await setupObservability([\n * { type: 'console' },\n * { type: 'otlp', url: 'http://localhost:4318/v1/traces' }\n * ]);\n *\n * // OTLP only\n * await setupObservability({\n * type: 'otlp',\n * url: 'https://cloud.langfuse.com/api/public/otlp/v1/traces',\n * headers: { 'Authorization': 'Basic xxx' }\n * });\n * ```\n *\n * @public\n */\n\nlet setupPromise: Promise<void> | null = null;\n\nexport async function setupObservability(\n configs?: ObservabilityConfig | ObservabilityConfig[]\n): Promise<void> {\n // Return cached promise if setup is in progress or completed\n if (setupPromise) {\n return setupPromise;\n }\n\n // Create the setup promise\n setupPromise = (async () => {\n try {\n // Normalize to array\n const configsArray = configs\n ? Array.isArray(configs)\n ? configs\n : [configs]\n : [];\n\n // Merge env and parameter configs\n const merged = mergeConfigs(configsArray);\n\n // Apply merged configs\n await applyMergedConfigs(merged);\n } catch (error) {\n // Reset promise on error to allow retry\n setupPromise = null;\n // Silent failure - observability should never block main flow\n console.warn(\n `[Observability] Setup failed: ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n })();\n\n return setupPromise;\n}\n","/**\n * Observability configuration types for AG-Kit Server.\n *\n * Provides a unified configuration interface for trace exporters:\n * - Console: Development/debugging output\n * - OTLP: Production export to Langfuse, Jaeger, etc.\n * - Custom: User-defined setup logic\n *\n * @packageDocumentation\n */\n\n/**\n * Trace exporter type constants.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * { type: ExporterType.Console }\n * { type: ExporterType.OTLP }\n * { type: ExporterType.Custom }\n * ```\n *\n * @public\n */\nexport const ExporterType = {\n /** Console exporter - outputs traces to stdout */\n Console: 'console',\n /** OTLP exporter - sends traces to OTLP-compatible backend */\n OTLP: 'otlp',\n /** Custom exporter - user-defined setup logic */\n Custom: 'custom',\n} as const;\n\n/**\n * Trace exporter type literal values.\n *\n * @public\n */\nexport type ExporterType = typeof ExporterType[keyof typeof ExporterType];\n\n/**\n * Batch processing configuration for span exporters.\n *\n * Used by BatchSpanProcessor to optimize performance:\n * - Collects spans in memory and exports them in batches\n * - Reduces I/O operations (console) or network requests (OTLP)\n * - Recommended for production environments\n *\n * @public\n */\nexport interface BatchConfig {\n /** Maximum number of spans per export batch (default: 100) */\n maxExportBatchSize?: number;\n /** Maximum delay in milliseconds before exporting (default: 5000) */\n scheduledDelayMillis?: number;\n /** Maximum queue size (default: 2048) */\n maxQueueSize?: number;\n /** Export timeout in milliseconds (default: 30000) */\n exportTimeoutMillis?: number;\n}\n\n/**\n * Console trace exporter configuration.\n *\n * Outputs traces to stdout in JSON format using ConsoleSpanExporter.\n * Useful for development and debugging.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * { type: ExporterType.Console }\n * { type: ExporterType.Console, batch: { maxExportBatchSize: 200 } }\n * ```\n *\n * @public\n */\nexport interface ConsoleTraceConfig {\n /** Discriminator for console exporter */\n type: typeof ExporterType.Console;\n /** Optional batch processing configuration */\n batch?: BatchConfig;\n}\n\n/**\n * OTLP trace exporter configuration.\n *\n * Exports traces via OTLP protocol to any compatible backend:\n * - Langfuse: https://cloud.langfuse.com/api/public/otlp/v1/traces\n * - Jaeger: http://localhost:4318/v1/traces\n * - OTel Collector: custom endpoint\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * {\n * type: ExporterType.OTLP,\n * url: 'https://cloud.langfuse.com/api/public/otlp/v1/traces',\n * headers: {\n * 'Authorization': 'Basic ' + btoa('pk-lf-xxx:sk-lf-xxx')\n * }\n * }\n * ```\n *\n * @public\n */\nexport interface OTLPTraceConfig {\n /** Discriminator for OTLP exporter */\n type: typeof ExporterType.OTLP;\n /** OTLP endpoint URL (http/https) */\n url: string;\n /** Optional HTTP headers for authentication */\n headers?: Record<string, string>;\n /** Request timeout in milliseconds (default: 10000) */\n timeout?: number;\n /** Optional batch processing configuration */\n batch?: BatchConfig;\n}\n\n/**\n * Custom trace exporter configuration.\n *\n * Allows users to provide custom trace setup logic.\n * Useful for integrations not covered by console/otlp.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * {\n * type: ExporterType.Custom,\n * setup: async () => {\n * const exporter = new MyCustomExporter();\n * const provider = new BasicTracerProvider();\n * provider.addSpanProcessor(new SimpleSpanProcessor(exporter));\n * provider.register();\n * }\n * }\n * ```\n *\n * @public\n */\nexport interface CustomTraceConfig {\n /** Discriminator for custom setup */\n type: typeof ExporterType.Custom;\n /** User-defined setup function */\n setup: () => Promise<void> | void;\n}\n\n/**\n * Union type of all supported trace exporter configurations.\n *\n * @public\n */\nexport type ObservabilityConfig =\n | ConsoleTraceConfig\n | OTLPTraceConfig\n | CustomTraceConfig;\n"],"mappings":";;;AA6CO,IAAM,gCAAN,MAA4D;AAAA;AAAA;AAAA;AAAA,EAIjE,OAAO,OAAuB,gBAAsD;AAClF,QAAI;AACF,iBAAW,QAAQ,OAAO;AACxB,cAAM,WAAW,KAAK,WAAW,IAAI;AAErC,cAAM,WAAW,KAAK,UAAU,QAAQ;AACxC,gBAAQ,IAAI,QAAQ;AAAA,MACtB;AACA,qBAAe,EAAE,MAAM,gBAAyB,CAAC;AAAA,IACnD,SAAS,OAAO;AACd,qBAAe,EAAE,MAAM,gBAAyB,MAAsB,CAAC;AAAA,IACzE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,WAA0B;AACxB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKA,aAA6B;AAC3B,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKQ,WAAW,MAA6C;AAC9D,UAAM,UAAU,KAAK,YAAY;AAIjC,UAAM,WAAY,KAAa,gBAAgB,KAAK,mBAAmB;AAEvE,WAAO;AAAA,MACL,MAAM,KAAK;AAAA,MACX,SAAS;AAAA,QACP,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,aAAa,QAAQ;AAAA,MACvB;AAAA,MACA,MAAM,KAAK;AAAA,MACX,WAAW;AAAA,MACX,YAAY,KAAK;AAAA,MACjB,UAAU,KAAK;AAAA,MACf,QAAQ;AAAA,QACN,aAAa,KAAK,OAAO;AAAA,QACzB,aAAa,KAAK,OAAO;AAAA,MA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KAAK,CACvD;AAAA,MACF;AAAA,IACF;AAAA,EACF,GAAG;AAEH,SAAO;AACT;;;AClVO,IAAM,eAAe;AAAA;AAAA,EAE1B,SAAS;AAAA;AAAA,EAET,MAAM;AAAA;AAAA,EAEN,QAAQ;AACV;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/server/SingleLineConsoleSpanExporter.ts","../src/server/setup.ts","../src/server/config.ts"],"sourcesContent":["/**\n * Custom console exporter that outputs single-line JSON.\n *\n * This exporter outputs spans as single-line JSON for easier parsing\n * with line-based tools (grep, jq, etc.).\n *\n * To switch back to standard multi-line output, use ConsoleSpanExporter instead.\n *\n * @example\n * ```typescript\n * // Single-line (current default)\n * const exporter = new SingleLineConsoleSpanExporter();\n *\n * // Multi-line (standard OTel)\n * import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base';\n * const exporter = new ConsoleSpanExporter();\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';\n\n/**\n * Export result codes.\n */\nexport enum ExportResultCode {\n SUCCESS = 0,\n FAILED = 1,\n}\n\n/**\n * Export result interface.\n */\nexport interface ExportResult {\n code: ExportResultCode;\n error?: Error;\n}\n\n/**\n * Custom console exporter that outputs single-line JSON.\n *\n * This exporter outputs spans as single-line JSON for easier parsing\n * with line-based tools (grep, jq, etc.).\n */\nexport class SingleLineConsoleSpanExporter implements SpanExporter {\n /**\n * Export spans as single-line JSON.\n */\n export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void {\n try {\n for (const span of spans) {\n const spanDict = this.spanToDict(span);\n // Single-line JSON output\n const jsonLine = JSON.stringify(spanDict);\n console.log(jsonLine);\n }\n resultCallback({ code: ExportResultCode.SUCCESS });\n } catch (error) {\n resultCallback({ code: ExportResultCode.FAILED, error: error as Error });\n }\n }\n\n /**\n * Shutdown the exporter.\n */\n shutdown(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Force flush the exporter.\n */\n forceFlush?(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Convert a ReadableSpan to a dictionary for JSON serialization.\n */\n private spanToDict(span: ReadableSpan): Record<string, unknown> {\n const context = span.spanContext();\n\n // Get parent span ID from parentSpanContext if available\n // Some versions use parentSpanId directly, others use parentSpanContext.spanId\n const parentId = (span as any).parentSpanId || span.parentSpanContext?.spanId;\n\n return {\n name: span.name,\n context: {\n trace_id: context.traceId,\n span_id: context.spanId,\n trace_flags: context.traceFlags,\n },\n kind: span.kind,\n parent_id: parentId,\n start_time: span.startTime,\n end_time: span.endTime,\n status: {\n status_code: span.status.code,\n description: span.status.message,\n },\n attributes: span.attributes,\n events: span.events.map((event) => ({\n name: event.name,\n timestamp: event.time,\n attributes: event.attributes,\n })),\n links: span.links.map((link) => ({\n context: {\n trace_id: link.context.traceId,\n span_id: link.context.spanId,\n },\n attributes: link.attributes,\n })),\n resource: {\n attributes: span.resource.attributes,\n },\n };\n }\n}\n\n/**\n * Check if single-line console exporter should be used.\n *\n * Set CONSOLE_EXPORTER_SINGLE_LINE=false to use standard multi-line output.\n */\nexport function isSingleLineConsoleExporterEnabled(): boolean {\n const value = process.env.CONSOLE_EXPORTER_SINGLE_LINE?.toLowerCase() || 'true';\n return ['true', '1', 'yes', 'on'].includes(value);\n}\n","/**\n * Observability setup implementation for the server.\n *\n * Merges configuration from environment variables and parameters,\n * then applies each exporter configuration.\n *\n * @packageDocumentation\n */\n\nimport type {\n BatchConfig,\n ObservabilityConfig,\n ConsoleTraceConfig,\n OTLPTraceConfig,\n CustomTraceConfig,\n} from './config';\nimport {\n SingleLineConsoleSpanExporter,\n isSingleLineConsoleExporterEnabled,\n} from './SingleLineConsoleSpanExporter';\n\nexport type {\n BatchConfig,\n ObservabilityConfig,\n ConsoleTraceConfig,\n OTLPTraceConfig,\n CustomTraceConfig,\n} from './config';\nexport { SingleLineConsoleSpanExporter } from './SingleLineConsoleSpanExporter';\n\n/**\n * Environment variable truthy values.\n * Matches Python SDK implementation for consistency.\n */\nconst TRUTHY_ENV_VALUES = new Set(['true', '1', 'yes', 'on']);\n\n/**\n * Merged configuration result.\n * Console config allows merge (param overrides env),\n * while OTLP and custom configs are arrays (additive).\n */\ninterface MergedConfig {\n console?: ConsoleTraceConfig | null;\n otlp: OTLPTraceConfig[];\n custom: CustomTraceConfig[];\n}\n\n/**\n * Default batch configuration.\n */\nconst DEFAULT_BATCH_CONFIG: Required<BatchConfig> = {\n maxExportBatchSize: 100,\n scheduledDelayMillis: 5000,\n maxQueueSize: 2048,\n exportTimeoutMillis: 30000,\n};\n\n/**\n * Merge environment variable and parameter configurations.\n *\n * - AUTO_TRACES_STDOUT env adds a console config\n * - Parameter configs override/extend env configs\n */\nfunction mergeConfigs(paramConfigs: ObservabilityConfig[]): MergedConfig {\n const result: MergedConfig = {\n console: null,\n otlp: [],\n custom: [],\n };\n\n // 1. Check AUTO_TRACES_STDOUT env\n const autoTracesStdout = process.env.AUTO_TRACES_STDOUT?.toLowerCase() || '';\n if (TRUTHY_ENV_VALUES.has(autoTracesStdout)) {\n result.console = { type: 'console' };\n console.debug(\n `[Observability] AUTO_TRACES_STDOUT=${autoTracesStdout}, console exporter enabled`\n );\n }\n\n // 2. Process parameter configs\n for (const config of paramConfigs) {\n switch (config.type) {\n case 'console':\n // Parameter overrides env (merge batch config)\n result.console = { ...result.console, ...config };\n break;\n\n case 'otlp':\n result.otlp.push(config);\n break;\n\n case 'custom':\n result.custom.push(config);\n break;\n }\n }\n\n return result;\n}\n\n/**\n * Apply batch configuration with defaults.\n */\nfunction resolveBatchConfig(batch?: BatchConfig): Required<BatchConfig> {\n return { ...DEFAULT_BATCH_CONFIG, ...batch };\n}\n\n/**\n * Safe wrapper for exporter setup functions.\n * Ensures individual exporter failures don't crash the entire setup.\n */\nasync function safeSetup(\n name: string,\n setupFn: () => Promise<void>\n): Promise<void> {\n try {\n await setupFn();\n } catch (error) {\n console.warn(\n `[Observability] ${name} setup failed (non-fatal): ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n // Don't rethrow - allow other exporters to continue\n }\n}\n\n/**\n * Setup console exporter.\n *\n * Uses SingleLineConsoleSpanExporter by default for easier parsing with line-based tools.\n * Set CONSOLE_EXPORTER_SINGLE_LINE=false to use standard multi-line output.\n */\nasync function setupConsoleExporter(config: ConsoleTraceConfig): Promise<void> {\n const { trace } = await import('@opentelemetry/api');\n const { resourceFromAttributes } = await import('@opentelemetry/resources');\n const { NodeTracerProvider } = await import('@opentelemetry/sdk-trace-node');\n const { ConsoleSpanExporter, BatchSpanProcessor } = await import(\n '@opentelemetry/sdk-trace-base'\n );\n\n const batchConfig = resolveBatchConfig(config.batch);\n\n // Choose exporter type based on CONSOLE_EXPORTER_SINGLE_LINE env var\n // Single-line: easier for parsing with line-based tools (grep, jq, etc.)\n // Multi-line: more human-readable for debugging\n const useSingleLine = isSingleLineConsoleExporterEnabled();\n const exporter = useSingleLine\n ? new SingleLineConsoleSpanExporter()\n : new ConsoleSpanExporter();\n const exporterType = useSingleLine ? 'single-line' : 'multi-line';\n\n // Check if a real TracerProvider already exists\n let provider = trace.getTracerProvider();\n const isRealProvider = 'addSpanProcessor' in provider;\n\n if (isRealProvider) {\n // Add processor to existing provider\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n (provider as any).addSpanProcessor(processor);\n\n console.info(\n `[Observability] Console exporter configured (${exporterType}, batch=${batchConfig.maxExportBatchSize}, ` +\n `delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n } else {\n // Create new provider with console exporter\n const resource = resourceFromAttributes({\n 'service.name': process.env.OTEL_SERVICE_NAME || 'ag-ui-server',\n 'service.version': '1.0.0',\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n\n const tracerProvider = new NodeTracerProvider({\n resource,\n spanProcessors: [processor],\n });\n\n tracerProvider.register();\n\n console.info(\n `[Observability] Console exporter configured (${exporterType}, batch=${batchConfig.maxExportBatchSize}, ` +\n `delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n }\n}\n\n/**\n * Setup OTLP exporter.\n */\nasync function setupOTLPExporter(config: OTLPTraceConfig): Promise<void> {\n const { trace } = await import('@opentelemetry/api');\n const { resourceFromAttributes } = await import('@opentelemetry/resources');\n const { NodeTracerProvider } = await import('@opentelemetry/sdk-trace-node');\n const { OTLPTraceExporter } = await import('@opentelemetry/exporter-trace-otlp-http');\n const { BatchSpanProcessor } = await import('@opentelemetry/sdk-trace-base');\n\n const batchConfig = resolveBatchConfig(config.batch);\n\n // Check if a real TracerProvider already exists\n let provider = trace.getTracerProvider();\n const isRealProvider = 'addSpanProcessor' in provider;\n\n if (isRealProvider) {\n // Add processor to existing provider\n const exporter = new OTLPTraceExporter({\n url: config.url,\n headers: config.headers,\n timeoutMillis: config.timeout ?? 10000,\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n (provider as any).addSpanProcessor(processor);\n\n console.info(\n `[Observability] OTLP exporter configured (url=${config.url}, ` +\n `batch=${batchConfig.maxExportBatchSize}, delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n } else {\n // Create new provider with OTLP exporter\n const resource = resourceFromAttributes({\n 'service.name': process.env.OTEL_SERVICE_NAME || 'ag-ui-server',\n 'service.version': '1.0.0',\n });\n\n const exporter = new OTLPTraceExporter({\n url: config.url,\n headers: config.headers,\n timeoutMillis: config.timeout ?? 10000,\n });\n\n const processor = new BatchSpanProcessor(exporter, batchConfig);\n\n const tracerProvider = new NodeTracerProvider({\n resource,\n spanProcessors: [processor],\n });\n\n tracerProvider.register();\n\n console.info(\n `[Observability] OTLP exporter configured (url=${config.url}, ` +\n `batch=${batchConfig.maxExportBatchSize}, delay=${batchConfig.scheduledDelayMillis}ms)`\n );\n }\n}\n\n/**\n * Setup custom exporter.\n */\nasync function setupCustomExporter(config: CustomTraceConfig): Promise<void> {\n await config.setup();\n console.info(`[Observability] Custom exporter setup completed`);\n}\n\n/**\n * Setup observability from merged configuration.\n *\n * @internal\n */\nasync function applyMergedConfigs(merged: MergedConfig): Promise<void> {\n const setupTasks: Promise<void>[] = [];\n\n // Apply console (non-blocking)\n if (merged.console) {\n setupTasks.push(safeSetup('Console exporter', () => setupConsoleExporter(merged.console!)));\n }\n\n // Apply otlp (non-blocking)\n for (const otlp of merged.otlp) {\n setupTasks.push(safeSetup(`OTLP exporter (${otlp.url})`, () => setupOTLPExporter(otlp)));\n }\n\n // Apply custom (non-blocking)\n for (const custom of merged.custom) {\n setupTasks.push(safeSetup('Custom exporter', () => setupCustomExporter(custom)));\n }\n\n // Wait for all exporters to complete (or fail gracefully)\n await Promise.all(setupTasks);\n\n if (merged.console || merged.otlp.length > 0 || merged.custom.length > 0) {\n console.info(`[Observability] Setup completed`);\n }\n}\n\n/**\n * Setup observability from configuration.\n *\n * Merges environment variable (AUTO_TRACES_STDOUT) with parameter configs,\n * then applies each exporter configuration.\n *\n * Environment variables act as presets, parameter configs override or extend.\n *\n * Returns a promise that resolves when setup is complete. This allows callers\n * to await initialization before proceeding, eliminating race conditions.\n *\n * The returned promise is cached - subsequent calls return the same promise\n * to avoid duplicate initialization.\n *\n * @param configs - Observability configuration(s)\n *\n * @example\n * ```typescript\n * // Console only (from env)\n * await setupObservability();\n *\n * // Console + OTLP\n * await setupObservability([\n * { type: 'console' },\n * { type: 'otlp', url: 'http://localhost:4318/v1/traces' }\n * ]);\n *\n * // OTLP only\n * await setupObservability({\n * type: 'otlp',\n * url: 'https://cloud.langfuse.com/api/public/otlp/v1/traces',\n * headers: { 'Authorization': 'Basic xxx' }\n * });\n * ```\n *\n * @public\n */\n\nlet setupPromise: Promise<void> | null = null;\n\nexport async function setupObservability(\n configs?: ObservabilityConfig | ObservabilityConfig[]\n): Promise<void> {\n // Return cached promise if setup is in progress or completed\n if (setupPromise) {\n return setupPromise;\n }\n\n // Create the setup promise\n setupPromise = (async () => {\n try {\n // Normalize to array\n const configsArray = configs\n ? Array.isArray(configs)\n ? configs\n : [configs]\n : [];\n\n // Merge env and parameter configs\n const merged = mergeConfigs(configsArray);\n\n // Apply merged configs\n await applyMergedConfigs(merged);\n } catch (error) {\n // Reset promise on error to allow retry\n setupPromise = null;\n // Silent failure - observability should never block main flow\n console.warn(\n `[Observability] Setup failed: ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n })();\n\n return setupPromise;\n}\n","/**\n * Observability configuration types for the server.\n *\n * Provides a unified configuration interface for trace exporters:\n * - Console: Development/debugging output\n * - OTLP: Production export to Langfuse, Jaeger, etc.\n * - Custom: User-defined setup logic\n *\n * @packageDocumentation\n */\n\n/**\n * Trace exporter type constants.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * { type: ExporterType.Console }\n * { type: ExporterType.OTLP }\n * { type: ExporterType.Custom }\n * ```\n *\n * @public\n */\nexport const ExporterType = {\n /** Console exporter - outputs traces to stdout */\n Console: 'console',\n /** OTLP exporter - sends traces to OTLP-compatible backend */\n OTLP: 'otlp',\n /** Custom exporter - user-defined setup logic */\n Custom: 'custom',\n} as const;\n\n/**\n * Trace exporter type literal values.\n *\n * @public\n */\nexport type ExporterType = typeof ExporterType[keyof typeof ExporterType];\n\n/**\n * Batch processing configuration for span exporters.\n *\n * Used by BatchSpanProcessor to optimize performance:\n * - Collects spans in memory and exports them in batches\n * - Reduces I/O operations (console) or network requests (OTLP)\n * - Recommended for production environments\n *\n * @public\n */\nexport interface BatchConfig {\n /** Maximum number of spans per export batch (default: 100) */\n maxExportBatchSize?: number;\n /** Maximum delay in milliseconds before exporting (default: 5000) */\n scheduledDelayMillis?: number;\n /** Maximum queue size (default: 2048) */\n maxQueueSize?: number;\n /** Export timeout in milliseconds (default: 30000) */\n exportTimeoutMillis?: number;\n}\n\n/**\n * Console trace exporter configuration.\n *\n * Outputs traces to stdout in JSON format using ConsoleSpanExporter.\n * Useful for development and debugging.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * { type: ExporterType.Console }\n * { type: ExporterType.Console, batch: { maxExportBatchSize: 200 } }\n * ```\n *\n * @public\n */\nexport interface ConsoleTraceConfig {\n /** Discriminator for console exporter */\n type: typeof ExporterType.Console;\n /** Optional batch processing configuration */\n batch?: BatchConfig;\n}\n\n/**\n * OTLP trace exporter configuration.\n *\n * Exports traces via OTLP protocol to any compatible backend:\n * - Langfuse: https://cloud.langfuse.com/api/public/otlp/v1/traces\n * - Jaeger: http://localhost:4318/v1/traces\n * - OTel Collector: custom endpoint\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * {\n * type: ExporterType.OTLP,\n * url: 'https://cloud.langfuse.com/api/public/otlp/v1/traces',\n * headers: {\n * 'Authorization': 'Basic ' + btoa('pk-lf-xxx:sk-lf-xxx')\n * }\n * }\n * ```\n *\n * @public\n */\nexport interface OTLPTraceConfig {\n /** Discriminator for OTLP exporter */\n type: typeof ExporterType.OTLP;\n /** OTLP endpoint URL (http/https) */\n url: string;\n /** Optional HTTP headers for authentication */\n headers?: Record<string, string>;\n /** Request timeout in milliseconds (default: 10000) */\n timeout?: number;\n /** Optional batch processing configuration */\n batch?: BatchConfig;\n}\n\n/**\n * Custom trace exporter configuration.\n *\n * Allows users to provide custom trace setup logic.\n * Useful for integrations not covered by console/otlp.\n *\n * @example\n * ```typescript\n * import { ExporterType } from '@cloudbase/agent-observability/server';\n *\n * {\n * type: ExporterType.Custom,\n * setup: async () => {\n * const exporter = new MyCustomExporter();\n * const provider = new BasicTracerProvider();\n * provider.addSpanProcessor(new SimpleSpanProcessor(exporter));\n * provider.register();\n * }\n * }\n * ```\n *\n * @public\n */\nexport interface CustomTraceConfig {\n /** Discriminator for custom setup */\n type: typeof ExporterType.Custom;\n /** User-defined setup function */\n setup: () => Promise<void> | void;\n}\n\n/**\n * Union type of all supported trace exporter configurations.\n *\n * @public\n */\nexport type ObservabilityConfig =\n | ConsoleTraceConfig\n | OTLPTraceConfig\n | CustomTraceConfig;\n"],"mappings":";;;AA6CO,IAAM,gCAAN,MAA4D;AAAA;AAAA;AAAA;AAAA,EA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|
"description": "OpenInference-compatible observability for AG-Kit",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@arizeai/openinference-semantic-conventions": "^2.1.7",
|
|
66
66
|
"@opentelemetry/api": "^1.9.0",
|
|
67
67
|
"@opentelemetry/semantic-conventions": "^1.39.0",
|
|
68
|
-
"@cloudbase/agent-shared": "^1.0.1-alpha.
|
|
68
|
+
"@cloudbase/agent-shared": "^1.0.1-alpha.23"
|
|
69
69
|
},
|
|
70
70
|
"optionalDependencies": {
|
|
71
71
|
"@opentelemetry/exporter-trace-otlp-http": "^0.210.0",
|
package/src/core/attributes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OBSERVABILITY_TRACER_NAME, OtelSpanAttributes } from "./constants";
|
|
1
|
+
import { OBSERVABILITY_TRACER_NAME, OtelSpanAttributes, OpenInferenceSpanKind } from "./constants";
|
|
2
2
|
import { SemanticConventions } from "@arizeai/openinference-semantic-conventions";
|
|
3
3
|
import { ObservationAttributes, TraceAttributes, ObservationType, LLMMessage, Document, ToolCall } from "../types";
|
|
4
4
|
import { type Attributes } from "@opentelemetry/api";
|
|
@@ -64,7 +64,7 @@ export function createTraceAttributes({
|
|
|
64
64
|
* - Uses `openinference.span.kind` for span type
|
|
65
65
|
* - Uses `llm.*` for LLM-specific attributes
|
|
66
66
|
* - Uses `tool.*` for tool-specific attributes
|
|
67
|
-
* -
|
|
67
|
+
* - Uses `observation.*` for non-standard attributes
|
|
68
68
|
*
|
|
69
69
|
* @param type - Observation type (llm, tool, chain, etc.)
|
|
70
70
|
* @param attributes - Observation attributes to convert
|
|
@@ -89,20 +89,24 @@ export function createObservationAttributes(
|
|
|
89
89
|
usageDetails,
|
|
90
90
|
} = attributes;
|
|
91
91
|
|
|
92
|
+
// Map observation type to OpenInference span kind
|
|
93
|
+
// Falls back to UNKNOWN for unknown types (consistent with Python SDK)
|
|
94
|
+
const spanKind = (OpenInferenceSpanKind as Record<string, string>)[type.toUpperCase()] || "UNKNOWN";
|
|
95
|
+
|
|
92
96
|
// Base attributes for all observation types
|
|
93
97
|
const otelAttributes: Attributes = {
|
|
94
|
-
[SemanticConventions.OPENINFERENCE_SPAN_KIND]:
|
|
98
|
+
[SemanticConventions.OPENINFERENCE_SPAN_KIND]: spanKind,
|
|
95
99
|
[OtelSpanAttributes.OBSERVATION_TYPE]: type,
|
|
96
100
|
[OtelSpanAttributes.OBSERVATION_LEVEL]: level,
|
|
97
101
|
[OtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
|
|
98
102
|
[OtelSpanAttributes.VERSION]: version,
|
|
99
103
|
// Use OpenInference input.value convention
|
|
100
104
|
[SemanticConventions.INPUT_VALUE]: _serialize(input),
|
|
101
|
-
// Also set
|
|
105
|
+
// Also set observation.input for compatibility
|
|
102
106
|
[OtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
|
|
103
107
|
// Use OpenInference output.value convention
|
|
104
108
|
[SemanticConventions.OUTPUT_VALUE]: _serialize(output),
|
|
105
|
-
// Also set
|
|
109
|
+
// Also set observation.output for compatibility
|
|
106
110
|
[OtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output),
|
|
107
111
|
};
|
|
108
112
|
|
|
@@ -128,7 +132,7 @@ export function createObservationAttributes(
|
|
|
128
132
|
if (modelParameters) {
|
|
129
133
|
otelAttributes[SemanticConventions.LLM_INVOCATION_PARAMETERS] =
|
|
130
134
|
_serialize(modelParameters);
|
|
131
|
-
// Also set
|
|
135
|
+
// Also set llm.model_parameters for compatibility
|
|
132
136
|
otelAttributes[OtelSpanAttributes.LLM_MODEL_PARAMETERS] =
|
|
133
137
|
_serialize(modelParameters);
|
|
134
138
|
}
|
|
@@ -149,7 +153,7 @@ export function createObservationAttributes(
|
|
|
149
153
|
usage.totalTokens;
|
|
150
154
|
}
|
|
151
155
|
}
|
|
152
|
-
// Also set
|
|
156
|
+
// Also set llm.usage_details for compatibility
|
|
153
157
|
otelAttributes[OtelSpanAttributes.LLM_USAGE_DETAILS] =
|
|
154
158
|
_serialize(usageDetails);
|
|
155
159
|
}
|
|
@@ -238,7 +242,7 @@ export function createObservationAttributes(
|
|
|
238
242
|
);
|
|
239
243
|
Object.assign(otelAttributes, metadataAttrs);
|
|
240
244
|
|
|
241
|
-
// Also add
|
|
245
|
+
// Also add observation.metadata for compatibility
|
|
242
246
|
const obsetvabilityMetadataAttrs = _flattenAndSerializeMetadata(
|
|
243
247
|
metadata,
|
|
244
248
|
OtelSpanAttributes.OBSERVATION_METADATA
|
|
@@ -281,10 +285,10 @@ function _serialize(obj: unknown): string | undefined {
|
|
|
281
285
|
*
|
|
282
286
|
* Converts nested metadata objects into dot-notation attribute keys.
|
|
283
287
|
* For example, `{ database: { host: 'localhost' } }` becomes
|
|
284
|
-
* `{ 'metadata.database.host': 'localhost' }` (or '
|
|
288
|
+
* `{ 'metadata.database.host': 'localhost' }` (or 'observation.metadata.database.host').
|
|
285
289
|
*
|
|
286
290
|
* @param metadata - Metadata object to flatten
|
|
287
|
-
* @param prefix - Attribute prefix (e.g., 'metadata' or '
|
|
291
|
+
* @param prefix - Attribute prefix (e.g., 'metadata' or 'observation.metadata')
|
|
288
292
|
* @returns Flattened metadata attributes
|
|
289
293
|
* @internal
|
|
290
294
|
*/
|
package/src/core/constants.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OTEL attribute constants for
|
|
2
|
+
* OTEL attribute constants for observability.
|
|
3
3
|
*
|
|
4
4
|
* Uses OpenInference semantic conventions where applicable:
|
|
5
5
|
* https://github.com/Arize-ai/openinference/tree/main/spec
|
|
6
6
|
*
|
|
7
|
-
* Falls back to
|
|
7
|
+
* Falls back to non-standard attributes where OpenInference
|
|
8
8
|
* doesn't define a standard.
|
|
9
9
|
*
|
|
10
10
|
* @module
|
|
@@ -21,21 +21,23 @@ export { OpenInferenceSpanKind };
|
|
|
21
21
|
/**
|
|
22
22
|
* SDK information
|
|
23
23
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
// Brandless defaults: avoid emitting project-specific identifiers into traces.
|
|
25
|
+
// Users can still override service.name via OTEL_SERVICE_NAME.
|
|
26
|
+
export const OBSERVABILITY_TRACER_NAME = "agui-tracer";
|
|
27
|
+
export const OBSERVABILITY_SDK_NAME = "observability";
|
|
28
|
+
export const OBSERVABILITY_SDK_VERSION = "0.1.0";
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
31
|
* Combined attribute namespace for internal use
|
|
30
|
-
* Provides a single namespace for all OTEL attributes used
|
|
32
|
+
* Provides a single namespace for all OTEL attributes used internally.
|
|
31
33
|
*
|
|
32
|
-
* Combines OpenInference SemanticConventions with
|
|
34
|
+
* Combines OpenInference SemanticConventions with non-standard attributes
|
|
33
35
|
*/
|
|
34
36
|
export const OtelSpanAttributes = {
|
|
35
37
|
// OpenInference - re-export all standard conventions
|
|
36
38
|
...SemanticConventions,
|
|
37
39
|
|
|
38
|
-
//
|
|
40
|
+
// Trace attributes (non-standard)
|
|
39
41
|
TRACE_NAME: "trace.name",
|
|
40
42
|
TRACE_TAGS: "trace.tags",
|
|
41
43
|
TRACE_PUBLIC: "trace.public",
|
|
@@ -43,7 +45,7 @@ export const OtelSpanAttributes = {
|
|
|
43
45
|
TRACE_INPUT: "trace.input",
|
|
44
46
|
TRACE_OUTPUT: "trace.output",
|
|
45
47
|
|
|
46
|
-
//
|
|
48
|
+
// Observation attributes (non-standard)
|
|
47
49
|
OBSERVATION_TYPE: "observation.type",
|
|
48
50
|
OBSERVATION_LEVEL: "observation.level",
|
|
49
51
|
OBSERVATION_STATUS_MESSAGE: "observation.status_message",
|
|
@@ -51,25 +53,21 @@ export const OtelSpanAttributes = {
|
|
|
51
53
|
OBSERVATION_OUTPUT: "observation.output",
|
|
52
54
|
OBSERVATION_METADATA: "observation.metadata",
|
|
53
55
|
|
|
54
|
-
//
|
|
56
|
+
// LLM-specific (non-standard)
|
|
55
57
|
LLM_COMPLETION_START_TIME: "llm.completion_start_time",
|
|
56
58
|
LLM_MODEL_PARAMETERS: "llm.model_parameters",
|
|
57
59
|
LLM_USAGE_DETAILS: "llm.usage_details",
|
|
58
60
|
LLM_COST_DETAILS: "llm.cost_details",
|
|
59
61
|
|
|
60
|
-
//
|
|
62
|
+
// Retriever-specific (non-standard)
|
|
61
63
|
RETRIEVER_NAME: "retriever.name",
|
|
62
64
|
RETRIEVER_QUERY: "retriever.query",
|
|
63
65
|
RETRIEVER_INDEX_ID: "retriever.index_id",
|
|
64
66
|
RETRIEVER_TOP_K: "retriever.top_k",
|
|
65
67
|
|
|
66
|
-
//
|
|
68
|
+
// General (non-standard)
|
|
67
69
|
ENVIRONMENT: "environment",
|
|
68
70
|
RELEASE: "release",
|
|
69
71
|
VERSION: "version",
|
|
70
72
|
} as const;
|
|
71
73
|
|
|
72
|
-
/**
|
|
73
|
-
* Type for the OtelSpanAttributes object values
|
|
74
|
-
*/
|
|
75
|
-
export type OtelSpanAttributeValues = typeof OtelSpanAttributes[keyof typeof OtelSpanAttributes];
|
package/src/core/spanWrapper.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Span, TimeInput } from "@opentelemetry/api";
|
|
1
|
+
import { Span, TimeInput, SpanStatus, SpanStatusCode } from "@opentelemetry/api";
|
|
2
2
|
|
|
3
3
|
import { createObservationAttributes, createTraceAttributes } from "./attributes";
|
|
4
4
|
import { getTracer } from "./tracerProvider";
|
|
@@ -83,7 +83,7 @@ abstract class BaseObservation {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/** Gets the
|
|
86
|
+
/** Gets the OpenTelemetry tracer instance */
|
|
87
87
|
protected get tracer() {
|
|
88
88
|
return getTracer();
|
|
89
89
|
}
|
|
@@ -97,6 +97,29 @@ abstract class BaseObservation {
|
|
|
97
97
|
this.otelSpan.end(endTime);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Sets the span status.
|
|
102
|
+
*
|
|
103
|
+
* @param status - The status to set on the span
|
|
104
|
+
*/
|
|
105
|
+
public setStatus(status: SpanStatus) {
|
|
106
|
+
this.otelSpan.setStatus(status);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sets the span status to ERROR.
|
|
111
|
+
*
|
|
112
|
+
* Convenience method for marking the span as failed.
|
|
113
|
+
*
|
|
114
|
+
* @param message - Error description message
|
|
115
|
+
*/
|
|
116
|
+
public setErrorStatus(message: string) {
|
|
117
|
+
this.otelSpan.setStatus({
|
|
118
|
+
code: SpanStatusCode.ERROR,
|
|
119
|
+
message,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
100
123
|
/**
|
|
101
124
|
* Updates the OTEL span attributes.
|
|
102
125
|
*
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TracerProvider, trace, context } from "@opentelemetry/api";
|
|
2
|
+
import { OBSERVABILITY_SDK_NAME, OBSERVABILITY_SDK_VERSION } from "./constants";
|
|
2
3
|
|
|
3
4
|
const OBSERVABILITY_GLOBAL_SYMBOL = Symbol.for("observability");
|
|
4
5
|
|
|
@@ -130,7 +131,3 @@ export function getTracer() {
|
|
|
130
131
|
OBSERVABILITY_SDK_VERSION
|
|
131
132
|
);
|
|
132
133
|
}
|
|
133
|
-
|
|
134
|
-
// SDK version - could be read from package.json in production
|
|
135
|
-
const OBSERVABILITY_SDK_NAME = "ag-kit-observability";
|
|
136
|
-
const OBSERVABILITY_SDK_VERSION = "0.1.0";
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,11 @@ import {
|
|
|
24
24
|
type Observation,
|
|
25
25
|
} from "./core/spanWrapper";
|
|
26
26
|
import { getTracer } from "./core/tracerProvider";
|
|
27
|
+
import {
|
|
28
|
+
OBSERVABILITY_TRACER_NAME,
|
|
29
|
+
OBSERVABILITY_SDK_NAME,
|
|
30
|
+
OBSERVABILITY_SDK_VERSION,
|
|
31
|
+
} from "./core/constants";
|
|
27
32
|
import {
|
|
28
33
|
ObservationType,
|
|
29
34
|
ObservationLevel,
|
|
@@ -93,6 +98,13 @@ export {
|
|
|
93
98
|
getTracer,
|
|
94
99
|
} from "./core/tracerProvider";
|
|
95
100
|
|
|
101
|
+
// Export constants
|
|
102
|
+
export {
|
|
103
|
+
OBSERVABILITY_TRACER_NAME,
|
|
104
|
+
OBSERVABILITY_SDK_NAME,
|
|
105
|
+
OBSERVABILITY_SDK_VERSION,
|
|
106
|
+
} from "./core/constants";
|
|
107
|
+
|
|
96
108
|
/**
|
|
97
109
|
* Options for starting observations (spans).
|
|
98
110
|
*
|
|
@@ -116,7 +128,7 @@ export type StartObservationOpts = StartObservationOptions & {
|
|
|
116
128
|
};
|
|
117
129
|
|
|
118
130
|
/**
|
|
119
|
-
* Creates an OpenTelemetry span with the
|
|
131
|
+
* Creates an OpenTelemetry span with the tracer.
|
|
120
132
|
*
|
|
121
133
|
* @param params - Parameters for span creation
|
|
122
134
|
* @returns The created OpenTelemetry span
|
|
@@ -209,7 +221,7 @@ export function startObservation(
|
|
|
209
221
|
): ObservationSpan;
|
|
210
222
|
|
|
211
223
|
/**
|
|
212
|
-
* Creates and starts a new
|
|
224
|
+
* Creates and starts a new observation.
|
|
213
225
|
*
|
|
214
226
|
* Supports multiple observation types with full TypeScript type safety:
|
|
215
227
|
* - **span**: General-purpose operations (default)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* LangChain Callback Handler for
|
|
2
|
+
* LangChain Callback Handler for Observability
|
|
3
3
|
*
|
|
4
|
-
* Converts LangChain callback events into
|
|
4
|
+
* Converts LangChain callback events into observations with OpenInference semantics.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type { AgentAction, AgentFinish } from "@langchain/core/agents";
|
|
@@ -79,10 +79,10 @@ type PromptInfo = {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
* LangChain Callback Handler for
|
|
82
|
+
* LangChain Callback Handler for Observability.
|
|
83
83
|
*
|
|
84
84
|
* This handler intercepts LangChain callbacks and converts them into
|
|
85
|
-
*
|
|
85
|
+
* Observations following OpenInference semantic conventions.
|
|
86
86
|
*
|
|
87
87
|
* @public
|
|
88
88
|
*/
|
|
@@ -789,11 +789,7 @@ export class CallbackHandler extends BaseCallbackHandler {
|
|
|
789
789
|
const statusMessage = attributes.statusMessage as string | undefined;
|
|
790
790
|
if (level === "ERROR") {
|
|
791
791
|
try {
|
|
792
|
-
|
|
793
|
-
observation.otelSpan.setStatus({
|
|
794
|
-
code: SpanStatusCode.ERROR,
|
|
795
|
-
message: statusMessage || "Unknown error",
|
|
796
|
-
});
|
|
792
|
+
observation.setErrorStatus(statusMessage || "Unknown error");
|
|
797
793
|
} catch (e) {
|
|
798
794
|
this.logger.debug?.("Failed to set span error status:", e);
|
|
799
795
|
}
|
package/src/server/config.ts
CHANGED
package/src/server/index.ts
CHANGED
package/src/server/setup.ts
CHANGED
package/src/types.ts
CHANGED