-
Notifications
You must be signed in to change notification settings - Fork 60
Send Trace to Zipkin
- Set up Zipkin server
- Set up client service to send tracing data to Zipkin server
- Verification
- Send custom trace to Zipkin
- References
There are several options for storing the collected tracing data:
- Store in memory (default)
- Store in MySQL database
- Store in Cassandra database
- Store in Elasticsearch
By default, Zipkin uses an in-memory data store for storing tracing data.
dependencies {
compile group: 'io.zipkin.java', name: 'zipkin-server', version: '1.30.0'
compile group: 'io.zipkin.java', name: 'zipkin-autoconfigure-ui', version: '1.30.0'
}
@SpringBootApplication
@EnableZipkinServer // Start Spring Boot application as Zipkin server
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
(Need to add)
(Need to add)
(Need to add)
There are several ways to send tracing data to Zipkin server:
- By HTTP (default)
- By Kafka
- By RabbitMQ
dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '1.3.5.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-sleuth-zipkin', version: '1.3.5.RELEASE'
}
You need to let the client service know where is the location of the Zipkin server.
spring:
zipkin:
baseUrl: http://localhost:9411/ # send tracing data to Zipkin server
By default, the client service will send tracing data to the Zipkin server by HTTP.
But if spring-cloud-starter-stream-kafka
is in the classpath of the client service (it needs to integrate with Kafka for other purposes), Spring will deem that you want to send tracing data by Kafka and the service will do so. If you still want to send tracing data by HTTP, you need to specify it in the application configuration explicitly by adding zipkin.sender.type
. The possible values for that parameter can be web
, kafka
or rabbit
.
spring:
zipkin:
baseUrl: http://localhost:9411/ # send tracing information to Zipkin server
sender:
type: web # because spring-cloud-starter-stream-kafka is included in classpath,
# so Spring will send the trace into Kafka automatically.
# if you still want to send the trace by http, you need to specify the way you want to send explicitly
By default, the client service will only write 10% of tracing data to the Zipkin server. If you want the client service to send all the tracing data to the Zipkin server, you can do it in 2 ways:
- Add the parameter
spring.sleuth.sampler.percentage
to the application configuration. - Replace the default
Sampler
class by theAlwaysSampler
class.
application.yml
spring:
sleuth:
sampler:
percentage: 1.0
public class Application {
@Bean
public Sampler defaultSampler() {
return new AlwaysSampler(); // Use AlwaysSampler to replace the default Sampler
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
(Need to add)
(Need to add)
After setting both sides, if you hitting any client service, you should see the tracing data in the Zipkin portal.
You can also send custom trace to Zipkin.
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
@Component
public class SomeClass {
@Autowired
Tracer tracer;
public void someFunction() {
Span newSpan = tracer.createSpan("yyyy"); // create a custom span (yyyy will show as call name)
try {
/* some lines */
} finally {
newSpan.tag("peer.service", "xxxx"); // tag the custom span, (xxxx will show as service name)
newSpan.logEvent(Span.CLIENT_RECV); // log the event to tell Spring Cloud Sleuth that it should capture the time when the call is complete
tracer.close(newSpan); // Close the trace
}
}
}
- Overview
- Getting Started
-
Technical Essentials
- Autowired
- SpringData JPA
- Configuration File Auto-loading
- Configuration Encryption
- Service Discovery with Eureka
- Resiliency Patterns with Hystrix
- Configure Hystrix
- Service Gateway with Zuul
- Zuul Filters
- Protect Service with Spring Security and OAuth2
- Use JWT as Access Token
- Store Clients and Users' Credentials to DB
- Integrate with Message Queue (Kafka)
- Integrate with Redis
- Tune Logging
- Log Aggregation
- Send Trace to Zipkin
- Build Runnable Jar
- Core Application Logic
- Components