Skip to content

Commit ec8c66e

Browse files
authored
[server] Metrics for sessions with JWTs WEB-102 (#17302)
* [server] Metrics for sessions with JWTs * retest * Fix * Fix * Fix * fix
1 parent 0f88afe commit ec8c66e

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

components/server/src/auth/jwt.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TestAuthJWT {
7676
const encoded = await sign({}, keypair.privateKey, {
7777
algorithm: "RS512",
7878
expiresIn: "1d",
79-
issuer: this.config.hostUrl.toStringWoRootSlash(),
79+
issuer: "https://mp-server-d7650ec945.preview.gitpod-dev.com",
8080
keyid: keypair.id,
8181
subject,
8282
});

components/server/src/prometheus-metrics.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function registerServerMetrics(registry: prometheusClient.Registry) {
2626
registry.registerMetric(spicedbClientLatency);
2727
registry.registerMetric(dashboardErrorBoundary);
2828
registry.registerMetric(jwtCookieIssued);
29+
registry.registerMetric(sessionsWithJWTs);
2930
}
3031

3132
const loginCounter = new prometheusClient.Counter({
@@ -67,6 +68,16 @@ export function reportJWTCookieIssued() {
6768
jwtCookieIssued.inc();
6869
}
6970

71+
const sessionsWithJWTs = new prometheusClient.Counter({
72+
name: "gitpod_server_sessions_with_jwts_total",
73+
help: "Total number of sessions which did/or did not contain JWTs",
74+
labelNames: ["present"],
75+
});
76+
77+
export function reportSessionHasJWT(jwtPresent: boolean) {
78+
sessionsWithJWTs.inc({ present: `${jwtPresent}` });
79+
}
80+
7081
const apiConnectionClosedCounter = new prometheusClient.Counter({
7182
name: "gitpod_server_api_connections_closed_total",
7283
help: "Total amount of closed API connections",

components/server/src/session-handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
1616
import { Config as DBConfig } from "@gitpod/gitpod-db/lib/config";
1717
import { Config } from "./config";
1818
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
19+
import { reportSessionHasJWT } from "./prometheus-metrics";
1920

2021
@injectable()
2122
export class SessionHandlerProvider {
@@ -40,7 +41,14 @@ export class SessionHandlerProvider {
4041

4142
options.store = this.createStore();
4243

43-
this.sessionHandler = session(options);
44+
this.sessionHandler = (req: express.Request, res: express.Response, next: express.NextFunction) => {
45+
const jwt = req.cookies[SessionHandlerProvider.getJWTCookieName(this.config.hostUrl)];
46+
const containedJWT = !!jwt;
47+
48+
reportSessionHasJWT(containedJWT);
49+
50+
return session(options)(req, res, next);
51+
};
4452
}
4553

4654
protected getCookieOptions(config: Config): express.CookieOptions {

0 commit comments

Comments
 (0)