Skip to content

ottl function delete_key should support dynamic keys #43081

@MShekow

Description

@MShekow

Component(s)

pkg/ottl

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

delete_key (and delete_matching_key) currently only supports static, hard-coded strings for the key that it deletes.

Describe the solution you'd like

It would be better if it could support dynamic key values, i.e., values that come from evaluating variables (or functions).

Describe alternatives you've considered

No response

Additional context

I encountered this issue when trying to delete a key. I'm parsing Kubernetes container logs as JSON, if body is JSON. I made up my own Pod annotation that indicates which of the JSON keys contains the actual message. And I then replace the body with the message. However, the message is also present in the log's attributes, which is not necessary, so I'd like to delete that attribute, but I cannot.

Config snippet:

exporters:
  otlphttp:
    endpoint: http://obs-vls-server:9428/insert/opentelemetry
processors:
  batch: {}
  k8sattributes:
    extract:
      metadata:
        - k8s.namespace.name
        ...
      annotations:
        # This makes the processor look for an annotation named "custom_json_log_message" on the pod and
        # make it available under resource.attributes
        - tag_name: custom_json_log_message
          key: custom_json_log_message
    filter:
      node_from_env_var: K8S_NODE_NAME
    passthrough: false
      ...

  transform/parse_json_body:
    error_mode: ignore
    log_statements:
      - context: log
        conditions:
          - body != nil and IsMatch(body, "^{.*}") and resource.attributes["custom_json_log_message"] != nil
        statements:
          - set(cache, ParseJSON(body))
          - flatten(cache, "")
          - merge_maps(attributes, cache, "upsert")
          - set(body, attributes[resource.attributes["custom_json_log_message"]]) where attributes[resource.attributes["custom_json_log_message"]] != nil
          # We would like to delete the attribute so it doesn’t clutter labels, but it seems that the method expects literal strings
          # - delete_matching_keys(attributes, Concat(["^", attributes["custom_json_log_message"], "$"], ""))
receivers:
  filelog:
    exclude: []
    include:
      - /var/log/pods/*/*/*.log
    include_file_name: false
    include_file_path: true
    operators:
      - id: container-parser
        max_log_size: 102400
        type: container
    retry_on_failure:
      enabled: true
    start_at: end
service:
  pipelines:
    logs:
      exporters:
        - otlphttp
      processors:
        - k8sattributes
        - transform/parse_json_body
        - memory_limiter
        - batch
      receivers:
        - otlp
        - filelog

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions