-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement client registration and metrics.
Adds implementation where the client register at startup and sends metrics to the server regulary. implements #3
- Loading branch information
ivaosthu
committed
Nov 9, 2016
1 parent
4752fa2
commit ef2cbcf
Showing
26 changed files
with
949 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package no.finn.unleash.metric; | ||
|
||
import no.finn.unleash.util.UnleashConfig; | ||
|
||
class ClientMetrics { | ||
|
||
private final String appName; | ||
private final String instanceId; | ||
private final MetricsBucket bucket; | ||
|
||
ClientMetrics(UnleashConfig config, MetricsBucket bucket) { | ||
this.appName = config.getAppName(); | ||
this.instanceId = config.getInstanceId(); | ||
this.bucket = bucket; | ||
} | ||
|
||
public String getAppName() { | ||
return appName; | ||
} | ||
|
||
public String getInstanceId() { | ||
return instanceId; | ||
} | ||
|
||
public MetricsBucket getBucket() { | ||
return bucket; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/no/finn/unleash/metric/ClientRegistration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package no.finn.unleash.metric; | ||
|
||
import no.finn.unleash.util.UnleashConfig; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Set; | ||
|
||
class ClientRegistration { | ||
private final String appName; | ||
private final String instanceId; | ||
private final Set<String> strategies; | ||
private final LocalDateTime started; | ||
private final long interval; | ||
|
||
ClientRegistration(UnleashConfig config, LocalDateTime started, Set<String> strategies) { | ||
this.appName = config.getAppName(); | ||
this.instanceId = config.getInstanceId(); | ||
this.started = started; | ||
this.strategies = strategies; | ||
this.interval = config.getSendMetricsInterval(); | ||
} | ||
|
||
public String getAppName() { | ||
return appName; | ||
} | ||
|
||
public String getInstanceId() { | ||
return instanceId; | ||
} | ||
|
||
public Set<String> getStrategies() { | ||
return strategies; | ||
} | ||
|
||
public LocalDateTime getStarted() { | ||
return started; | ||
} | ||
|
||
public long getInterval() { | ||
return interval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package no.finn.unleash.metric; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class MetricsBucket { | ||
private final Map<String, ToggleCount> toggles; | ||
private final LocalDateTime start; | ||
private LocalDateTime stop; | ||
|
||
MetricsBucket() { | ||
this.start = LocalDateTime.now(ZoneId.of("UTC")); | ||
this.toggles = new HashMap<>(); | ||
} | ||
|
||
void registerCount(String toggleName, boolean active) { | ||
if(toggles.containsKey(toggleName)) { | ||
toggles.get(toggleName).register(active); | ||
} else { | ||
ToggleCount counter = new ToggleCount(); | ||
counter.register(active); | ||
toggles.put(toggleName, counter); | ||
} | ||
} | ||
|
||
void end() { | ||
this.stop = LocalDateTime.now(ZoneId.of("UTC")); | ||
} | ||
|
||
public Map<String, ToggleCount> getToggles() { | ||
return toggles; | ||
} | ||
|
||
public LocalDateTime getStart() { | ||
return start; | ||
} | ||
|
||
public LocalDateTime getStop() { | ||
return stop; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package no.finn.unleash.metric; | ||
|
||
class ToggleCount { | ||
private long yes; | ||
private long no; | ||
|
||
public ToggleCount() { | ||
this.yes = 0; | ||
this.no = 0; | ||
} | ||
|
||
public void register(boolean active) { | ||
if(active) { | ||
yes++; | ||
} else { | ||
no++; | ||
} | ||
} | ||
|
||
public long getYes() { | ||
return yes; | ||
} | ||
|
||
public long getNo() { | ||
return no; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/no/finn/unleash/metric/UnleashMetricService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package no.finn.unleash.metric; | ||
|
||
import java.util.Set; | ||
|
||
public interface UnleashMetricService { | ||
void register(Set<String> strategies); | ||
void count(String toggleName, boolean active); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/no/finn/unleash/metric/UnleashMetricServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package no.finn.unleash.metric; | ||
|
||
import no.finn.unleash.util.UnleashConfig; | ||
import no.finn.unleash.util.UnleashScheduledExecutor; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Set; | ||
|
||
public class UnleashMetricServiceImpl implements UnleashMetricService { | ||
private final LocalDateTime started; | ||
private final UnleashConfig unleashConfig; | ||
private final long metricsInterval; | ||
private final UnleashMetricsSender unleashMetricsSender; | ||
|
||
//mutable | ||
private MetricsBucket currentMetricsBucket; | ||
|
||
public UnleashMetricServiceImpl(UnleashConfig unleashConfig, UnleashScheduledExecutor executor) { | ||
this(unleashConfig, new UnleashMetricsSender(unleashConfig), executor); | ||
} | ||
|
||
public UnleashMetricServiceImpl(UnleashConfig unleashConfig, | ||
UnleashMetricsSender unleashMetricsSender, | ||
UnleashScheduledExecutor executor) { | ||
this.currentMetricsBucket = new MetricsBucket(); | ||
this.started = LocalDateTime.now(ZoneId.of("UTC")); | ||
this.unleashConfig = unleashConfig; | ||
this.metricsInterval = unleashConfig.getSendMetricsInterval(); | ||
this.unleashMetricsSender = unleashMetricsSender; | ||
|
||
executor.setInterval(sendMetrics(), metricsInterval, metricsInterval); | ||
} | ||
|
||
@Override | ||
public void register(Set<String> strategies) { | ||
ClientRegistration registration = new ClientRegistration(unleashConfig, started, strategies); | ||
unleashMetricsSender.registerClient(registration); | ||
} | ||
|
||
@Override | ||
public void count(String toggleName, boolean active) { | ||
currentMetricsBucket.registerCount(toggleName, active); | ||
} | ||
|
||
private Runnable sendMetrics() { | ||
return () -> { | ||
MetricsBucket metricsBucket = this.currentMetricsBucket; | ||
this.currentMetricsBucket = new MetricsBucket(); | ||
metricsBucket.end(); | ||
ClientMetrics metrics = new ClientMetrics(unleashConfig, metricsBucket); | ||
unleashMetricsSender.sendMetrics(metrics); | ||
}; | ||
} | ||
} |
Oops, something went wrong.