Skip to content

target relabel does not work in otel-allocator #4094

Open
@hanjintao1996

Description

@hanjintao1996

Component(s)

target allocator

What happened?

Title: Bug in Apply Method: Modified lset Not Assigned Back to tItem.Labels

Description:
There is a bug in the Apply method of relabelConfigTargetFilter in relabel.go where the modified label set (lset) is not being assigned back to tItem.Labels. This results in any changes made during the relabeling process not being reflected in the target items.
File: cmd/otel-allocator/internal/prehook/relabel.go
In the code, if the target is not dropped, the modified labels will not be assigned to tItem.

func (tf *relabelConfigTargetFilter) Apply(targets []*target.Item) []*target.Item {
	numTargets := len(targets)

	// need to wait until relabelCfg is set
	if len(tf.relabelCfg) == 0 {
		return targets
	}

	writeIndex := 0
	for _, tItem := range targets {
		keepTarget := true
		lset := tItem.Labels
		for _, cfg := range tf.relabelCfg[tItem.JobName] {
			lset, keepTarget = relabel.Process(lset, cfg)
			if !keepTarget {
				break // inner loop
			}
		}

		if keepTarget {
			targets[writeIndex] = tItem
			writeIndex++
		}
	}

	targets = targets[:writeIndex]
	targets = slices.Clip(targets)
	tf.log.V(2).Info("Filtering complete", "seen", numTargets, "kept", len(targets))
	return targets
}

Steps to Reproduce:

Use a relabel configuration that modifies the label set.
Call the Apply method.
Observe that changes in labels are not reflected in the target items.
Expected Behavior:
The modified label set (lset) should be assigned back to tItem.Labels to ensure that any changes made during the relabeling process are reflected.

Actual Behavior:
The modified lset is not assigned back, resulting in no changes to the target items’ labels after processing.

Proposed Solution:
Assign the modified lset back to tItem.Labels within the keepTarget block:

if keepTarget {
    tItem.Labels = lset // Assign processed label set back to tItem
    tItem.TargetURL = tItem.Labels.Get("__address__")  // Assign processed label set back to tItem
    targets[writeIndex] = tItem
    writeIndex++
}

Please let me know if you need any further details.

Kubernetes Version

1.23.0

Operator version

v0.4.0

Collector version

v0.4.0

Environment information

Environment

OS: (e.g., "Ubuntu 20.04")
Compiler(if manually compiled): (e.g., "go 14.2")

Log output

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions