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

io.quarkus.smallrye.metrics.runtime.SmallRyeMetricsFactory Maven / Gradle / Ivy

There is a newer version: 3.17.0
Show newest version
package io.quarkus.smallrye.metrics.runtime;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.Gauge;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.SimpleTimer;
import org.eclipse.microprofile.metrics.Tag;

import io.quarkus.runtime.metrics.MetricsFactory;
import io.smallrye.metrics.MetricRegistries;

public class SmallRyeMetricsFactory implements MetricsFactory {
    public boolean metricsSystemSupported(String name) {
        return MetricsFactory.MP_METRICS.equals(name);
    }

    @Override
    public MetricBuilder builder(String name, MetricsFactory.Type type) {
        return new SmallRyeMetricBuilder(name, type);
    }

    static class SmallRyeMetricBuilder implements MetricsFactory.MetricBuilder {
        final MetricRegistry registry;
        final org.eclipse.microprofile.metrics.MetadataBuilder builder;
        List tags = new ArrayList<>();

        SmallRyeMetricBuilder(String name, MetricsFactory.Type type) {
            switch (type) {
                case APPLICATION:
                    registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
                    break;
                case BASE:
                    registry = MetricRegistries.get(MetricRegistry.Type.BASE);
                    break;
                default:
                case VENDOR:
                    registry = MetricRegistries.get(MetricRegistry.Type.VENDOR);
                    break;
            }
            builder = org.eclipse.microprofile.metrics.Metadata.builder()
                    .withName(name);
        }

        public MetricBuilder unit(String unit) {
            builder.withUnit(unit);
            return this;
        }

        @Override
        public MetricBuilder tag(String key, String value) {
            tags.add(new Tag(key, value));
            return this;
        }

        @Override
        public MetricBuilder description(String description) {
            builder.withDescription(description);
            return this;
        }

        @Override
        public void buildCounter(Supplier countFunction) {
            builder.withType(MetricType.COUNTER);
            registry.register(builder.build(), new SmallRyeCounter(countFunction), tags.toArray(new Tag[0]));
        }

        @Override
        public  void buildCounter(T obj, Function countFunction) {
            builder.withType(MetricType.COUNTER);
            registry.register(builder.build(), new SmallRyeFunctionCounter<>(obj, countFunction), tags.toArray(new Tag[0]));
        }

        @Override
        public void buildGauge(Supplier gaugeFunction) {
            builder.withType(MetricType.GAUGE);
            registry.register(builder.build(), new SmallRyeGauge(gaugeFunction), tags.toArray(new Tag[0]));
        }

        @Override
        public  void buildGauge(T obj, Function gaugeFunction) {
            builder.withType(MetricType.GAUGE);
            registry.register(builder.build(), new SmallRyeFunctionGauge<>(obj, gaugeFunction), tags.toArray(new Tag[0]));
        }

        @Override
        public TimeRecorder buildTimer() {
            builder.withType(MetricType.SIMPLE_TIMER);
            SimpleTimer timer = registry.simpleTimer(builder.build(), tags.toArray(new Tag[0]));
            return new SmallRyeTimeRecorder(timer);
        }

        @Override
        public Runnable buildTimer(Runnable f) {
            builder.withType(MetricType.SIMPLE_TIMER);
            SimpleTimer timer = registry.simpleTimer(builder.build(), tags.toArray(new Tag[0]));
            return new Runnable() {
                @Override
                public void run() {
                    timer.time(f);
                }
            };
        }

        @Override
        public  Callable buildTimer(Callable f) {
            builder.withType(MetricType.SIMPLE_TIMER);
            SimpleTimer timer = registry.simpleTimer(builder.build(), tags.toArray(new Tag[0]));
            return new Callable() {
                @Override
                public T call() throws Exception {
                    return timer.time(f);
                }
            };
        }

        @Override
        public  Supplier buildTimer(Supplier f) {
            builder.withType(MetricType.SIMPLE_TIMER);
            SimpleTimer timer = registry.simpleTimer(builder.build(), tags.toArray(new Tag[0]));
            return new Supplier() {
                @Override
                public T get() {
                    SimpleTimer.Context ctx = timer.time();
                    try {
                        return f.get();
                    } finally {
                        ctx.stop();
                    }
                }
            };
        }
    }

    private static class SmallRyeCounter implements Counter {
        final Supplier f;

        SmallRyeCounter(Supplier f) {
            this.f = f;
        }

        @Override
        public void inc() {
        }

        @Override
        public void inc(long l) {
        }

        @Override
        public long getCount() {
            return f.get().longValue();
        }
    }

    private static class SmallRyeFunctionCounter implements Counter {
        final T obj;
        final Function f;

        SmallRyeFunctionCounter(T obj, Function f) {
            this.obj = obj;
            this.f = f;
        }

        @Override
        public void inc() {
        }

        @Override
        public void inc(long l) {
        }

        @Override
        public long getCount() {
            return f.apply(obj).longValue();
        }
    }

    private static class SmallRyeGauge implements Gauge {
        final Supplier f;

        SmallRyeGauge(Supplier f) {
            this.f = f;
        }

        @Override
        public Long getValue() {
            return f.get().longValue();
        }
    }

    private static class SmallRyeFunctionGauge implements Gauge {
        final T obj;
        final Function f;

        SmallRyeFunctionGauge(T obj, Function f) {
            this.obj = obj;
            this.f = f;
        }

        @Override
        public R getValue() {
            return f.apply(obj);
        }
    }

    private static class SmallRyeTimeRecorder implements TimeRecorder {
        SimpleTimer timer;

        SmallRyeTimeRecorder(SimpleTimer timer) {
            this.timer = timer;
        }

        @Override
        public void update(Duration duration) {
            timer.update(duration);
        }

        @Override
        public void update(long duration, TimeUnit unit) {
            timer.update(Duration.ofNanos(unit.toNanos(duration)));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy