feat: harmonize log and metrics
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
GetContentPageBlocksBatch1,
|
||||
GetContentPageBlocksBatch2,
|
||||
} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import { contentPageSchema } from "./output"
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
createPageType,
|
||||
fetchContentPageRefs,
|
||||
generatePageTags,
|
||||
getContentPageCounter,
|
||||
} from "./utils"
|
||||
|
||||
import type { TrackingSDKPageData } from "@/types/components/tracking"
|
||||
@@ -30,13 +30,16 @@ export const contentPageQueryRouter = router({
|
||||
|
||||
const tags = generatePageTags(contentPageRefs, lang)
|
||||
|
||||
getContentPageCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.contentPage start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
const getContentPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"contentPage.get"
|
||||
)
|
||||
const metricsGetContentPage = getContentPageCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetContentPage.start()
|
||||
|
||||
const contentPageRequest = await batchRequest<GetContentPageSchema>([
|
||||
{
|
||||
@@ -69,13 +72,12 @@ export const contentPageQueryRouter = router({
|
||||
|
||||
const contentPage = contentPageSchema.safeParse(contentPageRequest.data)
|
||||
if (!contentPage.success) {
|
||||
console.error(
|
||||
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
||||
)
|
||||
console.error(contentPage.error?.format())
|
||||
metricsGetContentPage.validationError(contentPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetContentPage.success()
|
||||
|
||||
const tracking: TrackingSDKPageData = {
|
||||
pageId: contentPage.data.content_page.system.uid,
|
||||
domainLanguage: lang,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { batchRequest } from "@/lib/graphql/batchRequest"
|
||||
import {
|
||||
GetContentPageBlocksRefs,
|
||||
GetContentPageRefs,
|
||||
} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -18,37 +17,24 @@ import { contentPageRefsSchema } from "./output"
|
||||
import { TrackingChannelEnum } from "@/types/components/tracking"
|
||||
import { ContentPageEnum } from "@/types/enums/contentPage"
|
||||
import type { System } from "@/types/requests/system"
|
||||
import type {
|
||||
ContentPageRefs,
|
||||
GetContentPageRefsSchema,
|
||||
import {
|
||||
type ContentPageRefs,
|
||||
type GetContentPageRefsSchema,
|
||||
} from "@/types/trpc/routers/contentstack/contentPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
const meter = metrics.getMeter("trpc.contentPage")
|
||||
// OpenTelemetry metrics: ContentPage
|
||||
|
||||
export const getContentPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.contentPage.get"
|
||||
)
|
||||
|
||||
const getContentPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.contentPage.get"
|
||||
)
|
||||
const getContentPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.contentPage.get-fail"
|
||||
)
|
||||
const getContentPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.contentPage.get-success"
|
||||
)
|
||||
|
||||
export async function fetchContentPageRefs(lang: Lang, uid: string) {
|
||||
getContentPageRefsCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.contentPage.refs start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
const getContentPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"contentPage.get.refs"
|
||||
)
|
||||
const metricsGetContentPageRefs = getContentPageRefsCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetContentPageRefs.start()
|
||||
|
||||
const res = await batchRequest<GetContentPageRefsSchema>([
|
||||
{
|
||||
document: GetContentPageRefs,
|
||||
@@ -69,50 +55,17 @@ export async function fetchContentPageRefs(lang: Lang, uid: string) {
|
||||
])
|
||||
if (!res.data) {
|
||||
const notFoundError = notFound(res)
|
||||
getContentPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
code: notFoundError.code,
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.contentPage.refs not found error",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
lang,
|
||||
uid,
|
||||
},
|
||||
error: { code: notFoundError.code },
|
||||
})
|
||||
)
|
||||
metricsGetContentPageRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedData = contentPageRefsSchema.safeParse(res.data)
|
||||
if (!validatedData.success) {
|
||||
getContentPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedData.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.contentPage.refs validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: validatedData.error,
|
||||
})
|
||||
)
|
||||
metricsGetContentPageRefs.validationError(validatedData.error)
|
||||
return null
|
||||
}
|
||||
getContentPageRefsSuccessCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.contentPage.refs success",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
)
|
||||
|
||||
metricsGetContentPageRefs.success()
|
||||
|
||||
return validatedData.data
|
||||
}
|
||||
@@ -142,8 +95,7 @@ export function getConnections({ content_page }: ContentPageRefs) {
|
||||
case ContentPageEnum.ContentStack.blocks.Content:
|
||||
{
|
||||
if (block.content.length) {
|
||||
// TS has trouble infering the filtered types
|
||||
// @ts-ignore
|
||||
// @ts-expect-error: TS has trouble infering the filtered types
|
||||
connections.push(...block.content)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user