From 62c7459729e0ddbf03fd0b2f40b8077c89435274 Mon Sep 17 00:00:00 2001 From: Ben Sigelman Date: Tue, 13 Sep 2016 11:45:09 -0700 Subject: [PATCH 1/2] Adjust to the new key-value logging regime --- event.go | 10 ++++++++++ raw.go | 2 +- span.go | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/event.go b/event.go index d3c2b3ff16cc..e074fcc7d86a 100644 --- a/event.go +++ b/event.go @@ -19,7 +19,12 @@ type EventBaggage struct { Key, Value string } +// EventLogFields is received when LogFields or LogKV is called. +type EventLogFields opentracing.LogRecord + // EventLog is received when Log (or one of its derivatives) is called. +// +// DEPRECATED type EventLog opentracing.LogData // EventFinish is received when Finish is called. @@ -40,6 +45,11 @@ func (s *spanImpl) onLog(ld opentracing.LogData) { s.event(EventLog(ld)) } } +func (s *spanImpl) onLogFields(lr opentracing.LogRecord) { + if s.event != nil { + s.event(EventLogFields(lr)) + } +} func (s *spanImpl) onBaggage(key, value string) { if s.event != nil { s.event(EventBaggage{Key: key, Value: value}) diff --git a/raw.go b/raw.go index 288180e413f0..e038b3694c10 100644 --- a/raw.go +++ b/raw.go @@ -30,5 +30,5 @@ type RawSpan struct { Tags opentracing.Tags // The span's "microlog". - Logs []opentracing.LogData + Logs []opentracing.LogRecord } diff --git a/span.go b/span.go index 8a02c0212b94..90a5ea9af775 100644 --- a/span.go +++ b/span.go @@ -1,11 +1,13 @@ package basictracer import ( + "fmt" "sync" "time" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" + "github.com/opentracing/opentracing-go/log" ) // Span provides access to the essential details of the span, for use @@ -88,6 +90,35 @@ func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span { return s } +func (s *spanImpl) LogKV(keyValues ...interface{}) { + if len(keyValues)%2 != 0 { + s.LogFields(log.Error(fmt.Errorf("Non-even keyValues len: %v", len(keyValues)))) + return + } + fields, err := log.InterleavedKVToFields(keyValues...) + if err != nil { + s.LogFields(log.Error(err), log.String("function", "LogKV")) + return + } + s.LogFields(fields...) +} + +func (s *spanImpl) LogFields(fields ...log.Field) { + lr := opentracing.LogRecord{ + Fields: fields, + } + defer s.onLogFields(lr) + s.Lock() + defer s.Unlock() + if s.trim() || s.tracer.options.DropAllLogs { + return + } + if lr.Timestamp.IsZero() { + lr.Timestamp = time.Now() + } + s.raw.Logs = append(s.raw.Logs, lr) +} + func (s *spanImpl) LogEvent(event string) { s.Log(opentracing.LogData{ Event: event, @@ -113,7 +144,7 @@ func (s *spanImpl) Log(ld opentracing.LogData) { ld.Timestamp = time.Now() } - s.raw.Logs = append(s.raw.Logs, ld) + s.raw.Logs = append(s.raw.Logs, ld.ToLogRecord()) } func (s *spanImpl) Finish() { @@ -129,8 +160,11 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) { s.Lock() defer s.Unlock() - if opts.BulkLogData != nil { - s.raw.Logs = append(s.raw.Logs, opts.BulkLogData...) + if opts.LogRecords != nil { + s.raw.Logs = append(s.raw.Logs, opts.LogRecords...) + } + for _, ld := range opts.BulkLogData { + s.raw.Logs = append(s.raw.Logs, ld.ToLogRecord()) } s.raw.Duration = duration From 51b5ae49a0472c955f63e697685c1577cc42be9b Mon Sep 17 00:00:00 2001 From: Ben Sigelman Date: Tue, 13 Sep 2016 17:23:47 -0700 Subject: [PATCH 2/2] Remove check that's done in a subroutine --- span.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/span.go b/span.go index 90a5ea9af775..d88e74bd6d25 100644 --- a/span.go +++ b/span.go @@ -1,7 +1,6 @@ package basictracer import ( - "fmt" "sync" "time" @@ -91,10 +90,6 @@ func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span { } func (s *spanImpl) LogKV(keyValues ...interface{}) { - if len(keyValues)%2 != 0 { - s.LogFields(log.Error(fmt.Errorf("Non-even keyValues len: %v", len(keyValues)))) - return - } fields, err := log.InterleavedKVToFields(keyValues...) if err != nil { s.LogFields(log.Error(err), log.String("function", "LogKV"))