Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

misc: added metric for api errors

Sheen Capadnganbbc8091d44b3
diff --git a/backend/src/server/plugins/error-handler.ts b/backend/src/server/plugins/error-handler.ts
index a6142cabca..7f9e161972 100644
--- a/backend/src/server/plugins/error-handler.ts
+++ b/backend/src/server/plugins/error-handler.ts
@@ -1,8 +1,10 @@
import { ForbiddenError, PureAbility } from "@casl/ability";
+import opentelemetry from "@opentelemetry/api";
import fastifyPlugin from "fastify-plugin";
import jwt from "jsonwebtoken";
import { ZodError } from "zod";
+import { getConfig } from "@app/lib/config/env";
import {
BadRequestError,
DatabaseError,
@@ -35,8 +37,30 @@ enum HttpStatusCodes {
}
export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider) => {
+ const appCfg = getConfig();
+
+ const apiMeter = opentelemetry.metrics.getMeter("API");
+ const errorHistogram = apiMeter.createHistogram("API_errors", {
+ description: "API errors by type, status code, and name",
+ unit: "1"
+ });
+
server.setErrorHandler((error, req, res) => {
req.log.error(error);
+ if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
+ const { method } = req;
+ const route = req.routerPath;
+ const errorType =
+ error instanceof jwt.JsonWebTokenError ? "TokenError" : error.constructor.name || "UnknownError";
+
+ errorHistogram.record(1, {
+ route,
+ method,
+ type: errorType,
+ name: error.name
+ });
+ }
+
if (error instanceof BadRequestError) {
void res
.status(HttpStatusCodes.BadRequest)