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

[processor/attributes] Get values from maps #33934

Closed
pgillich opened this issue Jul 7, 2024 · 2 comments
Closed

[processor/attributes] Get values from maps #33934

pgillich opened this issue Jul 7, 2024 · 2 comments
Labels
enhancement New feature or request needs triage New item requiring triage processor/attributes Attributes processor

Comments

@pgillich
Copy link

pgillich commented Jul 7, 2024

Component(s)

processor/attributes

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

The fluentforwardreceiver puts metadata about Pod in .resourceLogs.scopeLogs.logRecords.attributes.kubernetes, see a part of the data in yaml:

attributes:
  - key: kubernetes
    value:
      kvlistValue:
        values:
          - key: pod_name
            value:
              stringValue: nginx-ingress-internal-ingress-nginx-controller-b66856474-97dmj
          - key: host
            value:
              stringValue: aks-rancher-10369385-vmss00000v
          - key: labels
            value:
              kvlistValue:
                values:
                  - key: app.kubernetes.io/name
                    value:
                      stringValue: ingress-nginx
          - key: annotations
            value:
              kvlistValue:
                values:
                  - key: kubectl.kubernetes.io/default-container
                    value:
                      stringValue: controller

If I'd like to forward the logs to Loki by lokiexporter, I have to configure the label filters, for example:

processors:
  attributes/loki_labels:
	actions:
	- action: insert
	  key: loki.attribute.labels
	  value: kubernetes.container_name, kubernetes.host, kubernetes.labels.app.kubernetes.io/name, kubernetes_annotations_kubectl_kubernetes_io_default_container

It works, because label filters will be kubernetes_container_name, kubernetes_host, kubernetes_labels_app_kubernetes_io_name, kubernetes_annotations_kubectl_kubernetes_io_default_container (. is converted to _, see: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/lokiexporter/README.md#labels ), but does not conform to https://opentelemetry.io/docs/specs/semconv/resource/k8s/ and https://opentelemetry.io/docs/specs/semconv/attributes-registry/k8s/ .

I've already renamed the kubernetes key to k8s by below configuration:

processors:
  attributes/kubernetes_to_k8s:
	actions:
	- action: insert
	  from_attribute: kubernetes
	  key: k8s
	- action: delete
	  key: kubernetes

, but I'm unable to rename or copy from anything in the kubernetes map, because the attributesprocessor cannot access it.

Whole json and yaml logs are attached:
log23_1.json.gz
log23_1.yaml.gz

Describe the solution you'd like

The solution will be an enhanced getter function, which can access inside the kubernetes map and can copy the value to k8s.x.y or k8s.x.label.y or k8s.x.annotation.y key, conform to https://opentelemetry.io/docs/specs/semconv/resource/k8s/ and https://opentelemetry.io/docs/specs/semconv/attributes-registry/k8s/ . Example for the configuration:

processors:
  attributes/kubernetes_to_k8s:
	actions:
	- action: insert
	  from_attribute: kubernetes.container_name
	  key: k8s.container.name
	- action: insert
	  from_attribute: kubernetes.host
	  key: k8s.node.name
	- action: insert
	  from_attribute: kubernetes.labels.app.kubernetes.io/name
	  key: k8s.label.app.kubernetes.io/name
	- action: insert
	  from_attribute: kubernetes.annotations.kubectl.kubernetes.io/default-container
	  key: k8s.annotation.kubectl.kubernetes.io/default-container

Technical details:

The getSourceAttributeValue call in func (ap *AttrProc) Process(ctx context.Context, logger *zap.Logger, attrs pcommon.Map) at INSERT shoud be decorated by a new function, which works similar to
The func getAttribute(attr string, attributes pcommon.Map) (pcommon.Value, bool) .

It's a tradeoff, because is does not enable changing the map, it just support to create a new key under the attributes from a map value.

Describe alternatives you've considered

Renaming the kubernetes key to k8s is not enough to conform to https://opentelemetry.io/docs/specs/semconv/resource/k8s/ and https://opentelemetry.io/docs/specs/semconv/attributes-registry/k8s/ .

Additional context

An example screenshot about Grafana Explorer with Loki datasource, after renaming the kubernetes attributes key to k8s (see k8s_host, instead of k8s_node_name and plural labels, which is deprecated at https://opentelemetry.io/docs/specs/semconv/attributes-registry/k8s/#k8s-deprecated-attributes :
image

@pgillich pgillich added enhancement New feature or request needs triage New item requiring triage labels Jul 7, 2024
@github-actions github-actions bot added the processor/attributes Attributes processor label Jul 7, 2024
Copy link
Contributor

github-actions bot commented Jul 7, 2024

Pinging code owners:

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

@pgillich
Copy link
Author

pgillich commented Jul 8, 2024

Finally, I found the solution, no code change is needed.

processors:
  transform/kubernetes_to_k8s:
    error_mode: ignore
    log_statements:
    - context: log
      statements:
      - set(attributes["k8s.node.name"], attributes["kubernetes"]["host"])
      - set(attributes["k8s.label"], attributes["kubernetes"]["labels"])
      - set(attributes["k8s.annotation"], attributes["kubernetes"]["annotations"])

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/attributes Attributes processor
Projects
None yet
Development

No branches or pull requests

1 participant