Skip to content

Commit

Permalink
fix(label): label with undefined id
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso authored and dnlup committed Mar 7, 2022
1 parent 25eb6ff commit 6d79705
Showing 1 changed file with 33 additions and 40 deletions.
73 changes: 33 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,52 +174,45 @@ module.exports = fp(
});
fastify.decorate('metrics', metrics);

if (
config.routes.timing ||
config.routes.errors ||
config.routes.hits
) {
fastify.addHook('onRoute', (options) => {
// TODO: check for duplicates when registering routes
options.config = options.config || {};
options.config.metrics = options.config.metrics || {};
options.config.metrics.routeId = getRouteId(options.config);
options.config.metrics.fastifyPrefix = normalizeFastifyPrefix(
options.prefix
);
options.config.metrics.routesPrefix = metrics.routesPrefix;
options.config.metrics[kMetricsLabel] = '';
});
if (mode === 'dynamic') {
let getLabel = config.routes.getLabel || dynamicMode.getLabel;
if (typeof getLabel !== 'function') {
throw new Error('"getLabel" must be a function.');
}
fastify.decorateRequest(kMetricsLabel, '');
fastify.decorateReply(kMetricsLabel, '');
fastify.addHook('onRequest', function (request, reply, next) {
// TODO: skip noId routes here and in hooks.
const label = getLabel.call(this, request, reply);
request[kMetricsLabel] = label;
reply[kMetricsLabel] = label;
next();
});
} else {
const getLabel = config.routes.getLabel || staticMode.getLabel;
if (typeof getLabel !== 'function') {
throw new Error('"getLabel" must be a function.');
}
fastify.addHook('onRoute', (options) => {
options.config = options.config || {};
options.config.metrics = options.config.metrics || {};
options.config.metrics.routeId = getRouteId(options.config);
options.config.metrics.fastifyPrefix = normalizeFastifyPrefix(
options.prefix
);
options.config.metrics.routesPrefix = metrics.routesPrefix;
options.config.metrics[kMetricsLabel] = '';
});
if (mode === 'dynamic') {
let getLabel = config.routes.getLabel || dynamicMode.getLabel;
if (typeof getLabel !== 'function') {
throw new Error('"getLabel" must be a function.');
}
fastify.decorateRequest(kMetricsLabel, '');
fastify.decorateReply(kMetricsLabel, '');
fastify.addHook('onRequest', function (request, reply, next) {
// TODO: skip noId routes here and in hooks.
fastify.addHook('onRoute', (options) => {
options.config.metrics[kMetricsLabel] = getLabel(options);
});
const label = getLabel.call(this, request, reply);
request[kMetricsLabel] = label;
reply[kMetricsLabel] = label;
next();
});
} else {
const getLabel = config.routes.getLabel || staticMode.getLabel;
if (typeof getLabel !== 'function') {
throw new Error('"getLabel" must be a function.');
}
// TODO: skip noId routes here and in hooks.
fastify.addHook('onRoute', (options) => {
options.config.metrics[kMetricsLabel] = getLabel(options);
});
}

fastify.addHook('onClose', hooks.onClose);

decorateWithMethods(fastify, mode, stats);

fastify.addHook('onClose', hooks.onClose);

if (config.routes.hits) {
fastify.addHook('onRequest', hooks.onRequest);
}
Expand Down

0 comments on commit 6d79705

Please sign in to comment.