Skip to content
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

Copy multiple attributes/labels in k8sattributes #34040

Closed
zeitlinger opened this issue Jul 11, 2024 · 2 comments
Closed

Copy multiple attributes/labels in k8sattributes #34040

zeitlinger opened this issue Jul 11, 2024 · 2 comments
Labels
enhancement New feature or request needs triage New item requiring triage processor/k8sattributes k8s Attributes processor

Comments

@zeitlinger
Copy link
Member

Component(s)

processor/k8sattributes

Is your feature request related to a problem? Please describe.

As a user, I'd like to add my resource attributes as k8s attributes/labels in a generic way:

opentelemetry.io/resource.deployment.environment should become deployment.environment and so on

Describe the solution you'd like

similar to to the existing regex: field=(?P<value>.+) syntax

k8sattributes:
  auth_type: "serviceAccount"
  passthrough: false
  extract:
   labels:
     - key_regex: opentelemetry.io/resource.(?P<key>.+)
       from: pod

Describe alternatives you've considered

No response

Additional context

No response

@zeitlinger zeitlinger added enhancement New feature or request needs triage New item requiring triage labels Jul 11, 2024
@github-actions github-actions bot added the processor/k8sattributes k8s Attributes processor label Jul 11, 2024
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@zeitlinger
Copy link
Member Author

apparently this feature already exists - untested and undocumented

package kube

import (
	"github.com/stretchr/testify/assert"
	"regexp"
	"testing"
)

func TestFieldExtractionRule_extractFromMetadata(t *testing.T) {
	type fields struct {
		Name                 string
		Key                  string
		KeyRegex             *regexp.Regexp
		HasKeyRegexReference bool
		Regex                *regexp.Regexp
		From                 string
	}
	tests := []struct {
		name   string
		fields fields
		want   map[string]string
	}{
		{
			name: "Test extractFromMetadata",
			fields: fields{
				Name:                 "$key",
				KeyRegex:             regexp.MustCompile("opentelemetry.io/resource.(?P<key>.+)"),
				HasKeyRegexReference: true,
			},
			want: map[string]string{
				"deployment.environment": "staging",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			r := &FieldExtractionRule{
				Name:                 tt.fields.Name,
				Key:                  tt.fields.Key,
				KeyRegex:             tt.fields.KeyRegex,
				HasKeyRegexReference: tt.fields.HasKeyRegexReference,
				Regex:                tt.fields.Regex,
				From:                 tt.fields.From,
			}
			m := map[string]string{
				"opentelemetry.io/resource.deployment.environment": "staging",
			}
			tags := make(map[string]string)
			r.extractFromMetadata(m, tags, "")
			assert.Equal(t, tt.want, tags)
		})
	}
}

here is a test for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage New item requiring triage processor/k8sattributes k8s Attributes processor
Projects
None yet
Development

No branches or pull requests

2 participants