Skip to content

Send Trace to Zipkin

Wuyi Chen edited this page Jul 3, 2019 · 15 revisions

Overview


Set up Zipkin server

Places for storing tracing data

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.

Basic setting (for storing in memory)

Set dependencies

build.gradle

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'
}

Set application bootstrap class

ZipkinServerApplication.java

@SpringBootApplication
@EnableZipkinServer                // Start Spring Boot application as Zipkin server
public class ZipkinServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZipkinServerApplication.class, args);
    }
}

Setting for storing tracing data into other places

Store in MySQL database

(Need to add)

Store in Cassandra database

(Need to add)

Store in Elasticsearch

(Need to add)


Set up client service to send tracing data to Zipkin server

Ways for sending tracing data to Zipkin server

There are several ways to send tracing data to Zipkin server:

  • By HTTP (default)
  • By Kafka
  • By RabbitMQ

Basic setting (for sending by HTTP)

Set dependencies

build.gradle

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'
}

Set configuration

You need to let the client service know where is the location of the Zipkin server.

application.yml

spring:
  zipkin:
     baseUrl: http://localhost:9411/              # send tracing data to Zipkin server

Specify the way for sending tracing data

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.

bootstrap.yml

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

Set percentage for transaction sampling

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 the AlwaysSampler class.

Way 1: Add the parameter spring.sleuth.sampler.percentage to the application configuration

application.yml

spring:
  sleuth:
    sampler:
      percentage: 1.0

Way 2: Replace the default Sampler class by the AlwaysSampler class

Application.java

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);
    }
}

Setting for sending trace by other ways

Send by Kafka

(Need to add)

Send by RabbitMQ

(Need to add)


Verification

After setting both sides, if you hitting any client service, you should see the tracing data in the Zipkin portal.


Send custom trace to Zipkin

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
        }
    }
}

References

Clone this wiki locally