Skip to content

Commit

Permalink
Merge pull request #73 from cdr/opencensus
Browse files Browse the repository at this point in the history
Remove direct coupling of opencensus
  • Loading branch information
nhooyr committed Dec 12, 2019
2 parents 8f021e1 + 8d8b837 commit a44f627
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ go get cdr.dev/slog
- [Stdlib](https://godoc.org/cdr.dev/slog#Stdlib) log adapter
- Skip caller frames with [slog.Helper](https://godoc.org/cdr.dev/slog#Helper)
- Encodes values as if with `json.Marshal`
- Transparently log [opencensus](https://godoc.org/go.opencensus.io/trace) trace and span IDs
- [Single dependency](https://godoc.org/cdr.dev/slog?imports) on go.opencensus.io
- Transparently log [opencensus](https://godoc.org/go.opencensus.io/trace) span's from the passed context
- [Zero dependencies](https://godoc.org/cdr.dev/slog?imports)
- Log to multiple sinks

## Example
Expand Down
8 changes: 4 additions & 4 deletions internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func c(w io.Writer, attrs ...color.Attribute) *color.Color {
// We also do not indent the fields as go's test does that automatically
// for extra lines in a log so if we did it here, the fields would be indented
// twice in test logs. So the Stderr logger indents all the fields itself.
func Fmt(w io.Writer, ent slog.SinkEntry) string {
func Fmt(w io.Writer, ent slog.SinkEntry, sc trace.SpanContext) string {
var ents string
ts := ent.Time.Format(TimeFormat)
ents += ts + " "
Expand Down Expand Up @@ -79,10 +79,10 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
msg = quote(msg)
ents += msg

if ent.SpanContext != (trace.SpanContext{}) {
if sc != (trace.SpanContext{}) {
ent.Fields = append(slog.M(
slog.F("trace", ent.SpanContext.TraceID),
slog.F("span", ent.SpanContext.SpanID),
slog.F("trace", sc.TraceID),
slog.F("span", sc.SpanID),
), ent.Fields...)
}

Expand Down
21 changes: 10 additions & 11 deletions internal/entryhuman/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

var kt = time.Date(2000, time.February, 5, 4, 4, 4, 4, time.UTC)

func TestEntry(t *testing.T) {
func TestFmt(t *testing.T) {
t.Parallel()

test := func(t *testing.T, in slog.SinkEntry, exp string) {
act := entryhuman.Fmt(ioutil.Discard, in)
test := func(t *testing.T, in slog.SinkEntry, sc trace.SpanContext, exp string) {
act := entryhuman.Fmt(ioutil.Discard, in, sc)
assert.Equal(t, exp, act, "entry")
}

Expand All @@ -33,7 +33,7 @@ func TestEntry(t *testing.T) {
File: "myfile",
Line: 100,
Func: "ignored",
}, `2000-02-05 04:04:04.000 [DEBUG] <myfile:100> "wowowow\tizi"`)
}, trace.SpanContext{}, `2000-02-05 04:04:04.000 [DEBUG] <myfile:100> "wowowow\tizi"`)
})

t.Run("multilineMessage", func(t *testing.T) {
Expand All @@ -42,7 +42,7 @@ func TestEntry(t *testing.T) {
test(t, slog.SinkEntry{
Message: "line1\nline2",
Level: slog.LevelInfo,
}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
}, trace.SpanContext{}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
"msg": line1
line2`)
})
Expand All @@ -53,18 +53,17 @@ line2`)
test(t, slog.SinkEntry{
Level: slog.LevelWarn,
LoggerNames: []string{"named", "meow"},
}, `0001-01-01 00:00:00.000 [WARN] (named.meow) <.:0> ""`)
}, trace.SpanContext{}, `0001-01-01 00:00:00.000 [WARN] (named.meow) <.:0> ""`)
})

t.Run("trace", func(t *testing.T) {
t.Parallel()

test(t, slog.SinkEntry{
Level: slog.LevelError,
SpanContext: trace.SpanContext{
SpanID: trace.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
TraceID: trace.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
},
}, trace.SpanContext{
SpanID: trace.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
TraceID: trace.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
}, `0001-01-01 00:00:00.000 [ERROR] <.:0> "" {"trace": "000102030405060708090a0b0c0d0e0f", "span": "0001020304050607"}`)
})

Expand All @@ -76,7 +75,7 @@ line2`)
Fields: slog.M(
slog.F("hey", "hi"),
),
})
}, trace.SpanContext{})
assert.Equal(t, "0001-01-01 00:00:00.000 \x1b[91m[CRITICAL]\x1b[0m\t\x1b[36m<.:0>\x1b[0m\t\"\"\t{\x1b[34m\"hey\"\x1b[0m: \x1b[32m\"hi\"\x1b[0m}", act, "entry")
})
}
13 changes: 4 additions & 9 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"runtime"
"sync"
"time"

"go.opencensus.io/trace"
)

// Sink is the destination of a Logger.
Expand Down Expand Up @@ -147,11 +145,10 @@ func (l Logger) log(ctx context.Context, level Level, msg string, fields Map) {

func (l Logger) entry(ctx context.Context, level Level, msg string, fields Map) SinkEntry {
ent := SinkEntry{
Time: time.Now().UTC(),
Level: level,
Message: msg,
Fields: fieldsFromContext(ctx).append(fields),
SpanContext: trace.FromContext(ctx).SpanContext(),
Time: time.Now().UTC(),
Level: level,
Message: msg,
Fields: fieldsFromContext(ctx).append(fields),
}
ent = ent.fillLoc(l.skip + 3)
return ent
Expand Down Expand Up @@ -275,8 +272,6 @@ type SinkEntry struct {
File string
Line int

SpanContext trace.SpanContext

Fields Map
}

Expand Down
11 changes: 3 additions & 8 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"runtime"
"testing"

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/assert"
)
Expand Down Expand Up @@ -74,7 +72,7 @@ func TestLogger(t *testing.T) {

File: slogTestFile,
Func: "cdr.dev/slog_test.TestLogger.func2",
Line: 66,
Line: 64,

Fields: slog.M(
slog.F("ctx", 1024),
Expand All @@ -90,8 +88,7 @@ func TestLogger(t *testing.T) {
l = l.Named("hello")
l = l.Named("hello2")

ctx, span := trace.StartSpan(bg, "trace")
ctx = slog.With(ctx, slog.F("ctx", io.EOF))
ctx := slog.With(bg, slog.F("ctx", io.EOF))
l = l.With(slog.F("with", 2))

l.Info(ctx, "meow", slog.F("hi", "xd"))
Expand All @@ -107,9 +104,7 @@ func TestLogger(t *testing.T) {

File: slogTestFile,
Func: "cdr.dev/slog_test.TestLogger.func3",
Line: 97,

SpanContext: span.SpanContext(),
Line: 94,

Fields: slog.M(
slog.F("with", 2),
Expand Down
4 changes: 3 additions & 1 deletion sloggers/sloghuman/sloghuman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"strings"

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/internal/syncwriter"
Expand All @@ -30,7 +32,7 @@ type humanSink struct {
}

func (s humanSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
str := entryhuman.Fmt(s.w2, ent)
str := entryhuman.Fmt(s.w2, ent, trace.FromContext(ctx).SpanContext())
lines := strings.Split(str, "\n")

// We need to add 4 spaces before every field line for readability.
Expand Down
7 changes: 4 additions & 3 deletions sloggers/slogjson/slogjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ func (s jsonSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
m = append(m, slog.F("logger_names", ent.LoggerNames))
}

if ent.SpanContext != (trace.SpanContext{}) {
sc := trace.FromContext(ctx).SpanContext()
if sc != (trace.SpanContext{}) {
m = append(m,
slog.F("trace", ent.SpanContext.TraceID),
slog.F("span", ent.SpanContext.SpanID),
slog.F("trace", sc.TraceID),
slog.F("span", sc.SpanID),
)
}

Expand Down
9 changes: 5 additions & 4 deletions sloggers/slogstackdriver/slogstackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ func (s stackdriverSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
}))
}

if ent.SpanContext != (trace.SpanContext{}) {
sc := trace.FromContext(ctx).SpanContext()
if sc != (trace.SpanContext{}) {
e = append(e,
slog.F("logging.googleapis.com/trace", s.traceField(ent.SpanContext.TraceID)),
slog.F("logging.googleapis.com/spanId", ent.SpanContext.SpanID.String()),
slog.F("logging.googleapis.com/trace_sampled", ent.SpanContext.IsSampled()),
slog.F("logging.googleapis.com/trace", s.traceField(sc.TraceID)),
slog.F("logging.googleapis.com/spanId", sc.SpanID.String()),
slog.F("logging.googleapis.com/trace_sampled", sc.IsSampled()),
)
}

Expand Down
4 changes: 3 additions & 1 deletion sloggers/slogtest/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"testing"

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/sloggers/sloghuman"
Expand Down Expand Up @@ -49,7 +51,7 @@ type testSink struct {

func (ts testSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
// The testing package logs to stdout and not stderr.
s := entryhuman.Fmt(os.Stdout, ent)
s := entryhuman.Fmt(os.Stdout, ent, trace.FromContext(ctx).SpanContext())

switch ent.Level {
case slog.LevelDebug, slog.LevelInfo, slog.LevelWarn:
Expand Down

0 comments on commit a44f627

Please sign in to comment.