Skip to content

Commit 4394738

Browse files
committed
Further enhancement to OtlpMetricsSender
Introduces an immutable Request object and builder.
1 parent 8db4d6e commit 4394738

File tree

5 files changed

+118
-65
lines changed

5 files changed

+118
-65
lines changed

docs/src/test/java/io/micrometer/docs/metrics/OtlpMeterRegistryCustomizationTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import io.micrometer.registry.otlp.OtlpMetricsSender;
2424
import org.junit.jupiter.api.Test;
2525

26-
import java.util.Map;
27-
2826
class OtlpMeterRegistryCustomizationTest {
2927

3028
@Test
@@ -49,7 +47,7 @@ void customizeOtlpSender() {
4947
private static class OtlpGrpcMetricsSender implements OtlpMetricsSender {
5048

5149
@Override
52-
public void send(String address, byte[] metricsData, Map<String, String> headers) {
50+
public void send(Request request) {
5351
}
5452

5553
}

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/MetricsSender.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpHttpMetricsSender.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,15 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18-
import io.micrometer.common.util.internal.logging.InternalLogger;
19-
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
2018
import io.micrometer.core.ipc.http.HttpSender;
2119

22-
import java.util.Map;
23-
2420
/**
2521
* An implementation of {@link OtlpMetricsSender} that uses an {@link HttpSender}.
2622
*
2723
* @since 1.15.0
2824
*/
2925
public class OtlpHttpMetricsSender implements OtlpMetricsSender {
3026

31-
private static final InternalLogger logger = InternalLoggerFactory.getInstance(OtlpHttpMetricsSender.class);
32-
3327
private final HttpSender httpSender;
3428

3529
private final String userAgentHeader;
@@ -41,19 +35,23 @@ public OtlpHttpMetricsSender(HttpSender httpSender) {
4135

4236
/**
4337
* Send a batch of OTLP Protobuf format metrics to an OTLP HTTP receiver.
44-
* @param address address of the OTLP HTTP receiver to which metrics will be sent
45-
* @param metricsData OTLP protobuf encoded batch of metrics
46-
* @param headers metadata to send as headers with the metrics data
47-
* @throws Throwable when there is an exception in sending the metrics; the caller
38+
* @param request metrics request to publish
39+
* @throws Exception when there is an exception in sending the metrics; the caller
4840
* should handle this in some way such as logging the exception
4941
*/
5042
@Override
51-
public void send(String address, byte[] metricsData, Map<String, String> headers) throws Throwable {
52-
HttpSender.Request.Builder httpRequest = this.httpSender.post(address)
43+
public void send(Request request) throws Exception {
44+
HttpSender.Request.Builder httpRequest = this.httpSender.post(request.getAddress())
5345
.withHeader("User-Agent", userAgentHeader)
54-
.withContent("application/x-protobuf", metricsData);
55-
headers.forEach(httpRequest::withHeader);
56-
HttpSender.Response response = httpRequest.send();
46+
.withContent("application/x-protobuf", request.getMetricsData());
47+
request.getHeaders().forEach(httpRequest::withHeader);
48+
HttpSender.Response response;
49+
try {
50+
response = httpRequest.send();
51+
}
52+
catch (Throwable e) {
53+
throw new Exception(e);
54+
}
5755
if (!response.isSuccessful()) {
5856
throw new OtlpHttpMetricsSendUnsuccessfulException(String
5957
.format("Server responded with HTTP status code %d and body %s", response.code(), response.body()));
@@ -71,7 +69,7 @@ private String getUserAgentHeader() {
7169

7270
private static class OtlpHttpMetricsSendUnsuccessfulException extends RuntimeException {
7371

74-
public OtlpHttpMetricsSendUnsuccessfulException(String message) {
72+
private OtlpHttpMetricsSendUnsuccessfulException(String message) {
7573
super(message);
7674
}
7775

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpMeterRegistry.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,12 @@ protected void publish() {
181181
.build())
182182
.build();
183183

184-
metricsSender.send(config.url(), request.toByteArray(), config.headers());
184+
metricsSender.send(OtlpMetricsSender.Request.builder(request.toByteArray())
185+
.address(config.url())
186+
.headers(config.headers())
187+
.build());
185188
}
186-
catch (Throwable e) {
189+
catch (Exception e) {
187190
logger.warn(String.format("Failed to publish metrics to OTLP receiver (context: %s)",
188191
getConfigurationContext()), e);
189192
}

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpMetricsSender.java

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,114 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18+
import io.micrometer.common.lang.Nullable;
19+
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.Collections;
1822
import java.util.Map;
23+
import java.util.Objects;
1924

2025
/**
21-
* This is responsible for sending OTLP format metrics to a compatible location. Specific
22-
* implementations can use different transports or clients for sending the metrics.
26+
* This is responsible for sending OTLP protobuf format metrics to a compatible location.
27+
* Specific implementations can use different transports or clients for sending the
28+
* metrics.
2329
*
2430
* @since 1.15.0
2531
*/
26-
public interface OtlpMetricsSender extends MetricsSender {
32+
public interface OtlpMetricsSender {
2733

2834
/**
2935
* Send a batch of OTLP Protobuf format metrics to an OTLP receiver.
30-
* @param address address of the OTLP receiver to which metrics will be sent
31-
* @param metricsData OTLP protobuf encoded batch of metrics
32-
* @param headers metadata to send as headers with the metrics data
33-
* @throws Throwable when there is an exception in sending the metrics; the caller
36+
* @param request metrics request to publish
37+
* @throws Exception when there is an exception in sending the metrics; the caller
3438
* should handle this in some way such as logging the exception
3539
*/
36-
@Override
37-
void send(String address, byte[] metricsData, Map<String, String> headers) throws Throwable;
40+
void send(Request request) throws Exception;
41+
42+
/**
43+
* Immutable representation of a payload of metrics to use with an
44+
* {@link OtlpMetricsSender}.
45+
*
46+
* @since 1.15.0
47+
*/
48+
class Request {
49+
50+
@Nullable
51+
private final String address;
52+
53+
private final Map<String, String> headers;
54+
55+
private final byte[] metricsData;
56+
57+
/**
58+
* Represents a payload of metrics to be sent.
59+
* @param address where to send the metrics
60+
* @param headers metadata to send as headers with the metrics data
61+
* @param metricsData OTLP protobuf encoded batch of metrics
62+
*/
63+
private Request(@Nullable String address, Map<String, String> headers, byte[] metricsData) {
64+
this.address = address;
65+
this.headers = headers;
66+
this.metricsData = metricsData;
67+
}
68+
69+
@Nullable
70+
public String getAddress() {
71+
return address;
72+
}
73+
74+
public byte[] getMetricsData() {
75+
return metricsData;
76+
}
77+
78+
public Map<String, String> getHeaders() {
79+
return headers;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "OtlpMetricsSender.Request for address: " + address + ", headers: " + headers + ", metricsData: "
85+
+ new String(metricsData, StandardCharsets.UTF_8);
86+
}
87+
88+
/**
89+
* Get a builder for a request.
90+
* @param metricsData OTLP protobuf encoded batch of metrics
91+
* @return builder
92+
*/
93+
public static Builder builder(byte[] metricsData) {
94+
return new Builder(metricsData);
95+
}
96+
97+
public static class Builder {
98+
99+
private final byte[] metricsData;
100+
101+
@Nullable
102+
private String address;
103+
104+
private Map<String, String> headers = Collections.emptyMap();
105+
106+
private Builder(byte[] metricsData) {
107+
this.metricsData = Objects.requireNonNull(metricsData);
108+
}
109+
110+
public Builder address(String address) {
111+
this.address = address;
112+
return this;
113+
}
114+
115+
public Builder headers(Map<String, String> headers) {
116+
this.headers = headers;
117+
return this;
118+
}
119+
120+
public Request build() {
121+
return new Request(address, headers, metricsData);
122+
}
123+
124+
}
125+
126+
}
38127

39128
}

0 commit comments

Comments
 (0)