Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/context_baggage #4404

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions core/trace/baggage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package trace

import (
"context"

"github.com/zeromicro/go-zero/core/logc"
"go.opentelemetry.io/otel/baggage"
)

// GetBaggageValue get baggage info from context, if key not exists, return "", false.
func GetBaggageValue(ctx context.Context, key string) (string, bool) {
b := baggage.FromContext(ctx)
m := b.Member(key)

if m.Value() == "" {
return "", false
}

return m.Value(), true
}

// WithBaggage append baggage by string key val.
func WithBaggage(parent context.Context, key, val string) context.Context {
member, err := baggage.NewMember(key, val)
if err != nil {
logc.Error(parent, err)
return parent
}

Check warning on line 28 in core/trace/baggage.go

View check run for this annotation

Codecov / codecov/patch

core/trace/baggage.go#L26-L28

Added lines #L26 - L28 were not covered by tests

b := baggage.FromContext(parent)
b, err = b.SetMember(member)
if err != nil {
logc.Error(parent, err)
return parent
}

Check warning on line 35 in core/trace/baggage.go

View check run for this annotation

Codecov / codecov/patch

core/trace/baggage.go#L33-L35

Added lines #L33 - L35 were not covered by tests

return baggage.ContextWithBaggage(parent, b)
}

// AddBaggagesFromMap append map kvs to current ctx baggage.
func AddBaggagesFromMap(parent context.Context, mp map[string]string) context.Context {
b := baggage.FromContext(parent)

for k, v := range mp {
m, err := baggage.NewMember(k, v)
if err != nil {
logc.Error(parent, err)
return parent
}

Check warning on line 49 in core/trace/baggage.go

View check run for this annotation

Codecov / codecov/patch

core/trace/baggage.go#L47-L49

Added lines #L47 - L49 were not covered by tests

b, err = b.SetMember(m)
if err != nil {
logc.Error(parent, err)
return parent
}

Check warning on line 55 in core/trace/baggage.go

View check run for this annotation

Codecov / codecov/patch

core/trace/baggage.go#L53-L55

Added lines #L53 - L55 were not covered by tests
}

return baggage.ContextWithBaggage(parent, b)
}
74 changes: 74 additions & 0 deletions core/trace/baggage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package trace

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/baggage"
)

func TestAddBaggagesFromMap(t *testing.T) {
ctx := AddBaggagesFromMap(context.Background(), map[string]string{"test": "test", "aaa": "aaa"})
b := baggage.FromContext(ctx)
assert.Equal(t, "test", b.Member("test").Value())
assert.Equal(t, "aaa", b.Member("aaa").Value())
}

func TestGetBaggageValue(t *testing.T) {
ctx := AddBaggagesFromMap(context.Background(), map[string]string{"test": "aaa"})

type args struct {
ctx context.Context
key string
}
tests := []struct {
name string
args args
want string
want1 bool
}{
{
"not exists",
args{
ctx: context.Background(),
key: "test",
},
"",
false,
},
{
"exists",
args{
ctx: ctx,
key: "test",
},
"aaa",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetBaggageValue(tt.args.ctx, tt.args.key)
if got != tt.want {
t.Errorf("GetBaggageValue() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GetBaggageValue() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestWithBaggage(t *testing.T) {
ctx := context.Background()
ctx = WithBaggage(ctx, "aaa", "aaa")
val, ok := GetBaggageValue(ctx, "aaa")
assert.True(t, ok)
assert.Equal(t, "aaa", val)

ctx = WithBaggage(ctx, "aaa", "bbb")
val, ok = GetBaggageValue(ctx, "aaa")
assert.True(t, ok)
assert.Equal(t, "bbb", val)
}