1
1
/*
2
- * Copyright 2012-2022 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .boot .actuate .autoconfigure .metrics ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Collections ;
20
21
import java .util .List ;
21
22
23
+ import io .micrometer .core .instrument .Clock ;
22
24
import io .micrometer .core .instrument .MeterRegistry ;
23
25
import io .micrometer .core .instrument .MeterRegistry .Config ;
24
26
import io .micrometer .core .instrument .Metrics ;
32
34
import org .mockito .junit .jupiter .MockitoExtension ;
33
35
34
36
import org .springframework .beans .factory .ObjectProvider ;
37
+ import org .springframework .boot .actuate .autoconfigure .metrics .MeterRegistryPostProcessor .CompositeMeterRegistries ;
35
38
36
39
import static org .assertj .core .api .Assertions .assertThat ;
37
40
import static org .mockito .BDDMockito .given ;
@@ -76,21 +79,34 @@ class MeterRegistryPostProcessorTests {
76
79
}
77
80
78
81
@ Test
79
- void postProcessAndInitializeWhenCompositeAppliesCustomizer () {
82
+ void postProcessAndInitializeWhenUserDefinedCompositeAppliesCustomizer () {
80
83
this .customizers .add (this .mockCustomizer );
81
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (false ,
82
- createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
83
- createObjectProvider (this .filters ), createObjectProvider (this .binders ));
84
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (
85
+ CompositeMeterRegistries .ONLY_USER_DEFINED , createObjectProvider (this .properties ),
86
+ createObjectProvider (this .customizers ), createObjectProvider (this .filters ),
87
+ createObjectProvider (this .binders ));
84
88
CompositeMeterRegistry composite = new CompositeMeterRegistry ();
85
89
postProcessAndInitialize (processor , composite );
86
90
then (this .mockCustomizer ).should ().customize (composite );
87
91
}
88
92
93
+ @ Test
94
+ void postProcessAndInitializeWhenAutoConfiguredCompositeAppliesCustomizer () {
95
+ this .customizers .add (this .mockCustomizer );
96
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries .AUTO_CONFIGURED ,
97
+ createObjectProvider (this .properties ), createObjectProvider (this .customizers ), null ,
98
+ createObjectProvider (this .binders ));
99
+ AutoConfiguredCompositeMeterRegistry composite = new AutoConfiguredCompositeMeterRegistry (Clock .SYSTEM ,
100
+ Collections .emptyList ());
101
+ postProcessAndInitialize (processor , composite );
102
+ then (this .mockCustomizer ).should ().customize (composite );
103
+ }
104
+
89
105
@ Test
90
106
void postProcessAndInitializeAppliesCustomizer () {
91
107
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
92
108
this .customizers .add (this .mockCustomizer );
93
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
109
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
94
110
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
95
111
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
96
112
postProcessAndInitialize (processor , this .mockRegistry );
@@ -101,7 +117,7 @@ void postProcessAndInitializeAppliesCustomizer() {
101
117
void postProcessAndInitializeAppliesFilter () {
102
118
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
103
119
this .filters .add (this .mockFilter );
104
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
120
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
105
121
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
106
122
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
107
123
postProcessAndInitialize (processor , this .mockRegistry );
@@ -112,41 +128,75 @@ void postProcessAndInitializeAppliesFilter() {
112
128
void postProcessAndInitializeBindsTo () {
113
129
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
114
130
this .binders .add (this .mockBinder );
115
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
131
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
116
132
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
117
133
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
118
134
postProcessAndInitialize (processor , this .mockRegistry );
119
135
then (this .mockBinder ).should ().bindTo (this .mockRegistry );
120
136
}
121
137
122
138
@ Test
123
- void postProcessAndInitializeWhenCompositeBindsTo () {
139
+ void whenUserDefinedCompositeThenPostProcessAndInitializeCompositeBindsTo () {
124
140
this .binders .add (this .mockBinder );
125
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (false ,
126
- createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
127
- createObjectProvider (this .filters ), createObjectProvider (this .binders ));
141
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (
142
+ CompositeMeterRegistries .ONLY_USER_DEFINED , createObjectProvider (this .properties ),
143
+ createObjectProvider (this .customizers ), createObjectProvider (this .filters ),
144
+ createObjectProvider (this .binders ));
128
145
CompositeMeterRegistry composite = new CompositeMeterRegistry ();
129
146
postProcessAndInitialize (processor , composite );
130
147
then (this .mockBinder ).should ().bindTo (composite );
131
148
}
132
149
133
150
@ Test
134
- void postProcessAndInitializeWhenCompositeExistsDoesNotBindTo () {
151
+ void whenUserDefinedCompositeThenPostProcessAndInitializeStandardRegistryDoesNotBindTo () {
152
+ given (this .mockRegistry .config ()).willReturn (this .mockConfig );
153
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (
154
+ CompositeMeterRegistries .ONLY_USER_DEFINED , createObjectProvider (this .properties ),
155
+ createObjectProvider (this .customizers ), createObjectProvider (this .filters ), null );
156
+ postProcessAndInitialize (processor , this .mockRegistry );
157
+ then (this .mockBinder ).shouldHaveNoInteractions ();
158
+ }
159
+
160
+ @ Test
161
+ void whenAutoConfiguredCompositeThenPostProcessAndInitializeAutoConfiguredCompositeBindsTo () {
162
+ this .binders .add (this .mockBinder );
163
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries .AUTO_CONFIGURED ,
164
+ createObjectProvider (this .properties ), createObjectProvider (this .customizers ), null ,
165
+ createObjectProvider (this .binders ));
166
+ AutoConfiguredCompositeMeterRegistry composite = new AutoConfiguredCompositeMeterRegistry (Clock .SYSTEM ,
167
+ Collections .emptyList ());
168
+ postProcessAndInitialize (processor , composite );
169
+ then (this .mockBinder ).should ().bindTo (composite );
170
+ }
171
+
172
+ @ Test
173
+ void whenAutoConfiguredCompositeThenPostProcessAndInitializeCompositeDoesNotBindTo () {
174
+ this .binders .add (this .mockBinder );
175
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries .AUTO_CONFIGURED ,
176
+ createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
177
+ createObjectProvider (this .filters ), null );
178
+ CompositeMeterRegistry composite = new CompositeMeterRegistry ();
179
+ postProcessAndInitialize (processor , composite );
180
+ then (this .mockBinder ).shouldHaveNoInteractions ();
181
+ }
182
+
183
+ @ Test
184
+ void whenAutoConfiguredCompositeThenPostProcessAndInitializeStandardRegistryDoesNotBindTo () {
135
185
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
136
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (false ,
186
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . AUTO_CONFIGURED ,
137
187
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
138
188
createObjectProvider (this .filters ), null );
139
189
postProcessAndInitialize (processor , this .mockRegistry );
140
190
then (this .mockBinder ).shouldHaveNoInteractions ();
141
191
}
142
192
143
193
@ Test
144
- void postProcessAndInitializeBeOrderedCustomizerThenFilterThenBindTo () {
194
+ void postProcessAndInitializeIsOrderedCustomizerThenFilterThenBindTo () {
145
195
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
146
196
this .customizers .add (this .mockCustomizer );
147
197
this .filters .add (this .mockFilter );
148
198
this .binders .add (this .mockBinder );
149
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
199
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
150
200
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
151
201
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
152
202
postProcessAndInitialize (processor , this .mockRegistry );
@@ -160,7 +210,7 @@ void postProcessAndInitializeBeOrderedCustomizerThenFilterThenBindTo() {
160
210
void postProcessAndInitializeWhenUseGlobalRegistryTrueAddsToGlobalRegistry () {
161
211
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
162
212
this .properties .setUseGlobalRegistry (true );
163
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
213
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
164
214
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
165
215
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
166
216
try {
@@ -175,7 +225,7 @@ void postProcessAndInitializeWhenUseGlobalRegistryTrueAddsToGlobalRegistry() {
175
225
@ Test
176
226
void postProcessAndInitializeWhenUseGlobalRegistryFalseDoesNotAddToGlobalRegistry () {
177
227
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
178
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
228
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
179
229
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
180
230
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
181
231
postProcessAndInitialize (processor , this .mockRegistry );
@@ -186,7 +236,7 @@ void postProcessAndInitializeWhenUseGlobalRegistryFalseDoesNotAddToGlobalRegistr
186
236
void postProcessDoesNotBindToUntilSingletonsInitialized () {
187
237
given (this .mockRegistry .config ()).willReturn (this .mockConfig );
188
238
this .binders .add (this .mockBinder );
189
- MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (true ,
239
+ MeterRegistryPostProcessor processor = new MeterRegistryPostProcessor (CompositeMeterRegistries . NONE ,
190
240
createObjectProvider (this .properties ), createObjectProvider (this .customizers ),
191
241
createObjectProvider (this .filters ), createObjectProvider (this .binders ));
192
242
processor .postProcessAfterInitialization (this .mockRegistry , "meterRegistry" );
0 commit comments