Skip to content

fix(redisotel): fix buggy append in reportPoolStats#3122

Merged
ndyakov merged 3 commits into
redis:masterfrom
wzy9607:otel
Aug 5, 2025
Merged

fix(redisotel): fix buggy append in reportPoolStats#3122
ndyakov merged 3 commits into
redis:masterfrom
wzy9607:otel

Conversation

@wzy9607

@wzy9607 wzy9607 commented Sep 15, 2024

Copy link
Copy Markdown
Contributor

The current append twice to conf.attrs approach in reportPoolStats may result in unexpected idleAttrs, due to append can mutate the underlying array of the original slice, as demonstrated at https://go.dev/play/p/jwRMofH91eQ?v=goprev and below.

Also, I replaced metric.WithAttributes in reportPoolStats with metric.WithAttributeSet, since WithAttributes is just WithAttributeSet with some extra works that are not needed here, see https://pkg.go.dev/go.opentelemetry.io/otel/metric@v1.22.0#WithAttributes.

demonstration of the bug

package main

import (
	"fmt"

	"go.opentelemetry.io/otel/attribute"
)

func main() {
	a := make([]attribute.KeyValue, 0, 4)
	a = append(a, attribute.String("a", "a"), attribute.String("b", "b"), attribute.String("c", "c"))
	idle := append(a, attribute.String("state", "idle"))
	used := append(a, attribute.String("state", "used"))
	fmt.Printf("a: %v\n", a)
	fmt.Printf("idle: %v\n", idle)
	fmt.Printf("used: %v\n", used)
}

outputs

a: [{a {4 0 a <nil>}} {b {4 0 b <nil>}} {c {4 0 c <nil>}}]
idle: [{a {4 0 a <nil>}} {b {4 0 b <nil>}} {c {4 0 c <nil>}} {state {4 0 used <nil>}}]
used: [{a {4 0 a <nil>}} {b {4 0 b <nil>}} {c {4 0 c <nil>}} {state {4 0 used <nil>}}]

while the intended result in reportPoolStats usecase is
idle: [{a {4 0 a <nil>}} {b {4 0 b <nil>}} {c {4 0 c <nil>}} {state {4 0 idle <nil>}}].

@wzy9607 wzy9607 changed the title redisotel: fix buggy append in reportPoolStats and use WithAttributeSet redisotel: fix buggy append in reportPoolStats Sep 15, 2024
@wzy9607 wzy9607 changed the title redisotel: fix buggy append in reportPoolStats fix(redisotel): fix buggy append in reportPoolStats Sep 15, 2024
@wzy9607 wzy9607 force-pushed the otel branch 3 times, most recently from 4ff1439 to fc5be74 Compare September 15, 2024 10:26
@ndyakov

ndyakov commented Feb 10, 2025

Copy link
Copy Markdown
Member

Hello @wzy9607, thank you for the contribution. Can you please add a test for that change with the idleAttrs. Interesting bug and nice catch!

@wzy9607 wzy9607 force-pushed the otel branch 3 times, most recently from 3426c02 to b9ac847 Compare March 9, 2025 08:30
@wzy9607

wzy9607 commented Mar 9, 2025

Copy link
Copy Markdown
Contributor Author

Hello @wzy9607, thank you for the contribution. Can you please add a test for that change with the idleAttrs. Interesting bug and nice catch!

done.

@monkey92t

Copy link
Copy Markdown
Collaborator

Yes, it does indeed have flaws and potential risks.

ndyakov
ndyakov previously approved these changes Apr 2, 2025
@ndyakov

ndyakov commented Apr 2, 2025

Copy link
Copy Markdown
Member

@monkey92t should we merge?

@wzy9607

wzy9607 commented May 31, 2025

Copy link
Copy Markdown
Contributor Author

I have solved the merge conflicts, can you take a look again? @monkey92t @ndyakov

@ndyakov

ndyakov commented Jul 30, 2025

Copy link
Copy Markdown
Member

@wzy9607 i don't see a response from @monkey92t . Feel free to resolve conflicts and we can merge.

The current append twice to `conf.attrs` approach in `reportPoolStats` may result in unexpected idleAttrs,
due to `append` [can mutate](golang/go#29115 (comment)) the underlying array of the original slice,
as demonstrated at <https://go.dev/play/p/jwRMofH91eQ?v=goprev>.

Also, I replaced `metric.WithAttributes` in `reportPoolStats` with `metric.WithAttributeSet`,
since `WithAttributes` is just `WithAttributeSet` with some extra works that are not needed here,
see <https://pkg.go.dev/go.opentelemetry.io/otel/metric@v1.22.0#WithAttributes>.
@wzy9607

wzy9607 commented Jul 30, 2025

Copy link
Copy Markdown
Contributor Author

@wzy9607 i don't see a response from @monkey92t . Feel free to resolve conflicts and we can merge.

Conflicts resolved.

@ndyakov ndyakov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the discussions looks like it is safe to include this. Thank you, @wzy9607.

@ndyakov ndyakov merged commit 4767d9d into redis:master Aug 5, 2025
33 of 34 checks passed
ofekshenawa pushed a commit that referenced this pull request Aug 10, 2025
The current append twice to `conf.attrs` approach in `reportPoolStats` may result in unexpected idleAttrs,
due to `append` [can mutate](golang/go#29115 (comment)) the underlying array of the original slice,
as demonstrated at <https://go.dev/play/p/jwRMofH91eQ?v=goprev>.

Also, I replaced `metric.WithAttributes` in `reportPoolStats` with `metric.WithAttributeSet`,
since `WithAttributes` is just `WithAttributeSet` with some extra works that are not needed here,
see <https://pkg.go.dev/go.opentelemetry.io/otel/metric@v1.22.0#WithAttributes>.

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants