Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions logctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
type ContextKey string

const (
opID ContextKey = "request_id"
opMessage ContextKey = "request_message"
operationKey ContextKey = "operation"
opID ContextKey = "request_id"
)

// SetID sets the request ID logged to the context.
Expand All @@ -25,3 +25,18 @@ func GetID(ctx context.Context) string {

return ""
}

// SetOperation sets the operation logged to the context.
func SetOperation(ctx context.Context, operation string) context.Context {
return context.WithValue(ctx, operationKey, operation)
}

// GetOperation returns the operation logged to the context, otherwise an empty string.
func GetOperation(ctx context.Context) string {
s, ok := ctx.Value(operationKey).(string)
if ok {
return s
}

return ""
}
9 changes: 9 additions & 0 deletions logctx/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ func TestID(t *testing.T) {
ctx = logctx.SetID(ctx, "123")
assert.Equal(t, "123", logctx.GetID(ctx))
}

func TestOperation(t *testing.T) {
ctx := context.Background()

assert.Empty(t, logctx.GetOperation(ctx))

ctx = logctx.SetOperation(ctx, "getTest")
assert.Equal(t, "getTest", logctx.GetOperation(ctx))
}