feat: dist tracing 4 (#25574)

* feat: distributed tracing 2

* feat: distributed tracing 2

* refactor: feedback

* refactor: feedback

* fix: type error

* fix: trpc error

* feat: distributed tracing - 3

* chore: translation

* refactor: improvements

* fix: feedback

* chore: remove

* feat: dist tracing 4

* fix: type errors

* fix: type errors

* fix: remove string type

* fix: type errors
This commit is contained in:
Udit Takkar authored and GitHub committed 2025-12-15 10:43:22 +00:00
1 parent 0d4108dd69
commit 4e1d05a7e7
15 files changed
+127 -26

No files matched your search

+19 -5
View File
@@ -18,6 +18,8 @@ import { HttpError as HttpCode } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
import type { TraceContext } from "@calcom/lib/tracing";
import { distributedTracing } from "@calcom/lib/tracing/factory";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import { BookingStatus } from "@calcom/prisma/enums";
@@ -30,7 +32,7 @@ export const config = {
},
};
export async function handleStripePaymentSuccess(event: Stripe.Event) {
export async function handleStripePaymentSuccess(event: Stripe.Event, traceContext: TraceContext) {
const paymentIntent = event.data.object as Stripe.PaymentIntent;
const payment = await prisma.payment.findFirst({
where: {
@@ -48,10 +50,10 @@ export async function handleStripePaymentSuccess(event: Stripe.Event) {
}
if (!payment?.bookingId) throw new HttpCode({ statusCode: 204, message: "Payment not found" });
await handlePaymentSuccess(payment.id, payment.bookingId);
await handlePaymentSuccess(payment.id, payment.bookingId, traceContext);
}
const handleSetupSuccess = async (event: Stripe.Event) => {
const handleSetupSuccess = async (event: Stripe.Event, traceContext: TraceContext) => {
const setupIntent = event.data.object as Stripe.SetupIntent;
const payment = await prisma.payment.findFirst({
where: {
@@ -63,6 +65,10 @@ const handleSetupSuccess = async (event: Stripe.Event) => {
const { booking, user, evt, eventType } = await getBooking(payment.bookingId);
const updatedTraceContext = distributedTracing.updateTrace(traceContext, {
bookingId: booking.id,
});
const bookingData: Prisma.BookingUpdateInput = {
paid: true,
};
@@ -126,6 +132,7 @@ const handleSetupSuccess = async (event: Stripe.Event) => {
booking,
paid: true,
platformClientParams: platformOAuthClient ? getPlatformParams(platformOAuthClient) : undefined,
traceContext: updatedTraceContext,
});
} else if (areEmailsEnabled) {
await sendOrganizerRequestEmail({ ...evt }, eventType.metadata);
@@ -133,7 +140,7 @@ const handleSetupSuccess = async (event: Stripe.Event) => {
}
};
type WebhookHandler = (event: Stripe.Event) => Promise<void>;
type WebhookHandler = (event: Stripe.Event, traceContext: TraceContext) => Promise<void>;
const webhookHandlers: Record<string, WebhookHandler | undefined> = {
"payment_intent.succeeded": handleStripePaymentSuccess,
@@ -163,6 +170,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const event = stripe.webhooks.constructEvent(payload, sig, process.env.STRIPE_WEBHOOK_SECRET);
const traceContext = distributedTracing.createTrace(`stripe_webhook`, {
meta: {
eventType: event.type,
eventId: event.id,
},
});
// bypassing this validation for e2e tests
// in order to successfully confirm the payment
if (!event.account && !process.env.NEXT_PUBLIC_IS_E2E) {
@@ -171,7 +185,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const handler = webhookHandlers[event.type];
if (handler) {
await handler(event);
await handler(event, traceContext);
} else {
/** Not really an error, just letting Stripe know that the webhook was received but unhandled */
throw new HttpCode({
@@ -2,14 +2,13 @@ import type { getEventTypeResponse } from "@calcom/features/bookings/lib/handleN
import { scheduleEmailReminder } from "@calcom/features/ee/workflows/lib/reminders/emailReminderManager";
import type { Workflow } from "@calcom/features/ee/workflows/lib/types";
import type { getDefaultEvent } from "@calcom/features/eventtypes/lib/defaultEvents";
import logger from "@calcom/lib/logger";
import { withReporting } from "@calcom/lib/sentryWrapper";
import type { TraceContext } from "@calcom/lib/tracing";
import { distributedTracing } from "@calcom/lib/tracing/factory";
import { WorkflowTriggerEvents, TimeUnit, WorkflowActions, WorkflowTemplates } from "@calcom/prisma/enums";
import type { ExtendedCalendarEvent } from "./reminderScheduler";
const log = logger.getSubLogger({ prefix: ["[scheduleMandatoryReminder]"] });
export type NewBookingEventType = Awaited<ReturnType<typeof getDefaultEvent>> | getEventTypeResponse;
async function _scheduleMandatoryReminder({
@@ -20,6 +19,7 @@ async function _scheduleMandatoryReminder({
seatReferenceUid,
isPlatformNoEmail = false,
isDryRun = false,
traceContext,
}: {
evt: ExtendedCalendarEvent;
workflows: Workflow[];
@@ -28,7 +28,26 @@ async function _scheduleMandatoryReminder({
seatReferenceUid: string | undefined;
isPlatformNoEmail?: boolean;
isDryRun?: boolean;
traceContext: TraceContext;
}) {
const reminderMeta = {
eventTitle: evt.title,
attendeeCount: evt.attendees.length,
organizerId: evt.organizer.id,
requiresConfirmation,
hideBranding,
seatReferenceUid,
isPlatformNoEmail,
};
const spanContext = distributedTracing.createSpan(
traceContext,
"schedule_mandatory_reminder",
reminderMeta
);
const tracingLogger = distributedTracing.getTracingLogger(spanContext);
if (isDryRun) return;
if (isPlatformNoEmail) return;
try {
@@ -67,11 +86,11 @@ async function _scheduleMandatoryReminder({
verifiedAt: new Date(),
});
} catch (error) {
log.error("Error while scheduling mandatory reminders", JSON.stringify({ error }));
tracingLogger.error("Error while scheduling mandatory reminders", JSON.stringify({ error }));
}
}
} catch (error) {
log.error("Error while scheduling mandatory reminders", JSON.stringify({ error }));
tracingLogger.error("Error while scheduling mandatory reminders", JSON.stringify({ error }));
}
}