Skip to content

Commit 8de94f4

Browse files
committed
use otel_annotations
1 parent 458992f commit 8de94f4

File tree

7 files changed

+19
-42
lines changed

7 files changed

+19
-42
lines changed

.chloggen/operator-resource-attributes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ subtext: |
1111
1212
If you are using the file log receiver, you can now create the same resource attributes as traces (via OTLP) received
1313
from an application instrumented with the OpenTelemetry Operator -
14-
simply by adding the `extract: { automatic_attributes: { enabled: true }}` configuration to the `k8sattributesprocessor` processor.
14+
simply by adding the `extract: { otel_annotations: true }` configuration to the `k8sattributesprocessor` processor.
1515
See the [documentation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/k8sattributesprocessor/README.md#config-example) for more details.

processor/k8sattributesprocessor/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,8 @@ k8sattributes/2:
275275
- tag_name: app.label.component
276276
key: app.kubernetes.io/component
277277
from: pod
278-
automatic_attributes:
279-
# Apply the operator rules - see https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md
280-
enabled: true
281-
annotation_prefixes: ["foo/"] # default is ["resource.opentelemetry.io/"] - use empty list to disable
278+
# Apply the operator rules - see https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md
279+
otel_annotations: true # default is false - use true to enable
282280
pod_association:
283281
- sources:
284282
# This rule associates all resources containing the 'k8s.pod.ip' attribute with the matching pods. If this attribute is not present in the resource, this rule will not be able to find the matching pod.

processor/k8sattributesprocessor/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ type ExtractConfig struct {
167167
// documentation for more details.
168168
Labels []FieldExtractConfig `mapstructure:"labels"`
169169

170-
AutomaticRules kube.AutomaticRules `mapstructure:"automatic_attributes"`
170+
// OtelAnnotations extracts all pod annotations with the prefix "resource.opentelemetry.io" as resource attributes
171+
// E.g. "resource.opentelemetry.io/foo" becomes "foo"
172+
OtelAnnotations bool `mapstructure:"otel_annotations"`
171173
}
172174

173175
// FieldExtractConfig allows specifying an extraction rule to extract a resource attribute from pod (or namespace)

processor/k8sattributesprocessor/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func createProcessorOpts(cfg component.Config) []option {
192192
opts = append(opts, withExtractMetadata(oCfg.Extract.Metadata...))
193193
opts = append(opts, withExtractLabels(oCfg.Extract.Labels...))
194194
opts = append(opts, withExtractAnnotations(oCfg.Extract.Annotations...))
195-
opts = append(opts, withAutomaticRules(oCfg.Extract.AutomaticRules))
195+
opts = append(opts, withOtelAnnotations(oCfg.Extract.OtelAnnotations))
196196

197197
// filters
198198
opts = append(opts, withFilterNode(oCfg.Filter.Node, oCfg.Filter.NodeFromEnvVar))

processor/k8sattributesprocessor/internal/kube/automatic.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type AutomaticRules struct {
1212
AnnotationPrefixes []string `mapstructure:"annotation_prefixes"`
1313
}
1414

15-
const DefaultAnnotationPrefix = "resource.opentelemetry.io/"
15+
const (
16+
DefaultAnnotationPrefix = "resource.opentelemetry.io/"
17+
)
1618

1719
func AutomaticAnnotationRule(prefix string) FieldExtractionRule {
1820
return FieldExtractionRule{

processor/k8sattributesprocessor/options.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,10 @@ func withExtractMetadata(fields ...string) option {
198198
}
199199
}
200200

201-
func withAutomaticRules(rules kube.AutomaticRules) option {
201+
func withOtelAnnotations(enabled bool) option {
202202
return func(p *kubernetesprocessor) error {
203-
if rules.Enabled {
204-
p.rules.AutomaticRules = rules
205-
prefixes := rules.AnnotationPrefixes
206-
if len(prefixes) == 0 {
207-
prefixes = []string{kube.DefaultAnnotationPrefix}
208-
}
209-
for _, prefix := range prefixes {
210-
p.rules.Annotations = append(p.rules.Annotations, kube.AutomaticAnnotationRule(prefix))
211-
}
203+
if enabled {
204+
p.rules.Annotations = append(p.rules.Annotations, kube.AutomaticAnnotationRule(kube.DefaultAnnotationPrefix))
212205
}
213206
return nil
214207
}

processor/k8sattributesprocessor/options_test.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -747,21 +747,18 @@ func TestWithExcludes(t *testing.T) {
747747
}
748748
}
749749

750-
func Test_withAutomaticRules(t *testing.T) {
750+
func TestOtelAnnotations(t *testing.T) {
751751
tests := []struct {
752752
name string
753-
rules kube.AutomaticRules
753+
enabled bool
754754
wantAnnotations []kube.FieldExtractionRule
755755
}{
756756
{
757-
name: "no automatic rules",
758-
rules: kube.AutomaticRules{},
757+
name: "no automatic rules",
759758
},
760759
{
761-
name: "default automatic rules",
762-
rules: kube.AutomaticRules{
763-
Enabled: true,
764-
},
760+
name: "default automatic rules",
761+
enabled: true,
765762
wantAnnotations: []kube.FieldExtractionRule{
766763
{
767764
Name: "$1",
@@ -771,26 +768,11 @@ func Test_withAutomaticRules(t *testing.T) {
771768
},
772769
},
773770
},
774-
{
775-
name: "custom automatic rules",
776-
rules: kube.AutomaticRules{
777-
Enabled: true,
778-
AnnotationPrefixes: []string{"custom.prefix/"},
779-
},
780-
wantAnnotations: []kube.FieldExtractionRule{
781-
{
782-
Name: "$1",
783-
KeyRegex: regexp.MustCompile(`^custom\.prefix/(.+)$`),
784-
HasKeyRegexReference: true,
785-
From: kube.MetadataFromPod,
786-
},
787-
},
788-
},
789771
}
790772
for _, tt := range tests {
791773
t.Run(tt.name, func(t *testing.T) {
792774
p := kubernetesprocessor{}
793-
rules := withAutomaticRules(tt.rules)
775+
rules := withOtelAnnotations(tt.enabled)
794776
err := rules(&p)
795777
assert.NoError(t, err)
796778
assert.Equal(t, tt.wantAnnotations, p.rules.Annotations)

0 commit comments

Comments
 (0)