Skip to content

Commit

Permalink
Merge pull request #16 from asecurityteam/setting-to-omit-path
Browse files Browse the repository at this point in the history
allow users of lib to omit 'path' tag
  • Loading branch information
mikerott authored Jan 4, 2024
2 parents 8838223 + ad89ecd commit e7e2d4c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
20 changes: 12 additions & 8 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type MetricsConfig struct {
PutIdle string `description:"Name of idle connection return count metric."`
BackendTag string `description:"Name of the tag containing the backend reference."`
PathTag string `description:"Name of the tag containing the path reference."`
Backend string `description:"Static value for the backend metric."`
Path string `description:"Static value for the path metric. If not specified, will be generated for each request."`
Backend string `description:"Static value for the backend tag metric."`
Path string `description:"Static value for the path tag metric. If not specified, will be generated for each request."`
OmitPathTag bool `description:"Boolean to omit path tag metric. Omitting the path tag may be desirable to avoid cardinality explosions for paths that vary greatly."`
}

// Name of the config root.
Expand Down Expand Up @@ -59,6 +60,7 @@ func (*MetricsComponent) Settings() *MetricsConfig {
PathTag: "client_path",
Backend: "",
Path: "",
OmitPathTag: false,
}
}

Expand Down Expand Up @@ -93,12 +95,14 @@ func (c *MetricsComponent) New(_ context.Context, conf *MetricsConfig) (func(htt
statstransport.TransportOptionTag(conf.BackendTag, conf.Backend),
}

if conf.Path == "" {
options = append(options, statstransport.TransportOptionRequestTag(func(r *http.Request) (string, string) {
return conf.PathTag, r.URL.EscapedPath()
}))
} else {
options = append(options, statstransport.TransportOptionTag(conf.PathTag, conf.Path))
if !conf.OmitPathTag {
if conf.Path == "" {
options = append(options, statstransport.TransportOptionRequestTag(func(r *http.Request) (string, string) {
return conf.PathTag, r.URL.EscapedPath()
}))
} else {
options = append(options, statstransport.TransportOptionTag(conf.PathTag, conf.Path))
}
}

return statstransport.NewTransport(options...), nil
Expand Down
85 changes: 80 additions & 5 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package httpstats

import (
"context"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"time"

"github.com/asecurityteam/settings"
"github.com/rs/xstats"
"github.com/stretchr/testify/assert"
)

const (
testdependency = "testdependency"
)

func TestMetricsConfigName(t *testing.T) {
Config := MetricsConfig{}
assert.Equal(t, "metrics", Config.Name())
Expand All @@ -28,7 +36,7 @@ func TestMetricsComponentNew(t *testing.T) {
func TestNew(t *testing.T) {
src := settings.NewMapSource(map[string]interface{}{
"metrics": map[string]interface{}{
"backend": "testdependency",
"backend": testdependency,
},
})
wrapper, err := New(context.Background(), src)
Expand All @@ -37,7 +45,7 @@ func TestNew(t *testing.T) {

tr := wrapper(http.DefaultTransport)
assert.Equal(
t, "client_dependency:testdependency",
t, fmt.Sprintf("client_dependency:%s", testdependency),
reflect.Indirect(reflect.ValueOf(tr)).FieldByName("tags").Index(0).String(),
)
}
Expand All @@ -54,27 +62,94 @@ func TestMetricsComponentNewNoDependencyError(t *testing.T) {
func TestMetricsComponentNewStaticPath(t *testing.T) {
cmp := NewComponent()
conf := cmp.Settings()
conf.Backend = "testdependency"
conf.Backend = testdependency
conf.Path = "/foo"
wrapper, err := cmp.New(context.Background(), conf)
assert.Nil(t, err)
assert.IsType(t, wrapper, func(next http.RoundTripper) http.RoundTripper { return nil }, wrapper)

tr := wrapper(http.DefaultTransport)
tags := reflect.Indirect(reflect.ValueOf(tr)).FieldByName("tags")
for i := 0; i < tags.Len(); i++ {
assert.Contains(t, []string{fmt.Sprintf("client_dependency:%s", testdependency), "client_path:/foo"}, tags.Index(i).String())
}
}

func TestMetricsComponentNonStaticPath(t *testing.T) {
cmp := NewComponent()
conf := cmp.Settings()
conf.Backend = testdependency
wrapper, err := cmp.New(context.Background(), conf)
assert.Nil(t, err)
assert.IsType(t, wrapper, func(next http.RoundTripper) http.RoundTripper { return nil }, wrapper)

tr := wrapper(http.DefaultTransport)

request := http.Request{URL: &url.URL{Path: `/some/random/path`}}

xStater := XStater{}
xStaterContext := xstats.NewContext(context.Background(), &xStater)

request = *request.WithContext(xStaterContext)

_, _ = tr.RoundTrip(&request)

expectedTags := []string{
"client_path:/some/random/path",
fmt.Sprintf("client_dependency:%s", testdependency),
"method:",
"status_code:502",
"status:error",
}

assert.Equal(t, len(expectedTags), len(xStater.TimingTags))

for i := 0; i < len(expectedTags); i++ {
assert.Contains(t, expectedTags, xStater.TimingTags[i])
}
}

func TestMetricsComponentNewStaticPathOmitPath(t *testing.T) {
cmp := NewComponent()
conf := cmp.Settings()
conf.Backend = testdependency
conf.Path = "/foo"
conf.OmitPathTag = true
wrapper, err := cmp.New(context.Background(), conf)
assert.Nil(t, err)
assert.IsType(t, wrapper, func(next http.RoundTripper) http.RoundTripper { return nil }, wrapper)

tr := wrapper(http.DefaultTransport)
tags := reflect.Indirect(reflect.ValueOf(tr)).FieldByName("tags")
for i := 0; i < tags.Len(); i++ {
assert.Contains(t, []string{"client_dependency:testdependency", "client_path:/foo"}, tags.Index(i).String())
assert.Contains(t, []string{fmt.Sprintf("client_dependency:%s", testdependency)}, tags.Index(i).String())
}
}

func TestMetricsComponentNewNoPathGiven(t *testing.T) {
cmp := NewComponent()
conf := cmp.Settings()
conf.Backend = "testdependency"
conf.Backend = testdependency
wrapper, err := cmp.New(context.Background(), conf)
assert.Nil(t, err)
assert.IsType(t, wrapper, func(next http.RoundTripper) http.RoundTripper { return nil }, wrapper)

tr := wrapper(http.DefaultTransport)
assert.Equal(t, 1, reflect.Indirect(reflect.ValueOf(tr)).FieldByName("requestTaggers").Len())
}

// XStater a rudimentary recorder of emitted stats things. Add recordings for the stat name and values if you need them
type XStater struct {
AddTagsTags []string
CountTags []string
GaugeTags []string
HistogramTags []string
TimingTags []string
}

func (x *XStater) AddTags(tags ...string) { x.AddTagsTags = tags }
func (x *XStater) GetTags() []string { return x.AddTagsTags }
func (x *XStater) Gauge(stat string, value float64, tags ...string) { x.GaugeTags = tags }
func (x *XStater) Count(stat string, count float64, tags ...string) { x.CountTags = tags }
func (x *XStater) Histogram(stat string, value float64, tags ...string) { x.HistogramTags = tags }
func (x *XStater) Timing(stat string, value time.Duration, tags ...string) { x.TimingTags = tags }

0 comments on commit e7e2d4c

Please sign in to comment.