@traccia2/sdk 0.0.1

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.
Files changed (130) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +503 -0
  3. package/dist/auto.d.ts +27 -0
  4. package/dist/auto.d.ts.map +1 -0
  5. package/dist/auto.js +171 -0
  6. package/dist/auto.js.map +1 -0
  7. package/dist/config/env-config.d.ts +21 -0
  8. package/dist/config/env-config.d.ts.map +1 -0
  9. package/dist/config/env-config.js +111 -0
  10. package/dist/config/env-config.js.map +1 -0
  11. package/dist/config/pricing-config.d.ts +27 -0
  12. package/dist/config/pricing-config.d.ts.map +1 -0
  13. package/dist/config/pricing-config.js +74 -0
  14. package/dist/config/pricing-config.js.map +1 -0
  15. package/dist/config/runtime-config.d.ts +65 -0
  16. package/dist/config/runtime-config.d.ts.map +1 -0
  17. package/dist/config/runtime-config.js +97 -0
  18. package/dist/config/runtime-config.js.map +1 -0
  19. package/dist/context/context.d.ts +29 -0
  20. package/dist/context/context.d.ts.map +1 -0
  21. package/dist/context/context.js +48 -0
  22. package/dist/context/context.js.map +1 -0
  23. package/dist/exporter/console-exporter.d.ts +18 -0
  24. package/dist/exporter/console-exporter.d.ts.map +1 -0
  25. package/dist/exporter/console-exporter.js +39 -0
  26. package/dist/exporter/console-exporter.js.map +1 -0
  27. package/dist/exporter/http-exporter.d.ts +57 -0
  28. package/dist/exporter/http-exporter.d.ts.map +1 -0
  29. package/dist/exporter/http-exporter.js +181 -0
  30. package/dist/exporter/http-exporter.js.map +1 -0
  31. package/dist/exporter/index.d.ts +7 -0
  32. package/dist/exporter/index.d.ts.map +1 -0
  33. package/dist/exporter/index.js +12 -0
  34. package/dist/exporter/index.js.map +1 -0
  35. package/dist/index.d.ts +10 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +32 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/integrations/index.d.ts +9 -0
  40. package/dist/integrations/index.d.ts.map +1 -0
  41. package/dist/integrations/index.js +16 -0
  42. package/dist/integrations/index.js.map +1 -0
  43. package/dist/integrations/langchain-callback.d.ts +72 -0
  44. package/dist/integrations/langchain-callback.d.ts.map +1 -0
  45. package/dist/integrations/langchain-callback.js +201 -0
  46. package/dist/integrations/langchain-callback.js.map +1 -0
  47. package/dist/integrations/langgraph-instrumentation.d.ts +57 -0
  48. package/dist/integrations/langgraph-instrumentation.d.ts.map +1 -0
  49. package/dist/integrations/langgraph-instrumentation.js +162 -0
  50. package/dist/integrations/langgraph-instrumentation.js.map +1 -0
  51. package/dist/processor/batch-processor.d.ts +68 -0
  52. package/dist/processor/batch-processor.d.ts.map +1 -0
  53. package/dist/processor/batch-processor.js +150 -0
  54. package/dist/processor/batch-processor.js.map +1 -0
  55. package/dist/processor/cost-processor.d.ts +16 -0
  56. package/dist/processor/cost-processor.d.ts.map +1 -0
  57. package/dist/processor/cost-processor.js +50 -0
  58. package/dist/processor/cost-processor.js.map +1 -0
  59. package/dist/processor/index.d.ts +9 -0
  60. package/dist/processor/index.d.ts.map +1 -0
  61. package/dist/processor/index.js +18 -0
  62. package/dist/processor/index.js.map +1 -0
  63. package/dist/processor/logging-processor.d.ts +13 -0
  64. package/dist/processor/logging-processor.d.ts.map +1 -0
  65. package/dist/processor/logging-processor.js +26 -0
  66. package/dist/processor/logging-processor.js.map +1 -0
  67. package/dist/processor/sampler.d.ts +20 -0
  68. package/dist/processor/sampler.d.ts.map +1 -0
  69. package/dist/processor/sampler.js +33 -0
  70. package/dist/processor/sampler.js.map +1 -0
  71. package/dist/processor/token-counter.d.ts +13 -0
  72. package/dist/processor/token-counter.d.ts.map +1 -0
  73. package/dist/processor/token-counter.js +40 -0
  74. package/dist/processor/token-counter.js.map +1 -0
  75. package/dist/tracer/index.d.ts +8 -0
  76. package/dist/tracer/index.d.ts.map +1 -0
  77. package/dist/tracer/index.js +15 -0
  78. package/dist/tracer/index.js.map +1 -0
  79. package/dist/tracer/provider.d.ts +59 -0
  80. package/dist/tracer/provider.d.ts.map +1 -0
  81. package/dist/tracer/provider.js +114 -0
  82. package/dist/tracer/provider.js.map +1 -0
  83. package/dist/tracer/span-context.d.ts +23 -0
  84. package/dist/tracer/span-context.d.ts.map +1 -0
  85. package/dist/tracer/span-context.js +34 -0
  86. package/dist/tracer/span-context.js.map +1 -0
  87. package/dist/tracer/span.d.ts +49 -0
  88. package/dist/tracer/span.d.ts.map +1 -0
  89. package/dist/tracer/span.js +118 -0
  90. package/dist/tracer/span.js.map +1 -0
  91. package/dist/tracer/tracer.d.ts +28 -0
  92. package/dist/tracer/tracer.d.ts.map +1 -0
  93. package/dist/tracer/tracer.js +75 -0
  94. package/dist/tracer/tracer.js.map +1 -0
  95. package/dist/types.d.ts +135 -0
  96. package/dist/types.d.ts.map +1 -0
  97. package/dist/types.js +16 -0
  98. package/dist/types.js.map +1 -0
  99. package/package.json +79 -0
  100. package/src/__tests__/exporter.test.ts +62 -0
  101. package/src/__tests__/integrations-langchain.test.ts +384 -0
  102. package/src/__tests__/integrations-langgraph.test.ts +479 -0
  103. package/src/__tests__/processor.test.ts +89 -0
  104. package/src/__tests__/span.test.ts +103 -0
  105. package/src/__tests__/tracer.test.ts +89 -0
  106. package/src/auto.ts +198 -0
  107. package/src/config/env-config.ts +93 -0
  108. package/src/config/pricing-config.ts +84 -0
  109. package/src/config/runtime-config.ts +108 -0
  110. package/src/context/context.ts +52 -0
  111. package/src/exporter/console-exporter.ts +38 -0
  112. package/src/exporter/http-exporter.ts +188 -0
  113. package/src/exporter/index.ts +7 -0
  114. package/src/index.ts +51 -0
  115. package/src/integrations/README.md +287 -0
  116. package/src/integrations/index.ts +13 -0
  117. package/src/integrations/langchain-callback.ts +229 -0
  118. package/src/integrations/langgraph-instrumentation.ts +174 -0
  119. package/src/processor/batch-processor.ts +180 -0
  120. package/src/processor/cost-processor.ts +57 -0
  121. package/src/processor/index.ts +9 -0
  122. package/src/processor/logging-processor.ts +26 -0
  123. package/src/processor/sampler.ts +35 -0
  124. package/src/processor/token-counter.ts +42 -0
  125. package/src/tracer/index.ts +8 -0
  126. package/src/tracer/provider.ts +130 -0
  127. package/src/tracer/span-context.ts +46 -0
  128. package/src/tracer/span.ts +145 -0
  129. package/src/tracer/tracer.ts +100 -0
  130. package/src/types.ts +155 -0
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Token counting processor for LLM spans.
3
+ */
4
+
5
+ import { ISpan, ISpanProcessor } from '../types';
6
+
7
+ const BYTES_PER_TOKEN = 4; // Rough approximation
8
+
9
+ /**
10
+ * Token counting processor.
11
+ */
12
+ export class TokenCountingProcessor implements ISpanProcessor {
13
+ onEnd(span: ISpan): void {
14
+ try {
15
+ // Estimate tokens from attributes if available
16
+ const promptText = span.attributes['prompt'] as string | undefined;
17
+ const completionText = span.attributes['completion'] as string | undefined;
18
+
19
+ if (promptText) {
20
+ const inputTokens = Math.ceil(promptText.length / BYTES_PER_TOKEN);
21
+ // Direct attribute modification for processors (after span ends)
22
+ span.attributes['input_tokens'] = inputTokens;
23
+ }
24
+
25
+ if (completionText) {
26
+ const outputTokens = Math.ceil(completionText.length / BYTES_PER_TOKEN);
27
+ // Direct attribute modification for processors (after span ends)
28
+ span.attributes['output_tokens'] = outputTokens;
29
+ }
30
+ } catch {
31
+ // Silently fail
32
+ }
33
+ }
34
+
35
+ async shutdown(): Promise<void> {
36
+ // No-op
37
+ }
38
+
39
+ async forceFlush(): Promise<void> {
40
+ // No-op
41
+ }
42
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Tracer module exports.
3
+ */
4
+
5
+ export { Span } from './span';
6
+ export { SpanContext } from './span-context';
7
+ export { Tracer } from './tracer';
8
+ export { TracerProvider } from './provider';
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Tracer provider for managing tracers and span processors.
3
+ */
4
+
5
+ import { randomBytes } from 'crypto';
6
+ import { ITracerProvider, ITracer, ISpanProcessor, ISampler, ISpan } from '../types';
7
+ import { Tracer } from './tracer';
8
+
9
+ /**
10
+ * TracerProvider manages tracers and span processors.
11
+ */
12
+ export class TracerProvider implements ITracerProvider {
13
+ private tracers: Map<string, Tracer> = new Map();
14
+ private spanProcessors: ISpanProcessor[] = [];
15
+ private sampler?: ISampler;
16
+ private resource: Record<string, string | number | boolean> = {};
17
+
18
+ constructor(resource?: Record<string, string | number | boolean>) {
19
+ this.resource = resource || {};
20
+ }
21
+
22
+ /**
23
+ * Get or create a tracer.
24
+ */
25
+ getTracer(name: string, version?: string): ITracer {
26
+ const key = version ? `${name}@${version}` : name;
27
+ let tracer = this.tracers.get(key);
28
+
29
+ if (!tracer) {
30
+ tracer = new Tracer(this, key);
31
+ this.tracers.set(key, tracer);
32
+ }
33
+
34
+ return tracer;
35
+ }
36
+
37
+ /**
38
+ * Add a span processor.
39
+ */
40
+ addSpanProcessor(processor: ISpanProcessor): void {
41
+ this.spanProcessors.push(processor);
42
+ }
43
+
44
+ /**
45
+ * Remove a span processor.
46
+ */
47
+ removeSpanProcessor(processor: ISpanProcessor): void {
48
+ const index = this.spanProcessors.indexOf(processor);
49
+ if (index > -1) {
50
+ this.spanProcessors.splice(index, 1);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Set the sampler.
56
+ */
57
+ setSampler(sampler: ISampler): void {
58
+ this.sampler = sampler;
59
+ }
60
+
61
+ /**
62
+ * Get the sampler.
63
+ */
64
+ getSampler(): ISampler | undefined {
65
+ return this.sampler;
66
+ }
67
+
68
+ /**
69
+ * Notify all processors that a span has ended.
70
+ */
71
+ notifySpanEnd(span: ISpan): void {
72
+ for (const processor of this.spanProcessors) {
73
+ try {
74
+ processor.onEnd(span);
75
+ } catch {
76
+ // Processors should not crash the tracing system
77
+ // Errors are silently swallowed for resilience
78
+ }
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Force flush all processors.
84
+ */
85
+ async forceFlush(timeout?: number): Promise<void> {
86
+ const promises: Promise<void>[] = [];
87
+
88
+ for (const processor of this.spanProcessors) {
89
+ const promise = Promise.resolve(processor.forceFlush(timeout));
90
+ promises.push(promise);
91
+ }
92
+
93
+ await Promise.all(promises);
94
+ }
95
+
96
+ /**
97
+ * Shutdown all processors.
98
+ */
99
+ async shutdown(): Promise<void> {
100
+ const promises: Promise<void>[] = [];
101
+
102
+ for (const processor of this.spanProcessors) {
103
+ const promise = Promise.resolve(processor.shutdown());
104
+ promises.push(promise);
105
+ }
106
+
107
+ await Promise.all(promises);
108
+ }
109
+
110
+ /**
111
+ * Generate a random trace ID.
112
+ */
113
+ generateTraceId(): string {
114
+ return randomBytes(16).toString('hex');
115
+ }
116
+
117
+ /**
118
+ * Generate a random span ID.
119
+ */
120
+ generateSpanId(): string {
121
+ return randomBytes(8).toString('hex');
122
+ }
123
+
124
+ /**
125
+ * Get the resource attributes.
126
+ */
127
+ getResource(): Record<string, string | number | boolean> {
128
+ return this.resource;
129
+ }
130
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Span context implementation.
3
+ */
4
+
5
+ import { ISpanContext } from '../types';
6
+
7
+ /**
8
+ * Implementation of span context carrying trace and span identification.
9
+ */
10
+ export class SpanContext implements ISpanContext {
11
+ public readonly traceId: string;
12
+ public readonly spanId: string;
13
+ public readonly traceFlags: number;
14
+ public readonly traceState?: string;
15
+
16
+ constructor(
17
+ traceId: string,
18
+ spanId: string,
19
+ traceFlags: number = 1,
20
+ traceState?: string
21
+ ) {
22
+ this.traceId = traceId;
23
+ this.spanId = spanId;
24
+ this.traceFlags = traceFlags;
25
+ this.traceState = traceState;
26
+ }
27
+
28
+ /**
29
+ * Check if this span context is valid.
30
+ */
31
+ isValid(): boolean {
32
+ return (
33
+ this.traceId.length > 0 &&
34
+ this.spanId.length > 0 &&
35
+ this.traceId !== '0'.repeat(32) &&
36
+ this.spanId !== '0'.repeat(16)
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Check if this context is sampled.
42
+ */
43
+ isSampled(): boolean {
44
+ return this.traceFlags === 1;
45
+ }
46
+ }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Span implementation with lifecycle management.
3
+ */
4
+
5
+ import { ISpan, ITracer, SpanStatus, SpanEvent, ISpanContext } from '../types';
6
+ import { SpanContext } from './span-context';
7
+ import { getConfig } from '../config/runtime-config';
8
+
9
+ /**
10
+ * Span implementation representing a unit of work.
11
+ */
12
+ export class Span implements ISpan {
13
+ public context: ISpanContext;
14
+ public name: string;
15
+ public parentSpanId?: string;
16
+ public attributes: Record<string, unknown>;
17
+ public events: SpanEvent[] = [];
18
+ public status: SpanStatus = SpanStatus.UNSET;
19
+ public statusDescription?: string;
20
+ public startTimeNs: number;
21
+ public endTimeNs?: number;
22
+
23
+ private ended = false;
24
+
25
+ constructor(
26
+ name: string,
27
+ _tracer: ITracer,
28
+ context: ISpanContext,
29
+ parentSpanId?: string,
30
+ attributes?: Record<string, unknown>
31
+ ) {
32
+ this.name = name;
33
+ this.context = context;
34
+ this.parentSpanId = parentSpanId;
35
+ this.attributes = attributes ? { ...attributes } : {};
36
+ this.startTimeNs = performance.now() * 1_000_000;
37
+
38
+ // Apply runtime metadata to tracestate
39
+ this.enrichTraceState();
40
+ }
41
+
42
+ /**
43
+ * Get the duration in nanoseconds.
44
+ */
45
+ get durationNs(): number | undefined {
46
+ if (this.endTimeNs === undefined) {
47
+ return undefined;
48
+ }
49
+ return this.endTimeNs - this.startTimeNs;
50
+ }
51
+
52
+ /**
53
+ * Set an attribute on the span.
54
+ */
55
+ setAttribute(key: string, value: unknown): void {
56
+ if (this.ended) {
57
+ return;
58
+ }
59
+ this.attributes[key] = value;
60
+ }
61
+
62
+ /**
63
+ * Add an event to the span.
64
+ */
65
+ addEvent(name: string, attributes?: Record<string, unknown>): void {
66
+ if (this.ended) {
67
+ return;
68
+ }
69
+ this.events.push({
70
+ name,
71
+ timestamp: performance.now() * 1_000_000,
72
+ attributes: attributes ? { ...attributes } : {},
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Record an exception on the span.
78
+ */
79
+ recordException(error: Error, attributes?: Record<string, unknown>): void {
80
+ if (this.ended) {
81
+ return;
82
+ }
83
+ this.status = SpanStatus.ERROR;
84
+ this.statusDescription = error.message;
85
+ this.addEvent('exception', {
86
+ ...attributes,
87
+ 'exception.type': error.name,
88
+ 'exception.message': error.message,
89
+ 'exception.stacktrace': error.stack,
90
+ });
91
+ }
92
+
93
+ /**
94
+ * End the span.
95
+ */
96
+ end(): void {
97
+ if (this.ended) {
98
+ return;
99
+ }
100
+ this.ended = true;
101
+ this.endTimeNs = performance.now() * 1_000_000;
102
+ }
103
+
104
+ /**
105
+ * Check if the span is still recording.
106
+ */
107
+ isRecording(): boolean {
108
+ return !this.ended;
109
+ }
110
+
111
+ /**
112
+ * Enrich the trace state with runtime metadata.
113
+ */
114
+ private enrichTraceState(): void {
115
+ try {
116
+ const config = getConfig();
117
+ let traceState = this.context.traceState || '';
118
+
119
+ const metadata: Record<string, string> = {};
120
+ if (config.tenantId) {
121
+ metadata['tenant'] = config.tenantId;
122
+ }
123
+ if (config.projectId) {
124
+ metadata['project'] = config.projectId;
125
+ }
126
+ if (config.debug) {
127
+ metadata['dbg'] = '1';
128
+ }
129
+
130
+ if (Object.keys(metadata).length > 0) {
131
+ const pairs = Object.entries(metadata).map(([k, v]) => `${k}=${v}`);
132
+ traceState = pairs.join(',');
133
+
134
+ this.context = new SpanContext(
135
+ this.context.traceId,
136
+ this.context.spanId,
137
+ this.context.traceFlags,
138
+ traceState
139
+ );
140
+ }
141
+ } catch {
142
+ // Silently fail on trace state enrichment
143
+ }
144
+ }
145
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Tracer implementation for creating and managing spans.
3
+ */
4
+
5
+ import { ITracer, ISpan, ISpanContext } from '../types';
6
+ import { Span } from './span';
7
+ import { SpanContext } from './span-context';
8
+ import { getCurrentSpan, runWithSpanAsync } from '../context/context';
9
+ import { TracerProvider } from './provider';
10
+ import { getConfig } from '../config/runtime-config';
11
+
12
+ /**
13
+ * Tracer for creating and managing spans.
14
+ */
15
+ export class Tracer implements ITracer {
16
+ private provider: TracerProvider;
17
+
18
+ constructor(provider: TracerProvider, _instrumentationScope: string) {
19
+ this.provider = provider;
20
+ }
21
+
22
+ /**
23
+ * Start a new span.
24
+ */
25
+ startSpan(
26
+ name: string,
27
+ options?: {
28
+ attributes?: Record<string, unknown>;
29
+ parent?: ISpan | null;
30
+ parentContext?: ISpanContext | null;
31
+ }
32
+ ): ISpan {
33
+ const parentSpan = options?.parent ?? getCurrentSpan();
34
+ const effectiveContext = options?.parentContext || (parentSpan?.context as ISpanContext);
35
+
36
+ let traceId: string;
37
+ let parentSpanId: string | undefined;
38
+ let traceFlags: number;
39
+
40
+ if (parentSpan) {
41
+ traceId = parentSpan.context.traceId;
42
+ parentSpanId = parentSpan.context.spanId;
43
+ traceFlags = parentSpan.context.traceFlags;
44
+ } else if (effectiveContext && new SpanContext(effectiveContext.traceId, effectiveContext.spanId).isValid()) {
45
+ traceId = effectiveContext.traceId;
46
+ parentSpanId = effectiveContext.spanId;
47
+ traceFlags = effectiveContext.traceFlags;
48
+ } else {
49
+ traceId = this.provider.generateTraceId();
50
+ parentSpanId = undefined;
51
+
52
+ const sampler = this.provider.getSampler();
53
+ const sampled = sampler ? sampler.shouldSample().sampled : true;
54
+ traceFlags = sampled ? 1 : 0;
55
+
56
+ // Debug override: force sampling for new root traces
57
+ const config = getConfig();
58
+ if (config.debug) {
59
+ traceFlags = 1;
60
+ }
61
+ }
62
+
63
+ const spanContext = new SpanContext(
64
+ traceId,
65
+ this.provider.generateSpanId(),
66
+ traceFlags,
67
+ effectiveContext?.traceState
68
+ );
69
+
70
+ return new Span(name, this, spanContext, parentSpanId, options?.attributes);
71
+ }
72
+
73
+ /**
74
+ * Start an active span and run a function within it.
75
+ */
76
+ async startActiveSpan<T = unknown>(
77
+ name: string,
78
+ fn: (span: ISpan) => Promise<T> | T,
79
+ options?: {
80
+ attributes?: Record<string, unknown>;
81
+ parent?: ISpan | null;
82
+ }
83
+ ): Promise<T> {
84
+ const span = this.startSpan(name, options);
85
+
86
+ try {
87
+ const result = await runWithSpanAsync(span, async () => {
88
+ return await Promise.resolve(fn(span));
89
+ });
90
+ return result;
91
+ } catch (error) {
92
+ if (error instanceof Error) {
93
+ span.recordException(error);
94
+ }
95
+ throw error;
96
+ } finally {
97
+ span.end();
98
+ }
99
+ }
100
+ }
package/src/types.ts ADDED
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Core types and interfaces for the tracing SDK.
3
+ */
4
+
5
+ /**
6
+ * Span status enumeration.
7
+ */
8
+ export enum SpanStatus {
9
+ UNSET = 0,
10
+ OK = 1,
11
+ ERROR = 2,
12
+ }
13
+
14
+ /**
15
+ * Span context carries trace and span identification across process boundaries.
16
+ */
17
+ export interface ISpanContext {
18
+ traceId: string;
19
+ spanId: string;
20
+ traceFlags: number;
21
+ traceState?: string;
22
+ }
23
+
24
+ /**
25
+ * Span event for capturing events during span execution.
26
+ */
27
+ export interface SpanEvent {
28
+ name: string;
29
+ timestamp: number;
30
+ attributes?: Record<string, unknown>;
31
+ }
32
+
33
+ /**
34
+ * Span interface for creating and ending spans.
35
+ */
36
+ export interface ISpan {
37
+ context: ISpanContext;
38
+ name: string;
39
+ parentSpanId?: string;
40
+ attributes: Record<string, unknown>;
41
+ events: SpanEvent[];
42
+ status: SpanStatus;
43
+ statusDescription?: string;
44
+ startTimeNs: number;
45
+ endTimeNs?: number;
46
+ durationNs: number | undefined;
47
+
48
+ setAttribute(key: string, value: unknown): void;
49
+ addEvent(name: string, attributes?: Record<string, unknown>): void;
50
+ end(): void;
51
+ recordException(error: Error, attributes?: Record<string, unknown>): void;
52
+ isRecording(): boolean;
53
+ }
54
+
55
+ /**
56
+ * Tracer for creating and managing spans.
57
+ */
58
+ export interface ITracer {
59
+ startSpan(
60
+ name: string,
61
+ options?: {
62
+ attributes?: Record<string, unknown>;
63
+ parent?: ISpan | null;
64
+ parentContext?: ISpanContext | null;
65
+ }
66
+ ): ISpan;
67
+
68
+ startActiveSpan<T = unknown>(
69
+ name: string,
70
+ fn: (span: ISpan) => Promise<T> | T,
71
+ options?: {
72
+ attributes?: Record<string, unknown>;
73
+ parent?: ISpan | null;
74
+ }
75
+ ): Promise<T> | T;
76
+ }
77
+
78
+ /**
79
+ * Span processor for handling spans at various lifecycle points.
80
+ */
81
+ export interface ISpanProcessor {
82
+ onStart?(span: ISpan): void;
83
+ onEnd(span: ISpan): void;
84
+ shutdown(): Promise<void> | void;
85
+ forceFlush(timeout?: number): Promise<void> | void;
86
+ }
87
+
88
+ /**
89
+ * Span exporter for sending spans to a backend.
90
+ */
91
+ export interface ISpanExporter {
92
+ export(spans: ISpan[]): Promise<boolean> | boolean;
93
+ shutdown(): Promise<void> | void;
94
+ }
95
+
96
+ /**
97
+ * Resource attributes describing the service.
98
+ */
99
+ export interface Resource {
100
+ [key: string]: string | number | boolean;
101
+ }
102
+
103
+ /**
104
+ * Tracer provider for managing tracers and processors.
105
+ */
106
+ export interface ITracerProvider {
107
+ getTracer(name: string, version?: string): ITracer;
108
+ addSpanProcessor(processor: ISpanProcessor): void;
109
+ removeSpanProcessor(processor: ISpanProcessor): void;
110
+ forceFlush(timeout?: number): Promise<void>;
111
+ shutdown(): Promise<void>;
112
+ }
113
+
114
+ /**
115
+ * Configuration for the SDK initialization.
116
+ */
117
+ export interface SDKConfig {
118
+ apiKey?: string;
119
+ endpoint?: string;
120
+ sampleRate?: number;
121
+ maxQueueSize?: number;
122
+ maxExportBatchSize?: number;
123
+ scheduleDelayMs?: number;
124
+ enableConsoleExporter?: boolean;
125
+ enableTokenCounting?: boolean;
126
+ enableCostTracking?: boolean;
127
+ enableSpanLogging?: boolean;
128
+ pricingOverride?: Record<string, Record<string, number>>;
129
+ sessionId?: string;
130
+ userId?: string;
131
+ tenantId?: string;
132
+ projectId?: string;
133
+ debug?: boolean;
134
+ attrTruncationLimit?: number;
135
+ loadEnv?: boolean;
136
+ autoInstrument?: boolean;
137
+ toolInclude?: string[];
138
+ maxToolSpans?: number;
139
+ maxSpanDepth?: number;
140
+ resource?: Resource;
141
+ }
142
+
143
+ /**
144
+ * Sampling result for span sampling decisions.
145
+ */
146
+ export interface SamplingResult {
147
+ sampled: boolean;
148
+ }
149
+
150
+ /**
151
+ * Sampler interface for making sampling decisions.
152
+ */
153
+ export interface ISampler {
154
+ shouldSample(): SamplingResult;
155
+ }