@ai-sdk/otel 0.0.0-6b196531-20260710185421

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.
@@ -0,0 +1,156 @@
1
+ import type {
2
+ AttributeValue,
3
+ Attributes,
4
+ Context,
5
+ Exception,
6
+ Span,
7
+ SpanContext,
8
+ SpanOptions,
9
+ SpanStatus,
10
+ TimeInput,
11
+ Tracer,
12
+ } from '@opentelemetry/api';
13
+
14
+ export class MockTracer implements Tracer {
15
+ spans: MockSpan[] = [];
16
+
17
+ get jsonSpans() {
18
+ return this.spans.map(span => ({
19
+ name: span.name,
20
+ attributes: span.attributes,
21
+ events: span.events,
22
+ ...(span.status && { status: span.status }),
23
+ }));
24
+ }
25
+
26
+ startSpan(name: string, options?: SpanOptions, context?: Context): Span {
27
+ const span = new MockSpan({
28
+ name,
29
+ options,
30
+ context,
31
+ });
32
+ this.spans.push(span);
33
+ return span;
34
+ }
35
+
36
+ startActiveSpan<F extends (span: Span) => unknown>(
37
+ name: string,
38
+ arg1: unknown,
39
+ arg2?: unknown,
40
+ arg3?: F,
41
+ ): ReturnType<any> {
42
+ if (typeof arg1 === 'function') {
43
+ const span = new MockSpan({
44
+ name,
45
+ });
46
+ this.spans.push(span);
47
+ return arg1(span);
48
+ }
49
+ if (typeof arg2 === 'function') {
50
+ const span = new MockSpan({
51
+ name,
52
+ options: arg1 as SpanOptions,
53
+ });
54
+ this.spans.push(span);
55
+ return arg2(span);
56
+ }
57
+ if (typeof arg3 === 'function') {
58
+ const span = new MockSpan({
59
+ name,
60
+ options: arg1 as SpanOptions,
61
+ context: arg2 as Context,
62
+ });
63
+ this.spans.push(span);
64
+ return arg3(span);
65
+ }
66
+ }
67
+ }
68
+
69
+ class MockSpan implements Span {
70
+ name: string;
71
+ context?: Context;
72
+ options?: SpanOptions;
73
+ attributes: Attributes;
74
+ events: Array<{
75
+ name: string;
76
+ attributes: Attributes | undefined;
77
+ time?: [number, number];
78
+ }> = [];
79
+ status?: SpanStatus;
80
+
81
+ readonly _spanContext: SpanContext = new MockSpanContext();
82
+
83
+ constructor({
84
+ name,
85
+ options,
86
+ context,
87
+ }: {
88
+ name: string;
89
+ options?: SpanOptions;
90
+ context?: Context;
91
+ }) {
92
+ this.name = name;
93
+ this.context = context;
94
+ this.options = options;
95
+ this.attributes = options?.attributes ?? {};
96
+ }
97
+
98
+ spanContext(): SpanContext {
99
+ return this._spanContext;
100
+ }
101
+
102
+ setAttribute(key: string, value: AttributeValue): this {
103
+ this.attributes = { ...this.attributes, [key]: value };
104
+ return this;
105
+ }
106
+
107
+ setAttributes(attributes: Attributes): this {
108
+ this.attributes = { ...this.attributes, ...attributes };
109
+ return this;
110
+ }
111
+
112
+ addEvent(name: string, attributes?: Attributes): this {
113
+ this.events.push({ name, attributes });
114
+ return this;
115
+ }
116
+
117
+ addLink() {
118
+ return this;
119
+ }
120
+ addLinks() {
121
+ return this;
122
+ }
123
+ setStatus(status: SpanStatus) {
124
+ this.status = status;
125
+ return this;
126
+ }
127
+ updateName() {
128
+ return this;
129
+ }
130
+ end() {
131
+ return this;
132
+ }
133
+ isRecording() {
134
+ return false;
135
+ }
136
+ recordException(exception: Exception, time?: TimeInput) {
137
+ const error =
138
+ typeof exception === 'string' ? new Error(exception) : exception;
139
+ this.events.push({
140
+ name: 'exception',
141
+ attributes: {
142
+ 'exception.type': error.constructor?.name || 'Error',
143
+ 'exception.name': error.name || 'Error',
144
+ 'exception.message': error.message || '',
145
+ 'exception.stack': error.stack || '',
146
+ },
147
+ time: Array.isArray(time) ? time : [0, 0],
148
+ });
149
+ }
150
+ }
151
+
152
+ class MockSpanContext implements SpanContext {
153
+ traceId = 'test-trace-id';
154
+ spanId = 'test-span-id';
155
+ traceFlags = 0;
156
+ }
@@ -0,0 +1,69 @@
1
+ import type { Span, SpanContext, Tracer } from '@opentelemetry/api';
2
+
3
+ /**
4
+ * Tracer implementation that does nothing (null object).
5
+ */
6
+ export const noopTracer: Tracer = {
7
+ startSpan(): Span {
8
+ return noopSpan;
9
+ },
10
+
11
+ startActiveSpan<F extends (span: Span) => unknown>(
12
+ name: unknown,
13
+ arg1: unknown,
14
+ arg2?: unknown,
15
+ arg3?: F,
16
+ ): ReturnType<any> {
17
+ if (typeof arg1 === 'function') {
18
+ return arg1(noopSpan);
19
+ }
20
+ if (typeof arg2 === 'function') {
21
+ return arg2(noopSpan);
22
+ }
23
+ if (typeof arg3 === 'function') {
24
+ return arg3(noopSpan);
25
+ }
26
+ },
27
+ };
28
+
29
+ const noopSpan: Span = {
30
+ spanContext() {
31
+ return noopSpanContext;
32
+ },
33
+ setAttribute() {
34
+ return this;
35
+ },
36
+ setAttributes() {
37
+ return this;
38
+ },
39
+ addEvent() {
40
+ return this;
41
+ },
42
+ addLink() {
43
+ return this;
44
+ },
45
+ addLinks() {
46
+ return this;
47
+ },
48
+ setStatus() {
49
+ return this;
50
+ },
51
+ updateName() {
52
+ return this;
53
+ },
54
+ end() {
55
+ return this;
56
+ },
57
+ isRecording() {
58
+ return false;
59
+ },
60
+ recordException() {
61
+ return this;
62
+ },
63
+ };
64
+
65
+ const noopSpanContext: SpanContext = {
66
+ traceId: '',
67
+ spanId: '',
68
+ traceFlags: 0,
69
+ };