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

Adding JSON metrics route option #52

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ API and process monitoring with [Prometheus](https://prometheus.io) for Node.js
- Process Metrics as recommended by Prometheus [itself](https://prometheus.io/docs/instrumenting/writing_clientlibs/#standard-and-runtime-collectors)
- Endpoint to retrieve the metrics - used for Prometheus scraping
- Prometheus format
- JSON format (`${path}.json`)
- JSON format (default to `${path}.json`, can be changed in options)
Copy link
Collaborator

Choose a reason for hiding this comment

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

default is /${metricsPath}.json, right?

- Support custom metrics
- [Http function to collect request.js HTTP request duration](#requestjs-http-request-duration-collector)

Expand All @@ -61,6 +61,7 @@ app.use(apiMetrics())
### Options

- metricsPath - Path to access the metrics. `default: /metrics`
- metricsJsonPath - Path to access the json formatted metrics. `default: {metricsPath}.json`
Copy link
Collaborator

Choose a reason for hiding this comment

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

default is /${metricsPath}.json, right?

- defaultMetricsInterval - the interval to collect the process metrics in milliseconds. `default: 10000`
- durationBuckets - Buckets for response time in seconds. `default: [0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]`
- requestSizeBuckets - Buckets for request size in bytes. `default: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000]`
Expand Down Expand Up @@ -88,6 +89,7 @@ curl http[s]://<host>:[port]/metrics.json

2. If you are using express framework and no route was found for the request (e.g: 404 status code), the request will not be collected. that's because we'll risk memory leak since the route is not a pattern but a hardcoded string.

3. You can specify your own metrics path via options. see : `metricsJsonPath`

## Custom Metrics

Expand Down
6 changes: 3 additions & 3 deletions src/express-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ class ExpressMiddleware {
res.set('Content-Type', Prometheus.register.contentType);
return res.end(Prometheus.register.metrics());
}
if (routeUrl === `${this.setupOptions.metricsRoute}.json`) {
debug('Request to /metrics endpoint');
if (routeUrl === this.setupOptions.metricsJsonRoute) {
debug('Request to /metrics/json endpoint');
return res.json(Prometheus.register.getMetricsAsJSON());
}

Expand All @@ -121,4 +121,4 @@ class ExpressMiddleware {
};
}

module.exports = ExpressMiddleware;
module.exports = ExpressMiddleware;
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class HttpMetricsCollector {

export interface ApiMetricsOpts {
metricsPath?: string;
metricsJsonPath?: string,
defaultMetricsInterval?: number;
durationBuckets?: number[];
requestSizeBuckets?: number[];
Expand Down
3 changes: 2 additions & 1 deletion src/metrics-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const setupOptions = {};

module.exports = (appVersion, projectName, framework = 'express') => {
return (options = {}) => {
const { metricsPath, defaultMetricsInterval = 10000, durationBuckets, requestSizeBuckets, responseSizeBuckets, useUniqueHistogramName, metricsPrefix, excludeRoutes, includeQueryParams } = options;
const { metricsPath, metricsJsonPath, defaultMetricsInterval = 10000, durationBuckets, requestSizeBuckets, responseSizeBuckets, useUniqueHistogramName, metricsPrefix, excludeRoutes, includeQueryParams } = options;
debug(`Init metrics middleware with options: ${JSON.stringify(options)}`);
setupOptions.metricsRoute = metricsPath || '/metrics';
setupOptions.metricsJsonRoute = metricsJsonPath || `${setupOptions.metricsRoute}.json`;
Copy link
Collaborator

@kobik kobik Oct 4, 2020

Choose a reason for hiding this comment

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

thinking of that again, IMO we need to sanitize metricsJsonRoute so it would always contain starting /

setupOptions.excludeRoutes = excludeRoutes || [];
setupOptions.includeQueryParams = includeQueryParams;
setupOptions.defaultMetricsInterval = defaultMetricsInterval;
Expand Down