Skip to content

Commit 81b4084

Browse files
author
Adrian Cole
committed
Migrates to Brave 6 and Zipkin Reporter 3
Signed-off-by: Adrian Cole <[email protected]>
1 parent 09a6ae5 commit 81b4084

File tree

13 files changed

+126
-294
lines changed

13 files changed

+126
-294
lines changed

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/BravePropagationConfigurations.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import brave.propagation.CurrentTraceContext.ScopeDecorator;
3131
import brave.propagation.Propagation;
3232
import brave.propagation.Propagation.Factory;
33-
import brave.propagation.Propagation.KeyFactory;
3433
import io.micrometer.tracing.brave.bridge.BraveBaggageManager;
3534

3635
import org.springframework.beans.factory.ObjectProvider;
@@ -99,12 +98,11 @@ BaggagePropagation.FactoryBuilder propagationFactoryBuilder(
9998
return builder;
10099
}
101100

102-
@SuppressWarnings("deprecation")
103101
private Factory createThrowAwayFactory() {
104102
return new Factory() {
105103

106104
@Override
107-
public <K> Propagation<K> create(KeyFactory<K> keyFactory) {
105+
public Propagation<String> get() {
108106
return null;
109107
}
110108

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/CompositePropagationFactory.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.function.Predicate;
2323
import java.util.stream.Stream;
2424

25-
import brave.internal.propagation.StringPropagationAdapter;
2625
import brave.propagation.B3Propagation;
2726
import brave.propagation.Propagation;
2827
import brave.propagation.Propagation.Factory;
@@ -71,9 +70,8 @@ public boolean requires128BitTraceId() {
7170
}
7271

7372
@Override
74-
@SuppressWarnings("deprecation")
75-
public <K> Propagation<K> create(Propagation.KeyFactory<K> keyFactory) {
76-
return StringPropagationAdapter.create(this.propagation, keyFactory);
73+
public Propagation<String> get() {
74+
return this.propagation;
7775
}
7876

7977
@Override

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/HttpSender.java

+40-87
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,37 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
21-
import java.io.UncheckedIOException;
22-
import java.util.Collections;
2321
import java.util.List;
2422
import java.util.zip.GZIPOutputStream;
2523

26-
import zipkin2.Call;
27-
import zipkin2.CheckResult;
28-
import zipkin2.codec.Encoding;
2924
import zipkin2.reporter.BytesMessageEncoder;
3025
import zipkin2.reporter.ClosedSenderException;
31-
import zipkin2.reporter.Sender;
26+
import zipkin2.reporter.BytesMessageSender;
27+
import zipkin2.reporter.Encoding;
3228

3329
import org.springframework.http.HttpHeaders;
3430
import org.springframework.util.unit.DataSize;
3531

3632
/**
37-
* A Zipkin {@link Sender} that uses an HTTP client to send JSON spans. Supports automatic
33+
* A Zipkin {@link BytesMessageSender} that uses an HTTP client to send JSON spans. Supports automatic
3834
* compression with gzip.
3935
*
4036
* @author Moritz Halbritter
4137
* @author Stefan Bratanov
4238
*/
43-
abstract class HttpSender extends Sender {
39+
abstract class HttpSender extends BytesMessageSender.Base {
4440

4541
private static final DataSize MESSAGE_MAX_SIZE = DataSize.ofKilobytes(512);
4642

43+
/**
44+
* Only use gzip compression on data which is bigger than this in bytes.
45+
*/
46+
private static final DataSize COMPRESSION_THRESHOLD = DataSize.ofKilobytes(1);
47+
4748
private volatile boolean closed;
4849

49-
@Override
50-
public Encoding encoding() {
51-
return Encoding.JSON;
50+
HttpSender() {
51+
super(Encoding.JSON);
5252
}
5353

5454
@Override
@@ -57,96 +57,49 @@ public int messageMaxBytes() {
5757
}
5858

5959
@Override
60-
public int messageSizeInBytes(List<byte[]> encodedSpans) {
61-
return encoding().listSizeInBytes(encodedSpans);
62-
}
63-
64-
@Override
65-
public int messageSizeInBytes(int encodedSizeInBytes) {
66-
return encoding().listSizeInBytes(encodedSizeInBytes);
67-
}
68-
69-
@Override
70-
public CheckResult check() {
71-
try {
72-
sendSpans(Collections.emptyList()).execute();
73-
return CheckResult.OK;
74-
}
75-
catch (IOException | RuntimeException ex) {
76-
return CheckResult.failed(ex);
77-
}
78-
}
79-
80-
@Override
81-
public void close() throws IOException {
60+
public void close() {
8261
this.closed = true;
8362
}
8463

8564
/**
86-
* The returned {@link HttpPostCall} will send span(s) as a POST to a zipkin endpoint
87-
* when executed.
88-
* @param batchedEncodedSpans list of encoded spans as a byte array
89-
* @return an instance of a Zipkin {@link Call} which can be executed
65+
* This will send span(s) as a POST to a zipkin endpoint. For example,
66+
* http://localhost:9411/api/v2/spans.
67+
*
68+
* @param headers headers for the POST request
69+
* @param body list of possibly gzipped, encoded spans.
9070
*/
91-
protected abstract HttpPostCall sendSpans(byte[] batchedEncodedSpans);
71+
abstract void postSpans(HttpHeaders headers, byte[] body);
9272

9373
@Override
94-
public Call<Void> sendSpans(List<byte[]> encodedSpans) {
74+
public void send(List<byte[]> encodedSpans) throws IOException {
9575
if (this.closed) {
9676
throw new ClosedSenderException();
9777
}
98-
return sendSpans(BytesMessageEncoder.JSON.encode(encodedSpans));
99-
}
100-
101-
abstract static class HttpPostCall extends Call.Base<Void> {
102-
103-
/**
104-
* Only use gzip compression on data which is bigger than this in bytes.
105-
*/
106-
private static final DataSize COMPRESSION_THRESHOLD = DataSize.ofKilobytes(1);
107-
108-
private final byte[] body;
109-
110-
HttpPostCall(byte[] body) {
111-
this.body = body;
112-
}
113-
114-
protected byte[] getBody() {
115-
if (needsCompression()) {
116-
return compress(this.body);
117-
}
118-
return this.body;
119-
}
120-
121-
protected byte[] getUncompressedBody() {
122-
return this.body;
78+
byte[] body = BytesMessageEncoder.JSON.encode(encodedSpans);
79+
HttpHeaders headers = getDefaultHeaders();
80+
if (needsCompression(body)) {
81+
body = compress(body);
82+
headers.set("Content-Encoding", "gzip");
12383
}
84+
postSpans(headers, body);
85+
}
12486

125-
protected HttpHeaders getDefaultHeaders() {
126-
HttpHeaders headers = new HttpHeaders();
127-
headers.set("b3", "0");
128-
headers.set("Content-Type", "application/json");
129-
if (needsCompression()) {
130-
headers.set("Content-Encoding", "gzip");
131-
}
132-
return headers;
133-
}
87+
HttpHeaders getDefaultHeaders() {
88+
HttpHeaders headers = new HttpHeaders();
89+
headers.set("b3", "0");
90+
headers.set("Content-Type", "application/json");
91+
return headers;
92+
}
13493

135-
private boolean needsCompression() {
136-
return this.body.length > COMPRESSION_THRESHOLD.toBytes();
137-
}
94+
private boolean needsCompression(byte[] body) {
95+
return body.length > COMPRESSION_THRESHOLD.toBytes();
96+
}
13897

139-
private byte[] compress(byte[] input) {
140-
ByteArrayOutputStream result = new ByteArrayOutputStream();
141-
try (GZIPOutputStream gzip = new GZIPOutputStream(result)) {
142-
gzip.write(input);
143-
}
144-
catch (IOException ex) {
145-
throw new UncheckedIOException(ex);
146-
}
147-
return result.toByteArray();
98+
private byte[] compress(byte[] input) throws IOException {
99+
ByteArrayOutputStream result = new ByteArrayOutputStream();
100+
try (GZIPOutputStream gzip = new GZIPOutputStream(result)) {
101+
gzip.write(input);
148102
}
149-
103+
return result.toByteArray();
150104
}
151-
152105
}

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinAutoConfiguration.java

+3-14
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
1818

19-
import zipkin2.Span;
20-
import zipkin2.codec.BytesEncoder;
21-
import zipkin2.codec.SpanBytesEncoder;
22-
import zipkin2.reporter.Sender;
19+
import zipkin2.reporter.BytesMessageSender;
2320

2421
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.BraveConfiguration;
2522
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.OpenTelemetryConfiguration;
26-
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.ReporterConfiguration;
2723
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.SenderConfiguration;
2824
import org.springframework.boot.autoconfigure.AutoConfiguration;
2925
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -44,9 +40,8 @@
4440
* @since 3.0.0
4541
*/
4642
@AutoConfiguration(after = RestTemplateAutoConfiguration.class)
47-
@ConditionalOnClass(Sender.class)
48-
@Import({ SenderConfiguration.class, ReporterConfiguration.class, BraveConfiguration.class,
49-
OpenTelemetryConfiguration.class })
43+
@ConditionalOnClass(BytesMessageSender.class)
44+
@Import({ SenderConfiguration.class, BraveConfiguration.class, OpenTelemetryConfiguration.class })
5045
@EnableConfigurationProperties(ZipkinProperties.class)
5146
public class ZipkinAutoConfiguration {
5247

@@ -56,10 +51,4 @@ PropertiesZipkinConnectionDetails zipkinConnectionDetails(ZipkinProperties prope
5651
return new PropertiesZipkinConnectionDetails(properties);
5752
}
5853

59-
@Bean
60-
@ConditionalOnMissingBean
61-
public BytesEncoder<Span> spanBytesEncoder() {
62-
return SpanBytesEncoder.JSON_V2;
63-
}
64-
6554
}

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurations.java

+21-29
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
1818

19+
import java.util.Optional;
20+
21+
import brave.handler.MutableSpan;
1922
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
23+
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporterBuilder;
2024
import zipkin2.Span;
21-
import zipkin2.codec.BytesEncoder;
22-
import zipkin2.reporter.AsyncReporter;
23-
import zipkin2.reporter.Reporter;
24-
import zipkin2.reporter.Sender;
25-
import zipkin2.reporter.brave.ZipkinSpanHandler;
25+
import zipkin2.reporter.BytesEncoder;
26+
import zipkin2.reporter.BytesMessageSender;
27+
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
28+
import zipkin2.reporter.brave.AsyncZipkinSpanHandler.Builder;
2629
import zipkin2.reporter.urlconnection.URLConnectionSender;
2730

2831
import org.springframework.beans.factory.ObjectProvider;
@@ -59,7 +62,7 @@ static class SenderConfiguration {
5962
static class UrlConnectionSenderConfiguration {
6063

6164
@Bean
62-
@ConditionalOnMissingBean(Sender.class)
65+
@ConditionalOnMissingBean(BytesMessageSender.class)
6366
URLConnectionSender urlConnectionSender(ZipkinProperties properties,
6467
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
6568
ZipkinConnectionDetails connectionDetails = connectionDetailsProvider
@@ -79,7 +82,7 @@ URLConnectionSender urlConnectionSender(ZipkinProperties properties,
7982
static class RestTemplateSenderConfiguration {
8083

8184
@Bean
82-
@ConditionalOnMissingBean(Sender.class)
85+
@ConditionalOnMissingBean(BytesMessageSender.class)
8386
ZipkinRestTemplateSender restTemplateSender(ZipkinProperties properties,
8487
ObjectProvider<ZipkinRestTemplateBuilderCustomizer> customizers,
8588
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
@@ -111,7 +114,7 @@ private RestTemplateBuilder applyCustomizers(RestTemplateBuilder restTemplateBui
111114
static class WebClientSenderConfiguration {
112115

113116
@Bean
114-
@ConditionalOnMissingBean(Sender.class)
117+
@ConditionalOnMissingBean(BytesMessageSender.class)
115118
ZipkinWebClientSender webClientSender(ZipkinProperties properties,
116119
ObjectProvider<ZipkinWebClientBuilderCustomizer> customizers,
117120
ObjectProvider<ZipkinConnectionDetails> connectionDetailsProvider) {
@@ -126,29 +129,16 @@ ZipkinWebClientSender webClientSender(ZipkinProperties properties,
126129
}
127130

128131
@Configuration(proxyBeanMethods = false)
129-
static class ReporterConfiguration {
130-
131-
@Bean
132-
@ConditionalOnMissingBean(Reporter.class)
133-
@ConditionalOnBean(Sender.class)
134-
AsyncReporter<Span> spanReporter(Sender sender, BytesEncoder<Span> encoder) {
135-
return AsyncReporter.builder(sender).build(encoder);
136-
}
137-
138-
}
139-
140-
@Configuration(proxyBeanMethods = false)
141-
@ConditionalOnClass(ZipkinSpanHandler.class)
132+
@ConditionalOnClass(AsyncZipkinSpanHandler.class)
142133
static class BraveConfiguration {
143134

144135
@Bean
145136
@ConditionalOnMissingBean
146-
@ConditionalOnBean(Reporter.class)
147-
@ConditionalOnEnabledTracing
148-
ZipkinSpanHandler zipkinSpanHandler(Reporter<Span> spanReporter) {
149-
return (ZipkinSpanHandler) ZipkinSpanHandler.newBuilder(spanReporter).build();
137+
@ConditionalOnBean(BytesMessageSender.class)
138+
AsyncZipkinSpanHandler spanReporter(BytesMessageSender sender, Optional<BytesEncoder<MutableSpan>> encoder) {
139+
Builder builder = AsyncZipkinSpanHandler.newBuilder(sender);
140+
return encoder.map(builder::build).orElseGet(builder::build);
150141
}
151-
152142
}
153143

154144
@Configuration(proxyBeanMethods = false)
@@ -157,10 +147,12 @@ static class OpenTelemetryConfiguration {
157147

158148
@Bean
159149
@ConditionalOnMissingBean
160-
@ConditionalOnBean(Sender.class)
150+
@ConditionalOnBean(BytesMessageSender.class)
161151
@ConditionalOnEnabledTracing
162-
ZipkinSpanExporter zipkinSpanExporter(BytesEncoder<Span> encoder, Sender sender) {
163-
return ZipkinSpanExporter.builder().setEncoder(encoder).setSender(sender).build();
152+
ZipkinSpanExporter zipkinSpanExporter(BytesMessageSender sender, Optional<BytesEncoder<Span>> encoder) {
153+
ZipkinSpanExporterBuilder builder = ZipkinSpanExporter.builder().setSender(sender);
154+
encoder.ifPresent(builder::setEncoder);
155+
return builder.build();
164156
}
165157

166158
}

0 commit comments

Comments
 (0)