Skip to content

open DataSourcePublicMetrics for easy extension (#10288) #10289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@

package org.springframework.boot.actuate.autoconfigure.metrics;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.After;
import org.junit.Test;

import org.springframework.boot.actuate.autoconfigure.cache.CacheStatisticsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.servlet.MockServletWebServerFactory;
import org.springframework.boot.actuate.cache.CachePublicMetrics;
Expand All @@ -44,6 +48,8 @@
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadata;
import org.springframework.boot.jdbc.metadata.TomcatDataSourcePoolMetadata;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.cache.CacheManager;
Expand All @@ -57,10 +63,7 @@
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import com.zaxxer.hikari.HikariDataSource;

/**
* Tests for {@link PublicMetricsAutoConfiguration}.
Expand Down Expand Up @@ -168,6 +171,15 @@ public void multipleDataSourcesWithCustomPrimary() {
"datasource.dataSource.active", "datasource.dataSource.usage");
}

@Test
public void customDataSourcePublicMetrics() {
load(CustomDataSourcePublicMetricsConfig.class);
PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage",
"datasource.primary.idle", "datasource.primary.poolSize");
}

@Test
public void customPrefix() {
load(MultipleDataSourcesWithPrimaryConfig.class,
Expand Down Expand Up @@ -306,6 +318,36 @@ public DataSource dataSource() {

}

@Configuration
static class CustomDataSourcePublicMetricsConfig {

@Bean
public DataSource myDataSource() {
return InitializedBuilder.create()
.type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
}

@Bean
public TomcatDataSourcePublicMetrics tomcatDataSourcePublicMetrics() {
return new TomcatDataSourcePublicMetrics();
}

static class TomcatDataSourcePublicMetrics extends DataSourcePublicMetrics {

@Override
protected void addMetricsForDataSourcePoolMetadata(Set<Metric<?>> metrics, DataSourcePoolMetadata metadata, String prefix) {
super.addMetricsForDataSourcePoolMetadata(metrics, metadata, prefix);
if (metadata instanceof TomcatDataSourcePoolMetadata) {
TomcatDataSourcePoolMetadata tomcatMetadata = (TomcatDataSourcePoolMetadata) metadata;
addMetric(metrics, prefix + "idle", tomcatMetadata.getIdle());
addMetric(metrics, prefix + "poolSize", tomcatMetadata.getPoolSize());
}
}
}

}


@Configuration
static class CustomDataSourcePublicMetrics {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class DataSourcePublicMetrics implements PublicMetrics {

private static final String DATASOURCE_SUFFIX = "dataSource";

protected static final String ACTIVE_METRIC_SUFFIX = "active";
protected static final String USAGE_METRIC_SUFFIX = "usage";

@Autowired
private ApplicationContext applicationContext;

Expand Down Expand Up @@ -77,13 +80,17 @@ public Collection<Metric<?>> metrics() {
String prefix = entry.getKey();
prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
DataSourcePoolMetadata metadata = entry.getValue();
addMetric(metrics, prefix + "active", metadata.getActive());
addMetric(metrics, prefix + "usage", metadata.getUsage());
addMetricsForDataSourcePoolMetadata(metrics, metadata, prefix);
}
return metrics;
}

private <T extends Number> void addMetric(Set<Metric<?>> metrics, String name,
protected void addMetricsForDataSourcePoolMetadata(Set<Metric<?>> metrics, DataSourcePoolMetadata metadata, String prefix) {
addMetric(metrics, prefix + ACTIVE_METRIC_SUFFIX, metadata.getActive());
addMetric(metrics, prefix + USAGE_METRIC_SUFFIX, metadata.getUsage());
}

protected <T extends Number> void addMetric(Set<Metric<?>> metrics, String name,
T value) {
if (value != null) {
metrics.add(new Metric<>(name, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ public String getValidationQuery() {
return getDataSource().getValidationQuery();
}

public Integer getIdle() {
return getDataSource().getIdle();
}

public Integer getPoolSize() {
return getDataSource().getPoolSize();
}
}