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

Instrumentation: Add gauge of total number of requests in flight #281

Merged
merged 3 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions src/service/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as promClient from 'prom-client';
import { Logger } from '../logger';
import { Browser, createBrowser } from '../browser';
import { ServiceConfig } from '../config';
import { metricsMiddleware } from './metrics_middleware';
import { setupHttpServerMetrics } from './metrics_middleware';
import { RenderOptions, RenderCSVOptions, HTTPHeaders, Metrics } from '../browser/browser';

export interface RenderRequest {
Expand Down Expand Up @@ -58,7 +58,10 @@ export class HttpServer {
stream: this.log.errorWriter,
})
);
this.app.use(metricsMiddleware(this.config.service.metrics, this.log));

if (this.config.service.metrics.enabled) {
setupHttpServerMetrics(this.app, this.config.service.metrics, this.log);
}
this.app.get('/', (req: express.Request, res: express.Response) => {
res.send('Grafana Image Renderer');
});
Expand Down
45 changes: 34 additions & 11 deletions src/service/metrics_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as promBundle from 'express-prom-bundle';
import * as promClient from 'prom-client';
import * as onFinished from 'on-finished';
import express = require('express');

import { MetricsConfig } from '../config';
import { Logger } from '../logger';

export const metricsMiddleware = (config: MetricsConfig, log: Logger) => {
if (!config.enabled) {
return (req, res, next) => {
next();
};
}

export const setupHttpServerMetrics = (app: express.Express, config: MetricsConfig, log: Logger) => {
log.info(
'Metrics enabled',
'collectDefaultMetrics',
Expand All @@ -17,13 +15,15 @@ export const metricsMiddleware = (config: MetricsConfig, log: Logger) => {
config.requestDurationBuckets.join(',')
);

const excludeRegExp = /^((?!(render)).)*$/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come these routes are excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to exclude all routes that don't contain render. I'm not sure it's really useful currently but it could be in the future (right now, the only route that is excluded is GET /)


const opts = {
httpDurationMetricName: 'grafana_image_renderer_service_http_request_duration_seconds',
metricType: 'histogram',
buckets: config.requestDurationBuckets,
excludeRoutes: [/^((?!(render)).)*$/],
excludeRoutes: [excludeRegExp],
promClient: {},
formatStatusCode: res => {
formatStatusCode: (res) => {
if (res && res.req && res.req.aborted) {
// Nginx non-standard code 499 Client Closed Request
// Used when the client has closed the request before
Expand All @@ -39,6 +39,29 @@ export const metricsMiddleware = (config: MetricsConfig, log: Logger) => {
opts.promClient.collectDefaultMetrics = {};
}

const bundle = promBundle(opts);
return bundle;
const metricsMiddleware = promBundle(opts);
app.use(metricsMiddleware);

const httpRequestsInFlight = new promClient.Gauge({
name: 'grafana_image_renderer_http_request_in_flight',
help: 'A gauge of requests currently being served by the image renderer.',
});
app.use(requestsInFlightMiddleware(httpRequestsInFlight, excludeRegExp));
};

const requestsInFlightMiddleware = (httpRequestsInFlight: promClient.Gauge, excludeRegExp: RegExp) => {
return (req, res, next) => {
const path = req.originalUrl || req.url;
if (path.match(excludeRegExp)) {
return next();
}

httpRequestsInFlight.inc();
onFinished(res, () => {
httpRequestsInFlight.dec();
next();
AgnesToulet marked this conversation as resolved.
Show resolved Hide resolved
});

next();
};
};