|
15 | 15 | */
|
16 | 16 | package io.micrometer.registry.otlp;
|
17 | 17 |
|
| 18 | +import io.micrometer.common.lang.Nullable; |
| 19 | + |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.Collections; |
18 | 22 | import java.util.Map;
|
| 23 | +import java.util.Objects; |
19 | 24 |
|
20 | 25 | /**
|
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. |
23 | 29 | *
|
24 | 30 | * @since 1.15.0
|
25 | 31 | */
|
26 |
| -public interface OtlpMetricsSender extends MetricsSender { |
| 32 | +public interface OtlpMetricsSender { |
27 | 33 |
|
28 | 34 | /**
|
29 | 35 | * 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 |
34 | 38 | * should handle this in some way such as logging the exception
|
35 | 39 | */
|
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 | + } |
38 | 127 |
|
39 | 128 | }
|
0 commit comments