Skip to content

Add namespace k8s tagger #3384

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

Merged
6 changes: 4 additions & 2 deletions processor/k8sprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"path"
"testing"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor/kube"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -60,11 +62,11 @@ func TestLoadConfig(t *testing.T) {
Metadata: []string{"k8s.pod.name", "k8s.pod.uid", "k8s.deployment.name", "k8s.cluster.name", "k8s.namespace.name", "k8s.node.name", "k8s.pod.start_time"},
Annotations: []FieldExtractConfig{
{TagName: "a1", Key: "annotation-one", From: "pod"},
{TagName: "a2", Key: "annotation-two", Regex: "field=(?P<value>.+)", From: "pod"},
{TagName: "a2", Key: "annotation-two", Regex: "field=(?P<value>.+)", From: kube.MetadataFromPod},
},
Labels: []FieldExtractConfig{
{TagName: "l1", Key: "label1", From: "pod"},
{TagName: "l2", Key: "label2", Regex: "field=(?P<value>.+)", From: "pod"},
{TagName: "l2", Key: "label2", Regex: "field=(?P<value>.+)", From: kube.MetadataFromPod},
},
},
Filter: FilterConfig{
Expand Down
15 changes: 11 additions & 4 deletions processor/k8sprocessor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,22 @@ func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.F
rules := []kube.FieldExtractionRule{}
for _, a := range fields {
name := a.TagName

switch a.From {
// By default if the From field is not set for labels and annotations we want to extract them from pod
case "", kube.MetadataFromPod:
a.From = kube.MetadataFromPod
case kube.MetadataFromNamespace:
a.From = kube.MetadataFromNamespace
default:
return rules, fmt.Errorf("from must contain exactly one value like pod or namespace")
}

if name == "" {
if a.From == kube.MetadataFromPod {
name = fmt.Sprintf("k8s.pod.%s.%s", fieldType, a.Key)
} else if a.From == kube.MetadataFromNamespace {
name = fmt.Sprintf("k8s.namespace.%s.%s", fieldType, a.Key)
// By default if the From field is not set for labels and annotations we want to extract them from pod
} else if a.From == "" {
name = fmt.Sprintf("k8s.pod.%s.%s", fieldType, a.Key)
a.From = kube.MetadataFromPod
}
}

Expand Down