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/DestinationCountryPage/DestinationCountryPage.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { createCounter } from "@/server/telemetry"
import {
contentStackBaseWithServiceProcedure,
contentstackExtendedProcedureUID,
@@ -17,14 +18,6 @@ import {
destinationCountryPageRefsSchema,
destinationCountryPageSchema,
} from "./output"
import {
getDestinationCountryPageCounter,
getDestinationCountryPageFailCounter,
getDestinationCountryPageRefsCounter,
getDestinationCountryPageRefsFailCounter,
getDestinationCountryPageRefsSuccessCounter,
getDestinationCountryPageSuccessCounter,
} from "./telemetry"
import { generatePageTags, getCityPages } from "./utils"
import {
@@ -41,11 +34,14 @@ export const destinationCountryPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
getDestinationCountryPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.destinationCountryPage.refs start",
JSON.stringify({ query: { lang, uid } })
const getDestinationCountryPageRefsCounter = createCounter(
"trpc.contentstack",
"destinationCountryPage.get.refs"
)
const metricsGetDestinationCountryPageRefs =
getDestinationCountryPageRefsCounter.init({ lang, uid })
metricsGetDestinationCountryPageRefs.start()
const refsResponse = await request<GetDestinationCountryPageRefsSchema>(
GetDestinationCountryPageRefs,
@@ -58,19 +54,7 @@ export const destinationCountryPageQueryRouter = router({
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getDestinationCountryPageRefsFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.destinationCountryPage.refs not found error",
JSON.stringify({
query: { lang, uid },
error: { code: notFoundError.code },
})
)
metricsGetDestinationCountryPageRefs.noDataError()
throw notFoundError
}
@@ -78,33 +62,25 @@ export const destinationCountryPageQueryRouter = router({
refsResponse.data
)
if (!validatedRefsData.success) {
getDestinationCountryPageRefsFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "validation_error",
error: JSON.stringify(validatedRefsData.error),
})
console.error(
"contentstack.destinationCountryPage.refs validation error",
JSON.stringify({ query: { lang, uid }, error: validatedRefsData.error })
metricsGetDestinationCountryPageRefs.validationError(
validatedRefsData.error
)
return null
}
getDestinationCountryPageRefsSuccessCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCountryPage.refs success",
JSON.stringify({ query: { lang, uid } })
)
metricsGetDestinationCountryPageRefs.success()
const tags = generatePageTags(validatedRefsData.data, lang)
getDestinationCountryPageCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCountryPage start",
JSON.stringify({
query: { lang, uid },
})
const getDestinationCountryPageCounter = createCounter(
"trpc.contentstack",
"destinationCountryPage.get"
)
const metricsGetDestinationCountryPage =
getDestinationCountryPageCounter.init({ lang, uid })
metricsGetDestinationCountryPage.start()
const response = await request<GetDestinationCountryPageData>(
GetDestinationCountryPage,
{
@@ -118,19 +94,7 @@ export const destinationCountryPageQueryRouter = router({
)
if (!response.data) {
const notFoundError = notFound(response)
getDestinationCountryPageFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.destinationCountryPage not found error",
JSON.stringify({
query: { lang, uid },
error: { code: notFoundError.code },
})
)
metricsGetDestinationCountryPage.noDataError()
throw notFoundError
}
@@ -139,32 +103,15 @@ export const destinationCountryPageQueryRouter = router({
)
if (!validatedResponse.success) {
getDestinationCountryPageFailCounter.add(1, {
lang,
uid: `${uid}`,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.destinationCountryPage validation error",
JSON.stringify({
query: { lang, uid },
error: validatedResponse.error,
})
)
metricsGetDestinationCountryPage.validationError(validatedResponse.error)
return null
}
const destinationCountryPage =
validatedResponse.data.destination_country_page
const country = destinationCountryPage.destination_settings.country
getDestinationCountryPageSuccessCounter.add(1, { lang, uid: `${uid}` })
console.info(
"contentstack.destinationCountryPage success",
JSON.stringify({
query: { lang, uid },
})
)
metricsGetDestinationCountryPage.success()
const system = destinationCountryPage.system
const pageName = `destinations|${country}`

View File

@@ -1,45 +0,0 @@
import { metrics } from "@opentelemetry/api"
const meter = metrics.getMeter("trpc.contentstack.destinationCountryPage")
export const getDestinationCountryPageRefsCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get"
)
export const getDestinationCountryPageRefsFailCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get-fail"
)
export const getDestinationCountryPageRefsSuccessCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get-success"
)
export const getDestinationCountryPageCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get"
)
export const getDestinationCountryPageSuccessCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get-success"
)
export const getDestinationCountryPageFailCounter = meter.createCounter(
"trpc.contentstack.destinationCountryPage.get-fail"
)
export const getCityListDataCounter = meter.createCounter(
"trpc.contentstack.cityListData.get"
)
export const getCityListDataSuccessCounter = meter.createCounter(
"trpc.contentstack.cityListData.get-success"
)
export const getCityListDataFailCounter = meter.createCounter(
"trpc.contentstack.cityListData.get-fail"
)
export const getCountryPageUrlsCounter = meter.createCounter(
"trpc.contentstack.getCountryPageUrls"
)
export const getCountryPageUrlsSuccessCounter = meter.createCounter(
"trpc.contentstack.getCountryPageUrls-success"
)
export const getCountryPageUrlsFailCounter = meter.createCounter(
"trpc.contentstack.getCountryPageUrls-fail"
)

View File

@@ -1,20 +1,13 @@
import { GetDestinationCityListData } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityListData.graphql"
import { GetCountryPageUrls } from "@/lib/graphql/Query/DestinationCountryPage/DestinationCountryPageUrl.graphql"
import { request } from "@/lib/graphql/request"
import { createCounter } from "@/server/telemetry"
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
import { getCitiesByCountry } from "../../hotels/utils"
import { destinationCityListDataSchema } from "../destinationCityPage/output"
import { countryPageUrlsSchema } from "./output"
import {
getCityListDataCounter,
getCityListDataFailCounter,
getCityListDataSuccessCounter,
getCountryPageUrlsCounter,
getCountryPageUrlsFailCounter,
getCountryPageUrlsSuccessCounter,
} from "./telemetry"
import { ApiCountry, type Country } from "@/types/enums/country"
import { DestinationCountryPageEnum } from "@/types/enums/destinationCountryPage"
@@ -77,11 +70,16 @@ export async function getCityListDataByCityIdentifier(
lang: Lang,
cityIdentifier: string
) {
getCityListDataCounter.add(1, { lang, cityIdentifier })
console.info(
"contentstack.cityListData start",
JSON.stringify({ query: { lang, cityIdentifier } })
const getCityListDataCounter = createCounter(
"trpc.contentstack",
"cityListData.get"
)
const metricsGetCityListData = getCityListDataCounter.init({
lang,
cityIdentifier,
})
metricsGetCityListData.start()
const response = await request<GetDestinationCityListDataResponse>(
GetDestinationCityListData,
@@ -96,15 +94,8 @@ export async function getCityListDataByCityIdentifier(
)
if (!response.data) {
getCityListDataFailCounter.add(1, {
lang,
cityIdentifier,
error_type: "not_found",
error: `Destination city page not found for cityIdentifier: ${cityIdentifier}`,
})
console.error(
"contentstack.cityListData not found error",
JSON.stringify({ query: { lang, cityIdentifier } })
metricsGetCityListData.dataError(
`Failed to get destination city page for cityIdentifier: ${cityIdentifier}`
)
return null
}
@@ -114,26 +105,11 @@ export async function getCityListDataByCityIdentifier(
)
if (!validatedResponse.success) {
getCityListDataFailCounter.add(1, {
lang,
cityIdentifier,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.cityListData validation error",
JSON.stringify({
query: { lang, cityIdentifier },
error: validatedResponse.error,
})
)
metricsGetCityListData.validationError(validatedResponse.error)
return null
}
getCityListDataSuccessCounter.add(1, { lang, cityIdentifier })
console.info(
"contentstack.cityListData success",
JSON.stringify({ query: { lang, cityIdentifier } })
)
metricsGetCityListData.success()
return validatedResponse.data
}
@@ -172,11 +148,13 @@ export async function getCityPages(
}
export async function getCountryPageUrls(lang: Lang) {
getCountryPageUrlsCounter.add(1, { lang })
console.info(
"contentstack.countryPageUrls start",
JSON.stringify({ query: { lang } })
const getCountryPageUrlsCounter = createCounter(
"trpc.contentstack",
"getCountryPageUrls"
)
const metricsGetCountryPageUrls = getCountryPageUrlsCounter.init({ lang })
metricsGetCountryPageUrls.start()
const tag = `${lang}:country_page_urls`
const response = await request<GetCountryPageUrlsData>(
@@ -191,14 +169,8 @@ export async function getCountryPageUrls(lang: Lang) {
)
if (!response.data) {
getCountryPageUrlsFailCounter.add(1, {
lang,
error_type: "not_found",
error: `Country pages not found for lang: ${lang}`,
})
console.error(
"contentstack.countryPageUrls not found error",
JSON.stringify({ query: { lang } })
metricsGetCountryPageUrls.dataError(
`Failed to get country pages for lang: ${lang}`
)
return []
}
@@ -208,26 +180,11 @@ export async function getCountryPageUrls(lang: Lang) {
)
if (!validatedCountryPageUrls.success) {
getCountryPageUrlsFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedCountryPageUrls.error),
})
console.error(
"contentstack.countryPageUrls validation error",
JSON.stringify({
query: { lang },
error: validatedCountryPageUrls.error,
})
)
metricsGetCountryPageUrls.validationError(validatedCountryPageUrls.error)
return []
}
getCountryPageUrlsSuccessCounter.add(1, { lang })
console.info(
"contentstack.countryPageUrls success",
JSON.stringify({ query: { lang } })
)
metricsGetCountryPageUrls.success()
return validatedCountryPageUrls.data
}