mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 13:27:22 +00:00
Merge pull request #2899 from Infisical/misc/add-custom-metrics-for-errors
misc: added metric for api errors
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import { ForbiddenError, PureAbility } from "@casl/ability";
|
import { ForbiddenError, PureAbility } from "@casl/ability";
|
||||||
|
import opentelemetry from "@opentelemetry/api";
|
||||||
import fastifyPlugin from "fastify-plugin";
|
import fastifyPlugin from "fastify-plugin";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { ZodError } from "zod";
|
import { ZodError } from "zod";
|
||||||
|
|
||||||
|
import { getConfig } from "@app/lib/config/env";
|
||||||
import {
|
import {
|
||||||
BadRequestError,
|
BadRequestError,
|
||||||
DatabaseError,
|
DatabaseError,
|
||||||
@@ -35,8 +37,30 @@ enum HttpStatusCodes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider) => {
|
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) => {
|
server.setErrorHandler((error, req, res) => {
|
||||||
req.log.error(error);
|
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) {
|
if (error instanceof BadRequestError) {
|
||||||
void res
|
void res
|
||||||
.status(HttpStatusCodes.BadRequest)
|
.status(HttpStatusCodes.BadRequest)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
|
import opentelemetry from "@opentelemetry/api";
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -158,6 +159,12 @@ export const secretQueueFactory = ({
|
|||||||
projectUserMembershipRoleDAL,
|
projectUserMembershipRoleDAL,
|
||||||
projectKeyDAL
|
projectKeyDAL
|
||||||
}: TSecretQueueFactoryDep) => {
|
}: TSecretQueueFactoryDep) => {
|
||||||
|
const integrationMeter = opentelemetry.metrics.getMeter("Integrations");
|
||||||
|
const errorHistogram = integrationMeter.createHistogram("integration_secret_sync_errors", {
|
||||||
|
description: "Integration secret sync errors",
|
||||||
|
unit: "1"
|
||||||
|
});
|
||||||
|
|
||||||
const removeSecretReminder = async (dto: TRemoveSecretReminderDTO) => {
|
const removeSecretReminder = async (dto: TRemoveSecretReminderDTO) => {
|
||||||
const appCfg = getConfig();
|
const appCfg = getConfig();
|
||||||
await queueService.stopRepeatableJob(
|
await queueService.stopRepeatableJob(
|
||||||
@@ -933,6 +940,19 @@ export const secretQueueFactory = ({
|
|||||||
`Secret integration sync error [projectId=${job.data.projectId}] [environment=${environment}] [secretPath=${job.data.secretPath}]`
|
`Secret integration sync error [projectId=${job.data.projectId}] [environment=${environment}] [secretPath=${job.data.secretPath}]`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const appCfg = getConfig();
|
||||||
|
if (appCfg.OTEL_TELEMETRY_COLLECTION_ENABLED) {
|
||||||
|
errorHistogram.record(1, {
|
||||||
|
version: 1,
|
||||||
|
integration: integration.integration,
|
||||||
|
integrationId: integration.id,
|
||||||
|
type: err instanceof AxiosError ? "AxiosError" : err?.constructor?.name || "UnknownError",
|
||||||
|
status: err instanceof AxiosError ? err.response?.status : undefined,
|
||||||
|
name: err instanceof Error ? err.name : undefined,
|
||||||
|
projectId: integration.projectId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const message =
|
const message =
|
||||||
// eslint-disable-next-line no-nested-ternary
|
// eslint-disable-next-line no-nested-ternary
|
||||||
(err instanceof AxiosError
|
(err instanceof AxiosError
|
||||||
|
|||||||
Reference in New Issue
Block a user