How can I create a dynamic Gauge in Micrometer within Quarkus?

Viewed 43

I'm starting with Micrometer within Quarkus to get metrics. I've followed the examples, but I think I don't understand how to create a Gauge that is backed by a method. My use case is that I'm converting PDFs and using a persistent volume for this, and I want to monitor the disk usage.

I have a PdfConverterService with a method public Integer getConversionDiskUsage() which returns the disk usage in percent.

But how can I incorporate it in my REST resource?

  @Inject
  PdfConverterService service;

  MeterRegistry registry;

  private List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);

  PdfConverterResource(final MeterRegistry registry) {
    this.registry = registry;
    this.registry.gauge("foo.bar", 42);
    this.registry.gauge("foo.foo.bar", Tags.of("A", "B"), this.list, List::size);

    this.registry.gauge("foo.baz", Tags.of("A", "B"), this.service, s-> s.getConversionDiskUsage().doubleValue());

    Gauge.builder("conversion.volume.usage",
        this.service, PdfConverterService::getConversionDiskUsage)
    .description("Shows the disk usage of the attached volume used for the PDF conversion")
    .register(this.registry);
  }

  @POST
  ....
  @Timed(value = "pdf.conversion.time", description = "A measure of how long it takes to perform the PDF conversion")
  public Response convertPdf(
    ....
  }

After calling the REST endpoint and then calling up the metrics, certain things work, but others don't.

# HELP foo_bar  
# TYPE foo_bar gauge
foo_bar 42.0
# HELP foo_foo_bar  
# TYPE foo_foo_bar gauge
foo_foo_bar{A="B",} 7.0
# HELP pdf_conversion_time_seconds A measure of how long it takes to perform the PDF conversion
# TYPE pdf_conversion_time_seconds summary
pdf_conversion_time_seconds_count{class="PdfConverterResource",exception="none",method="convertPdf",} 1.0
pdf_conversion_time_seconds_sum{class="PdfConverterResource",exception="none",method="convertPdf",} 0.2069797
# HELP pdf_conversion_time_seconds_max A measure of how long it takes to perform the PDF conversion
# TYPE pdf_conversion_time_seconds_max gauge
pdf_conversion_time_seconds_max{class="PdfConverterResource",exception="none",method="convertPdf",} 0.2069797

I wonder whether it's a problem that the PdfConverterService is injected with an annotation, but MeterRegistry is injected in the constructor.

What am I doing wrong?

0 Answers
Related