Skip to content

Commit

Permalink
remove 1 level of recursion from the context metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
shulin-sq committed Jun 13, 2024
1 parent 243b5f4 commit 573f147
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
24 changes: 12 additions & 12 deletions pkg/utils/gwlog/gwlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,91 +20,91 @@ func (s *TracedLogger) Infoln(args ...interface{}) {

func (t *TracedLogger) Infow(ctx context.Context, msg string, keysAndValues ...interface{}) {
if GetTrace(ctx) != "" {
keysAndValues = append(keysAndValues, string(traceID), GetTrace(ctx))
keysAndValues = append(keysAndValues, traceID, GetTrace(ctx))
}
t.InnerLogger.Infow(msg, keysAndValues...)
}

func (t *TracedLogger) Infof(ctx context.Context, template string, args ...interface{}) {
if GetTrace(ctx) != "" {
t.InnerLogger.Infow(fmt.Sprintf(template, args...), string(traceID), GetTrace(ctx))
t.InnerLogger.Infow(fmt.Sprintf(template, args...), traceID, GetTrace(ctx))
return
}
t.InnerLogger.Infof(template, args...)
}

func (t *TracedLogger) Info(ctx context.Context, msg string) {
if GetTrace(ctx) != "" {
t.InnerLogger.Infow(msg, string(traceID), GetTrace(ctx))
t.InnerLogger.Infow(msg, traceID, GetTrace(ctx))
return
}
t.InnerLogger.Info(msg)
}

func (t *TracedLogger) Errorw(ctx context.Context, msg string, keysAndValues ...interface{}) {
if GetTrace(ctx) != "" {
keysAndValues = append(keysAndValues, string(traceID), GetTrace(ctx))
keysAndValues = append(keysAndValues, traceID, GetTrace(ctx))
}
t.InnerLogger.Errorw(msg, keysAndValues)
}

func (t *TracedLogger) Errorf(ctx context.Context, template string, args ...interface{}) {
if GetTrace(ctx) != "" {
t.InnerLogger.Errorw(fmt.Sprintf(template, args...), string(traceID), GetTrace(ctx))
t.InnerLogger.Errorw(fmt.Sprintf(template, args...), traceID, GetTrace(ctx))
return
}
t.InnerLogger.Errorf(template, args...)
}

func (t *TracedLogger) Error(ctx context.Context, msg string) {
if GetTrace(ctx) != "" {
t.InnerLogger.Errorw(msg, string(traceID), GetTrace(ctx))
t.InnerLogger.Errorw(msg, traceID, GetTrace(ctx))
return
}
t.InnerLogger.Error(msg)
}

func (t *TracedLogger) Debugw(ctx context.Context, msg string, keysAndValues ...interface{}) {
if GetTrace(ctx) != "" {
keysAndValues = append(keysAndValues, string(traceID), GetTrace(ctx))
keysAndValues = append(keysAndValues, traceID, GetTrace(ctx))
}
t.InnerLogger.Debugw(msg, keysAndValues...)
}

func (t *TracedLogger) Debugf(ctx context.Context, template string, args ...interface{}) {
if GetTrace(ctx) != "" {
t.InnerLogger.Debugw(fmt.Sprintf(template, args...), string(traceID), GetTrace(ctx))
t.InnerLogger.Debugw(fmt.Sprintf(template, args...), traceID, GetTrace(ctx))
return
}
t.InnerLogger.Debugf(template, args...)
}

func (t *TracedLogger) Debug(ctx context.Context, msg string) {
if GetTrace(ctx) != "" {
t.InnerLogger.Debugw(msg, string(traceID), GetTrace(ctx))
t.InnerLogger.Debugw(msg, traceID, GetTrace(ctx))
return
}
t.InnerLogger.Debug(msg)
}

func (t *TracedLogger) Warnw(ctx context.Context, msg string, keysAndValues ...interface{}) {
if GetTrace(ctx) != "" {
keysAndValues = append(keysAndValues, string(traceID), GetTrace(ctx))
keysAndValues = append(keysAndValues, traceID, GetTrace(ctx))
}
t.InnerLogger.Warnw(msg, keysAndValues...)
}

func (t *TracedLogger) Warnf(ctx context.Context, template string, args ...interface{}) {
if GetTrace(ctx) != "" {
t.InnerLogger.Warnw(fmt.Sprintf(template, args...), string(traceID), GetTrace(ctx))
t.InnerLogger.Warnw(fmt.Sprintf(template, args...), traceID, GetTrace(ctx))
return
}
t.InnerLogger.Warnf(template, args...)
}

func (t *TracedLogger) Warn(ctx context.Context, msg string) {
if GetTrace(ctx) != "" {
t.InnerLogger.Warnw(msg, string(traceID), GetTrace(ctx))
t.InnerLogger.Warnw(msg, traceID, GetTrace(ctx))
return
}
t.InnerLogger.Warn(msg)
Expand Down
27 changes: 15 additions & 12 deletions pkg/utils/gwlog/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

type key string

const traceID key = "trace_id"
const metadata key = "metadata"

const traceID string = "trace_id"

type metadataValue struct {
m map[string]string
}
Expand All @@ -30,7 +31,11 @@ func newMetadata() *metadataValue {

func NewTrace(ctx context.Context) context.Context {
currID := uuid.New()
return context.WithValue(context.WithValue(ctx, traceID, currID.String()), metadata, newMetadata())

newCtx := context.WithValue(ctx, metadata, newMetadata())
AddMetadata(newCtx, traceID, currID.String())

return newCtx
}

func AddMetadata(ctx context.Context, key, value string) {
Expand All @@ -41,12 +46,7 @@ func AddMetadata(ctx context.Context, key, value string) {

func GetMetadata(ctx context.Context) []interface{} {
var fields []interface{}
/*
if ctx.Value(traceID) != nil {
fields = append(fields, string(traceID))
fields = append(fields, ctx.Value(traceID))
}
*/

if ctx.Value(metadata) != nil {
for k, v := range ctx.Value(metadata).(*metadataValue).m {
fields = append(fields, k)
Expand All @@ -57,9 +57,12 @@ func GetMetadata(ctx context.Context) []interface{} {
}

func GetTrace(ctx context.Context) string {
t := ctx.Value(traceID)
if t == nil {
return ""
if ctx.Value(metadata) != nil {
m := ctx.Value(metadata).(*metadataValue).m
if m == nil {
return ""
}
return ctx.Value(metadata).(*metadataValue).m[traceID]
}
return t.(string)
return ""
}
32 changes: 32 additions & 0 deletions pkg/utils/gwlog/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gwlog

import (
"context"
"fmt"
"testing"
)

func TestGetTrace(t *testing.T) {
if GetTrace(context.TODO()) != "" {
t.Errorf("expected context with no trace_id to return empty string")
}

if GetTrace(NewTrace(context.TODO())) == "" {
t.Errorf("expected context with trace_id to return non-empty string")
}
}

func TestMetadata(t *testing.T) {
ctx := NewTrace(context.TODO())
AddMetadata(ctx, "foo", "bar")

md := GetMetadata(ctx)
mdMap := map[string]bool{}
for _, m := range md {
mdMap[fmt.Sprint(m)] = true
}

if !mdMap["foo"] || !mdMap["bar"] {
t.Errorf("expected context to have metadata with key foo and val bar, got %s", md)
}
}

0 comments on commit 573f147

Please sign in to comment.