28
28
import com .zaxxer .hikari .HikariDataSource ;
29
29
import com .zaxxer .hikari .metrics .micrometer .MicrometerMetricsTrackerFactory ;
30
30
import io .micrometer .core .instrument .MeterRegistry ;
31
+ import io .micrometer .core .instrument .binder .MeterBinder ;
31
32
import org .apache .commons .logging .Log ;
32
33
import org .apache .commons .logging .LogFactory ;
33
34
34
35
import org .springframework .beans .factory .ObjectProvider ;
35
- import org .springframework .beans .factory .annotation .Autowired ;
36
36
import org .springframework .boot .actuate .autoconfigure .metrics .MetricsAutoConfiguration ;
37
37
import org .springframework .boot .actuate .autoconfigure .metrics .export .simple .SimpleMetricsExportAutoConfiguration ;
38
38
import org .springframework .boot .actuate .metrics .jdbc .DataSourcePoolMetrics ;
43
43
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
44
44
import org .springframework .boot .jdbc .DataSourceUnwrapper ;
45
45
import org .springframework .boot .jdbc .metadata .DataSourcePoolMetadataProvider ;
46
+ import org .springframework .context .annotation .Bean ;
46
47
import org .springframework .context .annotation .Configuration ;
47
48
import org .springframework .core .log .LogMessage ;
48
49
import org .springframework .util .StringUtils ;
@@ -66,33 +67,52 @@ static class DataSourcePoolMetadataMetricsConfiguration {
66
67
67
68
private static final String DATASOURCE_SUFFIX = "dataSource" ;
68
69
69
- @ Autowired
70
- void bindDataSourcesToRegistry (Map <String , DataSource > dataSources , MeterRegistry registry ,
70
+ @ Bean
71
+ DataSourcePoolMetadataMeterBinder dataSourcePoolMetadataMeterBinder (Map <String , DataSource > dataSources ,
71
72
ObjectProvider <DataSourcePoolMetadataProvider > metadataProviders ) {
72
- List <DataSourcePoolMetadataProvider > metadataProvidersList = metadataProviders .stream ()
73
- .collect (Collectors .toList ());
74
- dataSources .forEach (
75
- (name , dataSource ) -> bindDataSourceToRegistry (name , dataSource , metadataProvidersList , registry ));
73
+ return new DataSourcePoolMetadataMeterBinder (dataSources , metadataProviders );
76
74
}
77
75
78
- private void bindDataSourceToRegistry (String beanName , DataSource dataSource ,
79
- Collection <DataSourcePoolMetadataProvider > metadataProviders , MeterRegistry registry ) {
80
- String dataSourceName = getDataSourceName (beanName );
81
- new DataSourcePoolMetrics (dataSource , metadataProviders , dataSourceName , Collections .emptyList ())
82
- .bindTo (registry );
83
- }
76
+ static class DataSourcePoolMetadataMeterBinder implements MeterBinder {
77
+
78
+ private final Map <String , DataSource > dataSources ;
79
+
80
+ private final ObjectProvider <DataSourcePoolMetadataProvider > metadataProviders ;
81
+
82
+ DataSourcePoolMetadataMeterBinder (Map <String , DataSource > dataSources ,
83
+ ObjectProvider <DataSourcePoolMetadataProvider > metadataProviders ) {
84
+ this .dataSources = dataSources ;
85
+ this .metadataProviders = metadataProviders ;
86
+ }
84
87
85
- /**
86
- * Get the name of a DataSource based on its {@code beanName}.
87
- * @param beanName the name of the data source bean
88
- * @return a name for the given data source
89
- */
90
- private String getDataSourceName (String beanName ) {
91
- if (beanName .length () > DATASOURCE_SUFFIX .length ()
92
- && StringUtils .endsWithIgnoreCase (beanName , DATASOURCE_SUFFIX )) {
93
- return beanName .substring (0 , beanName .length () - DATASOURCE_SUFFIX .length ());
88
+ @ Override
89
+ public void bindTo (MeterRegistry registry ) {
90
+ List <DataSourcePoolMetadataProvider > metadataProvidersList = this .metadataProviders .stream ()
91
+ .collect (Collectors .toList ());
92
+ this .dataSources .forEach ((name , dataSource ) -> bindDataSourceToRegistry (name , dataSource ,
93
+ metadataProvidersList , registry ));
94
+ }
95
+
96
+ private void bindDataSourceToRegistry (String beanName , DataSource dataSource ,
97
+ Collection <DataSourcePoolMetadataProvider > metadataProviders , MeterRegistry registry ) {
98
+ String dataSourceName = getDataSourceName (beanName );
99
+ new DataSourcePoolMetrics (dataSource , metadataProviders , dataSourceName , Collections .emptyList ())
100
+ .bindTo (registry );
101
+ }
102
+
103
+ /**
104
+ * Get the name of a DataSource based on its {@code beanName}.
105
+ * @param beanName the name of the data source bean
106
+ * @return a name for the given data source
107
+ */
108
+ private String getDataSourceName (String beanName ) {
109
+ if (beanName .length () > DATASOURCE_SUFFIX .length ()
110
+ && StringUtils .endsWithIgnoreCase (beanName , DATASOURCE_SUFFIX )) {
111
+ return beanName .substring (0 , beanName .length () - DATASOURCE_SUFFIX .length ());
112
+ }
113
+ return beanName ;
94
114
}
95
- return beanName ;
115
+
96
116
}
97
117
98
118
}
@@ -101,34 +121,43 @@ private String getDataSourceName(String beanName) {
101
121
@ ConditionalOnClass (HikariDataSource .class )
102
122
static class HikariDataSourceMetricsConfiguration {
103
123
104
- private static final Log logger = LogFactory .getLog (HikariDataSourceMetricsConfiguration .class );
124
+ @ Bean
125
+ HikariDataSourceMeterBinder hikariDataSourceMeterBinder (ObjectProvider <DataSource > dataSources ) {
126
+ return new HikariDataSourceMeterBinder (dataSources );
127
+ }
105
128
106
- private final MeterRegistry registry ;
129
+ static class HikariDataSourceMeterBinder implements MeterBinder {
107
130
108
- HikariDataSourceMetricsConfiguration (MeterRegistry registry ) {
109
- this .registry = registry ;
110
- }
131
+ private static final Log logger = LogFactory .getLog (HikariDataSourceMeterBinder .class );
111
132
112
- @ Autowired
113
- void bindMetricsRegistryToHikariDataSources (Collection <DataSource > dataSources ) {
114
- for (DataSource dataSource : dataSources ) {
115
- HikariDataSource hikariDataSource = DataSourceUnwrapper .unwrap (dataSource , HikariConfigMXBean .class ,
116
- HikariDataSource .class );
117
- if (hikariDataSource != null ) {
118
- bindMetricsRegistryToHikariDataSource (hikariDataSource );
119
- }
133
+ private final ObjectProvider <DataSource > dataSources ;
134
+
135
+ HikariDataSourceMeterBinder (ObjectProvider <DataSource > dataSources ) {
136
+ this .dataSources = dataSources ;
120
137
}
121
- }
122
138
123
- private void bindMetricsRegistryToHikariDataSource (HikariDataSource hikari ) {
124
- if (hikari .getMetricRegistry () == null && hikari .getMetricsTrackerFactory () == null ) {
125
- try {
126
- hikari .setMetricsTrackerFactory (new MicrometerMetricsTrackerFactory (this .registry ));
139
+ @ Override
140
+ public void bindTo (MeterRegistry registry ) {
141
+ for (DataSource dataSource : this .dataSources ) {
142
+ HikariDataSource hikariDataSource = DataSourceUnwrapper .unwrap (dataSource , HikariConfigMXBean .class ,
143
+ HikariDataSource .class );
144
+ if (hikariDataSource != null ) {
145
+ bindMetricsRegistryToHikariDataSource (hikariDataSource , registry );
146
+ }
127
147
}
128
- catch (Exception ex ) {
129
- logger .warn (LogMessage .format ("Failed to bind Hikari metrics: %s" , ex .getMessage ()));
148
+ }
149
+
150
+ private void bindMetricsRegistryToHikariDataSource (HikariDataSource hikari , MeterRegistry registry ) {
151
+ if (hikari .getMetricRegistry () == null && hikari .getMetricsTrackerFactory () == null ) {
152
+ try {
153
+ hikari .setMetricsTrackerFactory (new MicrometerMetricsTrackerFactory (registry ));
154
+ }
155
+ catch (Exception ex ) {
156
+ logger .warn (LogMessage .format ("Failed to bind Hikari metrics: %s" , ex .getMessage ()));
157
+ }
130
158
}
131
159
}
160
+
132
161
}
133
162
134
163
}
0 commit comments