|
8 | 8 | import io.getunleash.engine.UnleashEngine; |
9 | 9 | import io.getunleash.engine.YggdrasilError; |
10 | 10 | import io.getunleash.engine.YggdrasilInvalidInputException; |
| 11 | +import io.getunleash.impactmetrics.CollectedMetric; |
| 12 | +import io.getunleash.impactmetrics.ImpactMetricsDataSource; |
11 | 13 | import io.getunleash.util.UnleashConfig; |
12 | 14 | import io.getunleash.util.UnleashScheduledExecutor; |
13 | 15 | import java.time.LocalDateTime; |
| 16 | +import java.util.Collections; |
14 | 17 | import java.util.HashSet; |
| 18 | +import java.util.List; |
15 | 19 | import java.util.Set; |
16 | 20 | import org.junit.jupiter.api.Test; |
17 | 21 | import org.mockito.ArgumentCaptor; |
@@ -359,6 +363,129 @@ public void url_not_found_immediately_increases_interval_to_max() throws Yggdras |
359 | 363 | assertThat(unleashMetricService.getSkips()).isEqualTo(0); |
360 | 364 | } |
361 | 365 |
|
| 366 | + @Test |
| 367 | + public void should_include_impact_metrics_in_clientmetrics_payload() throws YggdrasilError { |
| 368 | + UnleashConfig config = |
| 369 | + UnleashConfig.builder() |
| 370 | + .appName("test") |
| 371 | + .sendMetricsInterval(10) |
| 372 | + .unleashAPI("http://unleash.com") |
| 373 | + .impactMetricsRegistry(mock(ImpactMetricsDataSource.class)) |
| 374 | + .build(); |
| 375 | + |
| 376 | + UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class); |
| 377 | + DefaultHttpMetricsSender sender = mock(DefaultHttpMetricsSender.class); |
| 378 | + UnleashEngine engine = new UnleashEngine(); |
| 379 | + ImpactMetricsDataSource registry = mock(ImpactMetricsDataSource.class); |
| 380 | + |
| 381 | + CollectedMetric sample = |
| 382 | + new CollectedMetric( |
| 383 | + "feature_toggle_used", |
| 384 | + "tracks toggle usage", |
| 385 | + io.getunleash.impactmetrics.MetricType.COUNTER, |
| 386 | + Collections.emptyList()); |
| 387 | + when(registry.collect()).thenReturn(List.of(sample)); |
| 388 | + |
| 389 | + UnleashConfig configWithRegistry = |
| 390 | + UnleashConfig.builder() |
| 391 | + .appName("test") |
| 392 | + .sendMetricsInterval(10) |
| 393 | + .unleashAPI("http://unleash.com") |
| 394 | + .impactMetricsRegistry(registry) |
| 395 | + .build(); |
| 396 | + |
| 397 | + UnleashMetricServiceImpl unleashMetricService = |
| 398 | + new UnleashMetricServiceImpl(configWithRegistry, sender, executor, engine); |
| 399 | + |
| 400 | + ArgumentCaptor<Runnable> sendMetricsCallback = ArgumentCaptor.forClass(Runnable.class); |
| 401 | + verify(executor).setInterval(sendMetricsCallback.capture(), anyLong(), anyLong()); |
| 402 | + |
| 403 | + when(sender.sendMetrics(any(ClientMetrics.class))).thenReturn(200); |
| 404 | + |
| 405 | + sendMetricsCallback.getValue().run(); |
| 406 | + |
| 407 | + ArgumentCaptor<ClientMetrics> cmCaptor = ArgumentCaptor.forClass(ClientMetrics.class); |
| 408 | + verify(sender).sendMetrics(cmCaptor.capture()); |
| 409 | + |
| 410 | + ClientMetrics metricsSent = cmCaptor.getValue(); |
| 411 | + assertThat(metricsSent.getImpactMetrics()).isNotNull(); |
| 412 | + assertThat(metricsSent.getImpactMetrics()).hasSize(1); |
| 413 | + assertThat(metricsSent.getImpactMetrics().get(0).getName()) |
| 414 | + .isEqualTo("feature_toggle_used"); |
| 415 | + } |
| 416 | + |
| 417 | + @Test |
| 418 | + public void should_restore_impact_metrics_on_failure() throws YggdrasilError { |
| 419 | + ImpactMetricsDataSource registry = mock(ImpactMetricsDataSource.class); |
| 420 | + |
| 421 | + CollectedMetric sample = |
| 422 | + new CollectedMetric( |
| 423 | + "feature_toggle_used", |
| 424 | + "tracks toggle usage", |
| 425 | + io.getunleash.impactmetrics.MetricType.COUNTER, |
| 426 | + Collections.emptyList()); |
| 427 | + when(registry.collect()).thenReturn(List.of(sample)); |
| 428 | + |
| 429 | + UnleashConfig config = |
| 430 | + UnleashConfig.builder() |
| 431 | + .appName("test") |
| 432 | + .sendMetricsInterval(10) |
| 433 | + .unleashAPI("http://unleash.com") |
| 434 | + .impactMetricsRegistry(registry) |
| 435 | + .build(); |
| 436 | + |
| 437 | + UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class); |
| 438 | + DefaultHttpMetricsSender sender = mock(DefaultHttpMetricsSender.class); |
| 439 | + UnleashEngine engine = new UnleashEngine(); |
| 440 | + |
| 441 | + UnleashMetricServiceImpl unleashMetricService = |
| 442 | + new UnleashMetricServiceImpl(config, sender, executor, engine); |
| 443 | + |
| 444 | + ArgumentCaptor<Runnable> sendMetricsCallback = ArgumentCaptor.forClass(Runnable.class); |
| 445 | + verify(executor).setInterval(sendMetricsCallback.capture(), anyLong(), anyLong()); |
| 446 | + |
| 447 | + when(sender.sendMetrics(any(ClientMetrics.class))).thenReturn(500); |
| 448 | + |
| 449 | + sendMetricsCallback.getValue().run(); |
| 450 | + |
| 451 | + verify(registry, times(1)).restore(List.of(sample)); |
| 452 | + } |
| 453 | + |
| 454 | + @Test |
| 455 | + public void should_not_include_impact_metrics_field_when_empty() throws YggdrasilError { |
| 456 | + ImpactMetricsDataSource registry = mock(ImpactMetricsDataSource.class); |
| 457 | + when(registry.collect()).thenReturn(Collections.emptyList()); |
| 458 | + |
| 459 | + UnleashConfig config = |
| 460 | + UnleashConfig.builder() |
| 461 | + .appName("test") |
| 462 | + .sendMetricsInterval(10) |
| 463 | + .unleashAPI("http://unleash.com") |
| 464 | + .impactMetricsRegistry(registry) |
| 465 | + .build(); |
| 466 | + |
| 467 | + UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class); |
| 468 | + DefaultHttpMetricsSender sender = mock(DefaultHttpMetricsSender.class); |
| 469 | + UnleashEngine engine = new UnleashEngine(); |
| 470 | + |
| 471 | + UnleashMetricServiceImpl unleashMetricService = |
| 472 | + new UnleashMetricServiceImpl(config, sender, executor, engine); |
| 473 | + |
| 474 | + ArgumentCaptor<Runnable> sendMetricsCallback = ArgumentCaptor.forClass(Runnable.class); |
| 475 | + verify(executor).setInterval(sendMetricsCallback.capture(), anyLong(), anyLong()); |
| 476 | + |
| 477 | + when(sender.sendMetrics(any(ClientMetrics.class))).thenReturn(200); |
| 478 | + |
| 479 | + sendMetricsCallback.getValue().run(); |
| 480 | + |
| 481 | + ArgumentCaptor<ClientMetrics> cmCaptor = ArgumentCaptor.forClass(ClientMetrics.class); |
| 482 | + verify(sender).sendMetrics(cmCaptor.capture()); |
| 483 | + |
| 484 | + ClientMetrics metricsSent = cmCaptor.getValue(); |
| 485 | + assertThat(metricsSent.getImpactMetrics()).isNull(); |
| 486 | + verify(registry, times(1)).collect(); |
| 487 | + } |
| 488 | + |
362 | 489 | @Test |
363 | 490 | public void should_add_new_metrics_data_to_bucket() { |
364 | 491 | UnleashConfig config = |
|
0 commit comments