@ai-sdk/otel 0.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ import {
2
+ Attributes,
3
+ Span,
4
+ Tracer,
5
+ SpanStatusCode,
6
+ context,
7
+ } from '@opentelemetry/api';
8
+
9
+ export async function recordSpan<T>({
10
+ name,
11
+ tracer,
12
+ attributes,
13
+ fn,
14
+ endWhenDone = true,
15
+ }: {
16
+ name: string;
17
+ tracer: Tracer;
18
+ attributes: Attributes | PromiseLike<Attributes>;
19
+ fn: (span: Span) => Promise<T>;
20
+ endWhenDone?: boolean;
21
+ }) {
22
+ return tracer.startActiveSpan(
23
+ name,
24
+ { attributes: await attributes },
25
+ async span => {
26
+ // Capture the current context to maintain it across async generator yields
27
+ const ctx = context.active();
28
+
29
+ try {
30
+ // Execute within the captured context to ensure async generators
31
+ // don't lose the active span when they yield
32
+ const result = await context.with(ctx, () => fn(span));
33
+
34
+ if (endWhenDone) {
35
+ span.end();
36
+ }
37
+
38
+ return result;
39
+ } catch (error) {
40
+ try {
41
+ recordErrorOnSpan(span, error);
42
+ } finally {
43
+ // always stop the span when there is an error:
44
+ span.end();
45
+ }
46
+
47
+ throw error;
48
+ }
49
+ },
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Record an error on a span. Sets the span status to error. If the error is
55
+ * an instance of Error, an exception event with name, message, and stack
56
+ * will also be recorded.
57
+ *
58
+ * @param span - The span to record the error on.
59
+ * @param error - The error to record on the span.
60
+ */
61
+ export function recordErrorOnSpan(span: Span, error: unknown) {
62
+ if (error instanceof Error) {
63
+ span.recordException({
64
+ name: error.name,
65
+ message: error.message,
66
+ stack: error.stack,
67
+ });
68
+ span.setStatus({
69
+ code: SpanStatusCode.ERROR,
70
+ message: error.message,
71
+ });
72
+ } else {
73
+ span.setStatus({ code: SpanStatusCode.ERROR });
74
+ }
75
+ }
@@ -0,0 +1,78 @@
1
+ import type { Attributes, AttributeValue } from '@opentelemetry/api';
2
+ import type { TelemetrySettings } from 'ai';
3
+
4
+ type ResolvableAttributeValue = () =>
5
+ | AttributeValue
6
+ | PromiseLike<AttributeValue>
7
+ | undefined;
8
+
9
+ export async function selectTelemetryAttributes({
10
+ telemetry,
11
+ attributes,
12
+ }: {
13
+ telemetry?: TelemetrySettings;
14
+ attributes: {
15
+ [attributeKey: string]:
16
+ | AttributeValue
17
+ | { input: ResolvableAttributeValue }
18
+ | { output: ResolvableAttributeValue }
19
+ | undefined;
20
+ };
21
+ }): Promise<Attributes> {
22
+ // when telemetry is disabled, return an empty object to avoid serialization overhead:
23
+ if (telemetry?.isEnabled !== true) {
24
+ return {};
25
+ }
26
+
27
+ const resultAttributes: Attributes = {};
28
+
29
+ for (const [key, value] of Object.entries(attributes)) {
30
+ if (value == null) {
31
+ continue;
32
+ }
33
+
34
+ // input value, check if it should be recorded:
35
+ if (
36
+ typeof value === 'object' &&
37
+ 'input' in value &&
38
+ typeof value.input === 'function'
39
+ ) {
40
+ // default to true:
41
+ if (telemetry?.recordInputs === false) {
42
+ continue;
43
+ }
44
+
45
+ const result = await value.input();
46
+
47
+ if (result != null) {
48
+ resultAttributes[key] = result;
49
+ }
50
+
51
+ continue;
52
+ }
53
+
54
+ // output value, check if it should be recorded:
55
+ if (
56
+ typeof value === 'object' &&
57
+ 'output' in value &&
58
+ typeof value.output === 'function'
59
+ ) {
60
+ // default to true:
61
+ if (telemetry?.recordOutputs === false) {
62
+ continue;
63
+ }
64
+
65
+ const result = await value.output();
66
+
67
+ if (result != null) {
68
+ resultAttributes[key] = result;
69
+ }
70
+ continue;
71
+ }
72
+
73
+ // value is an attribute value already:
74
+ resultAttributes[key] = value as AttributeValue;
75
+ }
76
+
77
+ return resultAttributes;
78
+ }
@@ -0,0 +1,33 @@
1
+ import {
2
+ LanguageModelV4Message,
3
+ LanguageModelV4Prompt,
4
+ } from '@ai-sdk/provider';
5
+ import { convertDataContentToBase64String } from 'ai';
6
+
7
+ /**
8
+ * Helper utility to serialize prompt content for OpenTelemetry tracing.
9
+ * It is initially created because normalized LanguageModelV4Prompt carries
10
+ * images as Uint8Arrays, on which JSON.stringify acts weirdly, converting
11
+ * them to objects with stringified indices as keys, e.g. {"0": 42, "1": 69 }.
12
+ */
13
+ export function stringifyForTelemetry(prompt: LanguageModelV4Prompt): string {
14
+ return JSON.stringify(
15
+ prompt.map((message: LanguageModelV4Message) => ({
16
+ ...message,
17
+ content:
18
+ typeof message.content === 'string'
19
+ ? message.content
20
+ : message.content.map(part =>
21
+ part.type === 'file'
22
+ ? {
23
+ ...part,
24
+ data:
25
+ part.data instanceof Uint8Array
26
+ ? convertDataContentToBase64String(part.data)
27
+ : part.data,
28
+ }
29
+ : part,
30
+ ),
31
+ })),
32
+ );
33
+ }