-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactored packages: middlewares and exporters now at the top level
* added unit tests for the log and tracer packages * fixed a few issues with propagating fields in traces * added more options to expose the full set of zap settings Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
- Loading branch information
Showing
32 changed files
with
792 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Package amplitude exposes an exporter to route | ||
// span traces to the Amplitude API. | ||
// span traces to the Amplitude API, with some filteting. | ||
// | ||
// This is experimental and not optimized for production usage. | ||
package amplitude |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package log | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
func TestMustGetLogger(t *testing.T) { | ||
const appName = "my_app" | ||
observed, observedLogs := observer.New(zapcore.DebugLevel) | ||
|
||
zlog, closer := MustGetLogger( | ||
appName, | ||
WithZapOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { | ||
// forwards logged entries to an observable sink | ||
return zapcore.NewTee(c, observed) | ||
})), | ||
) | ||
defer closer() | ||
|
||
zlog.Info("test", zap.String("context", "x")) | ||
|
||
entries := observedLogs.All() | ||
require.Len(t, entries, 1) | ||
entry := entries[0] | ||
|
||
require.Equal(t, zapcore.InfoLevel, entry.Level) | ||
require.NotEmpty(t, entry.Time) | ||
require.Equal(t, appName, entry.LoggerName) | ||
require.Equal(t, "test", entry.Message) | ||
require.Len(t, entry.Context, 1) | ||
} | ||
|
||
var testMux sync.Mutex | ||
|
||
func TestMustGetTestLogger(t *testing.T) { | ||
testMux.Lock() | ||
defer testMux.Unlock() | ||
|
||
t.Run("with no env", mustGetTestLogger("")) | ||
|
||
t.Run("with env", mustGetTestLogger("1")) | ||
} | ||
|
||
func mustGetTestLogger(env string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
t.Setenv("DEBUG_TEST", env) | ||
|
||
observed, observedLogs := observer.New(zapcore.DebugLevel) | ||
|
||
zlf, zlg := MustGetTestLoggerConfig( | ||
WithZapOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { | ||
return zapcore.NewTee(c, observed) | ||
})), | ||
) | ||
require.NotNil(t, zlf) | ||
require.NotNil(t, zlg) | ||
|
||
l := zlf.Bg() | ||
l.Info("test") | ||
|
||
entries := observedLogs.All() | ||
if env == "" { | ||
require.Len(t, entries, 0) | ||
|
||
return | ||
} | ||
|
||
require.Len(t, entries, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package log_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/fredbi/go-trace/log" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
func ExampleMustGetLogger() { | ||
const appName = "my_app" | ||
|
||
zlog, closer := log.MustGetLogger( | ||
appName, | ||
log.WithLevel("debug"), | ||
log.WithOutput(log.Stdout), | ||
) | ||
|
||
zlog.Debug("test") | ||
defer closer() | ||
} | ||
|
||
func ExampleNewFactory() { | ||
ctx := context.Background() | ||
zlg, closer := log.MustGetLogger("root") // builds a named zap logger with sensible defaults | ||
defer closer() | ||
|
||
lgf := log.NewFactory(zlg) // builds a logger with trace propagation | ||
|
||
ctx, span := trace.StartSpan(ctx, "span name") | ||
defer span.End() | ||
lg := lgf.For(ctx) | ||
|
||
lg.Info("log propagated as a trace span") | ||
} | ||
|
||
func ExampleMustGetTestLoggerConfig() { | ||
os.Setenv("DEBUG_TEST", "1") | ||
zlf, zlg := log.MustGetTestLoggerConfig() | ||
|
||
zlg.Info("this is a logger visible only when DEBUG_TEST is set, e.g. for local testing") | ||
zlf.Bg().Info("this is a logger factory only visble when DEBUG_TEST is set, e.g. for local testing") | ||
} |
Oops, something went wrong.