Skip to content

Commit abc8daf

Browse files
committed
Tracing for email health endpoint
1 parent 9002b63 commit abc8daf

1 file changed

Lines changed: 41 additions & 34 deletions

File tree

apps/backend/src/app/health/email/route.tsx

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler";
2+
import { traceSpan } from "@/utils/telemetry";
23
import { yupNumber, yupObject, yupString, yupTuple } from "@stackframe/stack-shared/dist/schema-fields";
34
import { generateSecureRandomString } from "@stackframe/stack-shared/dist/utils/crypto";
45
import { getEnvVariable, getNodeEnvironment } from "@stackframe/stack-shared/dist/utils/env";
@@ -62,29 +63,31 @@ const fetchFromResend = async (): Promise<{ data: ResendEmail[] }> => {
6263
};
6364

6465
const performSignUp = async (email: string, password: string) => {
65-
const apiBaseUrl = getEnvVariable("NEXT_PUBLIC_STACK_API_URL");
66-
const response = await fetch(`${apiBaseUrl}/api/v1/auth/password/sign-up`, {
67-
method: "POST",
68-
headers: {
69-
"Content-Type": "application/json",
70-
"X-Stack-Access-Type": "client",
71-
"X-Stack-Publishable-Client-Key": getEnvVariable("STACK_EMAIL_MONITOR_PUBLISHABLE_CLIENT_KEY"),
72-
"X-Stack-Project-Id": getEnvVariable("STACK_EMAIL_MONITOR_PROJECT_ID"),
73-
},
74-
body: JSON.stringify({
75-
email,
76-
password,
77-
verification_callback_url: getEnvVariable("STACK_EMAIL_MONITOR_VERIFICATION_CALLBACK_URL"),
78-
}),
79-
});
66+
await traceSpan("performing sign-up", async () => {
67+
const apiBaseUrl = getEnvVariable("NEXT_PUBLIC_STACK_API_URL");
68+
const response = await fetch(`${apiBaseUrl}/api/v1/auth/password/sign-up`, {
69+
method: "POST",
70+
headers: {
71+
"Content-Type": "application/json",
72+
"X-Stack-Access-Type": "client",
73+
"X-Stack-Publishable-Client-Key": getEnvVariable("STACK_EMAIL_MONITOR_PUBLISHABLE_CLIENT_KEY"),
74+
"X-Stack-Project-Id": getEnvVariable("STACK_EMAIL_MONITOR_PROJECT_ID"),
75+
},
76+
body: JSON.stringify({
77+
email,
78+
password,
79+
verification_callback_url: getEnvVariable("STACK_EMAIL_MONITOR_VERIFICATION_CALLBACK_URL"),
80+
}),
81+
});
8082

81-
const responseBody = await response.text();
83+
const responseBody = await response.text();
8284

83-
if (!response.ok) {
84-
throw new StackAssertionError(`Sign-up failed: ${response.status} - ${responseBody}`, {
85-
responseBody,
86-
});
87-
}
85+
if (!response.ok) {
86+
throw new StackAssertionError(`Sign-up failed: ${response.status} - ${responseBody}`, {
87+
responseBody,
88+
});
89+
}
90+
});
8891
};
8992

9093
const isExpectedVerificationEmail = (email: ResendEmail, testEmail: string): boolean => {
@@ -99,25 +102,29 @@ const isExpectedVerificationEmail = (email: ResendEmail, testEmail: string): boo
99102
};
100103

101104
const waitForVerificationEmail = async (testEmail: string, useInbucket: boolean) => {
102-
const MAX_POLL_ATTEMPTS = 24;
103-
const POLL_INTERVAL_MS = 5000;
105+
await traceSpan("waiting for verification email", async () => {
106+
const MAX_POLL_ATTEMPTS = 24;
107+
const POLL_INTERVAL_MS = 5000;
104108

105-
for (let attempt = 1; attempt <= MAX_POLL_ATTEMPTS; attempt++) {
106-
await wait(POLL_INTERVAL_MS);
109+
for (let attempt = 1; attempt <= MAX_POLL_ATTEMPTS; attempt++) {
110+
await traceSpan(`waiting for verification email - attempt ${attempt}`, async () => {
111+
await wait(POLL_INTERVAL_MS);
107112

108-
const listData = useInbucket
109-
? await fetchFromInbucket(testEmail)
110-
: await fetchFromResend();
113+
const listData = useInbucket
114+
? await fetchFromInbucket(testEmail)
115+
: await fetchFromResend();
111116

112-
const emails = listData.data;
113-
const verificationEmail = emails.find((email) => isExpectedVerificationEmail(email, testEmail));
117+
const emails = listData.data;
118+
const verificationEmail = emails.find((email) => isExpectedVerificationEmail(email, testEmail));
114119

115-
if (verificationEmail) {
116-
return;
120+
if (verificationEmail) {
121+
return;
122+
}
123+
});
117124
}
118-
}
119125

120-
throw new StackAssertionError(`Couldn't find verification email in time limit`, { recipient_email: testEmail, max_poll_attempts: MAX_POLL_ATTEMPTS, poll_interval_ms: POLL_INTERVAL_MS });
126+
throw new StackAssertionError(`Couldn't find verification email in time limit`, { recipient_email: testEmail, max_poll_attempts: MAX_POLL_ATTEMPTS, poll_interval_ms: POLL_INTERVAL_MS });
127+
});
121128
};
122129

123130
export const POST = createSmartRouteHandler({

0 commit comments

Comments
 (0)