Skip to content

Commit

Permalink
Merge pull request #37 from opentracing/bhs/kv_logging
Browse files Browse the repository at this point in the history
Adjust to the new key-value logging regime
  • Loading branch information
bensigelman committed Sep 26, 2016
2 parents c181c29 + 51b5ae4 commit 9f35438
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
10 changes: 10 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ type RawSpan struct {
Tags opentracing.Tags

// The span's "microlog".
Logs []opentracing.LogData
Logs []opentracing.LogRecord
}
35 changes: 32 additions & 3 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

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
Expand Down Expand Up @@ -88,6 +89,31 @@ func (s *spanImpl) SetTag(key string, value interface{}) opentracing.Span {
return s
}

func (s *spanImpl) LogKV(keyValues ...interface{}) {
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,
Expand All @@ -113,7 +139,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() {
Expand All @@ -129,8 +155,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

Expand Down

0 comments on commit 9f35438

Please sign in to comment.