Skip to content

Commit cb04029

Browse files
ptodevtpaschalis
andauthored
otelcol.processor.discovery should not modify its targets attribute (#5170)
Fixed a bug where `otelcol.processor.discovery` could modify the `targets` passed by an upstream component. --------- Co-authored-by: Paschalis Tsilias <[email protected]>
1 parent 0ea5b12 commit cb04029

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ Main (unreleased)
7373
`msg`, followed by non-common fields. Previously, the position of `msg` was
7474
not consistent. (@rfratto)
7575

76+
### Bugfixes
77+
78+
- Fixed a bug where `otelcol.processor.discovery` could modify the `targets` passed by an upstream component. (@ptodev)
79+
7680
v0.36.1 (2023-09-06)
7781
--------------------
7882

component/otelcol/processor/discovery/discovery.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (args *Arguments) Validate() error {
7777

7878
// Component is the otelcol.exporter.discovery component.
7979
type Component struct {
80-
cfg Arguments
8180
consumer *promsdconsumer.Consumer
8281
logger log.Logger
8382
}
@@ -134,18 +133,17 @@ func (c *Component) Run(ctx context.Context) error {
134133
// Update implements Component.
135134
func (c *Component) Update(newConfig component.Arguments) error {
136135
cfg := newConfig.(Arguments)
137-
c.cfg = cfg
138136

139137
hostLabels := make(map[string]discovery.Target)
140138

141-
for _, labels := range c.cfg.Targets {
139+
for _, labels := range cfg.Targets {
142140
host, err := promsdconsumer.GetHostFromLabels(labels)
143141
if err != nil {
144142
level.Warn(c.logger).Log("msg", "ignoring target, unable to find address", "err", err)
145143
continue
146144
}
147-
promsdconsumer.CleanupLabels(labels)
148-
hostLabels[host] = labels
145+
146+
hostLabels[host] = promsdconsumer.NewTargetsWithNonInternalLabels(labels)
149147
}
150148

151149
err := c.consumer.UpdateOptions(promsdconsumer.Options{

docs/sources/flow/reference/components/otelcol.processor.discovery.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ information.
104104
## Examples
105105

106106
### Basic usage
107-
```
107+
```river
108108
discovery.http "dynamic_targets" {
109109
url = "https://example.com/scrape_targets"
110110
refresh_interval = "15s"
@@ -123,7 +123,7 @@ otelcol.processor.discovery "default" {
123123

124124
Outputs from more than one discovery process can be combined via the `concat` function.
125125

126-
```
126+
```river
127127
discovery.http "dynamic_targets" {
128128
url = "https://example.com/scrape_targets"
129129
refresh_interval = "15s"
@@ -149,7 +149,7 @@ It is not necessary to use a discovery component. In the example below, a `test_
149149
attribute will be added to a span if its IP address is "1.2.2.2". The `__internal_label__` will
150150
be not be added to the span, because it begins with a double underscore (`__`).
151151

152-
```
152+
```river
153153
otelcol.processor.discovery "default" {
154154
targets = [{
155155
"__address__" = "1.2.2.2",

docs/sources/flow/reference/components/otelcol.processor.span.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ This example creates a new span name from the values of attributes `db.svc`,
234234
`operation`, and `id`, in that order, separated by the value `::`.
235235
All attribute keys need to be specified in the span for the processor to rename it.
236236

237-
```
237+
```river
238238
otelcol.processor.span "default" {
239239
name {
240240
separator = "::"
@@ -247,20 +247,20 @@ otelcol.processor.span "default" {
247247
}
248248
```
249249

250-
For a span with the following attributes key/value pairs, the example
250+
For a span with the following attributes key/value pairs, the above
251251
Flow configuration will change the span name to `"location::get::1234"`:
252-
```
252+
```json
253253
{
254254
"db.svc": "location",
255255
"operation": "get",
256256
"id": "1234"
257257
}
258258
```
259259

260-
For a span with the following attributes key/value pairs, the example
260+
For a span with the following attributes key/value pairs, the above
261261
Flow configuration will not change the span name.
262262
This is because the attribute key `operation` isn't set:
263-
```
263+
```json
264264
{
265265
"db.svc": "location",
266266
"id": "1234"
@@ -269,7 +269,7 @@ This is because the attribute key `operation` isn't set:
269269

270270
### Creating a new span name from attribute values (no separator)
271271

272-
```
272+
```river
273273
otelcol.processor.span "default" {
274274
name {
275275
from_attributes = ["db.svc", "operation", "id"]
@@ -281,9 +281,9 @@ otelcol.processor.span "default" {
281281
}
282282
```
283283

284-
For a span with the following attributes key/value pairs, the example
284+
For a span with the following attributes key/value pairs, the above
285285
Flow configuration will change the span name to `"locationget1234"`:
286-
```
286+
```json
287287
{
288288
"db.svc": "location",
289289
"operation": "get",
@@ -298,7 +298,7 @@ Example input and output using the Flow configuration below:
298298
2. The span name will be changed to `/api/v1/document/{documentId}/update`
299299
3. A new attribute `"documentId"="12345678"` will be added to the span.
300300

301-
```
301+
```river
302302
otelcol.processor.span "default" {
303303
name {
304304
to_attributes {
@@ -321,7 +321,7 @@ if the span has the following properties:
321321
- The span name contains `/` anywhere in the string.
322322
- The span name is not `donot/change`.
323323

324-
```
324+
```river
325325
otelcol.processor.span "default" {
326326
include {
327327
match_type = "regexp"
@@ -348,7 +348,7 @@ otelcol.processor.span "default" {
348348

349349
This example changes the status of a span to "Error" and sets an error description.
350350

351-
```
351+
```river
352352
otelcol.processor.span "default" {
353353
status {
354354
code = "Error"
@@ -366,7 +366,7 @@ otelcol.processor.span "default" {
366366
This example sets the status to success only when attribute `http.status_code`
367367
is equal to `400`.
368368

369-
```
369+
```river
370370
otelcol.processor.span "default" {
371371
include {
372372
match_type = "strict"

pkg/traces/promsdprocessor/consumer/consumer.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,12 @@ func GetHostFromLabels(labels discovery.Target) (string, error) {
247247
return host, nil
248248
}
249249

250-
func CleanupLabels(labels discovery.Target) {
251-
for k := range labels {
252-
if strings.HasPrefix(k, "__") {
253-
delete(labels, k)
250+
func NewTargetsWithNonInternalLabels(labels discovery.Target) discovery.Target {
251+
res := make(discovery.Target)
252+
for k, v := range labels {
253+
if !strings.HasPrefix(k, "__") {
254+
res[k] = v
254255
}
255256
}
257+
return res
256258
}

pkg/traces/promsdprocessor/prom_sd_processor.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ func (p *promServiceDiscoProcessor) syncTargets(jobName string, group *targetgro
172172
continue
173173
}
174174

175-
promsdconsumer.CleanupLabels(labels)
176-
177175
level.Debug(p.logger).Log("msg", "adding host to hostLabels", "host", host)
178-
hostLabels[host] = labels
176+
hostLabels[host] = promsdconsumer.NewTargetsWithNonInternalLabels(labels)
179177
}
180178
}

0 commit comments

Comments
 (0)