Search in sources :

Example 1 with InstrumentedScheduledExecutorService

use of io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService in project micronaut-micrometer by micronaut-projects.

the class ExecutorServiceMetricsBinder method onCreated.

@Override
public ExecutorService onCreated(BeanCreatedEvent<ExecutorService> event) {
    ExecutorService executorService = event.getBean();
    // have to unwrap any Micronaut instrumentations to get the target
    ExecutorService unwrapped = executorService;
    while (unwrapped instanceof InstrumentedExecutorService) {
        InstrumentedExecutorService ies = (InstrumentedExecutorService) unwrapped;
        unwrapped = ies.getTarget();
    }
    // Netty EventLoopGroups require separate instrumentation.
    if (unwrapped.getClass().getName().startsWith("io.netty")) {
        return unwrapped;
    }
    MeterRegistry meterRegistry = meterRegistryProvider.get();
    BeanIdentifier beanIdentifier = event.getBeanIdentifier();
    // allow tags?
    List<Tag> tags = Collections.emptyList();
    // bind the service metrics
    new ExecutorServiceMetrics(unwrapped, beanIdentifier.getName(), tags).bindTo(meterRegistry);
    // allow timing
    final Timer timer = meterRegistry.timer("executor", Tags.concat(tags, "name", beanIdentifier.getName()));
    if (executorService instanceof ScheduledExecutorService) {
        return new InstrumentedScheduledExecutorService() {

            @Override
            public ScheduledExecutorService getTarget() {
                return (ScheduledExecutorService) executorService;
            }

            @Override
            public <T> Callable<T> instrument(Callable<T> task) {
                return timer.wrap(task);
            }

            @Override
            public Runnable instrument(Runnable command) {
                return timer.wrap(command);
            }
        };
    } else {
        return new InstrumentedExecutorService() {

            @Override
            public ExecutorService getTarget() {
                return executorService;
            }

            @Override
            public <T> Callable<T> instrument(Callable<T> task) {
                return timer.wrap(task);
            }

            @Override
            public Runnable instrument(Runnable command) {
                return timer.wrap(command);
            }
        };
    }
}
Also used : InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) InstrumentedExecutorService(io.micronaut.scheduling.instrument.InstrumentedExecutorService) Callable(java.util.concurrent.Callable) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) ExecutorServiceMetrics(io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics) Timer(io.micrometer.core.instrument.Timer) BeanIdentifier(io.micronaut.inject.BeanIdentifier) InstrumentedExecutorService(io.micronaut.scheduling.instrument.InstrumentedExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Tag(io.micrometer.core.instrument.Tag)

Aggregations

MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 Tag (io.micrometer.core.instrument.Tag)1 Timer (io.micrometer.core.instrument.Timer)1 ExecutorServiceMetrics (io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics)1 BeanIdentifier (io.micronaut.inject.BeanIdentifier)1 InstrumentedExecutorService (io.micronaut.scheduling.instrument.InstrumentedExecutorService)1 InstrumentedScheduledExecutorService (io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1