-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wildcard support to label to tag mappings for containers. (#2525)
* Add wildcard support to pod label to tag mappings for k8s. * Add reno * gofmt * fix tests
- Loading branch information
Showing
11 changed files
with
229 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2018 Datadog, Inc. | ||
|
||
package tmplvar | ||
|
||
import ( | ||
"bytes" | ||
"regexp" | ||
"unicode" | ||
) | ||
|
||
var tmplVarRegex = regexp.MustCompile(`%%.+?%%`) | ||
|
||
// TemplateVar is the info for a parsed template variable. | ||
type TemplateVar struct { | ||
Raw, Name, Key []byte | ||
} | ||
|
||
// ParseString returns parsed template variables found in the input string. | ||
func ParseString(s string) []TemplateVar { | ||
return Parse([]byte(s)) | ||
} | ||
|
||
// Parse returns parsed template variables found in the input data. | ||
func Parse(b []byte) []TemplateVar { | ||
var parsed []TemplateVar | ||
vars := tmplVarRegex.FindAll(b, -1) | ||
for _, v := range vars { | ||
name, key := parseTemplateVar(v) | ||
parsed = append(parsed, TemplateVar{v, name, key}) | ||
} | ||
return parsed | ||
} | ||
|
||
// parseTemplateVar extracts the name of the var and the key (or index if it can be | ||
// cast to an int) | ||
func parseTemplateVar(v []byte) (name, key []byte) { | ||
stripped := bytes.Map(func(r rune) rune { | ||
if unicode.IsSpace(r) || r == '%' { | ||
return -1 | ||
} | ||
return r | ||
}, v) | ||
split := bytes.SplitN(stripped, []byte("_"), 2) | ||
name = split[0] | ||
if len(split) == 2 { | ||
key = split[1] | ||
} else { | ||
key = []byte("") | ||
} | ||
return name, key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2018 Datadog, Inc. | ||
|
||
package tmplvar | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseTemplateVar(t *testing.T) { | ||
testCases := []struct { | ||
tmpl, name, key string | ||
}{ | ||
{ | ||
"%%host%%", | ||
"host", | ||
"", | ||
}, | ||
{ | ||
"%%host_0%%", | ||
"host", | ||
"0", | ||
}, | ||
{ | ||
"%%host 0%%", | ||
"host0", | ||
"", | ||
}, | ||
{ | ||
"%%host_0_1%%", | ||
"host", | ||
"0_1", | ||
}, | ||
{ | ||
"%%host_network_name%%", | ||
"host", | ||
"network_name", | ||
}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { | ||
name, key := parseTemplateVar([]byte(testCase.tmpl)) | ||
assert.Equal(t, testCase.name, string(name)) | ||
assert.Equal(t, testCase.key, string(key)) | ||
}) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
releasenotes/notes/support-wildcards-pod-labels-as-tags-45b873f7a94be498.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enhancements: | ||
- | | ||
Added support for wildcards to `DD_KUBERNETES_POD_LABELS_AS_TAGS`. For example, | ||
`DD_KUBERNETES_POD_LABELS_AS_TAGS='{"*":"kube_%%label%%"}'` will all pod labels as | ||
tags to your metrics with tags names prefixed by `kube_`. |