alloy icon indicating copy to clipboard operation
alloy copied to clipboard

add otel.processor.resource

Open vmignot opened this issue 1 year ago • 8 comments

Request

Hello,

There is a processor that can process resource attributes: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/resourceprocessor/README.md

Is it possible to implement it?

Today, otel.processor.attributes can only process span attributes. We need to be able to do the same, but for resources attributes.

Use case

I need this feature to drop resources attributes.

vmignot avatar Nov 07 '24 16:11 vmignot

This component is very useful. If Alloy can provide this component, then the following issues can be perfectly resolved.

https://github.com/grafana/alloy/issues/2033

elysiumHL avatar Nov 11 '24 08:11 elysiumHL

Not against adding the component but the otelcol.processor.transform can also process resource attributes and it's available in Alloy. To drop resource attributes I think that you could just set the context to "resource" and use the delete_key(attributes, <attributeName>) function. Are there functionalities that are possible only in the resource processor and not in the transform processor?

wildum avatar Nov 15 '24 13:11 wildum

Can you add resource attributes from an environment variable using otelcol.processor.transform ?

Now we use otelcol.processor.attributes to add a value for k8s.cluster.name like this:

otelcol.processor.attributes "add_custom_attributes" {
  action {
    key = "k8s.cluster.name"
    action = "upsert"
    value = env("CLUSTER_NAME")
  }

  output {
    metrics = [otelcol.processor.k8sattributes.default.input]
    traces  = [otelcol.processor.k8sattributes.default.input]
  }
}

Then we use otelcol.processor.transform to MOVE it from a attribute to a resource attribute like this:

statements = [
  `set(resource.attributes["k8s.cluster.name"], attributes["k8s.cluster.name"])`,
  `delete_key(attributes, "k8s.cluster.name")`,
]

These are on-prem clusters so we can't use resource detection for this value.

All this just feels wrong. Using otel.processor.resource would be better. Or am I missing something?

gazab avatar Dec 05 '24 10:12 gazab

why not do set(resource.attributes["k8s.cluster.name"], env("CLUSTER_NAME")) directly?

wildum avatar Dec 06 '24 14:12 wildum

@wildum we got this error when we tried:

level=error msg="failed to reload config" service=http err="error during the initial load: /etc/alloy/config.alloy:228:1: 
Failed to build component: decoding configuration: unable to parse OTTL statement \"set(resource.attributes[\\\"k8s.cluster.name\\\"], env(\\\"TENANT_ID\\\"))\": 
converter names must start with an uppercase letter but got 'env'"

from

statements = [
      `set(resource.attributes["k8s.cluster.name"], env("TENANT_ID"))`,
    ]

gazab avatar Dec 09 '24 12:12 gazab

ah sorry, this should work:

  trace_statements {
    context = "resource"
    statements = [
      string.format(`set(attributes["k8s.cluster.name"], %q)`, coalesce(sys.env("TENANT_ID"), "undefined")),
    ]
  }

coalesce is there just in case there is no env variable "TENANT_ID"

wildum avatar Dec 09 '24 13:12 wildum

Hello , can someone explain me how to remove fields like : "service.environment" from the resources An example :

"resources":{"service.environment":"Development","service.instance.id":"","service.name":"Api","service.version":"1.0.5-dev.0","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"serilog","telemetry.sdk.version":""}

otelcol.processor.attributes does not delete

DPopov-BS avatar Mar 26 '25 13:03 DPopov-BS

Hello , can someone explain me how to remove fields like : "service.environment" from the resources An example :

"resources":{"service.environment":"Development","service.instance.id":"","service.name":"Api","service.version":"1.0.5-dev.0","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"serilog","telemetry.sdk.version":""}

otelcol.processor.attributes does not delete

otelcol.processor.transform "reduce_resources_attributes" {
  error_mode = "ignore"

  trace_statements {
    context = "resource"
    statements = [
      `delete_key(attributes, "container.id")`,
      `delete_key(attributes, "host.arch")`,
      `delete_key(attributes, "host.name")`,
      `delete_key(attributes, "k8s.container.name")`,
      `delete_key(attributes, "k8s.replicaset.name")`,
      `delete_key(attributes, "os.description")`,
      `delete_key(attributes, "os.type")`,
      `delete_key(attributes, "process.command_line")`,
      `delete_key(attributes, "process.executable.path")`,
      `delete_key(attributes, "process.pid")`,
      `delete_key(attributes, "process.runtime.description")`,
      `delete_key(attributes, "process.runtime.name")`,
      `delete_key(attributes, "process.runtime.version")`,
      `delete_key(attributes, "service.instance.id")`,
    ]
  }

  output {
    traces  = [otelcol.processor.filter.default.input]
  }
}

use otelcol.processor.transform to remove works.

WEIZIBIN avatar Nov 20 '25 09:11 WEIZIBIN