Skip to content

Commit

Permalink
Ensure we handle undefined and booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Aug 29, 2024
1 parent 77a56fc commit 55c1dc6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
5 changes: 4 additions & 1 deletion apps/nextjs/src/app/api/chat/chatHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ function reportErrorTelemetry(
error: Error,
errorType: string,
statusMessage: string,
additionalAttributes: Record<string, unknown> = {},
additionalAttributes: Record<
string,
string | number | boolean | undefined
> = {},
) {
span.setTag("error", true);
span.setTag("error.type", errorType);
Expand Down
1 change: 0 additions & 1 deletion apps/nextjs/src/app/api/chat/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Aila, AilaInitializationOptions } from "@oakai/aila";
import { TracingSpan } from "@oakai/core/src/tracing/serverTracing";
import {
prisma as globalPrisma,
type PrismaClientWithAccelerate,
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/utils/testHelpers/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function expectTracingSpan(operationName: string) {
).toBeFalsy();
},
},
toHaveBeenExecutedWith: (expectedTags: Record<string, any>) => {
toHaveBeenExecutedWith: (expectedTags: Record<string, string | number>) => {
const span = mockTracer.spans.find(
(span) => span.tags["operation.name"] === operationName,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/tracing/mockTracer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TracingSpan } from "./serverTracing";

class MockSpan implements TracingSpan {
tags: Record<string, any> = {};
setTag(key: string, value: any) {
tags: Record<string, string | number | boolean | undefined> = {};
setTag(key: string, value: string | number | boolean | undefined) {
this.tags[key] = value;
}
finish() {}
Expand All @@ -13,8 +13,8 @@ class MockTracer {

trace(
name: string,
options: any,
callback: (span: TracingSpan) => Promise<any>,
options: Record<string, string | number>,
callback: (span: TracingSpan) => Promise<unknown>,
) {
const span = new MockSpan();
span.setTag("operation.name", name);
Expand Down
30 changes: 22 additions & 8 deletions packages/core/src/tracing/serverTracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mockTracer } from "./mockTracer";
initializeTracer({});

export interface TracingSpan {
setTag: (key: string, value: any) => void;
setTag: (key: string, value: string | number | boolean | undefined) => void;
finish: () => void;
}

Expand All @@ -13,31 +13,45 @@ export const tracer = isTest
: (baseTracer as unknown as {
trace: (
name: string,
options: any,
callback: (span: TracingSpan) => Promise<any>,
) => Promise<any>;
options: Record<string, string | number | boolean>,
callback: (span: TracingSpan) => Promise<unknown>,
) => Promise<unknown>;
});

export function withTelemetry<T>(
operationName: string,
options: Record<string, any>,
options: Record<string, string | number | boolean | undefined>,
handler: (span: TracingSpan) => Promise<T>,
): Promise<T> {
return tracer.trace(operationName, options, async (span) => {
const stringifiedOptions: Record<string, string | number> = Object.entries(
options,
).reduce((acc, [key, value]) => {
if (value !== undefined) {
return {
...acc,
[key]: typeof value === "boolean" ? value.toString() : value,
};
}
return acc;
}, {});

return tracer.trace(operationName, stringifiedOptions, async (span) => {
try {
return await handler(span);
} catch (error) {
span.setTag("error", true);
if (error instanceof Error) {
span.setTag("error_type", error.constructor.name);
span.setTag("error_message", error.message);
span.setTag("error_stack", error.stack);
if (error.stack) {
span.setTag("error_stack", error.stack);
}
} else {
span.setTag("error_message", String(error));
}
throw error;
} finally {
span.finish();
}
});
}) as Promise<T>;
}

0 comments on commit 55c1dc6

Please sign in to comment.