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

@@ -3,6 +3,7 @@ import { GetHotelPageCount } from "@/lib/graphql/Query/HotelPage/HotelPageCount.
import { GetHotelPageUrls } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { createCounter } from "@/server/telemetry"
import {
generateRefsResponseTag,
@@ -15,17 +16,6 @@ import {
hotelPageCountSchema,
hotelPageRefsSchema,
} from "./output"
import {
getHotelPageCountCounter,
getHotelPageCountFailCounter,
getHotelPageCountSuccessCounter,
getHotelPageRefsCounter,
getHotelPageRefsFailCounter,
getHotelPageRefsSuccessCounter,
getHotelPageUrlsCounter,
getHotelPageUrlsFailCounter,
getHotelPageUrlsSuccessCounter,
} from "./telemetry"
import { HotelPageEnum } from "@/types/enums/hotelPage"
import type { System } from "@/types/requests/system"
@@ -38,13 +28,13 @@ import type {
import type { Lang } from "@/constants/languages"
export async function fetchHotelPageRefs(lang: Lang, uid: string) {
getHotelPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.hotelPage.refs start",
JSON.stringify({
query: { lang, uid },
})
const getHotelPageRefsCounter = createCounter(
"trpc.contentstack",
"hotelPage.get.refs"
)
const metricsGetHotelPageRefs = getHotelPageRefsCounter.init({ lang, uid })
metricsGetHotelPageRefs.start()
const refsResponse = await request<GetHotelPageRefsSchema>(
GetHotelPageRefs,
@@ -56,26 +46,10 @@ export async function fetchHotelPageRefs(lang: Lang, uid: string) {
)
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getHotelPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "http_error",
error: JSON.stringify({
code: notFoundError.code,
}),
})
console.error(
"contentstack.hotelPage.refs not found error",
JSON.stringify({
query: {
lang,
uid,
},
error: { code: notFoundError.code },
})
)
metricsGetHotelPageRefs.noDataError()
throw notFoundError
}
return refsResponse.data
}
@@ -84,30 +58,20 @@ export function validateHotelPageRefs(
lang: Lang,
uid: string
) {
const getHotelPageRefsCounter = createCounter(
"trpc.contentstack",
"hotelPage.get.refs"
)
const metricsGetHotelPageRefs = getHotelPageRefsCounter.init({ lang, uid })
const validatedData = hotelPageRefsSchema.safeParse(data)
if (!validatedData.success) {
getHotelPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "validation_error",
error: JSON.stringify(validatedData.error),
})
console.error(
"contentstack.hotelPage.refs validation error",
JSON.stringify({
query: { lang, uid },
error: validatedData.error,
})
)
metricsGetHotelPageRefs.validationError(validatedData.error)
return null
}
getHotelPageRefsSuccessCounter.add(1, { lang, uid })
console.info(
"contentstack.hotelPage.refs success",
JSON.stringify({
query: { lang, uid },
})
)
metricsGetHotelPageRefs.success()
return validatedData.data
}
@@ -144,11 +108,14 @@ export function getConnections({ hotel_page }: HotelPageRefs) {
}
export async function getHotelPageCount(lang: Lang) {
getHotelPageCountCounter.add(1, { lang })
console.info(
"contentstack.hotelPageCount start",
JSON.stringify({ query: { lang } })
const getHotelPageCountCounter = createCounter(
"trpc.contentstack",
"hotelPageCount.get"
)
const metricsGetHotelPageCount = getHotelPageCountCounter.init({ lang })
metricsGetHotelPageCount.start()
const response = await request<GetHotelPageCountData>(
GetHotelPageCount,
{
@@ -161,50 +128,31 @@ export async function getHotelPageCount(lang: Lang) {
)
if (!response.data) {
getHotelPageCountFailCounter.add(1, {
lang,
error_type: "not_found",
error: `Hotel pages count not found for lang: ${lang}`,
})
console.error(
"contentstack.hotelPageCount not found error",
JSON.stringify({ query: { lang } })
)
metricsGetHotelPageCount.noDataError()
return 0
}
const validatedResponse = hotelPageCountSchema.safeParse(response.data)
if (!validatedResponse.success) {
getHotelPageCountFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.hotelPageCount validation error",
JSON.stringify({
query: { lang },
error: validatedResponse.error,
})
)
metricsGetHotelPageCount.validationError(validatedResponse.error)
return 0
}
getHotelPageCountSuccessCounter.add(1, { lang })
console.info(
"contentstack.hotelPageCount success",
JSON.stringify({ query: { lang } })
)
metricsGetHotelPageCount.success()
return validatedResponse.data
}
export async function getHotelPageUrls(lang: Lang) {
getHotelPageUrlsCounter.add(1, { lang })
console.info(
"contentstack.hotelPageUrls start",
JSON.stringify({ query: { lang } })
const getHotelPageUrlsCounter = createCounter(
"trpc.contentstack",
"hotelPageUrls.get"
)
const metricsGetHotelPageUrls = getHotelPageUrlsCounter.init({ lang })
metricsGetHotelPageUrls.start()
const count = await getHotelPageCount(lang)
if (count === 0) {
@@ -236,25 +184,11 @@ export async function getHotelPageUrls(lang: Lang) {
batchedHotelPageUrlsSchema.safeParse(batchedResponse)
if (!validatedResponse.success) {
getHotelPageUrlsFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.hotelPageUrls validation error",
JSON.stringify({
query: { lang },
error: validatedResponse.error,
})
)
metricsGetHotelPageUrls.validationError(validatedResponse.error)
return []
}
getHotelPageUrlsSuccessCounter.add(1, { lang })
console.info(
"contentstack.hotelPageUrl success",
JSON.stringify({ query: { lang } })
)
metricsGetHotelPageUrls.success()
return validatedResponse.data
}