feat: harmonize log and metrics
This commit is contained in:
@@ -1,29 +1,27 @@
|
||||
import { GetHotelPage } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import { hotelPageSchema } from "./output"
|
||||
import {
|
||||
getHotelPageCounter,
|
||||
getHotelPageFailCounter,
|
||||
getHotelPageSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import type { GetHotelPageData } from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
|
||||
export const hotelPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
getHotelPageCounter.add(1, { lang, uid: `${uid}` })
|
||||
console.info(
|
||||
"contentstack.hotelPage start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
|
||||
const getHotelPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"hotelPage.get"
|
||||
)
|
||||
const metricsGetHotelPage = getHotelPageCounter.init({ lang, uid })
|
||||
|
||||
metricsGetHotelPage.start()
|
||||
|
||||
const response = await request<GetHotelPageData>(
|
||||
GetHotelPage,
|
||||
{
|
||||
@@ -37,48 +35,19 @@ export const hotelPageQueryRouter = router({
|
||||
)
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
getHotelPageFailCounter.add(1, {
|
||||
lang,
|
||||
uid: `${uid}`,
|
||||
error_type: "not_found",
|
||||
error: JSON.stringify({ code: notFoundError.code }),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPage not found error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: { code: notFoundError.code },
|
||||
})
|
||||
)
|
||||
metricsGetHotelPage.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedHotelPage = hotelPageSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedHotelPage.success) {
|
||||
getHotelPageFailCounter.add(1, {
|
||||
lang,
|
||||
uid: `${uid}`,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedHotelPage.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPage validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: validatedHotelPage.error,
|
||||
})
|
||||
)
|
||||
metricsGetHotelPage.validationError(validatedHotelPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
getHotelPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
||||
console.info(
|
||||
"contentstack.hotelPage success",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
)
|
||||
metricsGetHotelPage.success()
|
||||
|
||||
return validatedHotelPage.data.hotel_page
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
const meter = metrics.getMeter("trpc.contentstack.hotelPage")
|
||||
|
||||
export const getHotelPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
export const getHotelPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
export const getHotelPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
|
||||
export const getHotelPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
export const getHotelPageSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
export const getHotelPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
|
||||
export const getHotelPageUrlsCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get"
|
||||
)
|
||||
export const getHotelPageUrlsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get-success"
|
||||
)
|
||||
export const getHotelPageUrlsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get-fail"
|
||||
)
|
||||
|
||||
export const getHotelPageCountCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageCount.get"
|
||||
)
|
||||
export const getHotelPageCountSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageCount.get-success"
|
||||
)
|
||||
export const getHotelPageCountFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageCount.get-fail"
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user