feat: harmonize log and metrics

This commit is contained in:
Michael Zetterberg
2025-04-17 07:16:11 +02:00
parent 858a81b16f
commit 5323a8e46e
58 changed files with 2324 additions and 4726 deletions

View File

@@ -1,13 +1,9 @@
import { createCounter } from "@/server/telemetry"
import { publicProcedure, router } from "@/server/trpc"
import { getCacheClient } from "@/services/dataCache"
import { jobylonFeedSchema } from "./output"
import {
getJobylonFeedCounter,
getJobylonFeedFailCounter,
getJobylonFeedSuccessCounter,
} from "./telemetry"
export const TWENTYFOUR_HOURS = 60 * 60 * 24
@@ -19,20 +15,19 @@ const feedUrl =
export const jobylonQueryRouter = router({
feed: router({
get: publicProcedure.query(async function () {
const jobylonFeedGetCounter = createCounter("trpc.jobylon.feed", "get")
const metricsJobylonFeedGet = jobylonFeedGetCounter.init()
metricsJobylonFeedGet.start()
const url = new URL(feedUrl)
url.search = new URLSearchParams({
format: "json",
}).toString()
const urlString = url.toString()
getJobylonFeedCounter.add(1, { url: urlString })
console.info(
"jobylon.feed start",
JSON.stringify({ query: { url: urlString } })
)
const cacheClient = await getCacheClient()
return await cacheClient.cacheOrGet(
const result = cacheClient.cacheOrGet(
"jobylon:feed",
async () => {
const response = await fetch(url, {
@@ -40,27 +35,14 @@ export const jobylonQueryRouter = router({
})
if (!response.ok) {
await metricsJobylonFeedGet.httpError(response)
const text = await response.text()
const error = {
status: response.status,
statusText: response.statusText,
text,
}
getJobylonFeedFailCounter.add(1, {
url: urlString,
error_type: "http_error",
error: JSON.stringify(error),
})
console.error(
"jobylon.feed error",
JSON.stringify({
query: { url: urlString },
error,
})
)
throw new Error(
`Failed to fetch Jobylon feed: ${JSON.stringify(error)}`
`Failed to fetch Jobylon feed: ${JSON.stringify({
status: response.status,
statusText: response.statusText,
text,
})}`
)
}
@@ -68,37 +50,20 @@ export const jobylonQueryRouter = router({
const validatedResponse = jobylonFeedSchema.safeParse(responseJson)
if (!validatedResponse.success) {
getJobylonFeedFailCounter.add(1, {
urlString,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
const errorData = JSON.stringify({
query: { url: urlString },
error: validatedResponse.error,
})
console.error("jobylon.feed error", errorData)
metricsJobylonFeedGet.validationError(validatedResponse.error)
throw new Error(
`Failed to parse Jobylon feed: ${JSON.stringify(errorData)}`
`Failed to parse Jobylon feed: ${JSON.stringify(validatedResponse.error)}`
)
}
getJobylonFeedSuccessCounter.add(1, {
url: urlString,
})
console.info(
"jobylon.feed success",
JSON.stringify({
query: { url: urlString },
})
)
return validatedResponse.data
},
"1d"
)
metricsJobylonFeedGet.success()
return result
}),
}),
})