Skip to content

Commit

Permalink
fix(sdk): serialize map outputs for non-promise outputs (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirga authored May 20, 2024
1 parent b434f61 commit b4a8948
Showing 1 changed file with 20 additions and 14 deletions.
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);
}
}

0 comments on commit b4a8948

Please sign in to comment.