feat: harmonize log and metrics
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import {
|
||||
GetAccountPage,
|
||||
GetAccountPageRefs,
|
||||
} from "@/lib/graphql/Query/AccountPage/AccountPage.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 {
|
||||
@@ -26,36 +25,17 @@ import type {
|
||||
GetAccountPageSchema,
|
||||
} from "@/types/trpc/routers/contentstack/accountPage"
|
||||
|
||||
const meter = metrics.getMeter("trpc.accountPage")
|
||||
|
||||
// OpenTelemetry metrics
|
||||
const getAccountPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get"
|
||||
)
|
||||
const getAccountPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get-success"
|
||||
)
|
||||
const getAccountPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get-fail"
|
||||
)
|
||||
const getAccountPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get"
|
||||
)
|
||||
const getAccountPageSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get-success"
|
||||
)
|
||||
const getAccountPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.accountPage.get-fail"
|
||||
)
|
||||
|
||||
export const accountPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
getAccountPageRefsCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.accountPage.refs start",
|
||||
JSON.stringify({ query: { lang, uid } })
|
||||
|
||||
const getAccountPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"accountPage.get.refs"
|
||||
)
|
||||
const metricsRefs = getAccountPageRefsCounter.init({ lang, uid })
|
||||
metricsRefs.start()
|
||||
|
||||
const refsResponse = await request<GetAccountPageRefsSchema>(
|
||||
GetAccountPageRefs,
|
||||
{
|
||||
@@ -70,19 +50,7 @@ export const accountPageQueryRouter = router({
|
||||
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
getAccountPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "not_found",
|
||||
error: JSON.stringify({ code: notFoundError.code }),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.accountPage.refs not found error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: { code: notFoundError.code },
|
||||
})
|
||||
)
|
||||
metricsRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
@@ -90,19 +58,7 @@ export const accountPageQueryRouter = router({
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedAccountPageRefs.success) {
|
||||
getAccountPageRefsFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedAccountPageRefs.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.accountPage.refs validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: validatedAccountPageRefs.error,
|
||||
})
|
||||
)
|
||||
metricsRefs.validationError(validatedAccountPageRefs.error)
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -112,12 +68,16 @@ export const accountPageQueryRouter = router({
|
||||
generateTagsFromSystem(lang, connections),
|
||||
generateTag(lang, validatedAccountPageRefs.data.account_page.system.uid),
|
||||
].flat()
|
||||
getAccountPageRefsSuccessCounter.add(1, { lang, uid, tags })
|
||||
getAccountPageCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.accountPage start",
|
||||
JSON.stringify({ query: { lang, uid } })
|
||||
|
||||
metricsRefs.success()
|
||||
|
||||
const getAccountPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"accountPage.get"
|
||||
)
|
||||
const metrics = getAccountPageCounter.init({ lang, uid })
|
||||
metrics.start()
|
||||
|
||||
const response = await request<GetAccountPageSchema>(
|
||||
GetAccountPage,
|
||||
{
|
||||
@@ -132,45 +92,18 @@ export const accountPageQueryRouter = router({
|
||||
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
getAccountPageFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "not_found",
|
||||
error: JSON.stringify({ code: notFoundError.code }),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.accountPage not found error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: { code: notFoundError.code },
|
||||
})
|
||||
)
|
||||
metrics.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedAccountPage = accountPageSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedAccountPage.success) {
|
||||
getAccountPageFailCounter.add(1, {
|
||||
lang,
|
||||
uid,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedAccountPage.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.accountPage validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
error: validatedAccountPage.error,
|
||||
})
|
||||
)
|
||||
metrics.validationError(validatedAccountPage.error)
|
||||
return null
|
||||
}
|
||||
getAccountPageSuccessCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.accountPage success",
|
||||
JSON.stringify({ query: { lang, uid } })
|
||||
)
|
||||
|
||||
metrics.success()
|
||||
|
||||
const parsedtitle = response.data.account_page.title
|
||||
.replaceAll(" ", "")
|
||||
|
||||
Reference in New Issue
Block a user