Skip to content

Adding JSON metrics route option#52

Open
Sunny-fr wants to merge 9 commits intoPayU:masterfrom
Sunny-fr:master
Open

Adding JSON metrics route option#52
Sunny-fr wants to merge 9 commits intoPayU:masterfrom
Sunny-fr:master

Conversation

@Sunny-fr
Copy link

Hello there,

The suffix for the metrics path on express is hard coded, it would be nice to have it "customizable" :

if (routeUrl === `${this.setupOptions.metricsRoute}.json`) {
  debug('Request to /metrics endpoint');
  return res.json(Prometheus.register.getMetricsAsJSON());
}

if (routeUrl === `${this.setupOptions.metricsRoute}.json`) {

Thank you

@kobik
Copy link
Collaborator

kobik commented Aug 17, 2020

Hi @Sunny-fr,

Thanks for your PR!

Can you please share your use-case?

@Sunny-fr
Copy link
Author

Yes of course.
In my company we use the same pattern for our grafana configs and metrics must look like this :

{APP}/metrics/json instead of {APP}/metrics.json

So in my code it looks like this :

const apiMetrics = require('prometheus-api-metrics')
const middleWare = function (app, router, config) {
  app.use(
    apiMetrics({
      metricsPath: config.publicPath + '/metrics',
      metricsJsonSuffix: '/json',
    })
  )
}

it's not perfect, i think the better way is to provide a full path to the json route.
example :

if (this.setupOptions.metricsJsonRoute !== '' ? this.setupOptions.metricsJsonRoute : routeUrl === `${this.setupOptions.metricsRoute}.json`) {
  debug('Request to /metrics endpoint');
  return res.json(Prometheus.register.getMetricsAsJSON());
}

But I tried to keep it simple and understandable :)

@kobik
Copy link
Collaborator

kobik commented Aug 18, 2020

i actually prefer your second alternative.

IMO, it's less confusing and more flexible.

"metricsJsonPath" defaults to  ``${setupOptions.metricsRoute}.json`
@Sunny-fr
Copy link
Author

Ok thanks !
Here it is :

        setupOptions.metricsRoute = metricsPath || '/metrics';
        setupOptions.metricsJsonRoute = metricsJsonPath || `${setupOptions.metricsRoute}.json`;

https://github.com/Sunny-fr/prometheus-api-metrics/blob/fcb3ae8fc5e64a4bd1c572d9374d4d298e28f3f4/src/metrics-middleware.js#L16

@kobik
Copy link
Collaborator

kobik commented Aug 27, 2020

Cool @Sunny-fr , we just need to mention it in the readme

@Sunny-fr
Copy link
Author

Ok, i'll make a proposition :)

@Sunny-fr Sunny-fr changed the title Adding a custom optional suffix path Adding JSON metrics route option Aug 31, 2020
"metricsJsonPath" defaults to  ``${setupOptions.metricsRoute}.json`
@Sunny-fr
Copy link
Author

Sunny-fr commented Sep 7, 2020

@kobik here it is, sorry for the delay.
Tell me if it's ok, thanks :)

@Sunny-fr
Copy link
Author

bump ! 👍

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 /

- 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?

### 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?

Copy link
Collaborator

@kobik kobik left a comment

Choose a reason for hiding this comment

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

see my comments @Sunny-fr

@kobik
Copy link
Collaborator

kobik commented Feb 9, 2021

Hi @Sunny-fr, did you have the chance to see my comments?

@Sunny-fr
Copy link
Author

Hello There,

Yes thank you, Im running out of time these days, but I'm willing to do it asap :)

Thanks again !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants