Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(sdk): serialize map outputs for non-promise outputs #276

Merged
merged 2 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions packages/traceloop-sdk/src/lib/tracing/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@traceloop/ai-semantic-conventions";
import { withAssociationProperties } from "./association";
import { shouldSendTraces } from ".";
import { Telemetry } from "../telemetry/telemetry";

export type DecoratorConfig = {
name: string;
Expand Down Expand Up @@ -82,8 +83,8 @@ function withEntity<
}),
);
}
} catch {
/* empty */
} catch (error) {
Telemetry.getInstance().logException(error);
}
}

Expand All @@ -92,19 +93,14 @@ function withEntity<
return res.then((resolvedRes) => {
try {
if (shouldSendTraces()) {
if (resolvedRes instanceof Map) {
span.setAttribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
JSON.stringify(Array.from(resolvedRes.entries())),
);
} else {
span.setAttribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
JSON.stringify(resolvedRes),
);
}
span.setAttribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
serialize(resolvedRes),
);
}
return resolvedRes;
} catch (error) {
Telemetry.getInstance().logException(error);
} finally {
span.end();
}
Expand All @@ -114,10 +110,12 @@ function withEntity<
if (shouldSendTraces()) {
span.setAttribute(
SpanAttributes.TRACELOOP_ENTITY_OUTPUT,
JSON.stringify(res),
serialize(res),
);
}
return res;
} catch (error) {
Telemetry.getInstance().logException(error);
} finally {
span.end();
}
Expand Down Expand Up @@ -240,3 +238,11 @@ export function tool(
) {
return entity(TraceloopSpanKindValues.TOOL, config ?? {});
}

function serialize(input: unknown): string {
if (input instanceof Map) {
return JSON.stringify(Array.from(input.entries()));
} else {
return JSON.stringify(input);
}
}
Loading