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

I can't push with pushgateway #1780

Closed
manuelabarca opened this issue Jun 22, 2023 · 15 comments
Closed

I can't push with pushgateway #1780

manuelabarca opened this issue Jun 22, 2023 · 15 comments
Labels
help wanted Extra attention is needed released waiting

Comments

@manuelabarca
Copy link

With the documentation that is in the repository it is not possible for me to understand how pushgateway works, I have the following and I have gatewayurl undefined errors.

Prom.module.ts

import { Module } from '@nestjs/common';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';
import createMetricsPlugin from 'apollo-metrics';
import { plugin as apolloTracingPlugin } from 'apollo-tracing';
import { Pushgateway, register } from 'prom-client';
import { PromService } from './prom.service';

export const TRACING_PLUGIN_KEY = 'TRACING_PLUGIN_KEY';
export const METRICS_PLUGIN_KEY = 'METRICS_PLUGIN_KEY';

@Module({
  imports: [
    PrometheusModule.register({
      pushgateway: {
        url: process.env.PUSH_GATEWAY_URL,
      },
      defaultLabels: {
        app: 'Mobile BFF',
      },
    }),
  ],
  providers: [
    PromService,
    {
      provide: TRACING_PLUGIN_KEY,
      useValue: apolloTracingPlugin(),
    },
    {
      provide: METRICS_PLUGIN_KEY,
      useValue: createMetricsPlugin(register),
    },
    Pushgateway,
  ],
  exports: [PromService, Pushgateway, TRACING_PLUGIN_KEY, METRICS_PLUGIN_KEY],
})
export class PromModule {}

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) {
    console.log('client', pushgateway);
  }

  onModuleInit() {
    this.logger.log('Pushing to prometheus gateway');
    setInterval(() => {
      this.pushgateway.push({
        jobName: 'mobile-bff',
      });
    }, 15000);
  }
}

Error in console:

TypeError: The "url" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)
    at Url.parse (node:url:169:3)
    at Object.urlParse [as parse] (node:url:156:13)
    at Pushgateway.useGateway (/Users/manuel.labarca/Desktop/mobile-bff/node_modules/prom-client/lib/pushgateway.js:46:31)
@willsoto
Copy link
Owner

Is process.env.PUSH_GATEWAY_URL defined?

@manuelabarca
Copy link
Author

manuelabarca commented Jun 22, 2023

Is process.env.PUSH_GATEWAY_URL defined?

Yes is defined.

I'm assuming that when I add Pushgateway to exports and providers, the url is stepped on again and is undefined, because I've been debugging and that happens, but if I don't put that in there, I get another error.

@willsoto
Copy link
Owner

I'm assuming that when I add Pushgateway to exports and providers, the url is stepped on again and is undefined

What do you mean by "stepped on?" Do you mean the debugger hits it?

@manuelabarca
Copy link
Author

manuelabarca commented Jun 22, 2023

What do you mean by "stepped on?" Do you mean the debugger hits it?

I mean that the url gets to gatewayurl in the library to be able to build the whole PushGateway, but when the debugger gets to the point of checking the providers and exports that url becomes undefined.

Do you happen to have a more complete example of pushgateway with the library ?

@willsoto
Copy link
Owner

There isn't much to it. The example in the docs is comprehensive.

This is all that happens when you provide those options:

const { url, options: gatewayOptions, registry } = options.pushgateway;
providers.push({
provide: promClient.Pushgateway,
useValue: PrometheusModule.configurePushgateway(
url,
gatewayOptions,
registry,
),
});
}

private static configurePushgateway(
url: string,
options?: unknown,
registry?: promClient.Registry,
): promClient.Pushgateway {
return new promClient.Pushgateway(url, options, registry);
}

Given the error, it seems like PUSH_GATEWAY_URL is not defined. I would try hard coding something to see if that fixes it.

I have some tests but if you think there is a bug here please open a PR with the failing test case.

@manuelabarca
Copy link
Author

manuelabarca commented Jun 22, 2023

There isn't much to it. The example in the docs is comprehensive.

This is all that happens when you provide those options:

const { url, options: gatewayOptions, registry } = options.pushgateway;
providers.push({
provide: promClient.Pushgateway,
useValue: PrometheusModule.configurePushgateway(
url,
gatewayOptions,
registry,
),
});
}

private static configurePushgateway(
url: string,
options?: unknown,
registry?: promClient.Registry,
): promClient.Pushgateway {
return new promClient.Pushgateway(url, options, registry);
}

Given the error, it seems like PUSH_GATEWAY_URL is not defined. I would try hard coding something to see if that fixes it.

I have some tests but if you think there is a bug here please open a PR with the failing test case.

This is my module

@Module({
  imports: [
    PrometheusModule.register({
      pushgateway: {
        url: 'http://prometheus-pushgateway',
      },
      defaultLabels: {
        app: 'Mobile BFF',
      },
    }),
  ],
  providers: [
    PromService,
    {
      provide: TRACING_PLUGIN_KEY,
      useValue: apolloTracingPlugin(),
    },
    {
      provide: METRICS_PLUGIN_KEY,
      useValue: createMetricsPlugin(register),
    },
    Pushgateway,
  ],
  exports: [PromService, TRACING_PLUGIN_KEY, METRICS_PLUGIN_KEY],
})
export class PromModule {}

This is my PromService

import { Injectable } from '@nestjs/common';
import { Pushgateway } from 'prom-client';

@Injectable()
export class PromService {
  constructor(private pushgateway: Pushgateway) {
    this.pushgateway.push({
      jobName: 'mobile-bff',
    });
  }
}

When add Pushgateway in providers i have this error:

TypeError: The "url" argument must be of type string. Received undefined

but when not add Pushgateway in providers i have this error:

Potential solutions:
- Is PromModule a valid NestJS module?
- If Pushgateway is a provider, is it part of the current PromModule?
- If Pushgateway is exported from a separate @Module, is that module imported within PromModule?
  @Module({
    imports: [ /* the Module containing Pushgateway */ ]
  })

@willsoto
Copy link
Owner

Can you provide a complete stack trace?

@manuelabarca
Copy link
Author

Can you provide a complete stack trace?

When I start the application, I get those errors I told you about.

@manuelabarca
Copy link
Author

Here image about the debug
Captura de pantalla 2023-06-22 a las 16 14 00

Captura de pantalla 2023-06-22 a las 16 11 46

@willsoto
Copy link
Owner

Neither of these things are a complete stack trace. I need to see where in the stack this library's code is being called.

@manuelabarca
Copy link
Author

Neither of these things are a complete stack trace. I need to see where in the stack this library's code is being called.

I can't understand what you need to help me, when you see my code you don't see anything strange by chance? Is that pushgateway import okay? I feel like by doing that I'm creating a new instance and stomping on the previously set values.

@willsoto
Copy link
Owner

That is the only possibility I see. I will try and reproduce tonight. Otherwise, I see nothing wrong since you are just telling NestJS what type to inject, not the exact instance.

But in terms of what I need from you, a complete stack trace would be the best.

TypeError: The "url" argument must be of type string. Received undefined

This is just the top line of the trace and it tells me nothing about the call stack so I can't see where the library code is being hit.

@willsoto
Copy link
Owner

I've been unable to reproduce this locally. Without a proper stack trace I can't do much more. Please provide a proper stack or open a PR with a failing test case

@willsoto willsoto added help wanted Extra attention is needed waiting labels Jun 23, 2023
@Terencesun
Copy link
Contributor

Hi, I maybe find the reason, could I try to send a pr to this repository? @willsoto

willsoto pushed a commit that referenced this issue Jul 14, 2023
## [5.2.1](v5.2.0...v5.2.1) (2023-07-14)

### Bug Fixes

* pushgateway injection ([#1810](#1810)) ([64cc5ae](64cc5ae)), closes [#1780](#1780) [#1809](#1809)
@willsoto
Copy link
Owner

🎉 This issue has been resolved in version 5.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed released waiting
Projects
None yet
Development

No branches or pull requests

3 participants