-
Notifications
You must be signed in to change notification settings - Fork 774
/
ZipkinSpanExporterBuilder.java
171 lines (158 loc) · 5.7 KB
/
ZipkinSpanExporterBuilder.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.zipkin;
import static io.opentelemetry.api.internal.Utils.checkArgument;
import static java.util.Objects.requireNonNull;
import io.opentelemetry.api.metrics.MeterProvider;
import java.net.InetAddress;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import zipkin2.Span;
import zipkin2.codec.BytesEncoder;
import zipkin2.codec.SpanBytesEncoder;
import zipkin2.reporter.Sender;
import zipkin2.reporter.okhttp3.OkHttpSender;
/** Builder class for {@link ZipkinSpanExporter}. */
public final class ZipkinSpanExporterBuilder {
private BytesEncoder<Span> encoder = SpanBytesEncoder.JSON_V2;
private Supplier<InetAddress> localIpAddressSupplier = LocalInetAddressSupplier.getInstance();
@Nullable private Sender sender;
private String endpoint = ZipkinSpanExporter.DEFAULT_ENDPOINT;
// compression is enabled by default, because this is the default of OkHttpSender,
// which is created when no custom sender is set (see OkHttpSender.Builder)
private boolean compressionEnabled = true;
private long readTimeoutMillis = TimeUnit.SECONDS.toMillis(10);
private MeterProvider meterProvider = MeterProvider.noop();
/**
* Sets the Zipkin sender. Implements the client side of the span transport. An {@link
* OkHttpSender} is a good default.
*
* <p>The {@link Sender#close()} method will be called when the exporter is shut down.
*
* @param sender the Zipkin sender implementation.
* @return this.
*/
public ZipkinSpanExporterBuilder setSender(Sender sender) {
requireNonNull(sender, "sender");
this.sender = sender;
return this;
}
/**
* Sets the {@link BytesEncoder}, which controls the format used by the {@link Sender}. Defaults
* to the {@link SpanBytesEncoder#JSON_V2}.
*
* @param encoder the {@code BytesEncoder} to use.
* @return this.
* @see SpanBytesEncoder
*/
public ZipkinSpanExporterBuilder setEncoder(BytesEncoder<Span> encoder) {
requireNonNull(encoder, "encoder");
this.encoder = encoder;
return this;
}
/**
* Sets the Supplier of InetAddress. This Supplier will be used by the {@link
* OtelToZipkinSpanTransformer} when creating the Zipkin local endpoint. The default
* implementation uses a Supplier that returns a single unchanging IP address that is captured at
* creation time.
*
* @param supplier - A supplier that returns an InetAddress that may be null.
* @return this
* @since 1.18.0
*/
public ZipkinSpanExporterBuilder setLocalIpAddressSupplier(Supplier<InetAddress> supplier) {
requireNonNull(supplier, "encoder");
this.localIpAddressSupplier = supplier;
return this;
}
/**
* Sets the zipkin endpoint. This will use the endpoint to assign an {@link OkHttpSender} instance
* to this builder.
*
* @param endpoint The Zipkin endpoint URL, ex. "http://zipkinhost:9411/api/v2/spans".
* @return this.
* @see OkHttpSender
*/
public ZipkinSpanExporterBuilder setEndpoint(String endpoint) {
requireNonNull(endpoint, "endpoint");
this.endpoint = endpoint;
return this;
}
/**
* Sets the method used to compress payloads. If unset, compression is enabled. Currently
* supported compression methods include "gzip" and "none".
*
* <p>The compression method is ignored when a custom Zipkin sender is set via {@link
* #setSender(Sender)}.
*
* @param compressionMethod The compression method, ex. "gzip".
* @return this.
* @see OkHttpSender
*/
public ZipkinSpanExporterBuilder setCompression(String compressionMethod) {
requireNonNull(compressionMethod, "compressionMethod");
checkArgument(
compressionMethod.equals("gzip") || compressionMethod.equals("none"),
"Unsupported compression method. Supported compression methods include: gzip, none.");
this.compressionEnabled = compressionMethod.equals("gzip");
return this;
}
/**
* Sets the maximum time to wait for the export of a batch of spans. If unset, defaults to 10s.
*
* @return this.
* @since 1.2.0
*/
public ZipkinSpanExporterBuilder setReadTimeout(long timeout, TimeUnit unit) {
requireNonNull(unit, "unit");
checkArgument(timeout >= 0, "timeout must be non-negative");
this.readTimeoutMillis = unit.toMillis(timeout);
return this;
}
/**
* Sets the maximum time to wait for the export of a batch of spans. If unset, defaults to 10s.
*
* @return this.
* @since 1.2.0
*/
public ZipkinSpanExporterBuilder setReadTimeout(Duration timeout) {
requireNonNull(timeout, "timeout");
setReadTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS);
return this;
}
/**
* Sets the {@link MeterProvider} to use to collect metrics related to export. If not set, metrics
* will not be collected.
*
* @return this.
* @since 1.17.0
*/
public ZipkinSpanExporterBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
this.meterProvider = meterProvider;
return this;
}
/**
* Builds a {@link ZipkinSpanExporter}.
*
* @return a {@code ZipkinSpanExporter}.
*/
public ZipkinSpanExporter build() {
Sender sender = this.sender;
if (sender == null) {
sender =
OkHttpSender.newBuilder()
.endpoint(endpoint)
.compressionEnabled(compressionEnabled)
.readTimeout((int) readTimeoutMillis)
.build();
}
OtelToZipkinSpanTransformer transformer =
OtelToZipkinSpanTransformer.create(localIpAddressSupplier);
return new ZipkinSpanExporter(encoder, sender, meterProvider, transformer);
}
}