Skip to content

Commit

Permalink
Capture log params from context metadata (#1049)
Browse files Browse the repository at this point in the history
* capture log params from context metadata

* refactor

* tidy

* fix lint error

* update const naming

* add tests

* add more test cases

* helper func

* fix

* address comments
  • Loading branch information
wbrowne authored Aug 19, 2024
1 parent 359931f commit a1c7fd2
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backend/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func withContextualLogAttributes(ctx context.Context, pCtx PluginContext) contex
args = append(args, "uname", pCtx.User.Name)
}
}

if ctxLogAttributes := log.ContextualAttributesFromIncomingContext(ctx); len(ctxLogAttributes) > 0 {
args = append(args, ctxLogAttributes...)
}

ctx = log.WithContextualAttributes(ctx, args)
return ctx
}
55 changes: 55 additions & 0 deletions backend/log/context_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package log

import (
"context"
"fmt"
"strings"

"google.golang.org/grpc/metadata"
)

const (
logParamsCtxMetadataKey = "loggerParamsCtxMetadata"
logParamSeparator = ":::"
)

// WithContextualAttributesForOutgoingContext will append the given key/value log parameters to the outgoing context.
func WithContextualAttributesForOutgoingContext(ctx context.Context, logParams []any) context.Context {
if len(logParams) == 0 || len(logParams)%2 != 0 {
return ctx
}

for i := 0; i < len(logParams); i += 2 {
k := fmt.Sprintf("%v", logParams[i])
v := fmt.Sprintf("%v", logParams[i+1])
if k == "" || v == "" {
continue
}

ctx = metadata.AppendToOutgoingContext(ctx, logParamsCtxMetadataKey, logParam(k, v))
}

return ctx
}

// ContextualAttributesFromIncomingContext returns the contextual key/value log parameters from the given incoming context.
func ContextualAttributesFromIncomingContext(ctx context.Context) []any {
logParams := metadata.ValueFromIncomingContext(ctx, logParamsCtxMetadataKey)
if len(logParams) == 0 {
return nil
}

var attrs []any
for _, param := range logParams {
kv := strings.Split(param, logParamSeparator)
if len(kv) != 2 || kv[0] == "" || kv[1] == "" {
continue
}
attrs = append(attrs, kv[0], kv[1])
}
return attrs
}

func logParam(k, v string) string {
return fmt.Sprintf("%s%s%s", k, logParamSeparator, v)
}
117 changes: 117 additions & 0 deletions backend/log/context_grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package log

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
)

func TestWithContextualAttributesForOutgoingContext(t *testing.T) {
tcs := []struct {
name string
logParams []any
expected []string
}{
{
name: "empty log params",
logParams: []any{},
expected: []string{},
},
{
name: "log params with odd number of elements",
logParams: []any{"key1", "value1", "key2"},
expected: []string{},
},
{
name: "log params with empty key",
logParams: []any{"", "value1"},
expected: []string{},
},
{
name: "log params with empty value",
logParams: []any{"key1", ""},
expected: []string{},
},
{
name: "log params with valid key and value",
logParams: []any{"key1", "value1"},
expected: []string{logParam("key1", "value1")},
},
{
name: "log params with multiple key value pairs",
logParams: []any{"key1", "value1", "key2", "value2"},
expected: []string{logParam("key1", "value1"), logParam("key2", "value2")},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := WithContextualAttributesForOutgoingContext(context.Background(), tc.logParams)
md, ok := metadata.FromOutgoingContext(ctx)
if len(tc.expected) == 0 {
require.False(t, ok)
return
}

require.True(t, ok)
got := md.Get(logParamsCtxMetadataKey)
if len(got) != len(tc.expected) {
t.Fatalf("expected %v, got %v", tc.expected, got)
}
for i := range got {
if got[i] != tc.expected[i] {
t.Fatalf("expected %v, got %v", tc.expected, got)
}
}
})
}
}

func TestContextualAttributesFromIncomingContext(t *testing.T) {
tcs := []struct {
name string
md metadata.MD
expected []any
}{
{
name: "empty metadata",
md: metadata.MD{},
expected: nil,
},
{
name: "metadata without log params",
md: metadata.MD{"key1": []string{"value1"}},
expected: nil,
},
{
name: "metadata with valid log params",
md: metadata.MD{logParamsCtxMetadataKey: []string{logParam("key1", "value1"), logParam("key2", "value2")}},
expected: []any{"key1", "value1", "key2", "value2"},
},
{
name: "metadata with missing key",
md: metadata.MD{logParamsCtxMetadataKey: []string{logParam("", "value1"), logParam("key2", "value2")}},
expected: []any{"key2", "value2"},
},
{
name: "metadata with missing value",
md: metadata.MD{logParamsCtxMetadataKey: []string{logParam("key1", ""), logParam("key2", "value2")}},
expected: []any{"key2", "value2"},
},
{
name: "metadata with invalid key + value",
md: metadata.MD{logParamsCtxMetadataKey: []string{logParam("", ""), logParam("key2", "value2")}},
expected: []any{"key2", "value2"},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
ctx := metadata.NewIncomingContext(context.Background(), tc.md)
got := ContextualAttributesFromIncomingContext(ctx)
require.Equal(t, tc.expected, got)
})
}
}

0 comments on commit a1c7fd2

Please sign in to comment.