Merged in chore/upgrade-sentry (pull request #3191)

feat: upgrade sentry and use metrics

* feat: upgrade sentry and use metrics

* remove ununsed deps

* rename span

* .


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-11-20 13:24:53 +00:00
parent 5eaaea527f
commit b1d7fbad88
14 changed files with 510 additions and 470 deletions

View File

@@ -1,4 +1,4 @@
import { trace, type Tracer } from "@opentelemetry/api"
import * as Sentry from "@sentry/nextjs"
import { getCacheClient } from "../dataCache"
import { env } from "../env/server"
@@ -11,24 +11,18 @@ interface ServiceTokenResponse {
expires_in: number
}
export async function getServiceToken() {
const tracer = trace.getTracer("getServiceToken")
return await tracer.startActiveSpan("getServiceToken", async () => {
export async function getServiceToken(): Promise<ServiceTokenResponse> {
return Sentry.startSpan({ name: "getServiceToken" }, async () => {
const scopes = env.CURITY_CLIENT_SERVICE_SCOPES
const cacheKey = getServiceTokenCacheKey(scopes)
const cacheClient = await getCacheClient()
const token = await getOrSetServiceTokenFromCache(cacheKey, scopes, tracer)
const token = await getOrSetServiceTokenFromCache(cacheKey, scopes)
if (token.expiresAt < Date.now()) {
await cacheClient.deleteKey(cacheKey)
const newToken = await getOrSetServiceTokenFromCache(
cacheKey,
scopes,
tracer
)
const newToken = await getOrSetServiceTokenFromCache(cacheKey, scopes)
return newToken.jwt
}
@@ -38,16 +32,14 @@ export async function getServiceToken() {
async function getOrSetServiceTokenFromCache(
cacheKey: string,
scopes: string[],
tracer: Tracer
scopes: string[]
) {
const cacheClient = await getCacheClient()
const token = await cacheClient.cacheOrGet(
cacheKey,
async () => {
return await tracer.startActiveSpan("fetch new token", async () => {
const newToken = await getJwt(scopes)
return newToken
return Sentry.startSpan({ name: "fetch new serviceToken" }, async () => {
return await getJwt(scopes)
})
},
"1h"
@@ -56,19 +48,10 @@ async function getOrSetServiceTokenFromCache(
}
async function getJwt(scopes: string[]) {
const getJwtCounter = createCounter("tokenManager", "getJwt")
const metricsGetJwt = getJwtCounter.init({
scopes,
})
metricsGetJwt.start()
const jwt = await fetchServiceToken(scopes)
const expiresAt = Date.now() + jwt.expires_in * 1000
metricsGetJwt.success()
return { expiresAt, jwt }
}