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

Default metrics are not being registered in the registry passed in register option #55

Open
mNalon opened this issue May 12, 2023 · 0 comments

Comments

@mNalon
Copy link

mNalon commented May 12, 2023

If I create a plugin with:

const myRegistry = new Registry()

const prometheusExporterPlugin = createPrometheusExporterPlugin({ app, register: myRegistry });

the default metrics are not registered in the registry passed in the options (considering the descriptions of the options in the README this should happen).

It seems this is being caused by a conflict between generateContext and toggleDefaultMetrics:

export function generateContext<C = BaseContext, S = Source, A = Args>(
  options: PluginOptions<C, S, A>
): Context<C, S, A> {
  const context: Context<C, S, A> = {
    app: options.app as Express,
    defaultLabels: {},
    defaultMetrics: true,
    disabledMetrics: [],
    durationHistogramsBuckets: [0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1, 5, 10],
    hostnameLabel: true,
    hostnameLabelName: 'hostname',
    metricsEndpoint: true,
    metricsEndpointPath: '/metrics',
    register,
    ...options,
    skipMetrics: {
      [MetricsNames.SERVER_STARTING]: () => false,
      [MetricsNames.SERVER_CLOSING]: () => false,
      [MetricsNames.QUERY_STARTED]: () => false,
      [MetricsNames.QUERY_FAILED]: () => false,
      [MetricsNames.QUERY_PARSE_STARTED]: () => false,
      [MetricsNames.QUERY_PARSE_FAILED]: () => false,
      [MetricsNames.QUERY_VALIDATION_STARTED]: () => false,
      [MetricsNames.QUERY_VALIDATION_FAILED]: () => false,
      [MetricsNames.QUERY_RESOLVED]: () => false,
      [MetricsNames.QUERY_EXECUTION_STARTED]: () => false,
      [MetricsNames.QUERY_EXECUTION_FAILED]: () => false,
      [MetricsNames.QUERY_DURATION]: () => false,
      [MetricsNames.QUERY_FIELD_RESOLUTION_DURATION]: () => false,
      ...(options.skipMetrics ?? {})
    },
    defaultMetricsOptions: {
      register,
      ...(options.defaultMetricsOptions ?? {})
    }
  };

the register reference used in defaultMetricsOptions points to the register imported from

import { DefaultMetricsCollectorConfiguration, LabelValues, register, Registry } from 'prom-client';

and then when toggleDefaultMetrics is called

export function toggleDefaultMetrics<C = AppContext, S = Source, A = Args>(
  register: Registry,
  { defaultMetrics, defaultMetricsOptions }: Context<C, S, A>
) {
  if (defaultMetrics) {
    collectDefaultMetrics({
      register,
      ...defaultMetricsOptions
    });
  }
}

the destructuring of defaultMetricsOptions overwrites the register, which points to the register passed in the options of createPrometheusExporterPlugin.

Is that expected? If it is not, I am willing to open a PR to fix this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant