jochen-schweizer / express-prom-bundle

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

Missing documentation on combination of autoregister false and metricsApp #103

Open ArktinenSieni opened 2 years ago

ArktinenSieni commented 2 years ago

Hey! In #101 there were additional options added, but they're missing examples. I am unsure how I should use combination of autoregister and metricsApp. I could use the suggestions in #25 , but I'd rather use the options presented in the #101 PR.

Routhinator commented 2 years ago

Also confused here, not sure if we just pass true in or an app instance..

Routhinator commented 2 years ago

Looks like an app instance. My solution that worked:

export function metrics(): express.Express {
  const metricsAppInstance = express();
 // do stuff if you like
  return metricsAppInstance
}

// The Express app is exported so that it can be used by serverless Functions.
export function app(metricsAppInstance: express.Express): express.Express {
  const server = express();
...
  // Prometheus Setup
  server.use(
    '*',
    promBundle({
      buckets: [0.10, 5, 15, 50, 100, 200, 300, 400, 500],
      includeMethod: true,
      includePath: true,
      customLabels: {
        app: 'angular-express-ssr'
      },
      autoregister: false,
      metricsPath: '/metrics',
      metricsApp: metricsAppInstance,
      promClient: {
        collectDefaultMetrics: {}
      },
    })
  );
...
return server;
}

function run(): void {
  const port = config.get('server.port') || 4000;
  const metricsPort = config.get('metrics.port') || 4001;

  const metricsServer = metrics();
  // Start up the Node server
  const server = app(metricsServer);
  metricsServer.listen(metricsPort, ()=>{
    console.log(`Node Express Prometheus Exporter listening on http://localhost:${metricsPort}`);
  })
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}