Skip to content

Commit b4a8948

Browse files
authored
fix(sdk): serialize map outputs for non-promise outputs (#276)
1 parent b434f61 commit b4a8948

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

packages/traceloop-sdk/src/lib/tracing/decorators.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "@traceloop/ai-semantic-conventions";
88
import { withAssociationProperties } from "./association";
99
import { shouldSendTraces } from ".";
10+
import { Telemetry } from "../telemetry/telemetry";
1011

1112
export type DecoratorConfig = {
1213
name: string;
@@ -82,8 +83,8 @@ function withEntity<
8283
}),
8384
);
8485
}
85-
} catch {
86-
/* empty */
86+
} catch (error) {
87+
Telemetry.getInstance().logException(error);
8788
}
8889
}
8990

@@ -92,19 +93,14 @@ function withEntity<
9293
return res.then((resolvedRes) => {
9394
try {
9495
if (shouldSendTraces()) {
95-
if (resolvedRes instanceof Map) {
96-
span.setAttribute(
97-
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
98-
JSON.stringify(Array.from(resolvedRes.entries())),
99-
);
100-
} else {
101-
span.setAttribute(
102-
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
103-
JSON.stringify(resolvedRes),
104-
);
105-
}
96+
span.setAttribute(
97+
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
98+
serialize(resolvedRes),
99+
);
106100
}
107101
return resolvedRes;
102+
} catch (error) {
103+
Telemetry.getInstance().logException(error);
108104
} finally {
109105
span.end();
110106
}
@@ -114,10 +110,12 @@ function withEntity<
114110
if (shouldSendTraces()) {
115111
span.setAttribute(
116112
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
117-
JSON.stringify(res),
113+
serialize(res),
118114
);
119115
}
120116
return res;
117+
} catch (error) {
118+
Telemetry.getInstance().logException(error);
121119
} finally {
122120
span.end();
123121
}
@@ -240,3 +238,11 @@ export function tool(
240238
) {
241239
return entity(TraceloopSpanKindValues.TOOL, config ?? {});
242240
}
241+
242+
function serialize(input: unknown): string {
243+
if (input instanceof Map) {
244+
return JSON.stringify(Array.from(input.entries()));
245+
} else {
246+
return JSON.stringify(input);
247+
}
248+
}

0 commit comments

Comments
 (0)