Adds proto3 encoding
Note: Zipkin server 2.8+ is required to be running for this to work.
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -272,7 +272,7 @@
|
||||
<spring-cloud-stream.version>Elmhurst.BUILD-SNAPSHOT</spring-cloud-stream.version>
|
||||
<spring-cloud-netflix.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
|
||||
<spring-cloud-openfeign.version>2.0.0.BUILD-SNAPSHOT</spring-cloud-openfeign.version>
|
||||
<brave.version>4.19.0</brave.version>
|
||||
<brave.version>4.19.2</brave.version>
|
||||
<spring-security-boot-autoconfigure.version>2.0.0.RELEASE</spring-security-boot-autoconfigure.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
<name>spring-cloud-sleuth-dependencies</name>
|
||||
<description>Spring Cloud Sleuth Dependencies</description>
|
||||
<properties>
|
||||
<brave.opentracing.version>0.30.0</brave.opentracing.version>
|
||||
<brave.opentracing.version>0.30.3</brave.opentracing.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
||||
@@ -51,6 +51,9 @@ final class RestTemplateSender extends Sender {
|
||||
if (encoder.equals(JSON_V2)) {
|
||||
this.mediaType = MediaType.APPLICATION_JSON;
|
||||
this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v2/spans";
|
||||
} else if (this.encoding == Encoding.PROTO3) {
|
||||
this.mediaType = MediaType.parseMediaType("application/x-protobuf");
|
||||
this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v2/spans";
|
||||
} else if (this.encoding == Encoding.JSON) {
|
||||
this.mediaType = MediaType.APPLICATION_JSON;
|
||||
this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v1/spans";
|
||||
|
||||
@@ -19,16 +19,20 @@ package org.springframework.cloud.sleuth.zipkin2.sender;
|
||||
import java.util.stream.Stream;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import zipkin2.Call;
|
||||
import zipkin2.Endpoint;
|
||||
import zipkin2.Span;
|
||||
import zipkin2.codec.Encoding;
|
||||
import zipkin2.codec.SpanBytesEncoder;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static zipkin2.codec.SpanBytesEncoder.JSON_V2;
|
||||
import static zipkin2.codec.SpanBytesEncoder.PROTO3;
|
||||
|
||||
public class RestTemplateSenderTest {
|
||||
static final Span SPAN = Span.newBuilder()
|
||||
@@ -64,9 +68,24 @@ public class RestTemplateSenderTest {
|
||||
.isEqualTo("[" + new String(JSON_V2.encode(SPAN), "UTF-8") + "]");
|
||||
}
|
||||
|
||||
@Test public void proto3() throws Exception {
|
||||
server.enqueue(new MockResponse());
|
||||
sender = new RestTemplateSender(new RestTemplate(), endpoint, PROTO3);
|
||||
|
||||
send(SPAN).execute();
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertThat(request.getHeader("Content-Type"))
|
||||
.isEqualTo("application/x-protobuf");
|
||||
|
||||
// proto3 encoding of ListOfSpan is simply a repeated span entry
|
||||
assertThat(request.getBody().readByteArray())
|
||||
.containsExactly(SpanBytesEncoder.PROTO3.encode(SPAN));
|
||||
}
|
||||
|
||||
Call<Void> send(Span... spans) {
|
||||
return sender.sendSpans(Stream.of(spans)
|
||||
.map(JSON_V2::encode)
|
||||
.collect(toList()));
|
||||
SpanBytesEncoder bytesEncoder = sender.encoding() == Encoding.JSON
|
||||
? SpanBytesEncoder.JSON_V2 : SpanBytesEncoder.PROTO3;
|
||||
return sender.sendSpans(Stream.of(spans).map(bytesEncoder::encode).collect(toList()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user