Skip to content

Commit 3ae3720

Browse files
committed
Better event logs
1 parent b1f4bf6 commit 3ae3720

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

apps/backend/src/lib/events.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as yup from "yup";
44
import { UnionToIntersection } from "@stackframe/stack-shared/dist/utils/types";
55
import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors";
66
import { prismaClient } from "@/prisma-client";
7+
import posthog from "posthog-js";
78

89
type EventType = {
910
id: string,
@@ -124,7 +125,7 @@ export async function logEvent<T extends EventType[]>(
124125
}
125126

126127

127-
// log event
128+
// log event in DB
128129
await prismaClient.event.create({
129130
data: {
130131
systemEventTypeIds: [...allEventTypes].map(eventType => eventType.id),
@@ -134,4 +135,15 @@ export async function logEvent<T extends EventType[]>(
134135
eventEndedAt: timeRange.end,
135136
},
136137
});
138+
139+
// log event in PostHog
140+
for (const eventType of allEventTypes) {
141+
const postHogEventName = `stack_${eventType.id.replace(/^\$/, "system_").replace(/-/g, "_")}`;
142+
posthog.capture(postHogEventName, {
143+
data,
144+
isWide,
145+
eventStartedAt: timeRange.start,
146+
eventEndedAt: timeRange.end,
147+
});
148+
}
137149
}

apps/backend/src/oauth/providers/base.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export type TokenSet = {
1010
accessTokenExpiredAt: Date,
1111
};
1212

13-
function processTokenSet(tokenSet: OIDCTokenSet): TokenSet {
13+
function processTokenSet(providerName: string, tokenSet: OIDCTokenSet): TokenSet {
1414
if (!tokenSet.access_token) {
1515
throw new StackAssertionError("No access token received", { tokenSet });
1616
}
1717

1818
if (!tokenSet.expires_in) {
19-
captureError("processTokenSet", "No expires_in received");
19+
captureError("processTokenSet", new StackAssertionError(`No expires_in received from OAuth provider ${providerName}.`, { tokenSetKeys: Object.keys(tokenSet) }));
2020
}
2121

2222
return {
@@ -123,7 +123,7 @@ export abstract class OAuthBaseProvider {
123123

124124
}
125125

126-
tokenSet = processTokenSet(tokenSet);
126+
tokenSet = processTokenSet(this.constructor.name, tokenSet);
127127

128128
return {
129129
userInfo: await this.postProcessUserInfo(tokenSet),
@@ -136,7 +136,7 @@ export abstract class OAuthBaseProvider {
136136
scope?: string,
137137
}): Promise<TokenSet> {
138138
const tokenSet = await this.oauthClient.refresh(options.refreshToken, { exchangeBody: { scope: options.scope } });
139-
return processTokenSet(tokenSet);
139+
return processTokenSet(this.constructor.name, tokenSet);
140140
}
141141

142142
abstract postProcessUserInfo(tokenSet: TokenSet): Promise<OAuthUserInfo>;

0 commit comments

Comments
 (0)