All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.micrometer.runtime.binder.mpmetrics.GaugeAdapter Maven / Gradle / Ivy

Go to download

Instrument the runtime and your application with dimensional metrics using Micrometer.

There is a newer version: 3.17.5
Show newest version
package io.quarkus.micrometer.runtime.binder.mpmetrics;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;

import org.eclipse.microprofile.metrics.Gauge;
import org.eclipse.microprofile.metrics.MetricType;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;

interface GaugeAdapter extends Gauge, MeterHolder {

    GaugeAdapter register(MpMetadata metadata, MetricDescriptor metricInfo, MeterRegistry registry);

    static class DoubleFunctionGauge implements GaugeAdapter {
        io.micrometer.core.instrument.Gauge gauge;

        final S obj;
        final ToDoubleFunction f;

        DoubleFunctionGauge(S obj, ToDoubleFunction f) {
            this.obj = obj;
            this.f = f;
        }

        public GaugeAdapter register(MpMetadata metadata, MetricDescriptor metricInfo, MeterRegistry registry) {
            gauge = io.micrometer.core.instrument.Gauge.builder(metricInfo.name(), obj, f)
                    .description(metadata.getDescription())
                    .tags(metricInfo.tags())
                    .baseUnit(metadata.getUnit())
                    .strongReference(true)
                    .register(registry);
            return this;
        }

        @Override
        public Meter getMeter() {
            return gauge;
        }

        @Override
        public Double getValue() {
            return gauge.value();
        }

        @Override
        public MetricType getType() {
            return MetricType.GAUGE;
        }
    }

    static class FunctionGauge implements GaugeAdapter {
        io.micrometer.core.instrument.Gauge gauge;

        final S obj;
        final Function f;

        FunctionGauge(S obj, Function f) {
            this.obj = obj;
            this.f = f;
        }

        public GaugeAdapter register(MpMetadata metadata, MetricDescriptor metricInfo, MeterRegistry registry) {
            gauge = io.micrometer.core.instrument.Gauge.builder(metricInfo.name(), obj, obj -> f.apply(obj).doubleValue())
                    .description(metadata.getDescription())
                    .tags(metricInfo.tags())
                    .baseUnit(metadata.getUnit())
                    .strongReference(true)
                    .register(registry);
            return this;
        }

        @Override
        public Meter getMeter() {
            return gauge;
        }

        @Override
        public R getValue() {
            return (R) (Double) gauge.value();
        }

        @Override
        public MetricType getType() {
            return MetricType.GAUGE;
        }
    }

    static class NumberSupplierGauge implements GaugeAdapter {
        io.micrometer.core.instrument.Gauge gauge;
        final Supplier supplier;

        NumberSupplierGauge(Supplier supplier) {
            this.supplier = supplier;
        }

        @Override
        public GaugeAdapter register(MpMetadata metadata, MetricDescriptor metricInfo, MeterRegistry registry) {
            if (gauge == null || metadata.cleanDirtyMetadata()) {
                gauge = io.micrometer.core.instrument.Gauge.builder(metricInfo.name(), (Supplier) supplier)
                        .description(metadata.getDescription())
                        .tags(metricInfo.tags())
                        .baseUnit(metadata.getUnit())
                        .strongReference(true).register(registry);
            }

            return this;
        }

        @Override
        public Meter getMeter() {
            return gauge;
        }

        @Override
        public T getValue() {
            return supplier.get();
        }

        @Override
        public MetricType getType() {
            return MetricType.GAUGE;
        }
    }
}