willsoto/nestjs-prometheus

Nest can't resolve dependencies of metric providers

Closed this issue ยท 12 comments

Version 5.4.0 introduces a prefix option that adds a prefix to all metrics. But this only works if you register the PrometheusModule in the same module that you create the metrics in. I updated from 5.3.0 to 5.4.0 and then I got the error but I also tested it using a fresh nestjs app and that also gave the error.

Steps to reproduce:

  1. Create a fresh NestJS app nest new project-name
  2. Install @willsoto/nestjs-prometheus and prom-client
  3. Register PrometheusModule inside App module.
  4. Create a new module nestjs g module module-name
  5. Create any metric provider inside the new module

Error message: Error: Nest can't resolve dependencies of the PROM_METRIC_METRIC_NAME (?). Please make sure that the argument Symbol(PROMETHEUS_OPTIONS) at index [0] is available in the ModuleNameModule context.

Probably this should have been a breaking change due to this...but this is expected yes. The module should be imported wherever it's needed. I personally prefer that practice over making modules global since that complicates things like testing

I am registering the PrometheusModule inside the root module and then I am importing the PrometheusModule inside every module that has a metrics provider and I still get this error. What is the correct way to do it?

Likely a bug here...out of curiosity do you also add the PrometheusModule to exports?

Likely a bug here...out of curiosity do you also add the PrometheusModule to exports?

No I am just registering the PrometheusModule inside the root module and then I have a few other modules that import the PrometheusModule and has metric providers made using makeXProvider.

Do you mind adding it to exports ? I believe it's required in order for the module to be visible to other modules.

So you would register PrometheusModule in ModuleA like:

@Module({ 
  imports: [PrometheusModule.register()], 
  exports: [PrometheusModule]
}) 
export class ModuleA {}

and then elsewhere:

@Module({ 
  imports: [ModuleA],
  providers: [makeGuageProvider()]
}) 
export class ModuleB {}

I tried it and I got the same error. But I believe exports is only used for providers.

Are you using register or registerAsync? I think I export all the providers in both

I have tried both

PrometheusModule.register()

and

PrometheusModule.registerAsync({
  useFactory: () => {
    return {};
  },
})

Okay. Probably a bug here. Just stay on 5.3.0 in the meantime until I have a chance to look into it (might be a few days). You are also welcome to take a look if you have any ideas.

The issue is fixed with #1901 but it might not be the best solution. I tried figuring out how to fix the problem but I am not very familiar with creating nestjs modules so I did not get very far. I will keep the issue open so you can have a chance to look into it.

๐ŸŽ‰ This issue has been resolved in version 5.5.1 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

[UPDATED]
registerAsync works for me, PrometheusModule.register() still error

I still got this error with version 5.5.1 and 6.0.0

[Nest] 341  - 01/30/2024, 10:40:38โ€ฏPM     LOG [NestFactory] Starting Nest application...
[Nest] 341  - 01/30/2024, 10:40:38โ€ฏPM   ERROR [ExceptionHandler] Nest cannot export a provider/module that is not a part of the currently processed module (PrometheusModule). Please verify whether the exported Symbol(PROMETHEUS_OPTIONS) is available in this particular context.

Possible Solutions:
- Is Symbol(PROMETHEUS_OPTIONS) part of the relevant providers/imports within PrometheusModule?

Error: Nest cannot export a provider/module that is not a part of the currently processed module (PrometheusModule). Please verify whether the exported Symbol(PROMETHEUS_OPTIONS) is available in this particular context.

Possible Solutions:
- Is Symbol(PROMETHEUS_OPTIONS) part of the relevant providers/imports within PrometheusModule?
    "engines": {
      "npm": "please-use-yarn",
      "node": ">=18.14.2",
      "yarn": ">= 1.19.1"
    },
  
    "@nestjs/cli": "^10.2.1",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@golevelup/nestjs-rabbitmq": "^3.3.0",
    "@liaoliaots/nestjs-redis": "^9.0.3",
    "@nestjs/bull": "^0.6.1",
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/jwt": "^9.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/terminus": "^9.1.2",
    "@nestjs/typeorm": "^9.0.1",