Better event logs

This commit is contained in:
Konstantin Wohlwend 2024-08-11 15:25:41 -07:00
parent b1f4bf6209
commit 3ae3720105
2 changed files with 17 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import * as yup from "yup";
import { UnionToIntersection } from "@stackframe/stack-shared/dist/utils/types";
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
import { prismaClient } from "@/prisma-client";
import posthog from "posthog-js";
type EventType = {
id: string,
@ -124,7 +125,7 @@ export async function logEvent<T extends EventType[]>(
}
// log event
// log event in DB
await prismaClient.event.create({
data: {
systemEventTypeIds: [...allEventTypes].map(eventType => eventType.id),
@ -134,4 +135,15 @@ export async function logEvent<T extends EventType[]>(
eventEndedAt: timeRange.end,
},
});
// log event in PostHog
for (const eventType of allEventTypes) {
const postHogEventName = `stack_${eventType.id.replace(/^\$/, "system_").replace(/-/g, "_")}`;
posthog.capture(postHogEventName, {
data,
isWide,
eventStartedAt: timeRange.start,
eventEndedAt: timeRange.end,
});
}
}

View File

@ -10,13 +10,13 @@ export type TokenSet = {
accessTokenExpiredAt: Date,
};
function processTokenSet(tokenSet: OIDCTokenSet): TokenSet {
function processTokenSet(providerName: string, tokenSet: OIDCTokenSet): TokenSet {
if (!tokenSet.access_token) {
throw new StackAssertionError("No access token received", { tokenSet });
}
if (!tokenSet.expires_in) {
captureError("processTokenSet", "No expires_in received");
captureError("processTokenSet", new StackAssertionError(`No expires_in received from OAuth provider ${providerName}.`, { tokenSetKeys: Object.keys(tokenSet) }));
}
return {
@ -123,7 +123,7 @@ export abstract class OAuthBaseProvider {
}
tokenSet = processTokenSet(tokenSet);
tokenSet = processTokenSet(this.constructor.name, tokenSet);
return {
userInfo: await this.postProcessUserInfo(tokenSet),
@ -136,7 +136,7 @@ export abstract class OAuthBaseProvider {
scope?: string,
}): Promise<TokenSet> {
const tokenSet = await this.oauthClient.refresh(options.refreshToken, { exchangeBody: { scope: options.scope } });
return processTokenSet(tokenSet);
return processTokenSet(this.constructor.name, tokenSet);
}
abstract postProcessUserInfo(tokenSet: TokenSet): Promise<OAuthUserInfo>;