diff --git a/pkg/net/trace/zipkin/zipkin.go b/pkg/net/trace/zipkin/zipkin.go index aff0fa6c4..e53069a84 100644 --- a/pkg/net/trace/zipkin/zipkin.go +++ b/pkg/net/trace/zipkin/zipkin.go @@ -2,6 +2,7 @@ package zipkin import ( "fmt" + protogen "github.com/bilibili/kratos/pkg/net/trace/proto" "time" "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) parentID := model.ID(ctx.ParentID) tags := raw.Tags() - logs := raw.Logs() span := model.SpanModel{ SpanContext: model.SpanContext{ TraceID: traceID, @@ -40,7 +40,7 @@ func (r *report) WriteSpan(raw *trace.Span) (err error) { Name: raw.OperationName(), Timestamp: raw.StartTime(), 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()} for _, tag := range tags { @@ -65,13 +65,32 @@ func (r *report) WriteSpan(raw *trace.Span) (err error) { } } } - for _, lg := range logs { - span.Tags[lg.Key] = string(lg.Value) - } + //log save to zipkin annotation + span.Annotations = r.converLogsToAnnotations(raw.Logs()) 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 +} + // Close close the report. func (r *report) Close() error { return r.rpt.Close() diff --git a/pkg/net/trace/zipkin/zipkin_test.go b/pkg/net/trace/zipkin/zipkin_test.go index 94d0ea566..a68884a91 100644 --- a/pkg/net/trace/zipkin/zipkin_test.go +++ b/pkg/net/trace/zipkin/zipkin_test.go @@ -37,6 +37,7 @@ func TestZipkin(t *testing.T) { t2 := trace.NewTracer("service2", report, true) sp1 := t1.New("option_1") sp2 := sp1.Fork("service3", "opt_client") + sp2.SetLog(trace.Log("log_k","log_v")) // inject header := make(http.Header) t1.Inject(sp2, trace.HTTPFormat, header)