一、基本信息
collector端口
调试方法
1)环境变量
CGO_ENABLED=0
SPAN_STORAGE_TYPE=memory
2)应用参数
--collector.zipkin.http-port=9411
二、Zipkin上报实现
cmd/collector/app/zipkin/http_handler.go:60
func (aH *APIHandler) RegisterRoutes(router *mux.Router) {
router.HandleFunc("/api/v1/spans", aH.saveSpans).Methods(http.MethodPost)
router.HandleFunc("/api/v2/spans", aH.saveSpansV2).Methods(http.MethodPost)
}
func (aH *APIHandler) saveSpansV2(w http.ResponseWriter, r *http.Request) {
bRead := r.Body
defer r.Body.Close()
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
···
}
bodyBytes, err := ioutil.ReadAll(bRead)
···
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
// 对span进行转换,统一转为thrift格式
var tSpans []*zipkincore.Span
switch contentType {
case "application/json":
tSpans, err = jsonToThriftSpansV2(bodyBytes, aH.zipkinV2Formats)
case "application/x-protobuf":
tSpans, err = protoToThriftSpansV2(bodyBytes)
default:
http.Error(w, "Unsupported Content-Type", http.StatusBadRequest)
return
}
// 保存span信息
if err = aH.saveThriftSpans(tSpans); err != nil {
http.Error(w, fmt.Sprintf("Cannot submit Zipkin batch: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(operations.PostSpansAcceptedCode)
}
1)支持v1和v2接口
2)数据转为protobuf格式
三、Jaeger上报实现
cmd/collector/app/http_handler.go:57
func (aH *APIHandler) RegisterRoutes(router *mux.Router) {
router.HandleFunc("/api/traces", aH.saveSpan).Methods(http.MethodPost)
}
func (aH *APIHandler) saveSpan(w http.ResponseWriter, r *http.Request) {
bodyBytes, err := ioutil.ReadAll(r.Body)
···
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
···
// 目前只接收thrift格式数据
if _, ok := acceptedThriftFormats[contentType]; !ok {
http.Error(w, fmt.Sprintf("Unsupported content type: %v", contentType), http.StatusBadRequest)
return
}
tdes := thrift.NewTDeserializer()
batch := &tJaeger.Batch{}
if err = tdes.Read(batch, bodyBytes); err != nil {
http.Error(w, fmt.Sprintf(UnableToReadBodyErrFormat, err), http.StatusBadRequest)
return
}
batches := []*tJaeger.Batch{batch}
opts := SubmitBatchOptions{InboundTransport: HTTPTransport}
if _, err = aH.jaegerBatchesHandler.SubmitBatches(batches, opts); err != nil {
http.Error(w, fmt.Sprintf("Cannot submit Jaeger batch: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
}
1)需要是thrift格式数据
四、上报数据
zipkin上报:http://localhost:9411/api/v2/spans,需要设置contenttype为application/json。
五、上报数据规范
上报数据的格式使用swagger进行定义,并保存到变量SwaggerJSON中。
swagger-gen\restapi\embedded_spec.go
https://wu-sheng.gitbooks.io/opentracing-io/content/pages/api/data-conventions.html