8000 fix: different interval headers are now sent for metrics/features (#289) · Unleash/unleash-client-java@ffa1820 · GitHub
[go: up one dir, main page]

Skip to content

Commit ffa1820

Browse files
authored
fix: different interval headers are now sent for metrics/features (#289)
* fix: different interval headers are now sent for metrics/features
1 parent 27f6735 commit ffa1820

File tree

8 files changed

+24
-10
lines changed

8 files changed

+24
-10
lines changed

src/main/java/io/getunleash/metric/DefaultHttpMetricsSender.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.time.Instant;
1616
import java.time.LocalDateTime;
1717
import java.util.concurrent.atomic.AtomicLong;
18+
import static io.getunleash.util.UnleashConfig.UNLEASH_INTERVAL;
1819

1920
public class DefaultHttpMetricsSender implements MetricSender {
2021

@@ -82,6 +83,7 @@ private int post(URL url, Object o) throws UnleashException {
8283
connection.setRequestMethod("POST");
8384
connection.setRequestProperty("Accept", "application/json");
8485
connection.setRequestProperty("Content-Type", "application/json");
86+
connection.setRequestProperty(UNLEASH_INTERVAL, this.unleashConfig.getSendMetricsIntervalMillis());
8587
UnleashConfig.setRequestProperties(connection, this.unleashConfig);
8688
connection.setUseCaches(false);
8789
connection.setDoInput(true);

src/main/java/io/getunleash/metric/OkHttpMetricsSender.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import okhttp3.RequestBody;
2020
import okhttp3.Response;
2121

22+
import static io.getunleash.util.UnleashConfig.UNLEASH_INTERVAL;
23+
2224
public class OkHttpMetricsSender implements MetricSender {
2325
private final UnleashConfig config;
2426
private final MediaType JSON =
@@ -88,7 +90,9 @@ public int sendMetrics(ClientMetrics metrics) {
8890

8991
private int post(HttpUrl url, Object o) {
9092
RequestBody body = RequestBody.create(gson.toJson(o), JSON);
91-
Request request = new Request.Builder().url(url).post(body).build();
93+
Request request = new Request.Builder().url(url).post(body)
94+
.addHeader(UNLEASH_INTERVAL, config.getSendMetricsIntervalMillis())
95+
.build();
9296
try (Response response = this.client.newCall(request).execute()) {
9397
return response.code();
9498
} catch (IOException ioEx) {

src/main/java/io/getunleash/repository/HttpFeatureFetcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18+
import static io.getunleash.util.UnleashConfig.UNLEASH_INTERVAL;
19+
1820
public class HttpFeatureFetcher implements FeatureFetcher {
1921
private static final Logger LOG = LoggerFactory.getLogger(HttpFeatureFetcher.class);
2022
private Optional<String> etag = Optional.empty();
@@ -35,6 +37,7 @@ public ClientFeaturesResponse fetchFeatures() throws UnleashException {
3537
HttpURLConnection connection = null;
3638
try {
3739
connection = openConnection(this.toggleUrl);
40+
connection.setRequestProperty(UNLEASH_INTERVAL, this.config.getFetchTogglesIntervalMillis());
3841
connection.connect();
3942

4043
return getFeatureResponse(connection, true);

src/main/java/io/getunleash/repository/OkHttpFeatureFetcher.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import okhttp3.Request;
1717
import okhttp3.Response;
1818

19+
import static io.getunleash.util.UnleashConfig.UNLEASH_INTERVAL;
20+
1921
public class OkHttpFeatureFetcher implements FeatureFetcher {
2022
private final HttpUrl toggleUrl;
2123
private final OkHttpClient client;
24+
private final String interval;
2225

2326
public OkHttpFeatureFetcher(UnleashConfig unleashConfig) {
27+
this.interval = unleashConfig.getFetchTogglesIntervalMillis();
2428
File tempDir = null;
2529
try {
2630
tempDir = Files.createTempDirectory("http_cache").toFile();
@@ -50,6 +54,7 @@ public OkHttpFeatureFetcher(UnleashConfig unleashConfig) {
5054
}
5155

5256
public OkHttpFeatureFetcher(UnleashConfig unleashConfig, OkHttpClient client) {
57+
this.interval = unleashConfig.getFetchTogglesIntervalMillis();
5358
this.client = OkHttpClientConfigurer.configureInterceptor(unleashConfig, client);
5459
this.toggleUrl =
5560
Objects.requireNonNull(
@@ -63,7 +68,9 @@ public OkHttpFeatureFetcher(UnleashConfig unleashConfig, OkHttpClient client) {
6368

6469
@Override
6570
public ClientFeaturesResponse fetchFeatures() throws UnleashException {
66-
Request request = new Request.Builder().url(toggleUrl).get().build();
71+
Request request = new Request.Builder().url(toggleUrl).get()
72+
.addHeader(UNLEASH_INTERVAL, interval)
73+
.build();
6774
int code = 200;
6875
try (Response response = client.newCall(request).execute()) {
6976
if (response.isSuccessful()) {

src/main/java/io/getunleash/util/OkHttpClientConfigurer.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public static OkHttpClient configureInterceptor(UnleashConfig config, OkHttpClie
2020
.addHeader(
2121
UNLEASH_INSTANCE_ID_HEADER,
2222
config.getInstanceId())
23-
.addHeader(
24-
UNLEASH_INTERVAL,
25-
config.getFetchTogglesIntervalMillis())
2623
.addHeader(
2724
UNLEASH_CONNECTION_ID_HEADER,
2825
config.getConnectionId())

src/main/java/io/getunleash/util/UnleashConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ public static void setRequestProperties(HttpURLConnection connection, UnleashCon
185185
config.customHttpHeadersProvider.getCustomHeaders().forEach(connection::setRequestProperty);
186186
// prevent overwrite
187187
connection.setRequestProperty(UNLEASH_CONNECTION_ID_HEADER, config.getConnectionId());
188-
connection.setRequestProperty(UNLEASH_INTERVAL, config.getFetchTogglesIntervalMillis());
189188
}
190189

191190
private void enableProxyAuthentication() {
@@ -241,6 +240,10 @@ public long getFetchTogglesInterval() {
241240
public String getFetchTogglesIntervalMillis() {
242241
return String.valueOf(fetchTogglesInterval * 1000);
243242
}
243+
public String getSendMetricsIntervalMillis() {
244+
return String.valueOf(sendMetricsInterval * 1000);
245+
}
246+
244247

245248
public Duration getFetchTogglesConnectTimeout() {
246249
return fetchTogglesConnectTimeout;

src/test/java/io/getunleash/metric/DefaultHttpMetricsSenderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void should_send_client_metrics() throws URISyntaxException {
7474
.withRequestBody(matching(".*appName.*"))
7575
.withRequestBody(matching(".*bucket.*"))
7676
.withHeader(
77-
UNLEASH_INTERVAL, matching(config.getFetchTogglesIntervalMillis()))
77+
UNLEASH_INTERVAL, matching(config.getSendMetricsIntervalMillis()))
7878
.withHeader(
7979
UNLEASH_CONNECTION_ID_HEADER, matching(config.getConnectionId()))
8080
.withHeader("UNLEASH-APPNAME", matching("test-app")));
@@ -93,7 +93,7 @@ public void should_handle_service_failure_when_sending_metrics() throws URISynta
9393
UnleashConfig.builder()
9494
.appName("test-app")
9595
.unleashAPI(uri)
96-
.fetchTogglesInterval(metricsInterval)
96+
.sendMetricsInterval(metricsInterval)
9797
.build();
9898

9999
DefaultHttpMetricsSender sender = new DefaultHttpMetricsSender(config);

src/test/java/io/getunleash/util/UnleashConfigTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ public void should_add_client_identification_headers_to_connection() throws IOEx
156156
UnleashConfig.setRequestProperties(connection, unleashConfig);
157157
assertThat(connection.getRequestProperty(UNLEASH_APP_NAME_HEADER)).isEqualTo(appName);
158158
assertThat(connection.getRequestProperty(UNLEASH_INSTANCE_ID_HEADER)).isEqualTo(instanceId);
159-
assertThat(connection.getRequestProperty(UNLEASH_INTERVAL))
160-
.isEqualTo(unleashConfig.getFetchTogglesIntervalMillis());
161159
assertThat(connection.getRequestProperty(UNLEASH_CONNECTION_ID_HEADER))
162160
.isEqualTo(unleashConfig.getConnectionId());
163161
assertThat(connection.getRequestProperty(UNLEASH_SDK_HEADER))

0 commit comments

Comments
 (0)
0