Skip to content

Commit da79285

Browse files
bazumoN2D4
andauthored
add opentelemetry log function and telemetry for primary_email_auth_enabled usage (hexclave#523)
Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
1 parent 6794a77 commit da79285

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

apps/backend/src/app/api/latest/users/crud.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PrismaTransaction } from "@/lib/types";
44
import { sendTeamMembershipDeletedWebhook, sendUserCreatedWebhook, sendUserDeletedWebhook, sendUserUpdatedWebhook } from "@/lib/webhooks";
55
import { RawQuery, prismaClient, rawQuery, retryTransaction } from "@/prisma-client";
66
import { createCrudHandlers } from "@/route-handlers/crud-handler";
7+
import { log } from "@/utils/telemetry";
78
import { runAsynchronouslyAndWaitUntil } from "@/utils/vercel";
89
import { BooleanTrue, Prisma } from "@prisma/client";
910
import { KnownErrors } from "@stackframe/stack-shared";
@@ -536,6 +537,10 @@ export const usersCrudHandlers = createLazyProxy(() => createCrudHandlers(usersC
536537
};
537538
},
538539
onCreate: async ({ auth, data }) => {
540+
log("create_user_endpoint_primaryAuthEnabled", {
541+
value: data.primary_email_auth_enabled,
542+
email: data.primary_email ?? undefined,
543+
});
539544
const result = await retryTransaction(async (tx) => {
540545
const passwordHash = await getPasswordHashFromData(data);
541546
await checkAuthData(tx, {

apps/backend/src/utils/telemetry.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AttributeValue, Span, trace } from "@opentelemetry/api";
2-
1+
import { Attributes, AttributeValue, Span, trace } from "@opentelemetry/api";
2+
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
33
const tracer = trace.getTracer('stack-backend');
44

55
export function withTraceSpan<P extends any[], T>(optionsOrDescription: string | { description: string, attributes?: Record<string, AttributeValue> }, fn: (...args: P) => Promise<T>): (...args: P) => Promise<T> {
@@ -24,6 +24,11 @@ export async function traceSpan<T>(optionsOrDescription: string | { description:
2424
});
2525
}
2626

27-
export function log(message: string, ...args: any[]) {
28-
console.log(`[${new Date().toISOString()}] ${message}`, ...args);
27+
export function log(message: string, attributes: Attributes) {
28+
const span = trace.getActiveSpan();
29+
if (span) {
30+
span.addEvent(message, attributes);
31+
} else {
32+
throw new StackAssertionError('No active span found');
33+
}
2934
}

0 commit comments

Comments
 (0)