-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathDefaultHttpMetricsSenderTest.java
More file actions
156 lines (133 loc) · 7.35 KB
/
DefaultHttpMetricsSenderTest.java
File metadata and controls
156 lines (133 loc) · 7.35 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package io.getunleash.metric;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static io.getunleash.util.UnleashConfig.UNLEASH_CONNECTION_ID_HEADER;
import static io.getunleash.util.UnleashConfig.UNLEASH_INTERVAL;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import io.getunleash.engine.MetricsBucket;
import io.getunleash.impactmetrics.BucketMetricOptions;
import io.getunleash.impactmetrics.CollectedMetric;
import io.getunleash.impactmetrics.Histogram;
import io.getunleash.impactmetrics.InMemoryMetricRegistry;
import io.getunleash.util.UnleashConfig;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
public class DefaultHttpMetricsSenderTest {
@RegisterExtension
static WireMockExtension serverMock =
WireMockExtension.newInstance()
.configureStaticDsl(true)
.options(wireMockConfig().dynamicPort().dynamicHttpsPort())
.build();
@Test
public void should_send_client_registration() throws URISyntaxException {
stubFor(
post(urlEqualTo("/client/register"))
.withHeader("UNLEASH-APPNAME", matching("test-app"))
.willReturn(aResponse().withStatus(200)));
URI uri = new URI("http://localhost:" + serverMock.getPort());
UnleashConfig config = UnleashConfig.builder().appName("test-app").unleashAPI(uri).build();
DefaultHttpMetricsSender sender = new DefaultHttpMetricsSender(config);
sender.registerClient(new ClientRegistration(config, LocalDateTime.now(), new HashSet<>()));
verify(
postRequestedFor(urlMatching("/client/register"))
.withRequestBody(matching(".*appName.*"))
.withRequestBody(matching(".*strategies.*"))
.withHeader(
UNLEASH_CONNECTION_ID_HEADER, matching(config.getConnectionId()))
.withHeader("UNLEASH-APPNAME", matching("test-app")));
}
@Test
public void should_send_client_metrics() throws URISyntaxException {
stubFor(
post(urlEqualTo("/client/metrics"))
.withHeader("UNLEASH-APPNAME", matching("test-app"))
.willReturn(aResponse().withStatus(200)));
URI uri = new URI("http://localhost:" + serverMock.getPort());
UnleashConfig config = UnleashConfig.builder().appName("test-app").unleashAPI(uri).build();
DefaultHttpMetricsSender sender = new DefaultHttpMetricsSender(config);
MetricsBucket bucket = new MetricsBucket(Instant.now(), Instant.now(), null);
ClientMetrics metrics = new ClientMetrics(config, bucket);
sender.sendMetrics(metrics);
verify(
postRequestedFor(urlMatching("/client/metrics"))
.withRequestBody(matching(".*appName.*"))
.withRequestBody(matching(".*bucket.*"))
.withHeader(
UNLEASH_INTERVAL, matching(config.getSendMetricsIntervalMillis()))
.withHeader(
UNLEASH_CONNECTION_ID_HEADER, matching(config.getConnectionId()))
.withHeader("UNLEASH-APPNAME", matching("test-app")));
}
@Test
public void should_handle_service_failure_when_sending_metrics() throws URISyntaxException {
stubFor(
post(urlEqualTo("/client/metrics"))
.withHeader("UNLEASH-APPNAME", matching("test-app"))
.willReturn(aResponse().withStatus(500)));
URI uri = new URI("http://localhost:" + serverMock.getPort());
long metricsInterval = 0;
UnleashConfig config =
UnleashConfig.builder()
.appName("test-app")
.unleashAPI(uri)
.sendMetricsInterval(metricsInterval)
.build();
DefaultHttpMetricsSender sender = new DefaultHttpMetricsSender(config);
MetricsBucket bucket = new MetricsBucket(Instant.now(), Instant.now(), null);
ClientMetrics metrics = new ClientMetrics(config, bucket);
sender.sendMetrics(metrics);
verify(
postRequestedFor(urlMatching("/client/metrics"))
.withRequestBody(matching(".*appName.*"))
.withRequestBody(matching(".*bucket.*"))
.withHeader(UNLEASH_INTERVAL, matching(String.valueOf(metricsInterval)))
.withHeader("UNLEASH-APPNAME", matching("test-app")));
}
@Test
public void should_send_impact_metrics_with_histogram_and_plus_inf_bucket()
throws URISyntaxException {
stubFor(
post(urlEqualTo("/client/metrics"))
.withHeader("UNLEASH-APPNAME", matching("test-app"))
.willReturn(aResponse().withStatus(200)));
URI uri = new URI("http://localhost:" + serverMock.getPort());
UnleashConfig config = UnleashConfig.builder().appName("test-app").unleashAPI(uri).build();
InMemoryMetricRegistry registry = new InMemoryMetricRegistry();
Histogram histogram =
registry.histogram(
new BucketMetricOptions(
"test_histogram", "testing histogram", List.of(1.0, 5.0)));
histogram.observe(0.5);
histogram.observe(10.0);
List<CollectedMetric> impactMetrics = registry.collect();
DefaultHttpMetricsSender sender = new DefaultHttpMetricsSender(config);
MetricsBucket bucket = new MetricsBucket(Instant.now(), Instant.now(), null);
ClientMetrics metrics = new ClientMetrics(config, bucket, impactMetrics);
sender.sendMetrics(metrics);
verify(
postRequestedFor(urlMatching("/client/metrics"))
.withRequestBody(matching(".*\"impactMetrics\".*"))
.withRequestBody(matching(".*\"name\"\\s*:\\s*\"test_histogram\".*"))
.withRequestBody(matching(".*\"type\"\\s*:\\s*\"HISTOGRAM\".*"))
.withRequestBody(matching(".*\"le\"\\s*:\\s*\"\\+Inf\".*"))
.withHeader(
UNLEASH_INTERVAL, matching(config.getSendMetricsIntervalMillis()))
.withHeader(
UNLEASH_CONNECTION_ID_HEADER, matching(config.getConnectionId()))
.withHeader("UNLEASH-APPNAME", matching("test-app")));
}
}