Skip to content

Commit

Permalink
feat: Added new metadata fields to Registration and Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Jul 25, 2024
1 parent 90a872a commit 9d57d58
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/main/java/io/getunleash/metric/ClientMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
private final String platformName;
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", "JRE");
this.platformVersion = System.getProperty("java.vm.version", "1.8");
this.yggdrasilVersion = null;
}

public String getAppName() {
Expand All @@ -34,6 +43,23 @@ public String getEnvironment() {
return environment;
}

public String getSpecVersion() {
return specVersion;
}

public String getPlatformName() {
return platformName;
}

public String getPlatformVersion() {
return platformVersion;
}

@Nullable
public String getYggdrasilVersion() {
return yggdrasilVersion;
}

@Override
public void publishTo(UnleashSubscriber unleashSubscriber) {
unleashSubscriber.clientMetrics(this);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/getunleash/metric/ClientRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,10 @@ public class ClientRegistration implements UnleashEvent {
private final LocalDateTime started;
private final long interval;
private final String environment;
private final String platformName;
private final String platformVersion;
@Nullable private final String yggdrasilVersion;
private final String specVersion;

ClientRegistration(UnleashConfig config, LocalDateTime started, Set<String> strategies) {
this.environment = config.getEnvironment();
Expand All @@ -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", "JRE");
this.platformVersion = System.getProperty("java.vm.version", "1.8");
this.yggdrasilVersion = null;
}

public String getAppName() {
Expand Down Expand Up @@ -53,6 +62,22 @@ public String getEnvironment() {
return environment;
}

public String getPlatformName() {
return platformName;
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +39,7 @@ public UnleashMetricServiceImpl(
300,
unleashConfig.getUnleashURLs().getClientMetricsURL());
long metricsInterval = unleashConfig.getSendMetricsInterval();

executor.setInterval(sendMetrics(), metricsInterval, metricsInterval);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Runnable> sendMetricsCallback = ArgumentCaptor.forClass(Runnable.class);
verify(executor).setInterval(sendMetricsCallback.capture(), anyLong(), anyLong());

sendMetricsCallback.getValue().run();
ArgumentCaptor<ClientMetrics> 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<String> 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();
}
}

0 comments on commit 9d57d58

Please sign in to comment.