diff --git a/src/main/java/io/getunleash/metric/ClientMetrics.java b/src/main/java/io/getunleash/metric/ClientMetrics.java index f59a8ecce..82eac44e9 100644 --- a/src/main/java/io/getunleash/metric/ClientMetrics.java +++ b/src/main/java/io/getunleash/metric/ClientMetrics.java @@ -2,6 +2,7 @@ import io.getunleash.event.UnleashEvent; import io.getunleash.event.UnleashSubscriber; +import io.getunleash.lang.Nullable; import io.getunleash.util.UnleashConfig; public class ClientMetrics implements UnleashEvent { @@ -10,12 +11,20 @@ public class ClientMetrics implements UnleashEvent { private final String instanceId; private final MetricsBucket bucket; private final String environment; + private final String specVersion; + @Nullable private final String platformName; + @Nullable private final String platformVersion; + @Nullable private final String yggdrasilVersion; ClientMetrics(UnleashConfig config, MetricsBucket bucket) { this.environment = config.getEnvironment(); this.appName = config.getAppName(); this.instanceId = config.getInstanceId(); this.bucket = bucket; + this.specVersion = config.getClientSpecificationVersion(); + this.platformName = System.getProperty("java.vm.name"); + this.platformVersion = System.getProperty("java.version"); + this.yggdrasilVersion = null; } public String getAppName() { @@ -34,6 +43,25 @@ public String getEnvironment() { return environment; } + public String getSpecVersion() { + return specVersion; + } + + @Nullable + public String getPlatformName() { + return platformName; + } + + @Nullable + public String getPlatformVersion() { + return platformVersion; + } + + @Nullable + public String getYggdrasilVersion() { + return yggdrasilVersion; + } + @Override public void publishTo(UnleashSubscriber unleashSubscriber) { unleashSubscriber.clientMetrics(this); diff --git a/src/main/java/io/getunleash/metric/ClientRegistration.java b/src/main/java/io/getunleash/metric/ClientRegistration.java index b3019de7b..a820ccf92 100644 --- a/src/main/java/io/getunleash/metric/ClientRegistration.java +++ b/src/main/java/io/getunleash/metric/ClientRegistration.java @@ -2,6 +2,7 @@ import io.getunleash.event.UnleashEvent; import io.getunleash.event.UnleashSubscriber; +import io.getunleash.lang.Nullable; import io.getunleash.util.UnleashConfig; import java.time.LocalDateTime; import java.util.Set; @@ -14,6 +15,10 @@ public class ClientRegistration implements UnleashEvent { private final LocalDateTime started; private final long interval; private final String environment; + @Nullable private final String platformName; + @Nullable private final String platformVersion; + @Nullable private final String yggdrasilVersion; + private final String specVersion; ClientRegistration(UnleashConfig config, LocalDateTime started, Set strategies) { this.environment = config.getEnvironment(); @@ -23,6 +28,10 @@ public class ClientRegistration implements UnleashEvent { this.started = started; this.strategies = strategies; this.interval = config.getSendMetricsInterval(); + this.specVersion = config.getClientSpecificationVersion(); + this.platformName = System.getProperty("java.vm.name"); + this.platformVersion = System.getProperty("java.version"); + this.yggdrasilVersion = null; } public String getAppName() { @@ -53,6 +62,24 @@ public String getEnvironment() { return environment; } + @Nullable + public String getPlatformName() { + return platformName; + } + + @Nullable + public String getPlatformVersion() { + return platformVersion; + } + + public @Nullable String getYggdrasilVersion() { + return yggdrasilVersion; + } + + public String getSpecVersion() { + return specVersion; + } + @Override public void publishTo(UnleashSubscriber unleashSubscriber) { unleashSubscriber.clientRegistered(this); diff --git a/src/main/java/io/getunleash/metric/UnleashMetricServiceImpl.java b/src/main/java/io/getunleash/metric/UnleashMetricServiceImpl.java index fa200cd1b..caf2c3b0e 100644 --- a/src/main/java/io/getunleash/metric/UnleashMetricServiceImpl.java +++ b/src/main/java/io/getunleash/metric/UnleashMetricServiceImpl.java @@ -13,7 +13,6 @@ public class UnleashMetricServiceImpl implements UnleashMetricService { private static final Logger LOGGER = LoggerFactory.getLogger(UnleashMetricServiceImpl.class); private final LocalDateTime started; private final UnleashConfig unleashConfig; - private final MetricSender metricSender; // mutable @@ -40,6 +39,7 @@ public UnleashMetricServiceImpl( 300, unleashConfig.getUnleashURLs().getClientMetricsURL()); long metricsInterval = unleashConfig.getSendMetricsInterval(); + executor.setInterval(sendMetrics(), metricsInterval, metricsInterval); } diff --git a/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java b/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java index 1640ce03b..ed5b631ad 100644 --- a/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java +++ b/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java @@ -5,6 +5,7 @@ import io.getunleash.util.UnleashConfig; import io.getunleash.util.UnleashScheduledExecutor; +import java.time.LocalDateTime; import java.util.HashSet; import java.util.Set; import org.junit.jupiter.api.Test; @@ -412,4 +413,49 @@ public void url_not_found_immediately_increases_interval_to_max() { assertThat(unleashMetricService.getFailures()).isEqualTo(0); assertThat(unleashMetricService.getSkips()).isEqualTo(0); } + + @Test + public void should_add_new_metrics_data_to_bucket() { + UnleashConfig config = + UnleashConfig.builder() + .appName("test") + .sendMetricsInterval(10) + .unleashAPI("http://unleash.com") + .build(); + + UnleashScheduledExecutor executor = mock(UnleashScheduledExecutor.class); + DefaultHttpMetricsSender sender = mock(DefaultHttpMetricsSender.class); + + UnleashMetricService unleashMetricService = + new UnleashMetricServiceImpl(config, sender, executor); + + ArgumentCaptor sendMetricsCallback = ArgumentCaptor.forClass(Runnable.class); + verify(executor).setInterval(sendMetricsCallback.capture(), anyLong(), anyLong()); + + sendMetricsCallback.getValue().run(); + ArgumentCaptor metricsSent = ArgumentCaptor.forClass(ClientMetrics.class); + verify(sender, times(1)).sendMetrics(metricsSent.capture()); + ClientMetrics metrics = metricsSent.getValue(); + assertThat(metrics.getSpecVersion()).isNotEmpty(); + assertThat(metrics.getYggdrasilVersion()).isNull(); + assertThat(metrics.getPlatformName()).isNotEmpty(); + assertThat(metrics.getPlatformVersion()).isNotEmpty(); + } + + @Test + public void client_registration_also_includes_new_metrics_metadata() { + UnleashConfig config = + UnleashConfig.builder() + .appName("test") + .sendMetricsInterval(10) + .unleashAPI("http://unleash.com") + .build(); + Set strategies = new HashSet<>(); + strategies.add("default"); + ClientRegistration reg = new ClientRegistration(config, LocalDateTime.now(), strategies); + assertThat(reg.getPlatformName()).isNotEmpty(); + assertThat(reg.getPlatformVersion()).isNotEmpty(); + assertThat(reg.getSpecVersion()).isEqualTo(config.getClientSpecificationVersion()); + assertThat(reg.getYggdrasilVersion()).isNull(); + } }