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 { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql"
import {
GetCurrentFooter,
@@ -26,11 +28,58 @@ import {
validateHeaderConfigSchema,
} from "./output"
const meter = metrics.getMeter("trpc.contentstack.base")
// OpenTelemetry metrics: ContactConfig
const getContactConfigCounter = meter.createCounter(
"trpc.contentstack.contactConfig.get"
)
const getContactConfigSuccessCounter = meter.createCounter(
"trpc.contentstack.contactConfig.get-success"
)
const getContactConfigFailCounter = meter.createCounter(
"trpc.contentstack.contactConfig.get-fail"
)
// OpenTelemetry metrics: Header
const getHeaderRefCounter = meter.createCounter(
"trpc.contentstack.header.ref.get"
)
const getHeaderRefSuccessCounter = meter.createCounter(
"trpc.contentstack.header.ref.get-success"
)
const getHeaderRefFailCounter = meter.createCounter(
"trpc.contentstack.header.ref.get-fail"
)
const getHeaderCounter = meter.createCounter("trpc.contentstack.header.get")
const getHeaderSuccessCounter = meter.createCounter(
"trpc.contentstack.header.get-success"
)
const getHeaderFailCounter = meter.createCounter(
"trpc.contentstack.header.get-fail"
)
// OpenTelemetry metrics: Footer
const getFooterRefCounter = meter.createCounter(
"trpc.contentstack.footer.ref.get"
)
const getFooterRefSuccessCounter = meter.createCounter(
"trpc.contentstack.footer.ref.get-success"
)
const getFooterRefFailCounter = meter.createCounter(
"trpc.contentstack.footer.ref.get-fail"
)
const getFooterCounter = meter.createCounter("trpc.contentstack.footer.get")
const getFooterSuccessCounter = meter.createCounter(
"trpc.contentstack.footer.get-success"
)
const getFooterFailCounter = meter.createCounter(
"trpc.contentstack.footer.get-fail"
)
export const baseQueryRouter = router({
contact: contentstackBaseProcedure.query(async ({ ctx }) => {
const { lang } = ctx
getContactConfigCounter.add(1, { lang })
console.info(
"contentstack.config start",
"contentstack.contactConfig start",
JSON.stringify({ query: { lang } })
)
const response = await request<ContactConfigData>(GetContactConfig, {
@@ -39,10 +88,18 @@ export const baseQueryRouter = router({
if (!response.data) {
const notFoundError = notFound(response)
getContactConfigFailCounter.add(1, {
lang,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.config not found error",
JSON.stringify({ query: { lang }, error: { code: notFoundError.code } })
)
throw notFoundError
}
@@ -51,8 +108,13 @@ export const baseQueryRouter = router({
)
if (!validatedContactConfigConfig.success) {
getContactConfigFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedContactConfigConfig.error),
})
console.error(
"contentstack.config validation error",
"contentstack.contactConfig validation error",
JSON.stringify({
query: { lang },
error: validatedContactConfigConfig.error,
@@ -60,8 +122,9 @@ export const baseQueryRouter = router({
)
return null
}
getContactConfigSuccessCounter.add(1, { lang })
console.info(
"contentstack.config success",
"contentstack.contactConfig success",
JSON.stringify({ query: { lang } })
)
return validatedContactConfigConfig.data.all_contact_config.items[0]
@@ -69,6 +132,7 @@ export const baseQueryRouter = router({
header: contentstackBaseProcedure
.input(langInput)
.query(async ({ input }) => {
getHeaderRefCounter.add(1, { lang: input.lang })
console.info(
"contentstack.header.ref start",
JSON.stringify({ query: { lang: input.lang } })
@@ -76,6 +140,7 @@ export const baseQueryRouter = router({
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
locale: input.lang,
})
getHeaderCounter.add(1, { lang: input.lang })
console.info(
"contentstack.header start",
JSON.stringify({
@@ -99,6 +164,11 @@ export const baseQueryRouter = router({
if (!response.data) {
const notFoundError = notFound(response)
getHeaderFailCounter.add(1, {
lang: input.lang,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.header not found error",
JSON.stringify({
@@ -116,6 +186,11 @@ export const baseQueryRouter = router({
)
if (!validatedHeaderConfig.success) {
getHeaderFailCounter.add(1, {
lang: input.lang,
error_type: "validation_error",
error: JSON.stringify(validatedHeaderConfig.error),
})
console.error(
"contentstack.header validation error",
JSON.stringify({
@@ -127,6 +202,7 @@ export const baseQueryRouter = router({
)
return null
}
getHeaderSuccessCounter.add(1, { lang: input.lang })
console.info(
"contentstack.header success",
JSON.stringify({
@@ -145,6 +221,7 @@ export const baseQueryRouter = router({
footer: contentstackBaseProcedure
.input(langInput)
.query(async ({ input }) => {
getFooterRefCounter.add(1, { lang: input.lang })
console.info(
"contentstack.footer.ref start",
JSON.stringify({ query: { lang: input.lang } })
@@ -153,6 +230,7 @@ export const baseQueryRouter = router({
locale: input.lang,
})
// There's currently no error handling/validation for the responseRef, should it be added?
getFooterCounter.add(1, { lang: input.lang })
console.info(
"contentstack.footer start",
JSON.stringify({
@@ -178,6 +256,11 @@ export const baseQueryRouter = router({
if (!response.data) {
const notFoundError = notFound(response)
getFooterFailCounter.add(1, {
lang: input.lang,
error_type: "not_found",
error: JSON.stringify({ code: notFoundError.code }),
})
console.error(
"contentstack.footer not found error",
JSON.stringify({
@@ -195,6 +278,11 @@ export const baseQueryRouter = router({
)
if (!validatedFooterConfig.success) {
getFooterFailCounter.add(1, {
lang: input.lang,
error_type: "validation_error",
error: JSON.stringify(validatedFooterConfig.error),
})
console.error(
"contentstack.footer validation error",
JSON.stringify({
@@ -204,6 +292,7 @@ export const baseQueryRouter = router({
)
return null
}
getFooterSuccessCounter.add(1, { lang: input.lang })
console.info(
"contentstack.footer success",
JSON.stringify({ query: { lang: input.lang } })