Skip to content

Commit 4b0bed2

Browse files
Adrian Colemhalbritter
Adrian Cole
authored andcommitted
Migrate to Brave 6 and Zipkin Reporter 3
Signed-off-by: Adrian Cole <[email protected]> See gh-39049
1 parent 3a565e4 commit 4b0bed2

23 files changed

+635
-593
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

+42-96
Original file line numberDiff line numberDiff line change
@@ -18,135 +18,81 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
21-
import java.io.UncheckedIOException;
22-
import java.util.Collections;
21+
import java.net.URI;
2322
import java.util.List;
2423
import java.util.zip.GZIPOutputStream;
2524

26-
import zipkin2.Call;
27-
import zipkin2.CheckResult;
28-
import zipkin2.codec.Encoding;
29-
import zipkin2.reporter.BytesMessageEncoder;
30-
import zipkin2.reporter.ClosedSenderException;
31-
import zipkin2.reporter.Sender;
25+
import zipkin2.reporter.BaseHttpSender;
26+
import zipkin2.reporter.BytesMessageSender;
27+
import zipkin2.reporter.Encoding;
28+
import zipkin2.reporter.HttpEndpointSupplier.Factory;
3229

3330
import org.springframework.http.HttpHeaders;
3431
import org.springframework.util.unit.DataSize;
3532

3633
/**
37-
* A Zipkin {@link Sender} that uses an HTTP client to send JSON spans. Supports automatic
38-
* compression with gzip.
34+
* A Zipkin {@link BytesMessageSender} that uses an HTTP client to send JSON spans.
35+
* Supports automatic compression with gzip.
3936
*
4037
* @author Moritz Halbritter
4138
* @author Stefan Bratanov
4239
*/
43-
abstract class HttpSender extends Sender {
40+
abstract class HttpSender extends BaseHttpSender<URI, byte[]> {
4441

45-
private static final DataSize MESSAGE_MAX_SIZE = DataSize.ofKilobytes(512);
46-
47-
private volatile boolean closed;
48-
49-
@Override
50-
public Encoding encoding() {
51-
return Encoding.JSON;
52-
}
42+
/**
43+
* Only use gzip compression on data which is bigger than this in bytes.
44+
*/
45+
private static final DataSize COMPRESSION_THRESHOLD = DataSize.ofKilobytes(1);
5346

54-
@Override
55-
public int messageMaxBytes() {
56-
return (int) MESSAGE_MAX_SIZE.toBytes();
47+
HttpSender(Encoding encoding, Factory endpointSupplierFactory, String endpoint) {
48+
super(encoding, endpointSupplierFactory, endpoint);
5749
}
5850

5951
@Override
60-
public int messageSizeInBytes(List<byte[]> encodedSpans) {
61-
return encoding().listSizeInBytes(encodedSpans);
52+
protected URI newEndpoint(String endpoint) {
53+
return URI.create(endpoint);
6254
}
6355

6456
@Override
65-
public int messageSizeInBytes(int encodedSizeInBytes) {
66-
return encoding().listSizeInBytes(encodedSizeInBytes);
57+
protected byte[] newBody(List<byte[]> list) {
58+
return this.encoding.encode(list);
6759
}
6860

6961
@Override
70-
public CheckResult check() {
71-
try {
72-
sendSpans(Collections.emptyList()).execute();
73-
return CheckResult.OK;
62+
protected void postSpans(URI endpoint, byte[] body) throws IOException {
63+
HttpHeaders headers = getDefaultHeaders();
64+
if (needsCompression(body)) {
65+
body = compress(body);
66+
headers.set("Content-Encoding", "gzip");
7467
}
75-
catch (IOException | RuntimeException ex) {
76-
return CheckResult.failed(ex);
77-
}
78-
}
79-
80-
@Override
81-
public void close() throws IOException {
82-
this.closed = true;
68+
postSpans(endpoint, headers, body);
8369
}
8470

8571
/**
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
72+
* This will send span(s) as a POST to a zipkin endpoint.
73+
* @param endpoint the POST endpoint. For example, http://localhost:9411/api/v2/spans.
74+
* @param headers headers for the POST request
75+
* @param body list of possibly gzipped, encoded spans.
9076
*/
91-
protected abstract HttpPostCall sendSpans(byte[] batchedEncodedSpans);
77+
abstract void postSpans(URI endpoint, HttpHeaders headers, byte[] body);
9278

93-
@Override
94-
public Call<Void> sendSpans(List<byte[]> encodedSpans) {
95-
if (this.closed) {
96-
throw new ClosedSenderException();
97-
}
98-
return sendSpans(BytesMessageEncoder.JSON.encode(encodedSpans));
79+
HttpHeaders getDefaultHeaders() {
80+
HttpHeaders headers = new HttpHeaders();
81+
headers.set("b3", "0");
82+
headers.set("Content-Type", this.encoding.mediaType());
83+
return headers;
9984
}
10085

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;
123-
}
124-
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-
}
134-
135-
private boolean needsCompression() {
136-
return this.body.length > COMPRESSION_THRESHOLD.toBytes();
137-
}
86+
private boolean needsCompression(byte[] body) {
87+
return body.length > COMPRESSION_THRESHOLD.toBytes();
88+
}
13889

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();
90+
private byte[] compress(byte[] input) throws IOException {
91+
ByteArrayOutputStream result = new ByteArrayOutputStream();
92+
try (GZIPOutputStream gzip = new GZIPOutputStream(result)) {
93+
gzip.write(input);
14894
}
149-
95+
return result.toByteArray();
15096
}
15197

15298
}

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

+6-10
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
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;
20+
import zipkin2.reporter.Encoding;
2321

2422
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.BraveConfiguration;
2523
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.OpenTelemetryConfiguration;
26-
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.ReporterConfiguration;
2724
import org.springframework.boot.actuate.autoconfigure.tracing.zipkin.ZipkinConfigurations.SenderConfiguration;
2825
import org.springframework.boot.autoconfigure.AutoConfiguration;
2926
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -44,9 +41,8 @@
4441
* @since 3.0.0
4542
*/
4643
@AutoConfiguration(after = RestTemplateAutoConfiguration.class)
47-
@ConditionalOnClass(Sender.class)
48-
@Import({ SenderConfiguration.class, ReporterConfiguration.class, BraveConfiguration.class,
49-
OpenTelemetryConfiguration.class })
44+
@ConditionalOnClass(BytesMessageSender.class)
45+
@Import({ SenderConfiguration.class, BraveConfiguration.class, OpenTelemetryConfiguration.class })
5046
@EnableConfigurationProperties(ZipkinProperties.class)
5147
public class ZipkinAutoConfiguration {
5248

@@ -58,8 +54,8 @@ PropertiesZipkinConnectionDetails zipkinConnectionDetails(ZipkinProperties prope
5854

5955
@Bean
6056
@ConditionalOnMissingBean
61-
public BytesEncoder<Span> spanBytesEncoder() {
62-
return SpanBytesEncoder.JSON_V2;
57+
public Encoding encoding(ZipkinProperties properties) {
58+
return properties.getEncoding();
6359
}
6460

6561
}

0 commit comments

Comments
 (0)