Skip to content

Commit 7fe776c

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

File tree

5 files changed

+122
-65
lines changed

5 files changed

+122
-65
lines changed

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

+1-3
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

-35
This file was deleted.

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

+18-16
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,47 @@
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;
3630

31+
/**
32+
* Metrics sender using the given {@link HttpSender}.
33+
* @param httpSender client to use to send metrics
34+
*/
3735
public OtlpHttpMetricsSender(HttpSender httpSender) {
3836
this.httpSender = httpSender;
3937
this.userAgentHeader = getUserAgentHeader();
4038
}
4139

4240
/**
4341
* 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
42+
* @param request metrics request to publish
43+
* @throws Exception when there is an exception in sending the metrics; the caller
4844
* should handle this in some way such as logging the exception
4945
*/
5046
@Override
51-
public void send(String address, byte[] metricsData, Map<String, String> headers) throws Throwable {
52-
HttpSender.Request.Builder httpRequest = this.httpSender.post(address)
47+
public void send(Request request) throws Exception {
48+
HttpSender.Request.Builder httpRequest = this.httpSender.post(request.getAddress())
5349
.withHeader("User-Agent", userAgentHeader)
54-
.withContent("application/x-protobuf", metricsData);
55-
headers.forEach(httpRequest::withHeader);
56-
HttpSender.Response response = httpRequest.send();
50+
.withContent("application/x-protobuf", request.getMetricsData());
51+
request.getHeaders().forEach(httpRequest::withHeader);
52+
HttpSender.Response response;
53+
try {
54+
response = httpRequest.send();
55+
}
56+
catch (Throwable e) {
57+
throw new Exception(e);
58+
}
5759
if (!response.isSuccessful()) {
5860
throw new OtlpHttpMetricsSendUnsuccessfulException(String
5961
.format("Server responded with HTTP status code %d and body %s", response.code(), response.body()));
@@ -71,7 +73,7 @@ private String getUserAgentHeader() {
7173

7274
private static class OtlpHttpMetricsSendUnsuccessfulException extends RuntimeException {
7375

74-
public OtlpHttpMetricsSendUnsuccessfulException(String message) {
76+
private OtlpHttpMetricsSendUnsuccessfulException(String message) {
7577
super(message);
7678
}
7779

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

+5-2
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

+98-9
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)