bfmatei / apollo-prometheus-exporter

Plugin for Apollo Server to export metrics in Prometheus format
MIT License
55 stars 29 forks source link

error: metric have already been registered #11

Closed zephyrpathsofglory closed 3 years ago

zephyrpathsofglory commented 3 years ago

I have an express server with express-prom-bundle and apollo-prometheus-exporter installed and set up, And when I start my server, it crashed.

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
Error: A metric with the name process_cpu_user_seconds_total has already been registered.

I guess the reason is that express-prom-bundle has registered the process_cpu_user_seconds_total metric, and apollo-prometheus-exporter also try to register this metric, so it crashed. (FYI, I found express-prom-bundle and apollo-prometheus-exporter called collectDefaultMetrics which is implement in prom-client)

Is it suitable to rescue and ignore the error when we re-register a metric? Or is there anything we can do?

bfmatei commented 3 years ago

Hello!

Yup, collectDefaultMetrics is called by apollo-prometheus-exporter but you can avoid errors by:

I think I should let this fail like it does currently but provide guidance about it in the readme.

zephyrpathsofglory commented 3 years ago

Hello!

Yup, collectDefaultMetrics is called by apollo-prometheus-exporter but you can avoid errors by:

* disabling the default metrics:
  ```ts
  createPrometheusExporterPlugin({ 
    app,
    defaultMetrics: false
  });
  ```

* providing a prefix for the default metrics:
  ```ts
  createPrometheusExporterPlugin({ 
    app,
    defaultMetricsOptions: {
      prefix: 'example-prefix',
    }
  });
  ```

* using another registry:
  ```ts
  import { Registry } from 'prom-client';

  // ...

  createPrometheusExporterPlugin({ 
    app,
    register: new Registry()
  });
  ```

I think I should let this fail like it does currently but provide guidance about it in the readme.

the solutions work.

Yes, more guidance is good. I will close the issue.