Skip to content

Commit

Permalink
filters/tracing: improve stateBagToTag
Browse files Browse the repository at this point in the history
* reduce allocations when tag value is string
* check statebag value first to bail out early if it is absent
* restrict max number of arguments to two

```
goos: linux
goarch: amd64
pkg: github.com/zalando/skipper/filters/tracing
cpu: Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
                            │   HEAD~1    │                HEAD                 │
                            │   sec/op    │   sec/op     vs base                │
StateBagToTag_StringValue-8   313.1n ± 2%   124.3n ± 1%  -60.29% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                │
                            │    B/op    │   B/op     vs base                 │
StateBagToTag_StringValue-8   19.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)

                            │   HEAD~1   │                HEAD                 │
                            │ allocs/op  │ allocs/op   vs base                 │
StateBagToTag_StringValue-8   2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)
```

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov committed Dec 4, 2023
1 parent eabe441 commit 8deb1eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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)
} else {
span.SetTag(f.tagName, fmt.Sprint(value))
}
span.SetTag(f.tagName, fmt.Sprint(value))
}

func (stateBagToTagFilter) Response(ctx filters.FilterContext) {}
func (f *stateBagToTagFilter) Response(ctx filters.FilterContext) {}
7 changes: 6 additions & 1 deletion filters/tracing/statebagtotag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ 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)
Expand Down

0 comments on commit 8deb1eb

Please sign in to comment.