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);
}
};
}
}
Aggregations