feat: add metrics

This commit is contained in:
Arvid Norlin
2024-08-22 16:50:55 +02:00
parent fac0d7acdc
commit 0b2936159f
9 changed files with 507 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
import { metrics } from "@opentelemetry/api"
import { Lang } from "@/constants/languages"
import {
GetLoyaltyPage,
@@ -33,9 +35,31 @@ import {
TrackingSDKPageData,
} from "@/types/components/tracking"
const meter = metrics.getMeter("trpc.loyaltyPage")
// OpenTelemetry metrics: LoyaltyPage
const getLoyaltyPageRefsCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get"
)
const getLoyaltyPageRefsSuccessCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get-success"
)
const getLoyaltyPageRefsFailCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get-fail"
)
const getLoyaltyPageCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get"
)
const getLoyaltyPageSuccessCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get-success"
)
const getLoyaltyPageFailCounter = meter.createCounter(
"trpc.contentstack.loyaltyPage.get-fail"
)
export const loyaltyPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
getLoyaltyPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.loyaltyPage.refs start",
JSON.stringify({
@@ -55,6 +79,14 @@ export const loyaltyPageQueryRouter = router({
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getLoyaltyPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "http_error",
error: JSON.stringify({
code: notFoundError.code,
}),
})
console.error(
"contentstack.loyaltyPage.refs not found error",
JSON.stringify({
@@ -73,6 +105,12 @@ export const loyaltyPageQueryRouter = router({
const validatedLoyaltyPageRefs =
validateLoyaltyPageRefsSchema.safeParse(cleanedData)
if (!validatedLoyaltyPageRefs.success) {
getLoyaltyPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "validation_error",
error: JSON.stringify(validatedLoyaltyPageRefs.error),
})
console.error(
"contentstack.loyaltyPage.refs validation error",
JSON.stringify({
@@ -82,6 +120,7 @@ export const loyaltyPageQueryRouter = router({
)
return null
}
getLoyaltyPageRefsSuccessCounter.add(1, { lang, uid })
console.info(
"contentstack.loyaltyPage.refs success",
JSON.stringify({
@@ -94,6 +133,7 @@ export const loyaltyPageQueryRouter = router({
generateTags(lang, connections),
generateTag(lang, validatedLoyaltyPageRefs.data.loyalty_page.system.uid),
].flat()
getLoyaltyPageCounter.add(1, { lang, uid })
console.info(
"contentstack.loyaltyPage start",
JSON.stringify({
@@ -111,7 +151,12 @@ export const loyaltyPageQueryRouter = router({
if (!response.data) {
const notFoundError = notFound(response)
getLoyaltyPageFailCounter.add(1, {
lang,
uid,
error_type: "http_error",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.loyaltyPage not found error",
JSON.stringify({
@@ -227,6 +272,12 @@ export const loyaltyPageQueryRouter = router({
const validatedLoyaltyPage =
validateLoyaltyPageSchema.safeParse(loyaltyPage)
if (!validatedLoyaltyPage.success) {
getLoyaltyPageFailCounter.add(1, {
lang,
uid,
error_type: "validation_error",
error: JSON.stringify(validatedLoyaltyPage.error),
})
console.error(
"contentstack.loyaltyPage validation error",
JSON.stringify({
@@ -245,6 +296,7 @@ export const loyaltyPageQueryRouter = router({
channel: TrackingChannelEnum["scandic-friends"],
pageType: "loyaltycontentpage",
}
getLoyaltyPageSuccessCounter.add(1, { lang, uid })
console.info(
"contentstack.loyaltyPage success",
JSON.stringify({ query: { lang, uid } })