Skip to content

Commit

Permalink
util/tracing: move extractMsgFromRecord to the tracking pkg
Browse files Browse the repository at this point in the history
This function was used in sql, but I want to start using it elsewhere
when dealing with traces.

Release note: None
Release justification: Minor code move, in support of the next commit.
  • Loading branch information
andreimatei committed Apr 1, 2020
1 parent af92537 commit d3a2605
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
17 changes: 1 addition & 16 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1499,21 +1499,6 @@ func (st *SessionTracing) TraceExecEnd(ctx context.Context, err error, count int
}
}

// extractMsgFromRecord extracts the message of the event, which is either in an
// "event" or "error" field.
func extractMsgFromRecord(rec tracing.LogRecord) string {
for _, f := range rec.Fields {
key := f.Key
if key == "event" {
return f.Value
}
if key == "error" {
return fmt.Sprint("error:", f.Value)
}
}
return "<event missing in trace message>"
}

const (
// span_idx INT NOT NULL, -- The span's index.
traceSpanIdxCol = iota
Expand Down Expand Up @@ -1765,7 +1750,7 @@ func getMessagesForSubtrace(
allLogs = append(allLogs,
logRecordRow{
timestamp: logTime,
msg: extractMsgFromRecord(span.Logs[i]),
msg: span.Logs[i].Msg(),
span: span,
// Add 1 to the index to account for the first dummy message in a
// span.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/log/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func eventInternal(ctx context.Context, isErr, withTags bool, format string, arg
if sp != nil {
// TODO(radu): pass tags directly to sp.LogKV when LightStep supports
// that.
sp.LogFields(otlog.String("event", msg))
sp.LogFields(otlog.String(tracing.LogMessageField, msg))
// if isErr {
// // TODO(radu): figure out a way to signal that this is an error. We
// // could use a different "error" key (provided it shows up in
Expand Down
24 changes: 23 additions & 1 deletion pkg/util/tracing/recorded_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

package tracing

import "strings"
import (
"fmt"
"strings"
)

// LogMessageField is the field name used for the opentracing.Span.LogFields()
// for a log message.
const LogMessageField = "event"

func (s *RecordedSpan) String() string {
sb := strings.Builder{}
Expand All @@ -20,3 +27,18 @@ func (s *RecordedSpan) String() string {
}
return sb.String()
}

// Msg extracts the message of the LogRecord, which is either in an "event" or
// "error" field.
func (l LogRecord) Msg() string {
for _, f := range l.Fields {
key := f.Key
if key == LogMessageField {
return f.Value
}
if key == "error" {
return fmt.Sprint("error:", f.Value)
}
}
return ""
}
6 changes: 3 additions & 3 deletions pkg/util/tracing/tracer_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (s *span) LogFields(fields ...otlog.Field) {
// TODO(radu): when LightStep supports arbitrary fields, we should make
// the formatting of the message consistent with that. Until then we treat
// legacy events that just have an "event" key specially.
if len(fields) == 1 && fields[0].Key() == "event" {
if len(fields) == 1 && fields[0].Key() == LogMessageField {
s.netTr.LazyPrintf("%s", fields[0].Value())
} else {
var buf bytes.Buffer
Expand Down Expand Up @@ -565,12 +565,12 @@ func (s *span) Tracer() opentracing.Tracer {

// LogEvent is part of the opentracing.Span interface. Deprecated.
func (s *span) LogEvent(event string) {
s.LogFields(otlog.String("event", event))
s.LogFields(otlog.String(LogMessageField, event))
}

// LogEventWithPayload is part of the opentracing.Span interface. Deprecated.
func (s *span) LogEventWithPayload(event string, payload interface{}) {
s.LogFields(otlog.String("event", event), otlog.Object("payload", payload))
s.LogFields(otlog.String(LogMessageField, event), otlog.Object("payload", payload))
}

// Log is part of the opentracing.Span interface. Deprecated.
Expand Down

0 comments on commit d3a2605

Please sign in to comment.