Merge pull request #248 from LibiChai/fix_zipkin

fix zipkin log; save log to zipkin model annotations
pull/250/head
Tony 5 years ago committed by GitHub
commit 7d1756fc41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      pkg/net/trace/zipkin/zipkin.go
  2. 1
      pkg/net/trace/zipkin/zipkin_test.go

@ -2,6 +2,7 @@ package zipkin
import ( import (
"fmt" "fmt"
protogen "github.com/bilibili/kratos/pkg/net/trace/proto"
"time" "time"
"github.com/bilibili/kratos/pkg/net/trace" "github.com/bilibili/kratos/pkg/net/trace"
@ -30,7 +31,6 @@ func (r *report) WriteSpan(raw *trace.Span) (err error) {
spanID := model.ID(ctx.SpanID) spanID := model.ID(ctx.SpanID)
parentID := model.ID(ctx.ParentID) parentID := model.ID(ctx.ParentID)
tags := raw.Tags() tags := raw.Tags()
logs := raw.Logs()
span := model.SpanModel{ span := model.SpanModel{
SpanContext: model.SpanContext{ SpanContext: model.SpanContext{
TraceID: traceID, TraceID: traceID,
@ -40,7 +40,7 @@ func (r *report) WriteSpan(raw *trace.Span) (err error) {
Name: raw.OperationName(), Name: raw.OperationName(),
Timestamp: raw.StartTime(), Timestamp: raw.StartTime(),
Duration: raw.Duration(), Duration: raw.Duration(),
Tags: make(map[string]string, len(tags)+len(logs)), Tags: make(map[string]string, len(tags)),
} }
span.LocalEndpoint = &model.Endpoint{ServiceName: raw.ServiceName()} span.LocalEndpoint = &model.Endpoint{ServiceName: raw.ServiceName()}
for _, tag := range tags { for _, tag := range tags {
@ -65,13 +65,32 @@ func (r *report) WriteSpan(raw *trace.Span) (err error) {
} }
} }
} }
for _, lg := range logs { //log save to zipkin annotation
span.Tags[lg.Key] = string(lg.Value) span.Annotations = r.converLogsToAnnotations(raw.Logs())
}
r.rpt.Send(span) r.rpt.Send(span)
return 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
}
// Close close the report. // Close close the report.
func (r *report) Close() error { func (r *report) Close() error {
return r.rpt.Close() return r.rpt.Close()

@ -37,6 +37,7 @@ func TestZipkin(t *testing.T) {
t2 := trace.NewTracer("service2", report, true) t2 := trace.NewTracer("service2", report, true)
sp1 := t1.New("option_1") sp1 := t1.New("option_1")
sp2 := sp1.Fork("service3", "opt_client") sp2 := sp1.Fork("service3", "opt_client")
sp2.SetLog(trace.Log("log_k","log_v"))
// inject // inject
header := make(http.Header) header := make(http.Header)
t1.Inject(sp2, trace.HTTPFormat, header) t1.Inject(sp2, trace.HTTPFormat, header)

Loading…
Cancel
Save