Skip to content

Commit

Permalink
Merge pull request #17 from chr15murray/main
Browse files Browse the repository at this point in the history
Add support for keepUrlTag
  • Loading branch information
javaducky authored Mar 30, 2022
2 parents d8c2b37 + 4127047 commit 0751e79
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {

KeepTags null.Bool `json:"keepTags" envconfig:"K6_KEEP_TAGS"`
KeepNameTag null.Bool `json:"keepNameTag" envconfig:"K6_KEEP_NAME_TAG"`
KeepUrlTag null.Bool `json:"keepUrlTag" envconfig:"K6_KEEP_URL_TAG"`
}

func NewConfig() Config {
Expand All @@ -51,6 +52,7 @@ func NewConfig() Config {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: make(map[string]string),
}
}
Expand Down Expand Up @@ -131,6 +133,10 @@ func (base Config) Apply(applied Config) Config {
base.KeepNameTag = applied.KeepNameTag
}

if applied.KeepUrlTag.Valid {
base.KeepUrlTag = applied.KeepUrlTag
}

if len(applied.Headers) > 0 {
for k, v := range applied.Headers {
base.Headers[k] = v
Expand Down Expand Up @@ -186,6 +192,10 @@ func ParseArg(arg string) (Config, error) {
c.KeepNameTag = null.BoolFrom(v)
}

if v, ok := params["keepUrlTag"].(bool); ok {
c.KeepUrlTag = null.BoolFrom(v)
}

c.Headers = make(map[string]string)
if v, ok := params["headers"].(map[string]interface{}); ok {
for k, v := range v {
Expand Down Expand Up @@ -284,6 +294,14 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string, a
}
}

if b, err := getEnvBool(env, "K6_KEEP_URL_TAG"); err != nil {
return result, err
} else {
if b.Valid {
result.KeepUrlTag = b
}
}

envHeaders := getEnvMap(env, "K6_PROMETHEUS_HEADERS_")
for k, v := range envHeaders {
result.Headers[k] = v
Expand Down
6 changes: 6 additions & 0 deletions pkg/remotewrite/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestConstructRemoteConfig(t *testing.T) {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: make(map[string]string),
},
errString: "",
Expand Down Expand Up @@ -146,6 +147,7 @@ func TestConstructRemoteConfig(t *testing.T) {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: make(map[string]string),
},
errString: "",
Expand Down Expand Up @@ -194,6 +196,7 @@ func TestConstructRemoteConfig(t *testing.T) {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: map[string]string{
"X-Header": "value",
},
Expand Down Expand Up @@ -230,6 +233,7 @@ func TestConstructRemoteConfig(t *testing.T) {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: map[string]string{
"X-Header": "value_from_env",
},
Expand Down Expand Up @@ -266,6 +270,7 @@ func TestConstructRemoteConfig(t *testing.T) {
FlushPeriod: types.NullDurationFrom(defaultFlushPeriod),
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
KeepUrlTag: null.BoolFrom(true),
Headers: map[string]string{
"X-Header": "value_from_arg",
},
Expand Down Expand Up @@ -315,6 +320,7 @@ func assertConfig(t *testing.T, actual, expected Config) {
assert.Equal(t, expected.FlushPeriod, actual.FlushPeriod)
assert.Equal(t, expected.KeepTags, actual.KeepTags)
assert.Equal(t, expected.KeepNameTag, expected.KeepNameTag)
assert.Equal(t, expected.KeepUrlTag, expected.KeepUrlTag)
assert.Equal(t, expected.Headers, actual.Headers)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/remotewrite/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func tagsToLabels(tags *stats.SampleTags, config Config) ([]prompb.Label, error)
continue
}

if !config.KeepUrlTag.Bool && name == "url" {
continue
}

labelPairs = append(labelPairs, prompb.Label{
Name: name,
Value: value,
Expand Down
21 changes: 21 additions & 0 deletions pkg/remotewrite/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ func TestTagsToLabels(t *testing.T) {
{Name: "name", Value: "nnn"},
},
},
"url-tag-discard": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepUrlTag: null.BoolFrom(false),
},
labels: []prompb.Label{
{Name: "foo", Value: "bar"},
},
},
"url-tag-keep": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepUrlTag: null.BoolFrom(true),
},
labels: []prompb.Label{
{Name: "foo", Value: "bar"},
{Name: "url", Value: "uuu"},
},
},
"discard-tags": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
config: Config{
Expand Down

0 comments on commit 0751e79

Please sign in to comment.