net.nemerosa.ontrack.boot.metrics.DefaultMetricsExporter Maven / Gradle / Ivy
package net.nemerosa.ontrack.boot.metrics;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import net.nemerosa.ontrack.model.metrics.MetricsSource;
import net.nemerosa.ontrack.model.support.OntrackConfigProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.dropwizard.DropwizardMetricServices;
import org.springframework.boot.actuate.metrics.export.AbstractMetricExporter;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
public class DefaultMetricsExporter extends AbstractMetricExporter {
private final Logger logger = LoggerFactory.getLogger(DefaultMetricsExporter.class);
private final OntrackConfigProperties config;
private final DropwizardMetricServices metricWriter;
private final ScheduledExecutorService executor;
private final Collection metricsSources;
@Autowired
public DefaultMetricsExporter(OntrackConfigProperties config, DropwizardMetricServices metricWriter, Collection metricsSources) {
super("");
this.config = config;
this.metricWriter = metricWriter;
this.metricsSources = metricsSources;
this.executor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("metrics-exporter-%d")
.build()
);
}
@PostConstruct
public void run() {
logger.info("[metrics] Scheduling export of metrics");
// List of metrics to export
metricsSources.forEach(
metricsSource -> logger.info("[metrics] Source: {}", metricsSource.getClass().getName())
);
// Scheduling of the export
executor.scheduleAtFixedRate(() -> {
try {
logger.trace("[metrics] Exporting...");
export();
} catch (RuntimeException ex) {
logger.error("RuntimeException thrown from {}#export. Exception was suppressed.", getClass().getSimpleName(), ex);
}
}, config.getMetricsPeriod(), config.getMetricsPeriod(), TimeUnit.SECONDS);
}
@Override
protected Iterable> next(String group) {
return metricsSources.stream().flatMap(MetricsSource::getMetrics).collect(Collectors.toList());
}
@Override
protected void write(String group, Collection> values) {
values.forEach(this::write);
}
private void write(Metric> metric) {
logger.trace("[metrics] {} -> {}", metric.getName(), metric.getValue());
metricWriter.submit(metric.getName(), metric.getValue().doubleValue());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy