go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

fix(redisotel): fix buggy append in reportPoolStats

Open wzy9607 opened this issue 1 year ago • 5 comments

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/[email protected]#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 avatar Sep 15 '24 09:09 wzy9607

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

ndyakov avatar Feb 10 '25 18:02 ndyakov

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.

wzy9607 avatar Mar 09 '25 08:03 wzy9607

Yes, it does indeed have flaws and potential risks.

monkey92t avatar Mar 25 '25 02:03 monkey92t

@monkey92t should we merge?

ndyakov avatar Apr 02 '25 12:04 ndyakov

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

wzy9607 avatar May 31 '25 11:05 wzy9607

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

ndyakov avatar Jul 30 '25 12:07 ndyakov

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

Conflicts resolved.

wzy9607 avatar Jul 30 '25 14:07 wzy9607