Add spans to GCal and Outlook

chore/add-cal-otel-new
Joe Au-Yeung 2023-09-14 12:50:49 -04:00
parent 1d320354c9
commit 9a0fa263bf
2 changed files with 28 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import { MeetLocationType } from "@calcom/app-store/locations";
import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser";
import type CalendarService from "@calcom/lib/CalendarService";
import logger from "@calcom/lib/logger";
import { tracer, context } from "@calcom/lib/server/otel-initializer";
import prisma from "@calcom/prisma";
import type {
Calendar,
@ -345,6 +346,7 @@ export default class GoogleCalendarService implements Calendar {
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<EventBusyDate[]> {
const gCalGetAvailabilitySpan = tracer.startSpan("gCalGetAvailability", undefined, context.active());
return new Promise(async (resolve, reject) => {
const myGoogleAuth = await this.auth.getToken();
const calendar = google.calendar({
@ -360,15 +362,24 @@ export default class GoogleCalendarService implements Calendar {
return;
}
const gCalGetCalendarsSpan = tracer.startSpan("gCalGetCalendarsSpan", undefined, context.active());
(selectedCalendarIds.length === 0
? calendar.calendarList
.list({
fields: "items(id)",
})
.then((cals) => cals.data.items?.map((cal) => cal.id).filter(Boolean) || [])
.then((cals) => {
gCalGetCalendarsSpan.end();
return cals.data.items?.map((cal) => cal.id).filter(Boolean) || [];
})
: Promise.resolve(selectedCalendarIds)
)
.then((calsIds) => {
const gCalFreeBusyRequestSpan = tracer.startSpan(
"gCalFreeBusyRequestSpan",
undefined,
context.active()
);
calendar.freebusy.query(
{
requestBody: {
@ -378,6 +389,7 @@ export default class GoogleCalendarService implements Calendar {
},
},
(err, apires) => {
gCalFreeBusyRequestSpan.end();
if (err) return reject(err);
// If there's no calendar we just skip
if (!apires?.data.calendars) return resolve([]);
@ -388,6 +400,7 @@ export default class GoogleCalendarService implements Calendar {
end: busyTime.end || "",
});
});
gCalGetAvailabilitySpan.end();
return c;
}, [] as Prisma.PromiseReturnType<CalendarService["getAvailability"]>);
resolve(result);

View File

@ -6,6 +6,7 @@ import dayjs from "@calcom/dayjs";
import { getLocation, getRichDescription } from "@calcom/lib/CalEventParser";
import { handleErrorsJson, handleErrorsRaw } from "@calcom/lib/errors";
import logger from "@calcom/lib/logger";
import { tracer, context } from "@calcom/lib/server/otel-initializer";
import prisma from "@calcom/prisma";
import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime";
import type {
@ -127,6 +128,11 @@ export default class Office365CalendarService implements Calendar {
dateTo: string,
selectedCalendars: IntegrationCalendar[]
): Promise<EventBusyDate[]> {
const outlookGetAvailabilitySpan = tracer.startSpan(
"outlookGetAvailabilitySpan",
undefined,
context.active()
);
const dateFromParsed = new Date(dateFrom);
const dateToParsed = new Date(dateTo);
@ -146,6 +152,7 @@ export default class Office365CalendarService implements Calendar {
return Promise.resolve([]);
}
// TODO fetch external cal id to avoid this call. Follow up #11283
const ids = await (selectedCalendarIds.length === 0
? this.listCalendars().then((cals) => cals.map((e_2) => e_2.externalId).filter(Boolean) || [])
: Promise.resolve(selectedCalendarIds));
@ -172,7 +179,7 @@ export default class Office365CalendarService implements Calendar {
// Recursively fetch nextLink responses
alreadySuccessResponse = await this.fetchResponsesWithNextLink(responseBatchApi.responses);
outlookGetAvailabilitySpan.end();
return alreadySuccessResponse ? this.processBusyTimes(alreadySuccessResponse) : [];
} catch (err) {
console.log(err);
@ -418,11 +425,16 @@ export default class Office365CalendarService implements Calendar {
};
private apiGraphBatchCall = async (requests: IRequest[]): Promise<Response> => {
const outlookBatchCallSpan = tracer.startSpan(
`outlookBatchCallSpan: ${requests.length} calls`,
undefined,
context.active()
);
const response = await this.fetcher(`/$batch`, {
method: "POST",
body: JSON.stringify({ requests }),
});
outlookBatchCallSpan.end();
return response;
};