willsoto / nestjs-prometheus

NestJS module for Prometheus
Apache License 2.0
499 stars 28 forks source link

gatewayUrl is undefined when i register url in pushgateway #2351

Closed skls1337 closed 1 month ago

skls1337 commented 1 month ago

PromService .ts

import { Injectable, OnModuleInit, Logger } from '@nestjs/common';
import * as client from 'prom-client';

@Injectable()
export class PromService implements OnModuleInit {
  private readonly logger = new Logger(PromService.name);

  constructor(private readonly pushgateway: client.Pushgateway<any>) {}

  onModuleInit() {
    this.logger.log('Pushing to prometheus gateway');
    console.log(this.pushgateway);
  }
}

PromModule.ts

import { Module } from '@nestjs/common';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';

import { Pushgateway } from 'prom-client';
import { PromService } from 'src/common/services/pushgateway.service';

@Module({
  imports: [
    PrometheusModule.register({
      defaultMetrics: {
        enabled: true,
        config: {
          prefix: 'backforfront_',
        },
      },
      pushgateway: {
        url: process.env.PUSHGATEWAY_URL,
      },
    }),
  ],
  providers: [PromService, Pushgateway],
  exports: [PromService, Pushgateway],
})
export class PromModule {}

Log from console on startup

Pushgateway {
2024-08-08 13:00:33   registry: Registry {
2024-08-08 13:00:33     _metrics: {
2024-08-08 13:00:33       backforfront_process_cpu_user_seconds_total: [Counter],
2024-08-08 13:00:33       backforfront_process_cpu_system_seconds_total: [Counter],
2024-08-08 13:00:33       backforfront_process_cpu_seconds_total: [Counter],
2024-08-08 13:00:33       backforfront_process_start_time_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_process_resident_memory_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_process_virtual_memory_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_process_heap_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_process_open_fds: [Gauge],
2024-08-08 13:00:33       backforfront_process_max_fds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_min_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_max_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_mean_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_stddev_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_p50_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_p90_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_eventloop_lag_p99_seconds: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_resources: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_resources_total: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_handles: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_handles_total: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_requests: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_active_requests_total: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_heap_size_total_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_heap_size_used_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_external_memory_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_heap_space_size_total_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_heap_space_size_used_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_heap_space_size_available_bytes: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_version_info: [Gauge],
2024-08-08 13:00:33       backforfront_nodejs_gc_duration_seconds: [Histogram]
2024-08-08 13:00:33     },
2024-08-08 13:00:33     _collectors: [],
2024-08-08 13:00:33     _defaultLabels: {},
2024-08-08 13:00:33     _contentType: 'text/plain; version=0.0.4; charset=utf-8'
2024-08-08 13:00:33   },
2024-08-08 13:00:33   gatewayUrl: undefined,
2024-08-08 13:00:33   requireJobName: true,
2024-08-08 13:00:33   requestOptions: {}
2024-08-08 13:00:33 }

Yes, my env is defined, there is already an issue closed on this topic, what am I doing wrong?

willsoto commented 1 month ago

I think you are clobbering the Pushgateway registered by this module with your own:

import { Pushgateway } from 'prom-client';
providers: [PromService, Pushgateway],

Remove the unnecessary registration and see if it works.

skls1337 commented 1 month ago

I tried your suggestion and I have the same behaviour.

I found out that when you init the module the gatewayUrl is undefined and forcing it to get a value fixes the connection to pushgateway. So maybe something is not going well here

onModuleInit() {
    this.logger.log('Pushing to prometheus gateway');
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    this.pushgateway.gatewayUrl = 'http://pushgateway:9091';
    this.pushgateway.push({
      jobName: 'backforfront',
    });
  }
willsoto commented 1 month ago

Yes, my env is defined,

can you hardcode it and see if you get the same result?

skls1337 commented 1 month ago

Can confirm with the env hardcoded that the gatewayUrl will come up as undefined, and if I do the this.pushgateway.push() I receive TypeError: The "url" argument must be of type string. Received undefined

willsoto commented 1 month ago

Yeah not sure what to tell you, I have a test that proves it works. Maybe the reference is being lost in the way you are importing...I would definitely not have that additional Pushgateway provider registered though as that isn't necessary.

skls1337 commented 1 month ago

Sure, I checked the tests and it proves it works. If I completely remove the onModuleInit it goes fine, but for some reason, I can't query Prometheus with my metrics for my nestjs service

skls1337 commented 1 month ago

Hi, I found out I can fix my problem with registerAsync

PrometheusModule.registerAsync({
      useFactory: () => {
        return {
          pushgateway: {
            url: process.env.PUSHGATEWAY_URL,
          },
        };
      },
    }),

So this can conclude my fix to the issue I had. Thx for the support!