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

@@ -4,6 +4,7 @@ import {
} from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPage.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { createCounter } from "@/server/telemetry"
import { contentStackUidWithServiceProcedure, router } from "@/server/trpc"
import { generateRefsResponseTag } from "@/utils/generateTag"
@@ -13,14 +14,6 @@ import {
destinationCityPageRefsSchema,
destinationCityPageSchema,
} from "./output"
import {
getDestinationCityPageCounter,
getDestinationCityPageFailCounter,
getDestinationCityPageRefsCounter,
getDestinationCityPageRefsFailCounter,
getDestinationCityPageRefsSuccessCounter,
getDestinationCityPageSuccessCounter,
} from "./telemetry"
import { generatePageTags } from "./utils"
import {
@@ -36,11 +29,14 @@ export const destinationCityPageQueryRouter = router({
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
const { lang, uid, serviceToken } = ctx
getDestinationCityPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.destinationCityPage.refs start",
JSON.stringify({ query: { lang, uid } })
const getDestinationCityPageRefsCounter = createCounter(
"trpc.contentstack",
"destinationCityPage.get.refs"
)
const metricsGetDestinationCityPageRefs =
getDestinationCityPageRefsCounter.init({ lang, uid })
metricsGetDestinationCityPageRefs.start()
const refsResponse = await request<GetDestinationCityPageRefsSchema>(
GetDestinationCityPageRefs,
@@ -53,19 +49,7 @@ export const destinationCityPageQueryRouter = router({
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getDestinationCityPageRefsFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.destinationCityPage.refs not found error",
JSON.stringify({
query: { lang, uid },
error: { code: notFoundError.code },
})
)
metricsGetDestinationCityPageRefs.noDataError()
throw notFoundError
}
@@ -73,33 +57,25 @@ export const destinationCityPageQueryRouter = router({
refsResponse.data
)
if (!validatedRefsData.success) {
getDestinationCityPageRefsFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "validation_error",
error: JSON.stringify(validatedRefsData.error),
})
console.error(
"contentstack.destinationCityPage.refs validation error",
JSON.stringify({ query: { lang, uid }, error: validatedRefsData.error })
)
metricsGetDestinationCityPageRefs.validationError(validatedRefsData.error)
return null
}
getDestinationCityPageRefsSuccessCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCityPage.refs success",
JSON.stringify({ query: { lang, uid } })
)
metricsGetDestinationCityPageRefs.success()
const tags = generatePageTags(validatedRefsData.data, lang)
getDestinationCityPageCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCityPage start",
JSON.stringify({
query: { lang, uid },
})
const getDestinationCityPageCounter = createCounter(
"trpc.contentstack",
"destinationCityPage.get"
)
const metricsGetDestinationCityPage = getDestinationCityPageCounter.init({
lang,
uid,
})
metricsGetDestinationCityPage.start()
const response = await request<GetDestinationCityPageData>(
GetDestinationCityPage,
{
@@ -113,40 +89,17 @@ export const destinationCityPageQueryRouter = router({
)
if (!response.data) {
const notFoundError = notFound(response)
getDestinationCityPageFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.destinationCityPage not found error",
JSON.stringify({
query: { lang, uid },
error: { code: notFoundError.code },
})
)
metricsGetDestinationCityPage.noDataError()
throw notFoundError
}
const validatedResponse = destinationCityPageSchema.safeParse(response.data)
if (!validatedResponse.success) {
getDestinationCityPageFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.destinationCityPage validation error",
JSON.stringify({
query: { lang, uid },
error: validatedResponse.error,
})
)
metricsGetDestinationCityPage.validationError(validatedResponse.error)
return null
}
const destinationCityPage = validatedResponse.data.destination_city_page
const cityIdentifier = destinationCityPage.destination_settings.city
if (!cityIdentifier) {
@@ -160,30 +113,16 @@ export const destinationCityPageQueryRouter = router({
})
if (!city) {
getDestinationCityPageFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "not_found",
error: `Couldn't find city with cityIdentifier: ${cityIdentifier}`,
})
console.error(
"contentstack.destinationCityPage not found error",
JSON.stringify({
query: { lang, uid },
error: `Couldn't find city with cityIdentifier: ${cityIdentifier}`,
})
metricsGetDestinationCityPage.dataError(
`Failed to get city data for ${cityIdentifier}`,
{
cityIdentifier,
}
)
return null
}
getDestinationCityPageSuccessCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCityPage success",
JSON.stringify({
query: { lang, uid },
})
)
metricsGetDestinationCityPage.success()
const system = destinationCityPage.system
const pageName = `destinations|${city.country}|${city.name}`

View File

@@ -1,43 +0,0 @@
import { metrics } from "@opentelemetry/api"
const meter = metrics.getMeter("trpc.contentstack.destinationCityPage")
export const getDestinationCityPageRefsCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get"
)
export const getDestinationCityPageRefsFailCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get-fail"
)
export const getDestinationCityPageRefsSuccessCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get-success"
)
export const getDestinationCityPageCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get"
)
export const getDestinationCityPageSuccessCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get-success"
)
export const getDestinationCityPageFailCounter = meter.createCounter(
"trpc.contentstack.destinationCityPage.get-fail"
)
export const getCityPageCountCounter = meter.createCounter(
"trpc.contentstack.cityPageCount.get"
)
export const getCityPageCountSuccessCounter = meter.createCounter(
"trpc.contentstack.cityPageCount.get-success"
)
export const getCityPageCountFailCounter = meter.createCounter(
"trpc.contentstack.cityPageCount.get-fail"
)
export const getCityPageUrlsCounter = meter.createCounter(
"trpc.contentstack.cityPageUrls.get"
)
export const getCityPageUrlsSuccessCounter = meter.createCounter(
"trpc.contentstack.cityPageUrls.get-success"
)
export const getCityPageUrlsFailCounter = meter.createCounter(
"trpc.contentstack.cityPageUrls.get-fail"
)

View File

@@ -1,18 +1,11 @@
import { GetCityPageCount } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPageCount.graphql"
import { GetCityPageUrls } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPageUrl.graphql"
import { request } from "@/lib/graphql/request"
import { createCounter } from "@/server/telemetry"
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
import { batchedCityPageUrlsSchema, cityPageCountSchema } from "./output"
import {
getCityPageCountCounter,
getCityPageCountFailCounter,
getCityPageCountSuccessCounter,
getCityPageUrlsCounter,
getCityPageUrlsFailCounter,
getCityPageUrlsSuccessCounter,
} from "./telemetry"
import { DestinationCityPageEnum } from "@/types/enums/destinationCityPage"
import type { System } from "@/types/requests/system"
@@ -76,11 +69,13 @@ export function getConnections({
}
export async function getCityPageCount(lang: Lang) {
getCityPageCountCounter.add(1, { lang })
console.info(
"contentstack.cityPageCount start",
JSON.stringify({ query: { lang } })
const getCityPageCountCounter = createCounter(
"trpc.contentstack",
"cityPageCount.get"
)
const metricsGetCityPageCount = getCityPageCountCounter.init({ lang })
metricsGetCityPageCount.start()
const response = await request<GetCityPageCountData>(
GetCityPageCount,
@@ -92,15 +87,10 @@ export async function getCityPageCount(lang: Lang) {
ttl: "max",
}
)
if (!response.data) {
getCityPageCountFailCounter.add(1, {
lang,
error_type: "not_found",
error: `City pages count not found for lang: ${lang}`,
})
console.error(
"contentstack.cityPageCount not found error",
JSON.stringify({ query: { lang } })
metricsGetCityPageCount.dataError(
`Failed to get city pages count for ${lang}`
)
return 0
}
@@ -108,35 +98,24 @@ export async function getCityPageCount(lang: Lang) {
const validatedResponse = cityPageCountSchema.safeParse(response.data)
if (!validatedResponse.success) {
getCityPageCountFailCounter.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,
})
)
metricsGetCityPageCount.validationError(validatedResponse.error)
return 0
}
getCityPageCountSuccessCounter.add(1, { lang })
console.info(
"contentstack.cityPageCount success",
JSON.stringify({ query: { lang } })
)
metricsGetCityPageCount.success()
return validatedResponse.data
}
export async function getCityPageUrls(lang: Lang) {
getCityPageUrlsCounter.add(1, { lang })
console.info(
"contentstack.cityPageUrls start",
JSON.stringify({ query: { lang } })
const getCityPageUrlsCounter = createCounter(
"trpc.contentstack",
"cityPageUrls.get"
)
const metricsGetCityPageUrls = getCityPageUrlsCounter.init({ lang })
metricsGetCityPageUrls.start()
const count = await getCityPageCount(lang)
if (count === 0) {
@@ -162,25 +141,11 @@ export async function getCityPageUrls(lang: Lang) {
const validatedResponse = batchedCityPageUrlsSchema.safeParse(batchedResponse)
if (!validatedResponse.success) {
getCityPageUrlsFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.cityPageUrls validation error",
JSON.stringify({
query: { lang },
error: validatedResponse.error,
})
)
metricsGetCityPageUrls.validationError(validatedResponse.error)
return []
}
getCityPageUrlsSuccessCounter.add(1, { lang })
console.info(
"contentstack.cityPageUrls success",
JSON.stringify({ query: { lang } })
)
metricsGetCityPageUrls.success()
return validatedResponse.data
}