net.nemerosa.ontrack.boot.metrics.DataSourceMetrics Maven / Gradle / Ivy
package net.nemerosa.ontrack.boot.metrics;
import net.nemerosa.ontrack.model.metrics.OntrackMetrics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProviders;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Component
public class DataSourceMetrics implements OntrackMetrics {
private final Map dataSources;
private final DataSourcePoolMetadataProviders provider;
@Autowired
public DataSourceMetrics(
Map dataSources,
Collection providers) {
this.dataSources = dataSources;
this.provider = new DataSourcePoolMetadataProviders(providers);
}
@Override
public Collection> metrics() {
List> metrics = new ArrayList<>();
dataSources.entrySet().stream().forEach(entry -> {
String name = entry.getKey();
String prefix = String.format("gauge.datasource.%s", name);
DataSource dataSource = entry.getValue();
DataSourcePoolMetadata poolMetadata = provider.getDataSourcePoolMetadata(dataSource);
metrics.add(new Metric<>(prefix + ".active", poolMetadata.getActive()));
metrics.add(new Metric<>(prefix + ".max", poolMetadata.getMax()));
metrics.add(new Metric<>(prefix + ".min", poolMetadata.getMin()));
metrics.add(new Metric<>(prefix + ".usage", poolMetadata.getUsage()));
});
return metrics;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy