|
15 | 15 | */
|
16 | 16 | package io.micrometer.dynatrace;
|
17 | 17 |
|
18 |
| -import io.micrometer.core.instrument.Counter; |
19 |
| -import io.micrometer.core.instrument.MockClock; |
20 |
| -import io.micrometer.core.instrument.Timer; |
| 18 | +import io.micrometer.core.instrument.*; |
21 | 19 | import io.micrometer.core.ipc.http.HttpSender;
|
22 | 20 | import org.junit.jupiter.api.BeforeEach;
|
23 | 21 | import org.junit.jupiter.api.Test;
|
24 | 22 | import org.mockito.ArgumentCaptor;
|
25 | 23 |
|
26 | 24 | import java.nio.charset.StandardCharsets;
|
| 25 | +import java.time.Duration; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | +import java.util.concurrent.ExecutorService; |
| 28 | +import java.util.concurrent.Executors; |
27 | 29 |
|
28 | 30 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
29 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -151,6 +153,142 @@ void shouldNotTrackPercentilesWithDynatraceSummary() throws Throwable {
|
151 | 153 | "#my.timer gauge dt.meta.unit=milliseconds"))));
|
152 | 154 | }
|
153 | 155 |
|
| 156 | + @Test |
| 157 | + void shouldTrackPercentilesWhenDynatraceSummaryInstrumentsNotUsed() throws Throwable { |
| 158 | + DynatraceConfig dynatraceConfig = getNonSummaryInstrumentsConfig(); |
| 159 | + |
| 160 | + DynatraceMeterRegistry registry = DynatraceMeterRegistry.builder(dynatraceConfig) |
| 161 | + .httpClient(httpClient) |
| 162 | + .clock(clock) |
| 163 | + .build(); |
| 164 | + |
| 165 | + HttpSender.Request.Builder builder = HttpSender.Request.build(config.uri(), httpClient); |
| 166 | + when(httpClient.post(config.uri())).thenReturn(builder); |
| 167 | + |
| 168 | + double[] trackedPercentiles = new double[] { 0.5, 0.7, 0.99 }; |
| 169 | + |
| 170 | + Timer timer = Timer.builder("my.timer").publishPercentiles(trackedPercentiles).register(registry); |
| 171 | + DistributionSummary distributionSummary = DistributionSummary.builder("my.ds") |
| 172 | + .publishPercentiles(trackedPercentiles) |
| 173 | + .register(registry); |
| 174 | + CountDownLatch lttCountDownLatch = new CountDownLatch(1); |
| 175 | + LongTaskTimer longTaskTimer = LongTaskTimer.builder("my.ltt") |
| 176 | + .publishPercentiles(trackedPercentiles) |
| 177 | + .register(registry); |
| 178 | + |
| 179 | + timer.record(Duration.ofMillis(100)); |
| 180 | + distributionSummary.record(100); |
| 181 | + |
| 182 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 183 | + executorService.submit(() -> longTaskTimer.record(() -> { |
| 184 | + clock.add(Duration.ofMillis(100)); |
| 185 | + |
| 186 | + try { |
| 187 | + assertThat(lttCountDownLatch.await(300, MILLISECONDS)).isTrue(); |
| 188 | + } |
| 189 | + catch (InterruptedException e) { |
| 190 | + throw new RuntimeException(e); |
| 191 | + } |
| 192 | + })); |
| 193 | + |
| 194 | + clock.add(dynatraceConfig.step()); |
| 195 | + |
| 196 | + registry.publish(); |
| 197 | + // release long task timer |
| 198 | + lttCountDownLatch.countDown(); |
| 199 | + |
| 200 | + verify(httpClient).send( |
| 201 | + assertArg((request -> assertThat(request.getEntity()).asString() |
| 202 | + .hasLineCount(16) |
| 203 | + .contains( |
| 204 | + // Timer lines |
| 205 | + "my.timer,dt.metrics.source=micrometer gauge,min=100,max=100,sum=100,count=1 " |
| 206 | + + clock.wallTime(), |
| 207 | + "#my.timer gauge dt.meta.unit=milliseconds", |
| 208 | + // Timer percentile lines. Percentiles are 0 because the step |
| 209 | + // rolled over. |
| 210 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0.5 gauge,0 " + clock.wallTime(), |
| 211 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0.7 gauge,0 " + clock.wallTime(), |
| 212 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0.99 gauge,0 " + clock.wallTime(), |
| 213 | + "#my.timer.percentile gauge dt.meta.unit=milliseconds", |
| 214 | + |
| 215 | + // DistributionSummary lines |
| 216 | + "my.ds,dt.metrics.source=micrometer gauge,min=100,max=100,sum=100,count=1 " |
| 217 | + + clock.wallTime(), |
| 218 | + // DistributionSummary percentile lines. Percentiles are 0 |
| 219 | + // because the step rolled over. |
| 220 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0.5 gauge,0 " + clock.wallTime(), |
| 221 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0.7 gauge,0 " + clock.wallTime(), |
| 222 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0.99 gauge,0 " + clock.wallTime(), |
| 223 | + |
| 224 | + // LongTaskTimer lines |
| 225 | + "my.ltt,dt.metrics.source=micrometer gauge,min=100,max=100,sum=100,count=1 " |
| 226 | + + clock.wallTime(), |
| 227 | + "#my.ltt gauge dt.meta.unit=milliseconds", |
| 228 | + // LongTaskTimer percentile lines |
| 229 | + // 0th percentile is missing because it doesn't clear the |
| 230 | + // "interpolatable line" threshold defined in |
| 231 | + // DefaultLongTaskTimer#takeSnapshot(). |
| 232 | + "my.ltt.percentile,dt.metrics.source=micrometer,phi=0.5 gauge,100 " + clock.wallTime(), |
| 233 | + "my.ltt.percentile,dt.metrics.source=micrometer,phi=0.7 gauge,100 " + clock.wallTime(), |
| 234 | + "my.ltt.percentile,dt.metrics.source=micrometer,phi=0.99 gauge,100 " + clock.wallTime(), |
| 235 | + "#my.ltt.percentile gauge dt.meta.unit=milliseconds")))); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + void shouldTrackPercentilesWhenDynatraceSummaryInstrumentsNotUsed_shouldExport0PercentileWhenSpecified() |
| 240 | + throws Throwable { |
| 241 | + DynatraceConfig dynatraceConfig = getNonSummaryInstrumentsConfig(); |
| 242 | + |
| 243 | + DynatraceMeterRegistry registry = DynatraceMeterRegistry.builder(dynatraceConfig) |
| 244 | + .httpClient(httpClient) |
| 245 | + .clock(clock) |
| 246 | + .build(); |
| 247 | + |
| 248 | + HttpSender.Request.Builder builder = HttpSender.Request.build(config.uri(), httpClient); |
| 249 | + when(httpClient.post(config.uri())).thenReturn(builder); |
| 250 | + |
| 251 | + // create instruments with an explicit 0 percentile. This should be exported. |
| 252 | + Timer timer = Timer.builder("my.timer").publishPercentiles(0, 0.5, 0.99).register(registry); |
| 253 | + DistributionSummary distributionSummary = DistributionSummary.builder("my.ds") |
| 254 | + .publishPercentiles(0, 0.5, 0.99) |
| 255 | + .register(registry); |
| 256 | + // For LongTaskTimer, the 0 percentile is not tracked as it doesn't clear the |
| 257 | + // "interpolatable line" threshold defined in DefaultLongTaskTimer#takeSnapshot(). |
| 258 | + // see shouldTrackPercentilesWhenDynatraceSummaryInstrumentsNotUsed for a test |
| 259 | + // that exports LongTaskTimer percentiles |
| 260 | + |
| 261 | + timer.record(Duration.ofMillis(100)); |
| 262 | + distributionSummary.record(100); |
| 263 | + |
| 264 | + clock.add(dynatraceConfig.step()); |
| 265 | + |
| 266 | + registry.publish(); |
| 267 | + |
| 268 | + verify(httpClient) |
| 269 | + .send(assertArg((request -> assertThat(request.getEntity()).asString() |
| 270 | + .hasLineCount(10) |
| 271 | + .contains( |
| 272 | + // Timer lines |
| 273 | + "my.timer,dt.metrics.source=micrometer gauge,min=100,max=100,sum=100,count=1 " |
| 274 | + + clock.wallTime(), |
| 275 | + "#my.timer gauge dt.meta.unit=milliseconds", |
| 276 | + // Timer percentile lines. Percentiles are 0 because the step |
| 277 | + // rolled over. |
| 278 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0 gauge,0 " + clock.wallTime(), |
| 279 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0.5 gauge,0 " + clock.wallTime(), |
| 280 | + "my.timer.percentile,dt.metrics.source=micrometer,phi=0.99 gauge,0 " + clock.wallTime(), |
| 281 | + "#my.timer.percentile gauge dt.meta.unit=milliseconds", |
| 282 | + |
| 283 | + // DistributionSummary lines |
| 284 | + "my.ds,dt.metrics.source=micrometer gauge,min=100,max=100,sum=100,count=1 " + clock.wallTime(), |
| 285 | + // DistributionSummary percentile lines. Percentiles are 0 because |
| 286 | + // the step rolled over. |
| 287 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0 gauge,0 " + clock.wallTime(), |
| 288 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0.5 gauge,0 " + clock.wallTime(), |
| 289 | + "my.ds.percentile,dt.metrics.source=micrometer,phi=0.99 gauge,0 " + clock.wallTime())))); |
| 290 | + } |
| 291 | + |
154 | 292 | @Test
|
155 | 293 | void shouldNotExportLinesWithZeroCount() throws Throwable {
|
156 | 294 | HttpSender.Request.Builder builder = HttpSender.Request.build(config.uri(), httpClient);
|
@@ -217,6 +355,35 @@ public DynatraceApiVersion apiVersion() {
|
217 | 355 | };
|
218 | 356 | }
|
219 | 357 |
|
| 358 | + private static DynatraceConfig getNonSummaryInstrumentsConfig() { |
| 359 | + return new DynatraceConfig() { |
| 360 | + @Override |
| 361 | + public String get(String key) { |
| 362 | + return null; |
| 363 | + } |
| 364 | + |
| 365 | + @Override |
| 366 | + public String uri() { |
| 367 | + return "http://localhost"; |
| 368 | + } |
| 369 | + |
| 370 | + @Override |
| 371 | + public String apiToken() { |
| 372 | + return "apiToken"; |
| 373 | + } |
| 374 | + |
| 375 | + @Override |
| 376 | + public DynatraceApiVersion apiVersion() { |
| 377 | + return DynatraceApiVersion.V2; |
| 378 | + } |
| 379 | + |
| 380 | + @Override |
| 381 | + public boolean useDynatraceSummaryInstruments() { |
| 382 | + return false; |
| 383 | + } |
| 384 | + }; |
| 385 | + } |
| 386 | + |
220 | 387 | private String formatDouble(double value) {
|
221 | 388 | if (value == (long) value) {
|
222 | 389 | return Long.toString((long) value);
|
|
0 commit comments