1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: add appName to http response time metrics (#2117)

This commit is contained in:
Ivar Conradi Østhus 2022-09-30 15:28:50 +02:00 committed by GitHub
parent bd9172b7a0
commit 5141e77bce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 6 deletions

View File

@ -73,6 +73,7 @@ exports[`should create default config 1`] = `
"embedProxyFrontend": false,
"personalAccessTokens": false,
"publicSignup": false,
"responseTimeWithAppName": false,
},
},
"flagResolver": FlagResolver {
@ -84,6 +85,7 @@ exports[`should create default config 1`] = `
"embedProxyFrontend": false,
"personalAccessTokens": false,
"publicSignup": false,
"responseTimeWithAppName": false,
},
"externalResolver": {
"isEnabled": [Function],

View File

@ -42,7 +42,7 @@ export default async function getApp(
app.set('port', config.server.port);
app.locals.baseUriPath = baseUriPath;
if (config.server.serverMetrics && config.eventBus) {
app.use(responseTimeMetrics(config.eventBus));
app.use(responseTimeMetrics(config.eventBus, config.flagResolver));
}
app.use(requestLogger(config));

View File

@ -53,7 +53,7 @@ test('should collect metrics for requests', async () => {
const metrics = await prometheusRegister.metrics();
expect(metrics).toMatch(
/http_request_duration_milliseconds{quantile="0\.99",path="somePath",method="GET",status="200"}.*1337/,
/http_request_duration_milliseconds{quantile="0\.99",path="somePath",method="GET",status="200",appName="undefined"}.*1337/,
);
});

View File

@ -57,7 +57,7 @@ export default class MetricsMonitor {
const requestDuration = new client.Summary({
name: 'http_request_duration_milliseconds',
help: 'App response time',
labelNames: ['path', 'method', 'status'],
labelNames: ['path', 'method', 'status', 'appName'],
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
maxAgeSeconds: 600,
ageBuckets: 5,
@ -143,8 +143,10 @@ export default class MetricsMonitor {
eventBus.on(
events.REQUEST_TIME,
({ path, method, time, statusCode }) => {
requestDuration.labels(path, method, statusCode).observe(time);
({ path, method, time, statusCode, appName }) => {
requestDuration
.labels(path, method, statusCode, appName)
.observe(time);
},
);

View File

@ -1,21 +1,31 @@
import * as responseTime from 'response-time';
import EventEmitter from 'events';
import { REQUEST_TIME } from '../metric-events';
import { IFlagResolver } from '../types/experimental';
// eslint-disable-next-line @typescript-eslint/naming-convention
const _responseTime = responseTime.default;
export function responseTimeMetrics(eventBus: EventEmitter): any {
export function responseTimeMetrics(
eventBus: EventEmitter,
flagResolver: IFlagResolver,
): any {
return _responseTime((req, res, time) => {
const { statusCode } = res;
const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)';
let appName;
if (flagResolver.isEnabled('responseTimeWithAppName')) {
appName = req.headers['unleash-appname'];
}
const timingInfo = {
path: pathname,
method: req.method,
statusCode,
time,
appName,
};
eventBus.emit(REQUEST_TIME, timingInfo);
});

View File

@ -26,6 +26,10 @@ export const defaultExperimentalOptions = {
process.env.UNLEASH_EXPERIMENTAL_PUBLIC_SIGNUP,
false,
),
responseTimeWithAppName: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_RESPONSE_TIME_WITH_APP_NAME,
false,
),
},
externalResolver: { isEnabled: (): boolean => false },
};

View File

@ -37,6 +37,7 @@ process.nextTick(async () => {
embedProxyFrontend: true,
batchMetrics: true,
anonymiseEventLog: false,
responseTimeWithAppName: true,
},
},
authentication: {