mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-27 01:19:00 +02:00
feat: error counters and memory gauge
This commit is contained in:
parent
783270599b
commit
5073d18b41
@ -1,10 +1,25 @@
|
|||||||
import type { IFlagResolver } from '../../../types/index.js';
|
import type { IFlagResolver } from '../../../types/index.js';
|
||||||
|
|
||||||
export const FEAUTRE_LINK_COUNT = 'feature_link_count';
|
export const FEAUTRE_LINK_COUNT = 'feature_link_count';
|
||||||
|
export const CLIENT_ERROR_COUNT = 'client_error_count';
|
||||||
|
export const SERVER_ERROR_COUNT = 'server_error_count';
|
||||||
|
export const HEAP_MEMORY_TOTAL = 'heap_memory_total';
|
||||||
|
|
||||||
export const defineImpactMetrics = (flagResolver: IFlagResolver) => {
|
export const defineImpactMetrics = (flagResolver: IFlagResolver) => {
|
||||||
flagResolver.impactMetrics?.defineCounter(
|
flagResolver.impactMetrics?.defineCounter(
|
||||||
FEAUTRE_LINK_COUNT,
|
FEAUTRE_LINK_COUNT,
|
||||||
'Count of feature links',
|
'Count of feature links',
|
||||||
);
|
);
|
||||||
|
flagResolver.impactMetrics?.defineCounter(
|
||||||
|
CLIENT_ERROR_COUNT,
|
||||||
|
'Count of 4xx errors',
|
||||||
|
);
|
||||||
|
flagResolver.impactMetrics?.defineCounter(
|
||||||
|
SERVER_ERROR_COUNT,
|
||||||
|
'Count of 5xx errors',
|
||||||
|
);
|
||||||
|
flagResolver.impactMetrics?.defineGauge(
|
||||||
|
HEAP_MEMORY_TOTAL,
|
||||||
|
'Total heap memory used by the application process',
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
@ -41,6 +41,7 @@ import {
|
|||||||
import type { SchedulerService } from './services/index.js';
|
import type { SchedulerService } from './services/index.js';
|
||||||
import type { IClientMetricsEnv } from './features/metrics/client-metrics/client-metrics-store-v2-type.js';
|
import type { IClientMetricsEnv } from './features/metrics/client-metrics/client-metrics-store-v2-type.js';
|
||||||
import { DbMetricsMonitor } from './metrics-gauge.js';
|
import { DbMetricsMonitor } from './metrics-gauge.js';
|
||||||
|
import { HEAP_MEMORY_TOTAL } from './features/metrics/impact/define-impact-metrics.js';
|
||||||
|
|
||||||
export function registerPrometheusPostgresMetrics(
|
export function registerPrometheusPostgresMetrics(
|
||||||
db: Knex,
|
db: Knex,
|
||||||
@ -1137,6 +1138,10 @@ export function registerPrometheusMetrics(
|
|||||||
collectAggDbMetrics: dbMetrics.refreshMetrics,
|
collectAggDbMetrics: dbMetrics.refreshMetrics,
|
||||||
collectStaticCounters: async () => {
|
collectStaticCounters: async () => {
|
||||||
try {
|
try {
|
||||||
|
config.flagResolver.impactMetrics?.updateGauge(
|
||||||
|
HEAP_MEMORY_TOTAL,
|
||||||
|
process.memoryUsage().heapUsed,
|
||||||
|
);
|
||||||
featureTogglesArchivedTotal.reset();
|
featureTogglesArchivedTotal.reset();
|
||||||
featureTogglesArchivedTotal.set(
|
featureTogglesArchivedTotal.set(
|
||||||
await instanceStatsService.getArchivedToggleCount(),
|
await instanceStatsService.getArchivedToggleCount(),
|
||||||
|
@ -1,14 +1,25 @@
|
|||||||
import url from 'url';
|
import url from 'url';
|
||||||
import type { RequestHandler } from 'express';
|
import type { RequestHandler } from 'express';
|
||||||
import type { IUnleashConfig } from '../types/option.js';
|
import type { IUnleashConfig } from '../types/option.js';
|
||||||
|
import {
|
||||||
|
CLIENT_ERROR_COUNT,
|
||||||
|
SERVER_ERROR_COUNT,
|
||||||
|
} from '../features/metrics/impact/define-impact-metrics.js';
|
||||||
|
|
||||||
const requestLogger: (config: IUnleashConfig) => RequestHandler = (config) => {
|
const requestLogger: (config: IUnleashConfig) => RequestHandler = (config) => {
|
||||||
const logger = config.getLogger('HTTP');
|
const logger = config.getLogger('HTTP');
|
||||||
const enable = config.server.enableRequestLogger;
|
const enable = config.server.enableRequestLogger;
|
||||||
|
const impactMetrics = config.flagResolver.impactMetrics;
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
if (enable) {
|
if (enable) {
|
||||||
res.on('finish', () => {
|
res.on('finish', () => {
|
||||||
const { pathname } = url.parse(req.originalUrl);
|
const { pathname } = url.parse(req.originalUrl);
|
||||||
|
if (res.statusCode >= 400 || res.statusCode < 500) {
|
||||||
|
impactMetrics?.incrementCounter(CLIENT_ERROR_COUNT);
|
||||||
|
}
|
||||||
|
if (res.statusCode >= 500) {
|
||||||
|
impactMetrics?.incrementCounter(SERVER_ERROR_COUNT);
|
||||||
|
}
|
||||||
logger.info(`${res.statusCode} ${req.method} ${pathname}`);
|
logger.info(`${res.statusCode} ${req.method} ${pathname}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user