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

how to use custom controller when withDefaultController: false #49

Open
darrenjennings opened this issue Jan 27, 2021 · 2 comments
Open

Comments

@darrenjennings
Copy link

@digikare/nestjs-prom version: 0.2.5

I am trying to remove the /api/metrics from the autogenerated swagger spec.

const MyPromModule = PromModule.forRoot({
  defaultLabels: {
    app: 'my-api'
  },
  withDefaultsMetrics: true,
  withDefaultController: false, // set to false so custom controller is used
  useHttpCounterMiddleware: true
})
MyPromModule.controllers = [PromController.forRoot('api/metrics')]

and then PromController is a copy paste from source(but with @ApiExcludeEndpoint)

import { Controller, Get, Header } from "@nestjs/common";
import * as client from 'prom-client';
import { PATH_METADATA } from '@nestjs/common/constants';
import { ApiExcludeEndpoint } from "@nestjs/swagger";

@Controller()
export class PromController {
  @Get()
  @ApiExcludeEndpoint()
  @Header('Content-Type', client.register.contentType)
  index() {
    console.log('test')

    return client.register.metrics();
  }

  public static forRoot(path = 'metrics') {
    Reflect.defineMetadata(PATH_METADATA, path, PromController);

    return PromController;
  }
}

When I hit /api/metrics, client.register.metrics(); always returns empty string.

@arinaspektor
Copy link

arinaspektor commented Apr 15, 2021

I did it this way:

import { Registry } from '@digikare/nestjs-prom';
import { Header } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common/';
import * as client from 'prom-client';


 @Controller('metrics')
export class PromController {
  private readonly _registry: Registry;

  constructor(
    private readonly promService: PromService,
  ) {
    this._registry = this.promService.getDefaultRegistry();
  }

  @Get()
  @Header('Content-Type', client.register.contentType)
  public async index() {
    return this._registry.metrics();
  }
}

@timakhalaya
Copy link

import {ApiTags} from "@nestjs/swagger";
import {PromController, PromModule} from "@digikare/nestjs-prom";

ApiTags('example')(PromController);
...

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

3 participants