Skip to content

Commit

Permalink
pkg: pass in logger object when create trace object
Browse files Browse the repository at this point in the history
  • Loading branch information
YoyinZyc committed Sep 26, 2019
1 parent 8d3e63f commit 79a7a0d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
3 changes: 2 additions & 1 deletion etcdserver/v3_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Authenticator interface {

func (s *EtcdServer) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
trace := traceutil.New("range",
s.getLogger(),
traceutil.Field{Key: "range_begin", Value: string(r.Key)},
traceutil.Field{Key: "range_end", Value: string(r.RangeEnd)},
)
Expand All @@ -102,7 +103,7 @@ func (s *EtcdServer) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeRe
traceutil.Field{Key: "response_revision", Value: resp.Header.Revision},
)
}
trace.LogIfLong(rangeTraceThreshold, s.getLogger())
trace.LogIfLong(rangeTraceThreshold)
}(time.Now())

if !r.Serializable {
Expand Down
24 changes: 12 additions & 12 deletions pkg/traceutil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func writeFields(fields []Field) string {

type Trace struct {
operation string
lg *zap.Logger
fields []Field
startTime time.Time
steps []step
Expand All @@ -60,8 +61,8 @@ type step struct {
fields []Field
}

func New(op string, fields ...Field) *Trace {
return &Trace{operation: op, startTime: time.Now(), fields: fields}
func New(op string, lg *zap.Logger, fields ...Field) *Trace {
return &Trace{operation: op, lg: lg, startTime: time.Now(), fields: fields}
}

// TODO returns a non-nil, empty Trace
Expand All @@ -76,11 +77,10 @@ func Get(ctx context.Context) *Trace {
return TODO()
}

func GetOrCreate(ctx context.Context, op string, fields ...Field) (context.Context, *Trace) {
func GetOrCreate(ctx context.Context, op string, lg *zap.Logger, fields ...Field) (context.Context, *Trace) {
trace, ok := ctx.Value("trace").(*Trace)
if !ok || trace == nil {
trace = New(op)
trace.fields = fields
trace = New(op, lg, fields...)
ctx = context.WithValue(ctx, "trace", trace)
}
return ctx, trace
Expand All @@ -97,23 +97,23 @@ func (t *Trace) AddField(fields ...Field) {
}

// Log dumps all steps in the Trace
func (t *Trace) Log(lg *zap.Logger) {
t.LogWithStepThreshold(0, lg)
func (t *Trace) Log() {
t.LogWithStepThreshold(0)
}

// LogIfLong dumps logs if the duration is longer than threshold
func (t *Trace) LogIfLong(threshold time.Duration, lg *zap.Logger) {
func (t *Trace) LogIfLong(threshold time.Duration) {
if time.Since(t.startTime) > threshold {
stepThreshold := threshold / time.Duration(len(t.steps)+1)
t.LogWithStepThreshold(stepThreshold, lg)
t.LogWithStepThreshold(stepThreshold)
}
}

// LogWithStepThreshold only dumps step whose duration is longer than step threshold
func (t *Trace) LogWithStepThreshold(threshold time.Duration, lg *zap.Logger) {
func (t *Trace) LogWithStepThreshold(threshold time.Duration) {
msg, fs := t.logInfo(threshold)
if lg != nil {
lg.Info(msg, fs...)
if t.lg != nil {
t.lg.Info(msg, fs...)
}
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/traceutil/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestGetOrCreate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, trace := GetOrCreate(tt.inputCtx, "test")
ctx, trace := GetOrCreate(tt.inputCtx, "test", nil)
if trace == nil {
t.Errorf("Expected trace object; Got nil")
} else if trace.operation != tt.outputTraceOp {
Expand All @@ -106,7 +106,7 @@ func TestCreate(t *testing.T) {
}
)

trace := New(op, fields[0], fields[1])
trace := New(op, nil, fields[0], fields[1])
if trace.operation != op {
t.Errorf("Expected %v; Got %v", op, trace.operation)
}
Expand Down Expand Up @@ -202,7 +202,8 @@ func TestLog(t *testing.T) {
for _, f := range tt.fields {
tt.trace.AddField(f)
}
tt.trace.Log(lg)
tt.trace.lg = lg
tt.trace.Log()
data, err := ioutil.ReadFile(logPath)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -279,7 +280,8 @@ func TestLogIfLong(t *testing.T) {
lcfg.ErrorOutputPaths = []string{logPath}
lg, _ := lcfg.Build()

tt.trace.LogIfLong(tt.threshold, lg)
tt.trace.lg = lg
tt.trace.LogIfLong(tt.threshold)
data, err := ioutil.ReadFile(logPath)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 79a7a0d

Please sign in to comment.