Zipkin

Description

Zipkin an open source distributed tracing system. This plugin is supported to collect tracing and report to Zipkin Collector based on Zipkin API specification.

It’s also works with Apache SkyWalking and Jaeger, which are support Zipkin v1/v2 format. And of course, it can integrate other tracing systems adapted to Zipkin v1/v2 format as well.

Attributes

NameTypeRequirementDefaultValidDescription
endpointstringrequiredthe http endpoint of Ziplin, for example: http://127.0.0.1:9411/api/v2/spans.
sample_rationumberrequired[0.00001, 1]the ratio of sample
service_namestringoptional“APISIX”service name for zipkin reporter
server_addrstringoptionalIPv4 address for zipkin reporter, default is nginx built-in variables $server_addr, here you can specify your external ip address.
span_versionintegeroptional2[1, 2]the version of span type

Currently each traced request will create spans below:

  1. request
  2. ├── proxy: from the beginning of the request to the beginning of header filter
  3. └── response: from the beginning of header filter to the beginning of log

Previously we created spans below:

  1. request
  2. ├── rewrite
  3. ├── access
  4. └── proxy
  5. └── body_filter

Note: the name of span doesn’t represent the corresponding Nginx’s phase.

If you need to be compatible with old style, we can set span_version to 1.

How To Enable

Here’s an example, enable the zipkin plugin on the specified route:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "plugins": {
  6. "zipkin": {
  7. "endpoint": "http://127.0.0.1:9411/api/v2/spans",
  8. "sample_ratio": 1,
  9. "service_name": "APISIX-IN-SG",
  10. "server_addr": "192.168.3.50"
  11. }
  12. },
  13. "upstream": {
  14. "type": "roundrobin",
  15. "nodes": {
  16. "127.0.0.1:1980": 1
  17. }
  18. }
  19. }'

You also can complete the above operation through the web interface, first add a route, then add zipkin plugin:

enable zipkin plugin

Test Plugin

Run the Zipkin instance

e.g. using docker:

  1. docker run -d -p 9411:9411 openzipkin/zipkin

Here is a test example:

  1. curl http://127.0.0.1:9080/index.html
  2. HTTP/1.1 200 OK
  3. ...

Then you can use a browser to access http://127.0.0.1:9411/zipkin, the webUI of Zipkin:

zipkin web-ui

zipkin web-ui list view

Run the Jaeger instance

Besides Zipkin, this plugin supports reporting the traces to Jaeger as well. Here is a sample run on docker. Run Jaeger backend on docker first:

  1. docker run -d --name jaeger \
  2. -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  3. -p 16686:16686 \
  4. -p 9411:9411 \
  5. jaegertracing/all-in-one:1.31

Create a route with Zipkin plugin like Zipkin’s example:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "plugins": {
  6. "zipkin": {
  7. "endpoint": "http://127.0.0.1:9411/api/v2/spans",
  8. "sample_ratio": 1,
  9. "service_name": "APISIX-IN-SG",
  10. "server_addr": "192.168.3.50"
  11. }
  12. },
  13. "upstream": {
  14. "type": "roundrobin",
  15. "nodes": {
  16. "127.0.0.1:1980": 1
  17. }
  18. }
  19. }'

Access the service:

  1. curl http://127.0.0.1:9080/index.html
  2. HTTP/1.1 200 OK
  3. ...

Then you can access http://127.0.0.1:16686, the WebUI of Jaeger, to view traceson browser:

jaeger web-ui

jaeger web-ui trace

Disable Plugin

When you want to disable the zipkin plugin, it is very simple, you can delete the corresponding json configuration in the plugin configuration, no need to restart the service, it will take effect immediately:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "plugins": {
  6. },
  7. "upstream": {
  8. "type": "roundrobin",
  9. "nodes": {
  10. "127.0.0.1:1980": 1
  11. }
  12. }
  13. }'

The zipkin plugin has been disabled now. It works for other plugins.

example code for upstream ( golang with Gin )

  1. func GetTracer(serviceName string, port int, enpoitUrl string, rate float64) *zipkin.Tracer {
  2. // create a reporter to be used by the tracer
  3. reporter := httpreporter.NewReporter(enpoitUrl)
  4. // set-up the local endpoint for our service host is ip:host
  5. thisip, _ := GetLocalIP()
  6. host := fmt.Sprintf("%s:%d", thisip, port)
  7. endpoint, _ := zipkin.NewEndpoint(serviceName, host)
  8. // set-up our sampling strategy
  9. sampler, _ := zipkin.NewCountingSampler(rate)
  10. // initialize the tracer
  11. tracer, _ := zipkin.NewTracer(
  12. reporter,
  13. zipkin.WithLocalEndpoint(endpoint),
  14. zipkin.WithSampler(sampler),
  15. )
  16. return tracer
  17. }
  18. func main(){
  19. r := gin.Default()
  20. tracer := GetTracer(...)
  21. // use middleware to extract parentID from http header that injected by APISIX
  22. r.Use(func(c *gin.Context) {
  23. span := this.Tracer.Extract(b3.ExtractHTTP(c.Request))
  24. childSpan := this.Tracer.StartSpan(spanName, zipkin.Parent(span))
  25. defer childSpan.Finish()
  26. c.Next()
  27. })
  28. }