Skip to content

Commit

Permalink
[exporter/prometheusremotewriteexporter] Do not modify attributes Map…
Browse files Browse the repository at this point in the history
… in prometheusRW (#9736)

* Do not modify attributes Map in prometheusRW

Modifying data in the prometheus remote write causes a race when more
than one exporters is accessing data.
To avoid this, all operations are done on cloned data.

* Update CHANGELOG.md
  • Loading branch information
kwapik authored May 5, 2022
1 parent 2e78267 commit 910f9c4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- `prometheusreceiver`: Fix the memory issue introduced in the 0.49.0 release (#9718)
- `couchdbreceiver`: Fix issue where the receiver would not respect custom metric settings (#9598)
- `nginxreceiver`: Include nginxreceiver in components (#9572)
- `pkg/translator/prometheusremotewrite`: Fix data race when used with other exporters (#9736)

## v0.50.0

Expand Down
8 changes: 6 additions & 2 deletions pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ func createAttributes(resource pcommon.Resource, attributes pcommon.Map, externa

// Ensure attributes are sorted by key for consistent merging of keys which
// collide when sanitized.
attributes.Sort()
attributes.Range(func(key string, value pcommon.Value) bool {
// Sorting is done on a cloned map, as the original attributes map can read at
// the same time in different places.
cloneAttributes := pcommon.NewMap()
attributes.CopyTo(cloneAttributes)
cloneAttributes.Sort()
cloneAttributes.Range(func(key string, value pcommon.Value) bool {
var finalKey = sanitize(key)
if existingLabel, alreadyExists := l[finalKey]; alreadyExists {
existingLabel.Value = existingLabel.Value + ";" + value.AsString()
Expand Down

0 comments on commit 910f9c4

Please sign in to comment.