Adds Zipkin v2 support (#720)

This adds the ability to use zipkin v2 endpoints natively. It also
allows wiring of standard zipkin reporters and senders.
This commit is contained in:
Adrian Cole
2017-10-03 14:47:33 +08:00
committed by Marcin Grzejszczak
parent 08a526c61b
commit 4ddf929294
28 changed files with 2112 additions and 19 deletions

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2013-2015 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 sample;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @author Spencer Gibb
*/
@Component
public class SampleBackground {
@Autowired
private Tracer tracer;
@Autowired
private Random random;
@Async
public void background() throws InterruptedException {
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("background-sleep-millis", String.valueOf(millis));
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2013-2015 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 sample;
import java.util.Random;
import java.util.concurrent.Callable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanAccessor;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author Spencer Gibb
*/
@RestController
public class SampleController implements
ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private static final Log log = LogFactory.getLog(SampleController.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private Tracer tracer;
@Autowired
private SpanAccessor accessor;
@Autowired
private SampleBackground controller;
@Autowired
private Random random;
private int port;
@RequestMapping("/")
public String hi() throws InterruptedException {
Thread.sleep(this.random.nextInt(1000));
log.info("Home page");
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/hi2", String.class);
return "hi/" + s;
}
@RequestMapping("/call")
public Callable<String> call() {
return new Callable<String>() {
@Override
public String call() throws Exception {
int millis = SampleController.this.random.nextInt(1000);
Thread.sleep(millis);
SampleController.this.tracer.addTag("callable-sleep-millis", String.valueOf(millis));
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
return "async hi: " + currentSpan;
}
};
}
@RequestMapping("/async")
public String async() throws InterruptedException {
log.info("async");
this.controller.background();
return "ho";
}
@RequestMapping("/hi2")
public String hi2() throws InterruptedException {
log.info("hi2");
int millis = this.random.nextInt(1000);
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
return "hi2";
}
@RequestMapping("/traced")
public String traced() throws InterruptedException {
Span span = this.tracer.createSpan("http:customTraceEndpoint",
new AlwaysSampler());
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
this.tracer.close(span);
return "traced/" + s;
}
@RequestMapping("/start")
public String start() throws InterruptedException {
int millis = this.random.nextInt(1000);
log.info(String.format("Sleeping for [%d] millis", millis));
Thread.sleep(millis);
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
String s = this.restTemplate.getForObject("http://localhost:" + this.port
+ "/call", String.class);
return "start/" + s;
}
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2013-2015 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 sample;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
/**
* @author Spencer Gibb
*/
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
public class SampleZipkinApplication {
private static final Log log = LogFactory.getLog(SampleZipkinApplication.class);
public static void main(String[] args) {
SpringApplication.run(SampleZipkinApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
// Use this for debugging (or if there is no Zipkin server running on port 9411)
@Bean
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
public Reporter<Span> spanReporter() {
return Reporter.CONSOLE;
}
}

View File

@@ -0,0 +1,18 @@
server:
port: 3380
spring:
application:
# This ends up as the service name in zipkin
name: testsleuthzipkin
zipkin:
# Uncomment to send to zipkin, replacing 192.168.99.100 with your zipkin IP address
# baseUrl: http://192.168.99.100:9411/
sleuth:
sampler:
percentage: 1.0
sample:
zipkin:
# When enabled=false, traces log to the console. Comment to send to zipkin
enabled: false

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2013-2015 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 integration;
import integration.ZipkinTests.WaitUntilZipkinIsUpConfig;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import zipkin.junit.ZipkinRule;
import zipkin.server.EnableZipkinServer;
import java.net.URI;
import java.util.Random;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.sleuth.zipkin2.ZipkinProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static java.util.concurrent.TimeUnit.SECONDS;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { WaitUntilZipkinIsUpConfig.class, SampleZipkinApplication.class },
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {"sample.zipkin.enabled=true"})
public class ZipkinTests extends AbstractIntegrationTest {
@ClassRule public static final ZipkinRule zipkin = new ZipkinRule();
private static final String APP_NAME = "testsleuthzipkin";
@Value("${local.server.port}")
private int port = 3380;
private String sampleAppUrl = "http://localhost:" + this.port;
@Autowired ZipkinProperties zipkinProperties;
@Override protected int getZipkinServerPort() {
return getPortFromProps();
}
private int getPortFromProps() {
return URI.create(this.zipkinProperties.getBaseUrl()).getPort();
}
@Test
public void should_propagate_spans_to_zipkin() {
long traceId = new Random().nextLong();
await().atMost(10, SECONDS).untilAsserted(() ->
httpMessageWithTraceIdInHeadersIsSuccessfullySent(
this.sampleAppUrl + "/hi2", traceId).run()
);
await().atMost(10, SECONDS).untilAsserted(() ->
allSpansWereRegisteredInZipkinWithTraceIdEqualTo(traceId).run()
);
}
@Override
protected String getAppName() {
return APP_NAME;
}
@Configuration
public static class WaitUntilZipkinIsUpConfig {
@Bean
@Primary
ZipkinProperties testZipkinProperties() {
ZipkinProperties zipkinProperties = new ZipkinProperties();
zipkinProperties.setBaseUrl(zipkin.httpUrl());
return zipkinProperties;
}
}
@SpringBootApplication
@EnableZipkinServer
protected static class ZipkinServer {
public static void main(String[] args) {
SpringApplication.run(ZipkinServer.class, args);
}
}
}

View File

@@ -0,0 +1,20 @@
package sample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SampleZipkinApplication.class)
@WebAppConfiguration
@TestPropertySource(properties = "sample.zipkin.enabled=false")
public class SampleSleuthApplicationTests {
@Test
public void contextLoads() {
}
}