Skip to content

Commit

Permalink
Revert "Merge pull request #73 from cdr/opencensus"
Browse files Browse the repository at this point in the history
This reverts commit a44f627, reversing
changes made to 8f021e1.
  • Loading branch information
nhooyr committed Dec 12, 2019
1 parent a44f627 commit 97798d6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 38 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) span's from the passed context
- [Zero dependencies](https://godoc.org/cdr.dev/slog?imports)
- 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
- 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, sc trace.SpanContext) string {
func Fmt(w io.Writer, ent slog.SinkEntry) 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, sc trace.SpanContext) string {
msg = quote(msg)
ents += msg

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

Expand Down
21 changes: 11 additions & 10 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 TestFmt(t *testing.T) {
func TestEntry(t *testing.T) {
t.Parallel()

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

Expand All @@ -33,7 +33,7 @@ func TestFmt(t *testing.T) {
File: "myfile",
Line: 100,
Func: "ignored",
}, trace.SpanContext{}, `2000-02-05 04:04:04.000 [DEBUG] <myfile:100> "wowowow\tizi"`)
}, `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 TestFmt(t *testing.T) {
test(t, slog.SinkEntry{
Message: "line1\nline2",
Level: slog.LevelInfo,
}, trace.SpanContext{}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
"msg": line1
line2`)
})
Expand All @@ -53,17 +53,18 @@ line2`)
test(t, slog.SinkEntry{
Level: slog.LevelWarn,
LoggerNames: []string{"named", "meow"},
}, trace.SpanContext{}, `0001-01-01 00:00:00.000 [WARN] (named.meow) <.:0> ""`)
}, `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,
}, 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},
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},
},
}, `0001-01-01 00:00:00.000 [ERROR] <.:0> "" {"trace": "000102030405060708090a0b0c0d0e0f", "span": "0001020304050607"}`)
})

Expand All @@ -75,7 +76,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: 9 additions & 4 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"runtime"
"sync"
"time"

"go.opencensus.io/trace"
)

// Sink is the destination of a Logger.
Expand Down Expand Up @@ -145,10 +147,11 @@ 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),
Time: time.Now().UTC(),
Level: level,
Message: msg,
Fields: fieldsFromContext(ctx).append(fields),
SpanContext: trace.FromContext(ctx).SpanContext(),
}
ent = ent.fillLoc(l.skip + 3)
return ent
Expand Down Expand Up @@ -272,6 +275,8 @@ type SinkEntry struct {
File string
Line int

SpanContext trace.SpanContext

Fields Map
}

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

"go.opencensus.io/trace"

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

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

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

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

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

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

SpanContext: span.SpanContext(),

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

"go.opencensus.io/trace"

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

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

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

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

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

sc := trace.FromContext(ctx).SpanContext()
if sc != (trace.SpanContext{}) {
if ent.SpanContext != (trace.SpanContext{}) {
e = append(e,
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()),
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()),
)
}

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

"go.opencensus.io/trace"

"cdr.dev/slog"
"cdr.dev/slog/internal/entryhuman"
"cdr.dev/slog/sloggers/sloghuman"
Expand Down Expand Up @@ -51,7 +49,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, trace.FromContext(ctx).SpanContext())
s := entryhuman.Fmt(os.Stdout, ent)

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

0 comments on commit 97798d6

Please sign in to comment.