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

filters/tracing: improve stateBagToTag #2775

Merged
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
22 changes: 14 additions & 8 deletions filters/tracing/statebagtotag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (stateBagToTagSpec) Name() string {
}

func (stateBagToTagSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) < 1 {
if len(args) < 1 || len(args) > 2 {
return nil, filters.ErrInvalidFilterParameters
}

Expand All @@ -43,7 +43,7 @@ func (stateBagToTagSpec) CreateFilter(args []interface{}) (filters.Filter, error
tagName = tagNameArg
}

return stateBagToTagFilter{
return &stateBagToTagFilter{
stateBagItemName: stateBagItemName,
tagName: tagName,
}, nil
Expand All @@ -53,16 +53,22 @@ func NewStateBagToTag() filters.Spec {
return stateBagToTagSpec{}
}

func (f stateBagToTagFilter) Request(ctx filters.FilterContext) {
func (f *stateBagToTagFilter) Request(ctx filters.FilterContext) {
value, ok := ctx.StateBag()[f.stateBagItemName]
if !ok {
return
}

span := opentracing.SpanFromContext(ctx.Request().Context())
if span == nil {
return
}
value, ok := ctx.StateBag()[f.stateBagItemName]
if !ok {
return

if _, ok := value.(string); ok {
span.SetTag(f.tagName, value)
Comment on lines +67 to +68
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See golang/go#64541 why not

if s, ok := value.(string); ok {
    span.SetTag(f.tagName, s)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we have similar cases all over the place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked all span.SetTag callsites and its fine. There may be other places that receive interface{} like SetTag of course but I think this is a very specific pattern here - optimizing for string case.

We could simply use span.SetTag(f.tagName, value) without type assertion but that is not guaranteed to work for types other than string, bool and ints:

// Adds a tag to the span.
	//
	// If there is a pre-existing tag set for `key`, it is overwritten.
	//
	// Tag values can be numeric types, strings, or bools. The behavior of
	// other tag value types is undefined at the OpenTracing level. If a
	// tracing system does not know how to handle a particular value type, it
	// may ignore the tag, but shall not panic.
	//
	// Returns a reference to this Span for chaining.
	SetTag(key string, value interface{}) Span

so I decided to keep fmt.Sprint()

} else {
span.SetTag(f.tagName, fmt.Sprint(value))
}
span.SetTag(f.tagName, fmt.Sprint(value))
}

func (stateBagToTagFilter) Response(ctx filters.FilterContext) {}
func (*stateBagToTagFilter) Response(ctx filters.FilterContext) {}
29 changes: 28 additions & 1 deletion filters/tracing/statebagtotag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,44 @@ func TestStateBagToTag_CreateFilter(t *testing.T) {
args: []interface{}{""},
err: filters.ErrInvalidFilterParameters,
},
{
msg: "too many args",
args: []interface{}{"foo", "bar", "baz"},
err: filters.ErrInvalidFilterParameters,
},
} {
t.Run(ti.msg, func(t *testing.T) {
f, err := NewStateBagToTag().CreateFilter(ti.args)

assert.Equal(t, ti.err, err)
if err == nil {
ff := f.(stateBagToTagFilter)
ff := f.(*stateBagToTagFilter)

assert.Equal(t, ti.stateBag, ff.stateBagItemName)
assert.Equal(t, ti.tag, ff.tagName)
}
})
}
}

func BenchmarkStateBagToTag_StringValue(b *testing.B) {
f, err := NewStateBagToTag().CreateFilter([]interface{}{"item", "tag"})
require.NoError(b, err)

span := tracingtest.NewSpan("start_span")

req := &http.Request{Header: http.Header{}}
req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))

ctx := &filtertest.Context{FRequest: req, FStateBag: map[string]interface{}{"item": "val"}}
f.Request(ctx)

require.Equal(b, "val", span.Tags["tag"])

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
f.Request(ctx)
}
}