mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-15 01:16:22 +02:00
fix: add appName to http response time metrics (#2117)
This commit is contained in:
parent
bd9172b7a0
commit
5141e77bce
@ -73,6 +73,7 @@ exports[`should create default config 1`] = `
|
|||||||
"embedProxyFrontend": false,
|
"embedProxyFrontend": false,
|
||||||
"personalAccessTokens": false,
|
"personalAccessTokens": false,
|
||||||
"publicSignup": false,
|
"publicSignup": false,
|
||||||
|
"responseTimeWithAppName": false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"flagResolver": FlagResolver {
|
"flagResolver": FlagResolver {
|
||||||
@ -84,6 +85,7 @@ exports[`should create default config 1`] = `
|
|||||||
"embedProxyFrontend": false,
|
"embedProxyFrontend": false,
|
||||||
"personalAccessTokens": false,
|
"personalAccessTokens": false,
|
||||||
"publicSignup": false,
|
"publicSignup": false,
|
||||||
|
"responseTimeWithAppName": false,
|
||||||
},
|
},
|
||||||
"externalResolver": {
|
"externalResolver": {
|
||||||
"isEnabled": [Function],
|
"isEnabled": [Function],
|
||||||
|
@ -42,7 +42,7 @@ export default async function getApp(
|
|||||||
app.set('port', config.server.port);
|
app.set('port', config.server.port);
|
||||||
app.locals.baseUriPath = baseUriPath;
|
app.locals.baseUriPath = baseUriPath;
|
||||||
if (config.server.serverMetrics && config.eventBus) {
|
if (config.server.serverMetrics && config.eventBus) {
|
||||||
app.use(responseTimeMetrics(config.eventBus));
|
app.use(responseTimeMetrics(config.eventBus, config.flagResolver));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.use(requestLogger(config));
|
app.use(requestLogger(config));
|
||||||
|
@ -53,7 +53,7 @@ test('should collect metrics for requests', async () => {
|
|||||||
|
|
||||||
const metrics = await prometheusRegister.metrics();
|
const metrics = await prometheusRegister.metrics();
|
||||||
expect(metrics).toMatch(
|
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/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ export default class MetricsMonitor {
|
|||||||
const requestDuration = new client.Summary({
|
const requestDuration = new client.Summary({
|
||||||
name: 'http_request_duration_milliseconds',
|
name: 'http_request_duration_milliseconds',
|
||||||
help: 'App response time',
|
help: 'App response time',
|
||||||
labelNames: ['path', 'method', 'status'],
|
labelNames: ['path', 'method', 'status', 'appName'],
|
||||||
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
|
percentiles: [0.1, 0.5, 0.9, 0.95, 0.99],
|
||||||
maxAgeSeconds: 600,
|
maxAgeSeconds: 600,
|
||||||
ageBuckets: 5,
|
ageBuckets: 5,
|
||||||
@ -143,8 +143,10 @@ export default class MetricsMonitor {
|
|||||||
|
|
||||||
eventBus.on(
|
eventBus.on(
|
||||||
events.REQUEST_TIME,
|
events.REQUEST_TIME,
|
||||||
({ path, method, time, statusCode }) => {
|
({ path, method, time, statusCode, appName }) => {
|
||||||
requestDuration.labels(path, method, statusCode).observe(time);
|
requestDuration
|
||||||
|
.labels(path, method, statusCode, appName)
|
||||||
|
.observe(time);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,21 +1,31 @@
|
|||||||
import * as responseTime from 'response-time';
|
import * as responseTime from 'response-time';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
import { REQUEST_TIME } from '../metric-events';
|
import { REQUEST_TIME } from '../metric-events';
|
||||||
|
import { IFlagResolver } from '../types/experimental';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const _responseTime = responseTime.default;
|
const _responseTime = responseTime.default;
|
||||||
|
|
||||||
export function responseTimeMetrics(eventBus: EventEmitter): any {
|
export function responseTimeMetrics(
|
||||||
|
eventBus: EventEmitter,
|
||||||
|
flagResolver: IFlagResolver,
|
||||||
|
): any {
|
||||||
return _responseTime((req, res, time) => {
|
return _responseTime((req, res, time) => {
|
||||||
const { statusCode } = res;
|
const { statusCode } = res;
|
||||||
|
|
||||||
const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)';
|
const pathname = req.route ? req.baseUrl + req.route.path : '(hidden)';
|
||||||
|
|
||||||
|
let appName;
|
||||||
|
if (flagResolver.isEnabled('responseTimeWithAppName')) {
|
||||||
|
appName = req.headers['unleash-appname'];
|
||||||
|
}
|
||||||
|
|
||||||
const timingInfo = {
|
const timingInfo = {
|
||||||
path: pathname,
|
path: pathname,
|
||||||
method: req.method,
|
method: req.method,
|
||||||
statusCode,
|
statusCode,
|
||||||
time,
|
time,
|
||||||
|
appName,
|
||||||
};
|
};
|
||||||
eventBus.emit(REQUEST_TIME, timingInfo);
|
eventBus.emit(REQUEST_TIME, timingInfo);
|
||||||
});
|
});
|
||||||
|
@ -26,6 +26,10 @@ export const defaultExperimentalOptions = {
|
|||||||
process.env.UNLEASH_EXPERIMENTAL_PUBLIC_SIGNUP,
|
process.env.UNLEASH_EXPERIMENTAL_PUBLIC_SIGNUP,
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
|
responseTimeWithAppName: parseEnvVarBoolean(
|
||||||
|
process.env.UNLEASH_EXPERIMENTAL_RESPONSE_TIME_WITH_APP_NAME,
|
||||||
|
false,
|
||||||
|
),
|
||||||
},
|
},
|
||||||
externalResolver: { isEnabled: (): boolean => false },
|
externalResolver: { isEnabled: (): boolean => false },
|
||||||
};
|
};
|
||||||
|
@ -37,6 +37,7 @@ process.nextTick(async () => {
|
|||||||
embedProxyFrontend: true,
|
embedProxyFrontend: true,
|
||||||
batchMetrics: true,
|
batchMetrics: true,
|
||||||
anonymiseEventLog: false,
|
anonymiseEventLog: false,
|
||||||
|
responseTimeWithAppName: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
authentication: {
|
authentication: {
|
||||||
|
Loading…
Reference in New Issue
Block a user