requestEntity = new RequestEntity<>(json, httpHeaders, HttpMethod.POST, URI.create(this.url));
+ this.restTemplate.exchange(requestEntity, String.class);
}
/**
diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java
index f6c95285a..90530d4d0 100644
--- a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java
+++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinAutoConfiguration.java
@@ -34,15 +34,23 @@ import org.springframework.cloud.sleuth.sampler.PercentageBasedSampler;
import org.springframework.cloud.sleuth.sampler.SamplerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-
+import org.springframework.web.client.RestTemplate;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
* enables reporting to Zipkin via HTTP. Has a default {@link Sampler} set as
* {@link PercentageBasedSampler}.
*
+ * The {@link ZipkinRestTemplateCustomizer} allows you to customize the {@link RestTemplate}
+ * that is used to send Spans to Zipkin. Its default implementation - {@link DefaultZipkinRestTemplateCustomizer}
+ * adds the GZip compression.
+ *
* @author Spencer Gibb
* @since 1.0.0
+ *
+ * @see PercentageBasedSampler
+ * @see ZipkinRestTemplateCustomizer
+ * @see DefaultZipkinRestTemplateCustomizer
*/
@Configuration
@EnableConfigurationProperties({ZipkinProperties.class, SamplerProperties.class})
@@ -52,9 +60,18 @@ public class ZipkinAutoConfiguration {
@Bean
@ConditionalOnMissingBean
- public ZipkinSpanReporter reporter(SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin) {
- return new HttpZipkinSpanReporter(zipkin.getBaseUrl(), zipkin.getFlushInterval(),
- zipkin.getCompression().isEnabled(), spanMetricReporter);
+ public ZipkinSpanReporter reporter(SpanMetricReporter spanMetricReporter, ZipkinProperties zipkin,
+ ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer) {
+ RestTemplate restTemplate = new RestTemplate();
+ zipkinRestTemplateCustomizer.customize(restTemplate);
+ return new HttpZipkinSpanReporter(restTemplate, zipkin.getBaseUrl(), zipkin.getFlushInterval(),
+ spanMetricReporter);
+ }
+
+ @Bean
+ @ConditionalOnMissingBean
+ public ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer(ZipkinProperties zipkinProperties) {
+ return new DefaultZipkinRestTemplateCustomizer(zipkinProperties);
}
@Bean
diff --git a/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinRestTemplateCustomizer.java b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinRestTemplateCustomizer.java
new file mode 100644
index 000000000..1edd4ab68
--- /dev/null
+++ b/spring-cloud-sleuth-zipkin/src/main/java/org/springframework/cloud/sleuth/zipkin/ZipkinRestTemplateCustomizer.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013-2016 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.cloud.sleuth.zipkin;
+
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * Implementations customize the {@link RestTemplate} used to report spans to Zipkin.
+ * For example, they can add an additional header needed by their environment.
+ *
+ * Implementors must gzip according to {@link ZipkinProperties.Compression},
+ * for example by using the {@link DefaultZipkinRestTemplateCustomizer}.
+ *
+ * @author Marcin Grzejszczak
+ *
+ * @since 1.1.0
+ */
+public interface ZipkinRestTemplateCustomizer {
+
+ void customize(RestTemplate restTemplate);
+}
diff --git a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java
index 02ace6de6..8daa6c77b 100644
--- a/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java
+++ b/spring-cloud-sleuth-zipkin/src/test/java/org/springframework/cloud/sleuth/zipkin/HttpZipkinSpanReporterTest.java
@@ -4,8 +4,9 @@ import org.junit.Rule;
import org.junit.Test;
import org.springframework.cloud.sleuth.metric.CounterServiceBasedSpanMetricReporter;
import org.springframework.cloud.sleuth.metric.SpanMetricReporter;
-import zipkin.Span;
+import org.springframework.web.client.RestTemplate;
+import zipkin.Span;
import zipkin.junit.HttpFailure;
import zipkin.junit.ZipkinRule;
@@ -18,11 +19,10 @@ public class HttpZipkinSpanReporterTest {
InMemorySpanCounter inMemorySpanCounter = new InMemorySpanCounter();
SpanMetricReporter spanMetricReporter = new CounterServiceBasedSpanMetricReporter("accepted", "dropped",
this.inMemorySpanCounter);
+ RestTemplate restTemplate = defaultRestTemplate();
- HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(
- this.zipkin.httpUrl(),
+ HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(restTemplate, this.zipkin.httpUrl(),
0, // so that tests can drive flushing explicitly
- false, // disable compression
this.spanMetricReporter
);
@@ -68,10 +68,8 @@ public class HttpZipkinSpanReporterTest {
@Test
public void postsCompressedSpans() throws Exception {
- this.reporter = new HttpZipkinSpanReporter(
- this.zipkin.httpUrl(),
+ this.reporter = new HttpZipkinSpanReporter(restTemplateWithCompression(), this.zipkin.httpUrl(),
0, // so that tests can drive flushing explicitly
- false, // enable compression
this.spanMetricReporter
);
@@ -116,4 +114,20 @@ public class HttpZipkinSpanReporterTest {
static Span span(long traceId, String spanName) {
return Span.builder().traceId(traceId).id(traceId).name(spanName).build();
}
+
+ private RestTemplate restTemplate(ZipkinProperties zipkinProperties) {
+ RestTemplate restTemplate = new RestTemplate();
+ new DefaultZipkinRestTemplateCustomizer(zipkinProperties).customize(restTemplate);
+ return restTemplate;
+ }
+
+ private RestTemplate defaultRestTemplate() {
+ return restTemplate(new ZipkinProperties());
+ }
+
+ private RestTemplate restTemplateWithCompression() {
+ ZipkinProperties zipkinProperties = new ZipkinProperties();
+ zipkinProperties.getCompression().setEnabled(true);
+ return restTemplate(zipkinProperties);
+ }
}