@cloudbase/agent-observability 0.0.16 → 0.0.18
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 +20 -5
- package/dist/chunk-5EXUNUGP.mjs +60 -0
- package/dist/chunk-5EXUNUGP.mjs.map +1 -0
- package/dist/{chunk-ZGEMAYS4.mjs → chunk-AHSI4KTT.mjs} +399 -92
- package/dist/chunk-AHSI4KTT.mjs.map +1 -0
- package/dist/index.d.mts +369 -33
- package/dist/index.d.ts +369 -33
- package/dist/index.js +399 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +16 -5
- package/dist/langchain.d.mts +7 -5
- package/dist/langchain.d.ts +7 -5
- package/dist/langchain.js +421 -60
- package/dist/langchain.js.map +1 -1
- package/dist/langchain.mjs +31 -11
- package/dist/langchain.mjs.map +1 -1
- package/dist/server.d.mts +72 -6
- package/dist/server.d.ts +72 -6
- package/dist/server.js +129 -13
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +96 -13
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
- package/src/core/attributes.ts +256 -11
- package/src/core/constants.ts +14 -16
- package/src/core/spanWrapper.ts +34 -33
- package/src/core/trace-context.ts +469 -0
- package/src/core/tracerProvider.ts +1 -4
- package/src/index.ts +54 -40
- package/src/langchain/CallbackHandler.ts +48 -17
- package/src/langchain/index.ts +1 -1
- package/src/server/SingleLineConsoleSpanExporter.ts +141 -0
- package/src/server/config.ts +2 -4
- package/src/server/index.ts +9 -3
- package/src/server/setup.ts +30 -20
- package/src/types.ts +112 -10
- package/dist/chunk-ZGEMAYS4.mjs.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import * as _opentelemetry_api from '@opentelemetry/api';
|
|
2
|
-
import { Span, TimeInput, Attributes, TracerProvider, SpanContext } from '@opentelemetry/api';
|
|
2
|
+
import { Span, TimeInput, SpanStatus, Attributes, TracerProvider, Link, SpanContext } from '@opentelemetry/api';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Types of observations (spans) supported by
|
|
5
|
+
* Types of observations (spans) supported by observability.
|
|
6
6
|
*
|
|
7
7
|
* These types align with OpenInference semantic conventions:
|
|
8
8
|
* https://github.com/Arize-ai/openinference/tree/main/spec
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* All types map directly to OpenInference span kinds:
|
|
11
|
+
* - `chain`: Multi-step workflows (OpenInference: CHAIN) - **default for generic operations**
|
|
11
12
|
* - `llm`: Language model calls (OpenInference: LLM)
|
|
12
13
|
* - `embedding`: Text embedding operations (OpenInference: EMBEDDING)
|
|
13
14
|
* - `agent`: AI agent workflows (OpenInference: AGENT)
|
|
14
15
|
* - `tool`: Tool/function calls (OpenInference: TOOL)
|
|
15
|
-
* - `chain`: Multi-step workflows (OpenInference: CHAIN)
|
|
16
16
|
* - `retriever`: Document retrieval (OpenInference: RETRIEVER)
|
|
17
17
|
* - `reranker`: Result reranking (OpenInference: RERANKER)
|
|
18
18
|
* - `guardrail`: Safety checks (OpenInference: GUARDRAIL)
|
|
@@ -20,7 +20,7 @@ import { Span, TimeInput, Attributes, TracerProvider, SpanContext } from '@opent
|
|
|
20
20
|
*
|
|
21
21
|
* @public
|
|
22
22
|
*/
|
|
23
|
-
type ObservationType = "
|
|
23
|
+
type ObservationType = "chain" | "llm" | "embedding" | "agent" | "tool" | "retriever" | "reranker" | "guardrail" | "evaluator";
|
|
24
24
|
/**
|
|
25
25
|
* Severity levels for observations.
|
|
26
26
|
*
|
|
@@ -45,6 +45,69 @@ type TokenUsage = {
|
|
|
45
45
|
/** Total number of tokens */
|
|
46
46
|
totalTokens?: number;
|
|
47
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* LLM message for input/output tracking.
|
|
50
|
+
*
|
|
51
|
+
* Follows OpenInference semantic convention:
|
|
52
|
+
* - llm.input_messages.{i}.message.role
|
|
53
|
+
* - llm.input_messages.{i}.message.content
|
|
54
|
+
* - llm.output_messages.{i}.message.role
|
|
55
|
+
* - llm.output_messages.{i}.message.content
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
type LLMMessage = {
|
|
60
|
+
/** Message role (e.g., 'user', 'assistant', 'system', 'tool') */
|
|
61
|
+
role: string;
|
|
62
|
+
/** Message content */
|
|
63
|
+
content?: string;
|
|
64
|
+
/** Tool calls for assistant messages */
|
|
65
|
+
toolCalls?: ToolCall[];
|
|
66
|
+
/** Tool call ID for tool messages */
|
|
67
|
+
toolCallId?: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Tool call details.
|
|
71
|
+
*
|
|
72
|
+
* Follows OpenInference semantic convention:
|
|
73
|
+
* - tool_call.id
|
|
74
|
+
* - tool_call.function.name
|
|
75
|
+
* - tool_call.function.arguments
|
|
76
|
+
*
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
type ToolCall = {
|
|
80
|
+
/** Tool call ID */
|
|
81
|
+
id: string;
|
|
82
|
+
/** Function name */
|
|
83
|
+
function: {
|
|
84
|
+
/** Function name */
|
|
85
|
+
name: string;
|
|
86
|
+
/** Function arguments as JSON string */
|
|
87
|
+
arguments: string;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Document for retrieval tracking.
|
|
92
|
+
*
|
|
93
|
+
* Follows OpenInference semantic convention:
|
|
94
|
+
* - retrieval.documents.{i}.document.id
|
|
95
|
+
* - retrieval.documents.{i}.document.content
|
|
96
|
+
* - retrieval.documents.{i}.document.score
|
|
97
|
+
* - retrieval.documents.{i}.document.metadata
|
|
98
|
+
*
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
type Document = {
|
|
102
|
+
/** Document ID */
|
|
103
|
+
id?: string;
|
|
104
|
+
/** Document content */
|
|
105
|
+
content?: string;
|
|
106
|
+
/** Relevance score */
|
|
107
|
+
score?: number;
|
|
108
|
+
/** Document metadata */
|
|
109
|
+
metadata?: Record<string, unknown>;
|
|
110
|
+
};
|
|
48
111
|
/**
|
|
49
112
|
* Base attributes for all observation types.
|
|
50
113
|
*
|
|
@@ -69,13 +132,18 @@ type BaseSpanAttributes = {
|
|
|
69
132
|
version?: string;
|
|
70
133
|
/** Environment where the operation is running (e.g., 'production', 'staging') */
|
|
71
134
|
environment?: string;
|
|
135
|
+
/** Allow additional custom attributes (e.g., http.*, agui.*) */
|
|
136
|
+
[key: string]: unknown;
|
|
72
137
|
};
|
|
73
138
|
/**
|
|
74
139
|
* LLM-specific attributes following OpenInference semantic conventions.
|
|
75
140
|
*
|
|
76
141
|
* Uses attributes like:
|
|
77
142
|
* - llm.model_name
|
|
78
|
-
* - llm.
|
|
143
|
+
* - llm.system
|
|
144
|
+
* - llm.provider
|
|
145
|
+
* - llm.input_messages
|
|
146
|
+
* - llm.output_messages
|
|
79
147
|
* - llm.token_count.*
|
|
80
148
|
* - llm.invocation_parameters
|
|
81
149
|
*
|
|
@@ -86,10 +154,22 @@ type LLMAttributes = BaseSpanAttributes & {
|
|
|
86
154
|
completionStartTime?: Date;
|
|
87
155
|
/** Name of the language model (e.g., 'gpt-4', 'claude-3-opus') */
|
|
88
156
|
model?: string;
|
|
157
|
+
/** AI system/vendor identifier (e.g., 'openai', 'anthropic') */
|
|
158
|
+
system?: string;
|
|
159
|
+
/** Hosting provider (e.g., 'openai', 'azure', 'aws') */
|
|
160
|
+
provider?: string;
|
|
89
161
|
/** Parameters passed to the model (temperature, max_tokens, etc.) */
|
|
90
162
|
modelParameters?: Record<string, string | number>;
|
|
91
163
|
/** Token usage metrics */
|
|
92
164
|
usageDetails?: TokenUsage | Record<string, number>;
|
|
165
|
+
/** Input messages for chat completions */
|
|
166
|
+
inputMessages?: LLMMessage[];
|
|
167
|
+
/** Output messages from the model */
|
|
168
|
+
outputMessages?: LLMMessage[];
|
|
169
|
+
/** MIME type of input (e.g., 'application/json') */
|
|
170
|
+
inputMimeType?: string;
|
|
171
|
+
/** MIME type of output (e.g., 'application/json') */
|
|
172
|
+
outputMimeType?: string;
|
|
93
173
|
};
|
|
94
174
|
/**
|
|
95
175
|
* Embedding-specific attributes following OpenInference semantic conventions.
|
|
@@ -108,10 +188,22 @@ type EmbeddingAttributes = LLMAttributes;
|
|
|
108
188
|
* - tool.name
|
|
109
189
|
* - tool.description
|
|
110
190
|
* - tool.parameters
|
|
191
|
+
* - tool_call.id
|
|
192
|
+
* - tool_call.function.name
|
|
193
|
+
* - tool_call.function.arguments
|
|
111
194
|
*
|
|
112
195
|
* @public
|
|
113
196
|
*/
|
|
114
|
-
type ToolAttributes = BaseSpanAttributes
|
|
197
|
+
type ToolAttributes = BaseSpanAttributes & {
|
|
198
|
+
/** Tool name */
|
|
199
|
+
toolName?: string;
|
|
200
|
+
/** Tool description */
|
|
201
|
+
toolDescription?: string;
|
|
202
|
+
/** Tool parameters schema */
|
|
203
|
+
toolParameters?: Record<string, unknown>;
|
|
204
|
+
/** Tool call details */
|
|
205
|
+
toolCall?: ToolCall;
|
|
206
|
+
};
|
|
115
207
|
/**
|
|
116
208
|
* Agent-specific attributes following OpenInference semantic conventions.
|
|
117
209
|
*
|
|
@@ -121,7 +213,10 @@ type ToolAttributes = BaseSpanAttributes;
|
|
|
121
213
|
*
|
|
122
214
|
* @public
|
|
123
215
|
*/
|
|
124
|
-
type AgentAttributes = BaseSpanAttributes
|
|
216
|
+
type AgentAttributes = BaseSpanAttributes & {
|
|
217
|
+
/** Agent name */
|
|
218
|
+
agentName?: string;
|
|
219
|
+
};
|
|
125
220
|
/**
|
|
126
221
|
* Chain-specific attributes following OpenInference semantic conventions.
|
|
127
222
|
*
|
|
@@ -138,11 +233,16 @@ type ChainAttributes = BaseSpanAttributes;
|
|
|
138
233
|
* Uses attributes like:
|
|
139
234
|
* - retriever.name
|
|
140
235
|
* - retriever.query
|
|
141
|
-
* -
|
|
236
|
+
* - retrieval.documents
|
|
142
237
|
*
|
|
143
238
|
* @public
|
|
144
239
|
*/
|
|
145
|
-
type RetrieverAttributes = BaseSpanAttributes
|
|
240
|
+
type RetrieverAttributes = BaseSpanAttributes & {
|
|
241
|
+
/** Retrieved documents */
|
|
242
|
+
documents?: Document[];
|
|
243
|
+
/** Query used for retrieval */
|
|
244
|
+
query?: string;
|
|
245
|
+
};
|
|
146
246
|
/**
|
|
147
247
|
* Reranker-specific attributes following OpenInference semantic conventions.
|
|
148
248
|
*
|
|
@@ -214,7 +314,7 @@ type TraceAttributes = {
|
|
|
214
314
|
*
|
|
215
315
|
* @public
|
|
216
316
|
*/
|
|
217
|
-
type Observation =
|
|
317
|
+
type Observation = ObservationLLM | ObservationEmbedding | ObservationAgent | ObservationTool | ObservationChain | ObservationRetriever | ObservationReranker | ObservationEvaluator | ObservationGuardrail;
|
|
218
318
|
/**
|
|
219
319
|
* Parameters for creating an observation wrapper.
|
|
220
320
|
*
|
|
@@ -247,7 +347,7 @@ declare abstract class BaseObservation {
|
|
|
247
347
|
/** The trace ID from the OpenTelemetry span context */
|
|
248
348
|
traceId: string;
|
|
249
349
|
constructor(params: ObservationParams);
|
|
250
|
-
/** Gets the
|
|
350
|
+
/** Gets the OpenTelemetry tracer instance */
|
|
251
351
|
protected get tracer(): _opentelemetry_api.Tracer;
|
|
252
352
|
/**
|
|
253
353
|
* Ends the observation, marking it as complete.
|
|
@@ -255,6 +355,20 @@ declare abstract class BaseObservation {
|
|
|
255
355
|
* @param endTime - Optional end time, defaults to current time
|
|
256
356
|
*/
|
|
257
357
|
end(endTime?: TimeInput): void;
|
|
358
|
+
/**
|
|
359
|
+
* Sets the span status.
|
|
360
|
+
*
|
|
361
|
+
* @param status - The status to set on the span
|
|
362
|
+
*/
|
|
363
|
+
setStatus(status: SpanStatus): void;
|
|
364
|
+
/**
|
|
365
|
+
* Sets the span status to ERROR.
|
|
366
|
+
*
|
|
367
|
+
* Convenience method for marking the span as failed.
|
|
368
|
+
*
|
|
369
|
+
* @param message - Error description message
|
|
370
|
+
*/
|
|
371
|
+
setErrorStatus(message: string): void;
|
|
258
372
|
/**
|
|
259
373
|
* Updates the OTEL span attributes.
|
|
260
374
|
*
|
|
@@ -305,21 +419,8 @@ declare abstract class BaseObservation {
|
|
|
305
419
|
asType: "guardrail";
|
|
306
420
|
}): ObservationGuardrail;
|
|
307
421
|
startObservation(name: string, attributes?: BaseSpanAttributes, options?: {
|
|
308
|
-
asType?: "
|
|
309
|
-
}):
|
|
310
|
-
}
|
|
311
|
-
type ObservationSpanParams = {
|
|
312
|
-
otelSpan: Span;
|
|
313
|
-
attributes?: BaseSpanAttributes;
|
|
314
|
-
};
|
|
315
|
-
/**
|
|
316
|
-
* General-purpose observation for tracking operations.
|
|
317
|
-
*
|
|
318
|
-
* @public
|
|
319
|
-
*/
|
|
320
|
-
declare class ObservationSpan extends BaseObservation {
|
|
321
|
-
constructor(params: ObservationSpanParams);
|
|
322
|
-
update(attributes: BaseSpanAttributes): ObservationSpan;
|
|
422
|
+
asType?: "chain";
|
|
423
|
+
}): ObservationChain;
|
|
323
424
|
}
|
|
324
425
|
type ObservationLLMParams = {
|
|
325
426
|
otelSpan: Span;
|
|
@@ -468,7 +569,7 @@ declare function createTraceAttributes({ name, userId, sessionId, version, relea
|
|
|
468
569
|
* - Uses `openinference.span.kind` for span type
|
|
469
570
|
* - Uses `llm.*` for LLM-specific attributes
|
|
470
571
|
* - Uses `tool.*` for tool-specific attributes
|
|
471
|
-
* -
|
|
572
|
+
* - Uses `observation.*` for non-standard attributes
|
|
472
573
|
*
|
|
473
574
|
* @param type - Observation type (llm, tool, chain, etc.)
|
|
474
575
|
* @param attributes - Observation attributes to convert
|
|
@@ -541,6 +642,240 @@ declare function getTracerProvider(): TracerProvider;
|
|
|
541
642
|
*/
|
|
542
643
|
declare function getTracer(): _opentelemetry_api.Tracer;
|
|
543
644
|
|
|
645
|
+
/**
|
|
646
|
+
* Trace context extraction and validation utilities.
|
|
647
|
+
*
|
|
648
|
+
* This module provides utilities for extracting and validating W3C-compatible
|
|
649
|
+
* trace context from HTTP headers, supporting external trace inheritance.
|
|
650
|
+
*
|
|
651
|
+
* @packageDocumentation
|
|
652
|
+
*/
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Result of trace context validation.
|
|
656
|
+
*/
|
|
657
|
+
interface TraceContextValidation {
|
|
658
|
+
traceId?: string;
|
|
659
|
+
parentSpanId?: string;
|
|
660
|
+
errors: string[];
|
|
661
|
+
isValid: boolean;
|
|
662
|
+
hasTraceId: boolean;
|
|
663
|
+
hasParentSpanId: boolean;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Processed trace context result with all necessary data for span creation.
|
|
667
|
+
*
|
|
668
|
+
* This encapsulates the complete result of extracting, validating, and processing
|
|
669
|
+
* external trace context from HTTP headers.
|
|
670
|
+
*/
|
|
671
|
+
interface ProcessedTraceContext {
|
|
672
|
+
/** Validated trace ID (undefined if invalid or not provided) */
|
|
673
|
+
traceId?: string;
|
|
674
|
+
/** Validated parent span ID (undefined if invalid or not provided) */
|
|
675
|
+
parentSpanId?: string;
|
|
676
|
+
/** Whether external trace context was successfully inherited */
|
|
677
|
+
isInherited: boolean;
|
|
678
|
+
/** Whether parent span link was created */
|
|
679
|
+
hasParentLink: boolean;
|
|
680
|
+
/** Span attributes ready to merge into span attributes object */
|
|
681
|
+
spanAttributes: Record<string, any>;
|
|
682
|
+
/** Span links ready to pass to start_observation */
|
|
683
|
+
links: Link[];
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Validate W3C Trace Context trace-id format.
|
|
687
|
+
*
|
|
688
|
+
* Must be 32 hex characters (128-bit) and not all zeros.
|
|
689
|
+
*
|
|
690
|
+
* Performance: Uses parseInt() instead of regex for 45% faster validation
|
|
691
|
+
* and better handling of edge cases (extremely long strings).
|
|
692
|
+
*
|
|
693
|
+
* @param traceId - The trace ID string to validate
|
|
694
|
+
* @returns True if valid, false otherwise
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* ```typescript
|
|
698
|
+
* validateTraceId('7EBd0D2dd5A198C3Cc66a51EeEcdd0F4') // true
|
|
699
|
+
* validateTraceId('invalid') // false
|
|
700
|
+
* validateTraceId('00000000000000000000000000000000') // false
|
|
701
|
+
* ```
|
|
702
|
+
*
|
|
703
|
+
* @public
|
|
704
|
+
*/
|
|
705
|
+
declare function validateTraceId(traceId: string): boolean;
|
|
706
|
+
/**
|
|
707
|
+
* Validate W3C Trace Context span-id format.
|
|
708
|
+
*
|
|
709
|
+
* Must be 16 hex characters (64-bit) and not all zeros.
|
|
710
|
+
*
|
|
711
|
+
* Performance: Uses parseInt() instead of regex for 45% faster validation
|
|
712
|
+
* and better handling of edge cases (extremely long strings).
|
|
713
|
+
*
|
|
714
|
+
* @param spanId - The span ID string to validate
|
|
715
|
+
* @returns True if valid, false otherwise
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```typescript
|
|
719
|
+
* validateSpanId('00f067aa0ba902b7') // true
|
|
720
|
+
* validateSpanId('invalid') // false
|
|
721
|
+
* validateSpanId('0000000000000000') // false
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
* @public
|
|
725
|
+
*/
|
|
726
|
+
declare function validateSpanId(spanId: string): boolean;
|
|
727
|
+
/**
|
|
728
|
+
* Validate and normalize external trace context.
|
|
729
|
+
*
|
|
730
|
+
* Supports four scenarios:
|
|
731
|
+
* 1. Both missing → Valid (generate new trace)
|
|
732
|
+
* 2. Only traceId → Valid (inherit trace, new root span)
|
|
733
|
+
* 3. Both provided → Valid (inherit trace + parent link)
|
|
734
|
+
* 4. Only parentSpanId → Invalid (parent without trace)
|
|
735
|
+
*
|
|
736
|
+
* @param traceId - External trace ID from x-trace-id header
|
|
737
|
+
* @param parentSpanId - External parent span ID from x-parent-span-id header
|
|
738
|
+
* @returns Validation result with normalized values and any errors
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* ```typescript
|
|
742
|
+
* // Scenario 1: Generate new trace
|
|
743
|
+
* const result = validateTraceContext(undefined, undefined);
|
|
744
|
+
* result.isValid // true
|
|
745
|
+
* result.hasTraceId // false
|
|
746
|
+
*
|
|
747
|
+
* // Scenario 2: Inherit trace only
|
|
748
|
+
* const result = validateTraceContext('9bBab669fdD6eAEB3Ac0A522f5DeE0BF', undefined);
|
|
749
|
+
* result.isValid // true
|
|
750
|
+
* result.traceId // '9bbab669fdd6eaeb3ac0a522f5dee0bf'
|
|
751
|
+
*
|
|
752
|
+
* // Scenario 3: Inherit trace + parent
|
|
753
|
+
* const result = validateTraceContext(
|
|
754
|
+
* '9bBab669fdD6eAEB3Ac0A522f5DeE0BF',
|
|
755
|
+
* '00f067aa0ba902b7'
|
|
756
|
+
* );
|
|
757
|
+
* result.isValid // true
|
|
758
|
+
* result.hasParentSpanId // true
|
|
759
|
+
*
|
|
760
|
+
* // Scenario 4: Invalid - parent without trace
|
|
761
|
+
* const result = validateTraceContext(undefined, '00f067aa0ba902b7');
|
|
762
|
+
* result.isValid // false
|
|
763
|
+
* result.errors.length > 0 // true
|
|
764
|
+
* ```
|
|
765
|
+
*
|
|
766
|
+
* @public
|
|
767
|
+
*/
|
|
768
|
+
declare function validateTraceContext(traceId?: string, parentSpanId?: string): TraceContextValidation;
|
|
769
|
+
/**
|
|
770
|
+
* Create an OpenTelemetry Link to an external span.
|
|
771
|
+
*
|
|
772
|
+
* This creates a loose coupling to an external span, avoiding the
|
|
773
|
+
* "orphaned span" problem when the parent span data is not available
|
|
774
|
+
* in the current tracing backend.
|
|
775
|
+
*
|
|
776
|
+
* @param traceId - External trace ID (32 hex chars)
|
|
777
|
+
* @param parentSpanId - External parent span ID (16 hex chars)
|
|
778
|
+
* @param linkType - Semantic relationship type (default: "follows_from")
|
|
779
|
+
* @param sourceSystem - Name of the external system (default: "gateway")
|
|
780
|
+
* @returns OpenTelemetry Link object
|
|
781
|
+
*
|
|
782
|
+
* @example
|
|
783
|
+
* ```typescript
|
|
784
|
+
* const link = createSpanLinkFromContext(
|
|
785
|
+
* '9bBab669fdD6eAEB3Ac0A522f5DeE0BF',
|
|
786
|
+
* '00f067aa0ba902b7'
|
|
787
|
+
* );
|
|
788
|
+
* // Use link when creating span
|
|
789
|
+
* const span = tracer.startSpan('my-operation', { links: [link] });
|
|
790
|
+
* ```
|
|
791
|
+
*
|
|
792
|
+
* @public
|
|
793
|
+
*/
|
|
794
|
+
declare function createSpanLinkFromContext(traceId: string, parentSpanId: string, linkType?: string, sourceSystem?: string): Link;
|
|
795
|
+
/**
|
|
796
|
+
* Extract trace context from HTTP headers.
|
|
797
|
+
*
|
|
798
|
+
* Only supports custom x-trace-id and x-parent-span-id headers.
|
|
799
|
+
* Does not support W3C traceparent header.
|
|
800
|
+
*
|
|
801
|
+
* @param headers - HTTP headers (case-insensitive)
|
|
802
|
+
* @param traceIdHeader - Header name for trace ID (default: "x-trace-id")
|
|
803
|
+
* @param parentSpanIdHeader - Header name for parent span ID (default: "x-parent-span-id")
|
|
804
|
+
* @returns Tuple of [traceId, parentSpanId], both may be undefined
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```typescript
|
|
808
|
+
* const headers = new Headers({
|
|
809
|
+
* 'x-trace-id': '9bBab669fdD6eAEB3Ac0A522f5DeE0BF',
|
|
810
|
+
* 'x-parent-span-id': '00f067aa0ba902b7'
|
|
811
|
+
* });
|
|
812
|
+
* const [traceId, parentSpanId] = extractTraceContextFromHeaders(headers);
|
|
813
|
+
* ```
|
|
814
|
+
*
|
|
815
|
+
* @public
|
|
816
|
+
*/
|
|
817
|
+
declare function extractTraceContextFromHeaders(headers: Headers | Record<string, string>, traceIdHeader?: string, parentSpanIdHeader?: string): [string | undefined, string | undefined];
|
|
818
|
+
/**
|
|
819
|
+
* Extract, validate, and process trace context from HTTP headers.
|
|
820
|
+
*
|
|
821
|
+
* This is a high-level convenience method that combines extraction, validation,
|
|
822
|
+
* link creation, and attribute preparation into a single call. It handles all
|
|
823
|
+
* error cases and returns a ready-to-use ProcessedTraceContext object.
|
|
824
|
+
*
|
|
825
|
+
* @param headers - HTTP headers (case-insensitive)
|
|
826
|
+
* @param logger - Optional logger for validation errors
|
|
827
|
+
* @param linkType - Semantic relationship type for SpanLink (default: "follows_from")
|
|
828
|
+
* @param sourceSystem - Name of the external system (default: "gateway")
|
|
829
|
+
* @returns ProcessedTraceContext with all processed data ready for span creation
|
|
830
|
+
*
|
|
831
|
+
* @example
|
|
832
|
+
* ```typescript
|
|
833
|
+
* // Basic usage
|
|
834
|
+
* const result = processTraceContextFromHeaders(request.headers, logger);
|
|
835
|
+
* if (result.isInherited) {
|
|
836
|
+
* Object.assign(spanAttributes, result.spanAttributes);
|
|
837
|
+
* }
|
|
838
|
+
* const serverSpan = startObservation(
|
|
839
|
+
* "AG-UI.Server",
|
|
840
|
+
* spanAttributes,
|
|
841
|
+
* { links: result.links.length > 0 ? result.links : undefined }
|
|
842
|
+
* );
|
|
843
|
+
*
|
|
844
|
+
* // Scenario 1: No headers
|
|
845
|
+
* const result = processTraceContextFromHeaders(new Headers());
|
|
846
|
+
* result.isInherited // false
|
|
847
|
+
* result.links // []
|
|
848
|
+
*
|
|
849
|
+
* // Scenario 2: Only trace_id
|
|
850
|
+
* const headers = new Headers({ 'x-trace-id': '9bBab669fdD6eAEB3Ac0A522f5DeE0BF' });
|
|
851
|
+
* const result = processTraceContextFromHeaders(headers);
|
|
852
|
+
* result.isInherited // true
|
|
853
|
+
* result.hasParentLink // false
|
|
854
|
+
* result.spanAttributes['trace.external_trace_id'] // '7ebd0d2dd5a198c3cc66a51eeecdd0f4'
|
|
855
|
+
*
|
|
856
|
+
* // Scenario 3: Both headers
|
|
857
|
+
* const headers = new Headers({
|
|
858
|
+
* 'x-trace-id': '9bBab669fdD6eAEB3Ac0A522f5DeE0BF',
|
|
859
|
+
* 'x-parent-span-id': '00f067aa0ba902b7'
|
|
860
|
+
* });
|
|
861
|
+
* const result = processTraceContextFromHeaders(headers);
|
|
862
|
+
* result.isInherited // true
|
|
863
|
+
* result.hasParentLink // true
|
|
864
|
+
* result.links.length // 1
|
|
865
|
+
*
|
|
866
|
+
* // Scenario 4: Invalid (only parent_span_id)
|
|
867
|
+
* const headers = new Headers({ 'x-parent-span-id': '00f067aa0ba902b7' });
|
|
868
|
+
* const result = processTraceContextFromHeaders(headers);
|
|
869
|
+
* result.isInherited // false
|
|
870
|
+
* result.links // []
|
|
871
|
+
* ```
|
|
872
|
+
*
|
|
873
|
+
* @public
|
|
874
|
+
*/
|
|
875
|
+
declare function processTraceContextFromHeaders(headers: Headers | Record<string, string>, logger?: {
|
|
876
|
+
warn?: (msg: string) => void;
|
|
877
|
+
}, linkType?: string, sourceSystem?: string): ProcessedTraceContext;
|
|
878
|
+
|
|
544
879
|
/**
|
|
545
880
|
* Observability - OpenTelemetry-based tracing with OpenInference semantic conventions
|
|
546
881
|
*
|
|
@@ -557,6 +892,8 @@ type StartObservationOptions = {
|
|
|
557
892
|
startTime?: Date;
|
|
558
893
|
/** Parent span context to attach this observation to */
|
|
559
894
|
parentSpanContext?: SpanContext;
|
|
895
|
+
/** Optional links to external or related spans */
|
|
896
|
+
links?: Link[];
|
|
560
897
|
};
|
|
561
898
|
/**
|
|
562
899
|
* Options for startObservation function.
|
|
@@ -564,7 +901,7 @@ type StartObservationOptions = {
|
|
|
564
901
|
* @public
|
|
565
902
|
*/
|
|
566
903
|
type StartObservationOpts = StartObservationOptions & {
|
|
567
|
-
/** Type of observation to create. Defaults to '
|
|
904
|
+
/** Type of observation to create. Defaults to 'chain' */
|
|
568
905
|
asType?: ObservationType;
|
|
569
906
|
};
|
|
570
907
|
declare function startObservation(name: string, attributes?: BaseSpanAttributes, options?: StartObservationOpts & {
|
|
@@ -598,8 +935,8 @@ declare function startObservation(name: string, attributes: GuardrailAttributes,
|
|
|
598
935
|
asType: "guardrail";
|
|
599
936
|
}): ObservationGuardrail;
|
|
600
937
|
declare function startObservation(name: string, attributes?: BaseSpanAttributes, options?: StartObservationOpts & {
|
|
601
|
-
asType?: "
|
|
602
|
-
}):
|
|
938
|
+
asType?: "chain";
|
|
939
|
+
}): ObservationChain;
|
|
603
940
|
/**
|
|
604
941
|
* Updates the currently active trace with new attributes.
|
|
605
942
|
*
|
|
@@ -644,7 +981,6 @@ type StartActiveObservationOpts = StartObservationOpts & {
|
|
|
644
981
|
/** Whether to automatically end the observation when the function exits. Default: true */
|
|
645
982
|
endOnExit?: boolean;
|
|
646
983
|
};
|
|
647
|
-
declare function startActiveObservation<F extends (observation: ObservationSpan) => unknown>(name: string, fn: F, options?: StartActiveObservationOpts): ReturnType<F>;
|
|
648
984
|
declare function startActiveObservation<F extends (observation: ObservationLLM) => unknown>(name: string, fn: F, options?: StartActiveObservationOpts): ReturnType<F>;
|
|
649
985
|
declare function startActiveObservation<F extends (observation: ObservationEmbedding) => unknown>(name: string, fn: F, options?: StartActiveObservationOpts): ReturnType<F>;
|
|
650
986
|
declare function startActiveObservation<F extends (observation: ObservationAgent) => unknown>(name: string, fn: F, options?: StartActiveObservationOpts): ReturnType<F>;
|
|
@@ -725,4 +1061,4 @@ type ObserveOptions = Omit<StartObservationOpts, "name"> & {
|
|
|
725
1061
|
*/
|
|
726
1062
|
declare function observe<T extends (...args: any[]) => any>(fn: T, options?: ObserveOptions): T;
|
|
727
1063
|
|
|
728
|
-
export { type AgentAttributes, type BaseSpanAttributes, type ChainAttributes, type EmbeddingAttributes, type EvaluatorAttributes, type GuardrailAttributes, type LLMAttributes, type Observation, ObservationAgent, type ObservationAttributes, ObservationChain, ObservationEmbedding, ObservationEvaluator, ObservationGuardrail, ObservationLLM, type ObservationLevel, ObservationReranker, ObservationRetriever,
|
|
1064
|
+
export { type AgentAttributes, type BaseSpanAttributes, type ChainAttributes, type Document, type EmbeddingAttributes, type EvaluatorAttributes, type GuardrailAttributes, type LLMAttributes, type LLMMessage, type Observation, ObservationAgent, type ObservationAttributes, ObservationChain, ObservationEmbedding, ObservationEvaluator, ObservationGuardrail, ObservationLLM, type ObservationLevel, ObservationReranker, ObservationRetriever, ObservationTool, type ObservationType, type ObserveOptions, type ProcessedTraceContext, type RerankerAttributes, type RetrieverAttributes, type StartActiveObservationOpts, type StartObservationOptions, type StartObservationOpts, type ToolAttributes, type ToolCall, type TraceAttributes, type TraceContextValidation, createObservationAttributes, createSpanLinkFromContext, createTraceAttributes, extractTraceContextFromHeaders, getActiveSpanId, getActiveTraceId, getTracer, getTracerProvider, observe, processTraceContextFromHeaders, setTracerProvider, startActiveObservation, startObservation, updateActiveObservation, updateActiveTrace, validateSpanId, validateTraceContext, validateTraceId };
|