feat: harmonize log and metrics
This commit is contained in:
@@ -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}`
|
||||
|
||||
Reference in New Issue
Block a user