Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wbrowne committed Aug 16, 2024
1 parent 5af317a commit 11ae6e4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func withContextualLogAttributes(ctx context.Context, pCtx PluginContext) contex
}
}

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

Expand Down
7 changes: 7 additions & 0 deletions backend/log/context_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func WithContextualAttributesForOutgoingContext(ctx context.Context, logParams [
for i := 0; i < len(logParams); i += 2 {
k := logParams[i].(string)
v := logParams[i+1].(string)
if k == "" || v == "" {
continue
}

ctx = metadata.AppendToOutgoingContext(ctx, logParamsCtxMetadataKey, fmt.Sprintf("%s%s%s", k, logParamSeparator, v))
}

Expand All @@ -38,6 +42,9 @@ func ContextualAttributesFromIncomingContext(ctx context.Context) []any {
var attrs []any
for _, param := range logParams {
kv := strings.Split(param, logParamSeparator)
if len(kv) != 2 {
continue
}
attrs = append(attrs, kv[0], kv[1])
}
return attrs
Expand Down
107 changes: 107 additions & 0 deletions backend/log/context_grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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{"key1:value1"},
},
{
name: "log params with multiple key value pairs",
logParams: []any{"key1", "value1", "key2", "value2"},
expected: []string{"key1:value1", "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{"key1:value1", "key2:value2"}},
expected: []any{"key1", "value1", "key2", "value2"},
},
{
name: "metadata with invalid log params",
md: metadata.MD{logParamsCtxMetadataKey: []string{"key1", "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 11ae6e4

Please sign in to comment.