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,8 +1,7 @@
import { metrics } from "@opentelemetry/api"
import { GetCollectionPageRefs } from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { createCounter } from "@/server/telemetry"
import { getCacheClient } from "@/services/dataCache"
import {
@@ -21,31 +20,17 @@ import type {
} from "@/types/trpc/routers/contentstack/collectionPage"
import type { Lang } from "@/constants/languages"
const meter = metrics.getMeter("trpc.collectionPage")
// OpenTelemetry metrics: CollectionPage
export const getCollectionPageCounter = meter.createCounter(
"trpc.contentstack.collectionPage.get"
)
const getCollectionPageRefsCounter = meter.createCounter(
"trpc.contentstack.collectionPage.get"
)
const getCollectionPageRefsFailCounter = meter.createCounter(
"trpc.contentstack.collectionPage.get-fail"
)
const getCollectionPageRefsSuccessCounter = meter.createCounter(
"trpc.contentstack.collectionPage.get-success"
)
export async function fetchCollectionPageRefs(lang: Lang, uid: string) {
getCollectionPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.collectionPage.refs start",
JSON.stringify({
query: { lang, uid },
})
const getCollectionPageRefsCounter = createCounter(
"trpc.contentstack",
"collectionPage.get.refs"
)
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
lang,
uid,
})
metricsGetCollectionPageRefs.start()
const cacheClient = await getCacheClient()
const cacheKey = generateRefsResponseTag(lang, uid)
@@ -61,24 +46,7 @@ export async function fetchCollectionPageRefs(lang: Lang, uid: string) {
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getCollectionPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "http_error",
error: JSON.stringify({
code: notFoundError.code,
}),
})
console.error(
"contentstack.collectionPage.refs not found error",
JSON.stringify({
query: {
lang,
uid,
},
error: { code: notFoundError.code },
})
)
metricsGetCollectionPageRefs.noDataError()
throw notFoundError
}
@@ -90,30 +58,22 @@ export function validateCollectionPageRefs(
lang: Lang,
uid: string
) {
const getCollectionPageRefsCounter = createCounter(
"trpc.contentstack",
"collectionPage.get.refs"
)
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
lang,
uid,
})
const validatedData = collectionPageRefsSchema.safeParse(data)
if (!validatedData.success) {
getCollectionPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "validation_error",
error: JSON.stringify(validatedData.error),
})
console.error(
"contentstack.collectionPage.refs validation error",
JSON.stringify({
query: { lang, uid },
error: validatedData.error,
})
)
metricsGetCollectionPageRefs.validationError(validatedData.error)
return null
}
getCollectionPageRefsSuccessCounter.add(1, { lang, uid })
console.info(
"contentstack.collectionPage.refs success",
JSON.stringify({
query: { lang, uid },
})
)
metricsGetCollectionPageRefs.success()
return validatedData.data
}