* feat(calendar-subscription): add telemetry metrics for cache performance and webhook calls - Add Sentry span telemetry to CalendarCacheWrapper.getAvailability() and getAvailabilityWithTimeZones() to measure: - Cache fetch duration (cacheFetchDurationMs) - Original calendar fetch duration (originalFetchDurationMs) - Number of calendars from cache vs original source - Number of events from each source - Whether cache was used (cacheUsed) - Add Sentry span telemetry to CalendarSubscriptionService.processWebhook() to track: - Provider (google_calendar, office365_calendar) - Success/failure status - Processing duration (durationMs) - Error messages on failure - Channel ID and selected calendar ID - Create telemetry module with types, sentry-span, and no-op-span implementations Co-Authored-By: Volnei Munhoz <[email protected]> * refactor(calendar-subscription): move telemetry to @calcom/lib for reusability - Add withSpan helper to @calcom/lib/sentryWrapper for reusable telemetry - Update CalendarCacheWrapper to use withSpan from @calcom/lib - Update CalendarTelemetryWrapper to use withSpan from @calcom/lib - Update CalendarSubscriptionService to use withSpan from @calcom/lib - Remove old telemetry module from calendar-subscription feature - Add telemetry tracking when cache is disabled for performance comparison Co-Authored-By: Volnei Munhoz <[email protected]> * fix(calendar-subscription): only apply telemetry wrapper when Sentry is configured - Fix unit test failures by checking if Sentry is configured before wrapping - Remove ESLint inline rules from getCalendar.ts Co-Authored-By: Volnei Munhoz <[email protected]> * refactor(calendar-subscription): move CalendarTelemetryWrapper to telemetry directory - Move CalendarTelemetryWrapper from cache/ to telemetry/ directory - Update import path in getCalendar.ts Co-Authored-By: Volnei Munhoz <[email protected]> * feat(telemetry): add console fallback when Sentry is not configured - Add console logging fallback in development mode when Sentry is not configured - Create isTelemetryEnabled() helper to check if telemetry should be enabled - Update getCalendar.ts to use isTelemetryEnabled() helper - Logs span name, operation, duration, and attributes to console via @calcom/lib/logger Co-Authored-By: Volnei Munhoz <[email protected]> * refactor(calendar-subscription): wrap all calendars with CalendarTelemetryWrapper - CalendarTelemetryWrapper now wraps ALL cache-supported calendars with cacheEnabled parameter - CalendarCacheWrapper keeps cache-specific metrics with distinct op (calendar.cache.internal.*) - CalendarTelemetryWrapper uses canonical op (calendar.getAvailability) for consistent querying - Nested spans: outer telemetry wrapper measures end-to-end, inner cache wrapper measures cache internals Co-Authored-By: Volnei Munhoz <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
203 lines
6.6 KiB
TypeScript
203 lines
6.6 KiB
TypeScript
import logger from "@calcom/lib/logger";
|
|
import { withSpan } from "@calcom/lib/sentryWrapper";
|
|
import type {
|
|
Calendar,
|
|
CalendarEvent,
|
|
CalendarServiceEvent,
|
|
EventBusyDate,
|
|
IntegrationCalendar,
|
|
NewCalendarEventType,
|
|
SelectedCalendarEventTypeIds,
|
|
} from "@calcom/types/Calendar";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["CalendarTelemetryWrapper"] });
|
|
|
|
/**
|
|
* A wrapper to add telemetry to all calendar services.
|
|
* This provides consistent metrics for comparing performance between cached and non-cached calendar loading.
|
|
*
|
|
* @see Calendar
|
|
*/
|
|
export class CalendarTelemetryWrapper implements Calendar {
|
|
constructor(
|
|
private deps: {
|
|
originalCalendar: Calendar;
|
|
calendarType: string;
|
|
cacheSupported: boolean;
|
|
cacheEnabled: boolean;
|
|
credentialId: number;
|
|
}
|
|
) {}
|
|
|
|
getCredentialId?(): number {
|
|
return this.deps.originalCalendar.getCredentialId ? this.deps.originalCalendar.getCredentialId() : -1;
|
|
}
|
|
|
|
createEvent(
|
|
event: CalendarServiceEvent,
|
|
credentialId: number,
|
|
externalCalendarId?: string
|
|
): Promise<NewCalendarEventType> {
|
|
return this.deps.originalCalendar.createEvent(event, credentialId, externalCalendarId);
|
|
}
|
|
|
|
updateEvent(
|
|
uid: string,
|
|
event: CalendarServiceEvent,
|
|
externalCalendarId?: string | null
|
|
): Promise<NewCalendarEventType | NewCalendarEventType[]> {
|
|
return this.deps.originalCalendar.updateEvent(uid, event, externalCalendarId);
|
|
}
|
|
|
|
deleteEvent(uid: string, event: CalendarEvent, externalCalendarId?: string | null): Promise<unknown> {
|
|
return this.deps.originalCalendar.deleteEvent(uid, event, externalCalendarId);
|
|
}
|
|
|
|
/**
|
|
* Retrieves availability with telemetry tracking when cache is disabled.
|
|
*
|
|
* @param dateFrom - Start date (ISO string)
|
|
* @param dateTo - End date (ISO string)
|
|
* @param selectedCalendars - List of calendars to retrieve availability from
|
|
* @returns Array of busy date ranges
|
|
*/
|
|
async getAvailability(
|
|
dateFrom: string,
|
|
dateTo: string,
|
|
selectedCalendars: IntegrationCalendar[]
|
|
): Promise<EventBusyDate[]> {
|
|
return withSpan(
|
|
{
|
|
name: "CalendarTelemetryWrapper.getAvailability",
|
|
op: "calendar.getAvailability",
|
|
attributes: {
|
|
credentialId: this.deps.credentialId,
|
|
calendarCount: selectedCalendars?.length ?? 0,
|
|
calendarType: this.deps.calendarType,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
},
|
|
},
|
|
async (span) => {
|
|
log.debug("getAvailability", {
|
|
dateFrom,
|
|
dateTo,
|
|
calendarCount: selectedCalendars.length,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
});
|
|
|
|
if (!selectedCalendars?.length) return [];
|
|
|
|
const startTime = performance.now();
|
|
const results = await this.deps.originalCalendar.getAvailability(dateFrom, dateTo, selectedCalendars);
|
|
const totalFetchDurationMs = performance.now() - startTime;
|
|
|
|
span.setAttribute("totalFetchDurationMs", totalFetchDurationMs);
|
|
span.setAttribute("totalEventsCount", results.length);
|
|
|
|
log.info("Calendar fetch completed", {
|
|
calendarCount: selectedCalendars.length,
|
|
totalFetchDurationMs,
|
|
totalEventsCount: results.length,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
});
|
|
|
|
return results;
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Retrieves availability with time zones and telemetry tracking when cache is disabled.
|
|
*
|
|
* @param dateFrom - Start date (ISO string)
|
|
* @param dateTo - End date (ISO string)
|
|
* @param selectedCalendars - List of calendars to retrieve availability from
|
|
* @returns Array of time-zone-aware availability ranges
|
|
*/
|
|
async getAvailabilityWithTimeZones(
|
|
dateFrom: string,
|
|
dateTo: string,
|
|
selectedCalendars: IntegrationCalendar[]
|
|
): Promise<{ start: Date | string; end: Date | string; timeZone: string }[]> {
|
|
// Check if the original calendar supports this method
|
|
if (!this.deps.originalCalendar.getAvailabilityWithTimeZones) {
|
|
return [];
|
|
}
|
|
|
|
return withSpan(
|
|
{
|
|
name: "CalendarTelemetryWrapper.getAvailabilityWithTimeZones",
|
|
op: "calendar.getAvailabilityWithTimeZones",
|
|
attributes: {
|
|
credentialId: this.deps.credentialId,
|
|
calendarCount: selectedCalendars?.length ?? 0,
|
|
calendarType: this.deps.calendarType,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
},
|
|
},
|
|
async (span) => {
|
|
log.debug("getAvailabilityWithTimeZones", {
|
|
dateFrom,
|
|
dateTo,
|
|
calendarCount: selectedCalendars.length,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
});
|
|
|
|
if (!selectedCalendars?.length) return [];
|
|
|
|
const startTime = performance.now();
|
|
const results = await this.deps.originalCalendar.getAvailabilityWithTimeZones?.(
|
|
dateFrom,
|
|
dateTo,
|
|
selectedCalendars
|
|
);
|
|
const totalFetchDurationMs = performance.now() - startTime;
|
|
|
|
span.setAttribute("totalFetchDurationMs", totalFetchDurationMs);
|
|
span.setAttribute("totalEventsCount", results?.length ?? 0);
|
|
|
|
log.info("Calendar fetch with timezones completed", {
|
|
calendarCount: selectedCalendars.length,
|
|
totalFetchDurationMs,
|
|
totalEventsCount: results?.length ?? 0,
|
|
cacheSupported: this.deps.cacheSupported,
|
|
cacheEnabled: this.deps.cacheEnabled,
|
|
});
|
|
|
|
return results ?? [];
|
|
}
|
|
);
|
|
}
|
|
|
|
fetchAvailabilityAndSetCache?(selectedCalendars: IntegrationCalendar[]): Promise<unknown> {
|
|
return this.deps.originalCalendar.fetchAvailabilityAndSetCache?.(selectedCalendars) || Promise.resolve();
|
|
}
|
|
|
|
listCalendars(event?: CalendarEvent): Promise<IntegrationCalendar[]> {
|
|
return this.deps.originalCalendar.listCalendars(event);
|
|
}
|
|
|
|
testDelegationCredentialSetup?(): Promise<boolean> {
|
|
return this.deps.originalCalendar.testDelegationCredentialSetup?.() || Promise.resolve(false);
|
|
}
|
|
|
|
watchCalendar?(options: {
|
|
calendarId: string;
|
|
eventTypeIds: SelectedCalendarEventTypeIds;
|
|
}): Promise<unknown> {
|
|
return this.deps.originalCalendar.watchCalendar?.(options) || Promise.resolve();
|
|
}
|
|
|
|
unwatchCalendar?(options: {
|
|
calendarId: string;
|
|
eventTypeIds: SelectedCalendarEventTypeIds;
|
|
}): Promise<void> {
|
|
return this.deps.originalCalendar.unwatchCalendar?.(options) || Promise.resolve();
|
|
}
|
|
}
|