-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathUnleashMetricServiceImpl.java
More file actions
84 lines (71 loc) · 2.78 KB
/
UnleashMetricServiceImpl.java
File metadata and controls
84 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package io.getunleash.metric;
import io.getunleash.engine.MetricsBucket;
import io.getunleash.engine.UnleashEngine;
import io.getunleash.util.Throttler;
import io.getunleash.util.UnleashConfig;
import io.getunleash.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 MetricSender metricSender;
// synchronization is handled in the engine itself
private final UnleashEngine engine;
private final Throttler throttler;
public UnleashMetricServiceImpl(
UnleashConfig unleashConfig, UnleashScheduledExecutor executor, UnleashEngine engine) {
this(
unleashConfig,
unleashConfig.getMetricSenderFactory().apply(unleashConfig),
executor,
engine);
}
public UnleashMetricServiceImpl(
UnleashConfig unleashConfig,
MetricSender metricSender,
UnleashScheduledExecutor executor,
UnleashEngine engine) {
this.started = LocalDateTime.now(ZoneId.of("UTC"));
this.unleashConfig = unleashConfig;
this.metricSender = metricSender;
this.throttler =
new Throttler(
(int) unleashConfig.getSendMetricsInterval(),
300,
unleashConfig.getUnleashURLs().getClientMetricsURL());
this.engine = engine;
long metricsInterval = unleashConfig.getSendMetricsInterval();
executor.setInterval(sendMetrics(), metricsInterval, metricsInterval);
}
@Override
public void register(Set<String> strategies) {
ClientRegistration registration =
new ClientRegistration(unleashConfig, started, strategies);
metricSender.registerClient(registration);
}
private Runnable sendMetrics() {
return () -> {
if (throttler.performAction()) {
MetricsBucket bucket = this.engine.getMetrics();
ClientMetrics metrics = new ClientMetrics(unleashConfig, bucket);
int statusCode = metricSender.sendMetrics(metrics);
if (statusCode >= 200 && statusCode < 400) {
throttler.decrementFailureCountAndResetSkips();
}
if (statusCode >= 400) {
throttler.handleHttpErrorCodes(statusCode);
}
} else {
throttler.skipped();
}
};
}
protected int getSkips() {
return this.throttler.getSkips();
}
protected int getFailures() {
return this.throttler.getFailures();
}
}