jochen-schweizer / express-prom-bundle

express middleware with standard prometheus metrics in one bundle
MIT License
310 stars 68 forks source link

metrics can be attached to specific endpoint programatically, but will raise TS2339 #44

Open arnaudruffin opened 5 years ago

arnaudruffin commented 5 years ago

Hi

I am attaching metrics programatically in TypeScript. This used to work fine with a previous version (5.0.2) but with recent updates I now have a TypeScript error: error TS2339: Property 'metricsMiddleware' does not exist on type 'RequestHandler'.

Here is how I used to do it in 5.0.2:

import expressPromBundle from "express-prom-bundle";
const PROMETHEUS_BUNDLE = expressPromBundle({...})
[...]
expressApp.route("/metrics").get(function (req, res) {
            PROMETHEUS_BUNDLE.metricsMiddleware(req, res);
        });

Here is how I try to do it now (with the error TS2339):

expressApp.use("/metrics", PROMETHEUS_BUNDLE_METRICS_MIDDLEWARE.metricsMiddleware);

This is only a typing issue and does not prevent my server from running correctly.

poteirard commented 4 years ago

I have the same problem. I think this line is wrong:

// /types/index.d.ts
declare function express_prom_bundle(opts: express_prom_bundle.Opts): RequestHandler;

It should be something like:

declare function express_prom_bundle(opts: express_prom_bundle.Opts):
{ 
    metrics: ...
    metricsMiddleware: RequestHandler,
    promClient: ...
}

Maybe @raszi can put a bit of light on this? I would be happy to help with a PR

raszi commented 4 years ago

That is not the correct way to use the library AFAIK:

import * as promBundle from 'express-prom-bundle'
const metricsMiddleware = promBundle({...});

expressApp.route("/metrics").get(function (req, res) {
  return metricsMiddleware(req, res);
});

Or more simply as the documentation says:

import * as promBundle from 'express-prom-bundle'
const metricsMiddleware = promBundle({...});

expressApp.use(metricsMiddleware);
raszi commented 4 years ago

The type tests might also help you here.