-
Notifications
You must be signed in to change notification settings - Fork 775
/
OtlpGrpcMetricExporterBuilder.java
198 lines (177 loc) · 7.29 KB
/
OtlpGrpcMetricExporterBuilder.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.otlp.metrics;
import static io.opentelemetry.api.internal.Utils.checkArgument;
import static java.util.Objects.requireNonNull;
import io.grpc.ManagedChannel;
import io.opentelemetry.exporter.internal.grpc.GrpcExporter;
import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder;
import io.opentelemetry.exporter.internal.otlp.OtlpUserAgent;
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* Builder utility for this exporter.
*
* @since 1.14.0
*/
public final class OtlpGrpcMetricExporterBuilder {
private static final String GRPC_SERVICE_NAME =
"opentelemetry.proto.collector.metrics.v1.MetricsService";
// Visible for testing
static final String GRPC_ENDPOINT_PATH = "/" + GRPC_SERVICE_NAME + "/Export";
private static final String DEFAULT_ENDPOINT_URL = "http://localhost:4317";
private static final URI DEFAULT_ENDPOINT = URI.create(DEFAULT_ENDPOINT_URL);
private static final long DEFAULT_TIMEOUT_SECS = 10;
private static final AggregationTemporalitySelector DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR =
AggregationTemporalitySelector.alwaysCumulative();
// Visible for testing
final GrpcExporterBuilder<MetricsRequestMarshaler> delegate;
private AggregationTemporalitySelector aggregationTemporalitySelector =
DEFAULT_AGGREGATION_TEMPORALITY_SELECTOR;
private DefaultAggregationSelector defaultAggregationSelector =
DefaultAggregationSelector.getDefault();
OtlpGrpcMetricExporterBuilder() {
delegate =
GrpcExporter.builder(
"otlp",
"metric",
DEFAULT_TIMEOUT_SECS,
DEFAULT_ENDPOINT,
() -> MarshalerMetricsServiceGrpc::newFutureStub,
GRPC_ENDPOINT_PATH);
OtlpUserAgent.addUserAgentHeader(delegate::addHeader);
}
/**
* Sets the managed chanel to use when communicating with the backend. Takes precedence over
* {@link #setEndpoint(String)} if both are called.
*
* <p>Note: calling this overrides the spec compliant {@code User-Agent} header. To ensure spec
* compliance, set {@link io.grpc.ManagedChannelBuilder#userAgent(String)} to {@link
* OtlpUserAgent#getUserAgent()} when building the channel.
*
* @param channel the channel to use
* @return this builder's instance
* @deprecated Use {@link #setEndpoint(String)}. If you have a use case not satisfied by the
* methods on this builder, please file an issue to let us know what it is.
*/
@Deprecated
public OtlpGrpcMetricExporterBuilder setChannel(ManagedChannel channel) {
delegate.setChannel(channel);
return this;
}
/**
* Sets the maximum time to wait for the collector to process an exported batch of metrics. If
* unset, defaults to {@value DEFAULT_TIMEOUT_SECS}s.
*/
public OtlpGrpcMetricExporterBuilder setTimeout(long timeout, TimeUnit unit) {
requireNonNull(unit, "unit");
checkArgument(timeout >= 0, "timeout must be non-negative");
delegate.setTimeout(timeout, unit);
return this;
}
/**
* Sets the maximum time to wait for the collector to process an exported batch of metrics. If
* unset, defaults to {@value DEFAULT_TIMEOUT_SECS}s.
*/
public OtlpGrpcMetricExporterBuilder setTimeout(Duration timeout) {
requireNonNull(timeout, "timeout");
delegate.setTimeout(timeout);
return this;
}
/**
* Sets the OTLP endpoint to connect to. If unset, defaults to {@value DEFAULT_ENDPOINT_URL}. The
* endpoint must start with either http:// or https://.
*/
public OtlpGrpcMetricExporterBuilder setEndpoint(String endpoint) {
requireNonNull(endpoint, "endpoint");
delegate.setEndpoint(endpoint);
return this;
}
/**
* Sets the method used to compress payloads. If unset, compression is disabled. Currently
* supported compression methods include "gzip" and "none".
*/
public OtlpGrpcMetricExporterBuilder setCompression(String compressionMethod) {
requireNonNull(compressionMethod, "compressionMethod");
checkArgument(
compressionMethod.equals("gzip") || compressionMethod.equals("none"),
"Unsupported compression method. Supported compression methods include: gzip, none.");
delegate.setCompression(compressionMethod);
return this;
}
/**
* Sets the certificate chain to use for verifying servers when TLS is enabled. The {@code byte[]}
* should contain an X.509 certificate collection in PEM format. If not set, TLS connections will
* use the system default trusted certificates.
*/
public OtlpGrpcMetricExporterBuilder setTrustedCertificates(byte[] trustedCertificatesPem) {
delegate.setTrustedCertificates(trustedCertificatesPem);
return this;
}
/**
* Sets ths client key and the certificate chain to use for verifying client when TLS is enabled.
* The key must be PKCS8, and both must be in PEM format.
*/
public OtlpGrpcMetricExporterBuilder setClientTls(byte[] privateKeyPem, byte[] certificatePem) {
delegate.setClientTls(privateKeyPem, certificatePem);
return this;
}
/**
* Add header to request. Optional. Applicable only if {@link
* OtlpGrpcMetricExporterBuilder#setChannel(ManagedChannel)} is not used to set channel.
*
* @param key header key
* @param value header value
* @return this builder's instance
*/
public OtlpGrpcMetricExporterBuilder addHeader(String key, String value) {
delegate.addHeader(key, value);
return this;
}
/**
* Set the {@link AggregationTemporalitySelector} used for {@link
* MetricExporter#getAggregationTemporality(InstrumentType)}.
*
* <p>If unset, defaults to {@link AggregationTemporalitySelector#alwaysCumulative()}.
*
* <p>{@link AggregationTemporalitySelector#deltaPreferred()} is a common configuration for delta
* backends.
*/
public OtlpGrpcMetricExporterBuilder setAggregationTemporalitySelector(
AggregationTemporalitySelector aggregationTemporalitySelector) {
requireNonNull(aggregationTemporalitySelector, "aggregationTemporalitySelector");
this.aggregationTemporalitySelector = aggregationTemporalitySelector;
return this;
}
/**
* Set the {@link DefaultAggregationSelector} used for {@link
* MetricExporter#getDefaultAggregation(InstrumentType)}.
*
* <p>If unset, defaults to {@link DefaultAggregationSelector#getDefault()}.
*
* @since 1.16.0
*/
public OtlpGrpcMetricExporterBuilder setDefaultAggregationSelector(
DefaultAggregationSelector defaultAggregationSelector) {
requireNonNull(defaultAggregationSelector, "defaultAggregationSelector");
this.defaultAggregationSelector = defaultAggregationSelector;
return this;
}
/**
* Constructs a new instance of the exporter based on the builder's values.
*
* @return a new exporter's instance
*/
public OtlpGrpcMetricExporter build() {
return new OtlpGrpcMetricExporter(
delegate.build(), aggregationTemporalitySelector, defaultAggregationSelector);
}
}