You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/net/trace/zipkin/zipkin.go

98 lines
2.4 KiB

6 years ago
package zipkin
import (
"fmt"
protogen "github.com/go-kratos/kratos/pkg/net/trace/proto"
6 years ago
"time"
"github.com/go-kratos/kratos/pkg/net/trace"
6 years ago
"github.com/openzipkin/zipkin-go/model"
"github.com/openzipkin/zipkin-go/reporter"
"github.com/openzipkin/zipkin-go/reporter/http"
)
type report struct {
rpt reporter.Reporter
}
func newReport(c *Config) *report {
return &report{
rpt: http.NewReporter(c.Endpoint,
http.Timeout(time.Duration(c.Timeout)),
http.BatchSize(c.BatchSize),
),
}
}
// WriteSpan write a trace span to queue.
func (r *report) WriteSpan(raw *trace.Span) (err error) {
6 years ago
ctx := raw.Context()
traceID := model.TraceID{Low: ctx.TraceID}
spanID := model.ID(ctx.SpanID)
parentID := model.ID(ctx.ParentID)
6 years ago
tags := raw.Tags()
6 years ago
span := model.SpanModel{
SpanContext: model.SpanContext{
TraceID: traceID,
ID: spanID,
ParentID: &parentID,
},
Name: raw.OperationName(),
6 years ago
Timestamp: raw.StartTime(),
Duration: raw.Duration(),
Tags: make(map[string]string, len(tags)),
6 years ago
}
span.LocalEndpoint = &model.Endpoint{ServiceName: raw.ServiceName()}
6 years ago
for _, tag := range tags {
6 years ago
switch tag.Key {
case trace.TagSpanKind:
6 years ago
switch tag.Value.(string) {
case "client":
span.Kind = model.Client
case "server":
span.Kind = model.Server
case "producer":
span.Kind = model.Producer
case "consumer":
span.Kind = model.Consumer
}
6 years ago
default:
v, ok := tag.Value.(string)
if ok {
span.Tags[tag.Key] = v
} else {
span.Tags[tag.Key] = fmt.Sprint(v)
}
}
}
//log save to zipkin annotation
span.Annotations = r.converLogsToAnnotations(raw.Logs())
6 years ago
r.rpt.Send(span)
return
}
func (r *report) converLogsToAnnotations(logs []*protogen.Log) (annotations []model.Annotation) {
annotations = make([]model.Annotation, 0, len(annotations))
for _, lg := range logs {
annotations = append(annotations, r.converLogToAnnotation(lg)...)
}
return annotations
}
func (r *report) converLogToAnnotation(log *protogen.Log) (annotations []model.Annotation) {
annotations = make([]model.Annotation, 0, len(log.Fields))
for _, field := range log.Fields {
val := string(field.Value)
annotation := model.Annotation{
Timestamp: time.Unix(0, log.Timestamp),
Value: field.Key + " : " + val,
}
annotations = append(annotations, annotation)
}
return annotations
}
6 years ago
// Close close the report.
func (r *report) Close() error {
return r.rpt.Close()
}