feat: harmonize log and metrics
This commit is contained in:
@@ -7,8 +7,8 @@ import {
|
||||
cardsGridSchema,
|
||||
} from "../schemas/blocks/cardsGrid"
|
||||
import {
|
||||
dynamicContentSchema as blockDynamicContentSchema,
|
||||
dynamicContentRefsSchema,
|
||||
dynamicContentSchema as blockDynamicContentSchema,
|
||||
} from "../schemas/blocks/dynamicContent"
|
||||
import {
|
||||
shortcutsRefsSchema,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { GetCollectionPage } from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import { collectionPageSchema } from "./output"
|
||||
import {
|
||||
fetchCollectionPageRefs,
|
||||
generatePageTags,
|
||||
getCollectionPageCounter,
|
||||
validateCollectionPageRefs,
|
||||
} from "./utils"
|
||||
|
||||
@@ -32,13 +32,16 @@ export const collectionPageQueryRouter = router({
|
||||
}
|
||||
const tags = generatePageTags(collectionPageRefs, lang)
|
||||
|
||||
getCollectionPageCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.collectionPage start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
const getCollectionPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"collectionPage.get"
|
||||
)
|
||||
const metricsGetCollectionPage = getCollectionPageCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetCollectionPage.start()
|
||||
|
||||
const response = await request<GetCollectionPageSchema>(
|
||||
GetCollectionPage,
|
||||
@@ -51,13 +54,12 @@ export const collectionPageQueryRouter = router({
|
||||
|
||||
const collectionPage = collectionPageSchema.safeParse(response.data)
|
||||
if (!collectionPage.success) {
|
||||
console.error(
|
||||
`Failed to validate CollectionPage Data - (lang: ${lang}, uid: ${uid})`
|
||||
)
|
||||
console.error(collectionPage.error?.format())
|
||||
metricsGetCollectionPage.validationError(collectionPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetCollectionPage.success()
|
||||
|
||||
const tracking: TrackingSDKPageData = {
|
||||
pageId: collectionPage.data.collection_page.system.uid,
|
||||
domainLanguage: lang,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { GetCollectionPageRefs } from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
|
||||
import { getCacheClient } from "@/services/dataCache"
|
||||
import {
|
||||
@@ -21,31 +20,17 @@ import type {
|
||||
} from "@/types/trpc/routers/contentstack/collectionPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
const meter = metrics.getMeter("trpc.collectionPage")
|
||||
// OpenTelemetry metrics: CollectionPage
|
||||
|
||||
export const getCollectionPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.collectionPage.get"
|
||||
)
|
||||
|
||||
const getCollectionPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.collectionPage.get"
|
||||
)
|
||||
const getCollectionPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.collectionPage.get-fail"
|
||||
)
|
||||
const getCollectionPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.collectionPage.get-success"
|
||||
)
|
||||
|
||||
export async function fetchCollectionPageRefs(lang: Lang, uid: string) {
|
||||
getCollectionPageRefsCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.collectionPage.refs start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
const getCollectionPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"collectionPage.get.refs"
|
||||
)
|
||||
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetCollectionPageRefs.start()
|
||||
|
||||
const cacheClient = await getCacheClient()
|
||||
const cacheKey = generateRefsResponseTag(lang, uid)
|
||||
@@ -61,24 +46,7 @@ export async function fetchCollectionPageRefs(lang: Lang, uid: string) {
|
||||
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
getCollectionPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
code: notFoundError.code,
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.collectionPage.refs not found error",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
lang,
|
||||
uid,
|
||||
},
|
||||
error: { code: notFoundError.code },
|
||||
})
|
||||
)
|
||||
metricsGetCollectionPageRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
@@ -90,30 +58,22 @@ export function validateCollectionPageRefs(
|
||||
lang: Lang,
|
||||
uid: string
|
||||
) {
|
||||
const getCollectionPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"collectionPage.get.refs"
|
||||
)
|
||||
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
const validatedData = collectionPageRefsSchema.safeParse(data)
|
||||
if (!validatedData.success) {
|
||||
getCollectionPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedData.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.collectionPage.refs validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: validatedData.error,
|
||||
})
|
||||
)
|
||||
metricsGetCollectionPageRefs.validationError(validatedData.error)
|
||||
return null
|
||||
}
|
||||
getCollectionPageRefsSuccessCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.collectionPage.refs success",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
)
|
||||
|
||||
metricsGetCollectionPageRefs.success()
|
||||
|
||||
return validatedData.data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user