From df9b5643eac89972de252e767071ca8c66ca9b79 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 24 Aug 2018 12:31:49 -0700 Subject: [PATCH] make logging prettier 1. Pretty format the JSON. 2. Manually convert tag values. According to the opentracing spec, these are only supposed to be strings, ints, and booleans. We were using the standard MarshalJSON interface which was trying to marshal our peer IDs as raw byte strings. --- log.go | 8 ++++++-- tracer/recorder.go | 24 +++++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/log.go b/log.go index 7732d7d..e570561 100644 --- a/log.go +++ b/log.go @@ -336,13 +336,17 @@ func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Logg accum["system"] = e.system accum["time"] = FormatRFC3339(time.Now()) - out, err := json.Marshal(accum) + var buf bytes.Buffer + encoder := json.NewEncoder(&buf) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + err = encoder.Encode(accum) if err != nil { el.Errorf("ERROR FORMATTING EVENT ENTRY: %s", err) return } - writer.WriterGroup.Write(append(out, '\n')) + writer.WriterGroup.Write(buf.Bytes()) } // DEPRECATED diff --git a/tracer/recorder.go b/tracer/recorder.go index 60ed1f8..0107103 100644 --- a/tracer/recorder.go +++ b/tracer/recorder.go @@ -1,6 +1,8 @@ package loggabletracer import ( + "bytes" + "encoding/base64" "encoding/json" "fmt" "os" @@ -65,6 +67,18 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) { } } + tags := make(map[string]interface{}, len(span.Tags)) + for k, v := range span.Tags { + switch vt := v.(type) { + case bool, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint64: + tags[k] = v + case []byte: + base64.StdEncoding.EncodeToString(vt) + default: + tags[k] = fmt.Sprint(v) + } + } + spanlog := &LoggableSpan{ TraceID: span.Context.TraceID, SpanID: span.Context.SpanID, @@ -72,15 +86,19 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) { Operation: span.Operation, Start: span.Start, Duration: span.Duration, - Tags: span.Tags, + Tags: tags, Logs: sl, } - out, err := json.Marshal(spanlog) + var buf bytes.Buffer + encoder := json.NewEncoder(&buf) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + err := encoder.Encode(spanlog) if err != nil { fmt.Fprintf(os.Stderr, "ERROR FORMATTING SPAN ENTRY: %s\n", err) return } - writer.WriterGroup.Write(append(out, '\n')) + writer.WriterGroup.Write(buf.Bytes()) }