Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: added metric for api errors #2899

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions backend/src/server/plugins/error-handler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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", {
maidul98 marked this conversation as resolved.
Show resolved Hide resolved
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";
maidul98 marked this conversation as resolved.
Show resolved Hide resolved

errorHistogram.record(1, {
route,
method,
type: errorType,
name: error.name
});
}

if (error instanceof BadRequestError) {
void res
.status(HttpStatusCodes.BadRequest)
Expand Down
20 changes: 20 additions & 0 deletions backend/src/services/secret/secret-queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-await-in-loop */
import opentelemetry from "@opentelemetry/api";
import { AxiosError } from "axios";

import {
Expand Down Expand Up @@ -158,6 +159,12 @@ export const secretQueueFactory = ({
projectUserMembershipRoleDAL,
projectKeyDAL
}: 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 appCfg = getConfig();
await queueService.stopRepeatableJob(
Expand Down Expand Up @@ -933,6 +940,19 @@ export const secretQueueFactory = ({
`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 =
// eslint-disable-next-line no-nested-ternary
(err instanceof AxiosError
Expand Down
Loading