Skip to content

[processor/k8sattributes] Automatic resource attributes #37114

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

Closed

Conversation

zeitlinger
Copy link
Member

@zeitlinger zeitlinger commented Jan 9, 2025

Description

Implements https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md#how-serviceversion-should-be-calculate

The OTel operator has a set of rules to create resource attributes - which is described here.

This PR allows the collector to create resource attributes with the same logic.

Why

If the operator supplies resource attributes (that are sent with OTLP), it seems redundant that the collector should be able to do the same - it will just overwrite the resource attribute with identical values.

This feature gets interesting if you are not only getting all data from OTLP - but some data elsewhere, e.g. from the file log receiver.

It's crucial to use the same values for resource attributes across signals - this makes correlation possible.
For example, it allows you to find file based the log entries on the same service instance as a trace you're viewing (around the same time).

Alternatives

The same result can be achieved using the transforprocessor - but it's a very long and hard-to-understand configuration.
It that way, it's similar to the container parser.

Testing

Unit tests were added - e2e tests later

Documentation

Added here
This also has a complete config.

Copy link
Contributor

@fatsheep9146 fatsheep9146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a changelog and also updating the document to describe how to use this feature?

@zeitlinger zeitlinger changed the title Operator resource attributes [processor/k8sattributes] Operator resource attributes Jan 10, 2025
@zeitlinger
Copy link
Member Author

@fatsheep9146 added changelog and docs

I also tried adding e2e tests - but didn't get it to work. Please advise if/how to do this.

@zeitlinger
Copy link
Member Author

@TylerHelmuth @ChrsMark can you take a look?

# app.kubernetes.io/version => service.version
# app.kubernetes.io/part-of => service.namespace
# This setting is ignored if 'enabled' is set to false
labels: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use like resource_labels_enabled instead of labels? I think labels are easy to be confused with other field also called labels, but they are of slice type.

Copy link
Member Author

@zeitlinger zeitlinger Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's the config format I have in mind:

    automatic_attributes:
      # Apply the operator rules - see https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md
      well_known_labels: true # default is false
      annotation_prefixes: ["foo/"] # default is ["resource.opentelemetry.io/"] - use empty list to disable
      exclude: ["service.version"] # default is empty list
  1. the feature to add the attributes is added when the automatic_attributes: key is added (even when it has no content
  1. allow the customization of annotation prefix
  2. allow to exclude attributes

@ChrsMark @fatsheep9146 wdyt?

Copy link
Contributor

@fatsheep9146 fatsheep9146 Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the key automatic_attributes and well_known_labels
I wonder that why we need annotation_prefixes, you mentioned that you want to implement the same logic with The OTel operator, but I don't see the otel operator can support user-defined annotation prefix. (Maybe I missed it)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: The first split-off PR is #38825

  • annotation_prefixes is not part of the operator
  • I thought that some more flexibility is in the spirit of the collector - but no strong opinion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the #38825 adds the otel_annotations setting which will handle the "resource.opentelemetry.io/foo" becomes "foo" part. Do we we have consensus on the rest of the settings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so - since it's defined in the otel semantic conventions: https://opentelemetry.io/docs/specs/semconv/non-normative/k8s-attributes/

Copy link
Contributor

@fatsheep9146 fatsheep9146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeitlinger I want to make sure I understand it right, so the scenario of this pr is like, if I create a pod PodA which export log to a otel-collector which has k8sattributes processor enabled with this operator_rules.enabled to be true.

Then if the PodA has a annotation called

resource.opentelemetry.io/foo: "bar"

Then the log exported to the otel-collector will have a new attribute called foo added to its resource attributes with value bar?

Copy link
Member

@ChrsMark ChrsMark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This proposal has shown up a couple of times in the past. We need to decide on where is the best place to implement this.

Check open-telemetry/opentelemetry-helm-charts#905 and #27261.

@zeitlinger
Copy link
Member Author

@zeitlinger I want to make sure I understand it right, so the scenario of this pr is like, if I create a pod PodA which export log to a otel-collector which has k8sattributes processor enabled with this operator_rules.enabled to be true.

Then if the PodA has a annotation called

resource.opentelemetry.io/foo: "bar"

Then the log exported to the otel-collector will have a new attribute called foo added to its resource attributes with value bar?

correct

Copy link
Contributor

github-actions bot commented Feb 1, 2025

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 1, 2025
@zeitlinger
Copy link
Member Author

please keep open - the spec issue is still ongoing

@github-actions github-actions bot removed the Stale label Feb 4, 2025
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 18, 2025
@dmitryax dmitryax removed the Stale label Feb 18, 2025
@zeitlinger
Copy link
Member Author

please keep open

Copy link
Contributor

github-actions bot commented Mar 5, 2025

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Mar 5, 2025
@atoulme atoulme removed the Stale label Mar 7, 2025
@zeitlinger
Copy link
Member Author

the spec is merged - I'm planning to finish the PR this week

@zeitlinger zeitlinger force-pushed the operator-resource-attributes branch from 61b534d to 4dbf965 Compare March 11, 2025 11:10
@zeitlinger
Copy link
Member Author

the spec is merged - I'm planning to finish the PR this week

PR has been updated to reflect the spec
Tests will be fixed once the config syntax is agreed upon

@atoulme
Copy link
Contributor

atoulme commented Mar 17, 2025

Please resolve the CI issues:

Error: k8sattributesprocessor/internal/kube/client.go:89:5: error-naming: error var cannotRetrieveImage should have name of the form errFoo (revive)
var cannotRetrieveImage = errors.New("cannot retrieve image name")
    ^

Please also address the conflict.

Once ready, please mark the PR as ready to review again.

@zeitlinger zeitlinger force-pushed the operator-resource-attributes branch from bc0d323 to 387ebca Compare March 19, 2025 09:56
@zeitlinger zeitlinger changed the title [processor/k8sattributes] Operator resource attributes [processor/k8sattributes] Automatic resource attributes Mar 19, 2025
@zeitlinger
Copy link
Member Author

@dmitryax as discussed in SIG, I'll break it down im multiple PRs, starting with the annotations

@zeitlinger
Copy link
Member Author

@zeitlinger I want to make sure I understand it right, so the scenario of this pr is like, if I create a pod PodA which export log to a otel-collector which has k8sattributes processor enabled with this operator_rules.enabled to be true.

Then if the PodA has a annotation called

resource.opentelemetry.io/foo: "bar"

Then the log exported to the otel-collector will have a new attribute called foo added to its resource attributes with value bar?

that's correct - that's what https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md#specify-resource-attributes-using-kubernetes-annotations says

@zeitlinger
Copy link
Member Author

This proposal has shown up a couple of times in the past. We need to decide on where is the best place to implement this.

Check open-telemetry/opentelemetry-helm-charts#905 and #27261.

Those are older discussions about semantic conventions - which have now been merged: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/k8s-attributes.md

This PR is about implementing this spec.

@zeitlinger zeitlinger marked this pull request as draft March 21, 2025 08:50
@zeitlinger
Copy link
Member Author

Converted to draft, because some PRs will be split off first - #38825 so far

enabled: true
well_known_labels: true # default is false
annotation_prefixes: ["foo/"] # default is ["resource.opentelemetry.io/"] - use empty list to disable
exclude: ["service.version"] # default is empty list
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just move well known attributes from this section to where all the other pre-defined resource attributes are configured extract.metadata, and disabled by default?

Then this section would be responsible for taking resource attributes from annotations only. Instead of calling it automatic_attributes, it can be something like

extract:
  otel_annotations: <bool> // false by default

Then, if we want to provide flexibility for annotations extraction, we could add extract.annotations with the same syntax as we currently have for extract.labels. That is not really needed right now.

Providing such interface would be consistent with the existing one and easier to understand by the end users. Please let me know WDYT.

dmitryax pushed a commit that referenced this pull request Apr 11, 2025
…prefix (#38825)

This is the first part of
#37114
- the ability to use an annotation prefix to define a resource attribute

---------

Co-authored-by: Christos Markou <[email protected]>
@zeitlinger
Copy link
Member Author

the second and (maybe) final part is #39335

@zeitlinger zeitlinger closed this Apr 11, 2025
@zeitlinger zeitlinger deleted the operator-resource-attributes branch April 11, 2025 12:19
akshays-19 pushed a commit to akshays-19/opentelemetry-collector-contrib that referenced this pull request Apr 23, 2025
…prefix (open-telemetry#38825)

This is the first part of
open-telemetry#37114
- the ability to use an annotation prefix to define a resource attribute

---------

Co-authored-by: Christos Markou <[email protected]>
Fiery-Fenix pushed a commit to Fiery-Fenix/opentelemetry-collector-contrib that referenced this pull request Apr 24, 2025
…prefix (open-telemetry#38825)

This is the first part of
open-telemetry#37114
- the ability to use an annotation prefix to define a resource attribute

---------

Co-authored-by: Christos Markou <[email protected]>
dmitryax pushed a commit that referenced this pull request May 20, 2025
dragonlord93 pushed a commit to dragonlord93/opentelemetry-collector-contrib that referenced this pull request May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
processor/k8sattributes k8s Attributes processor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants