redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
20.06k stars 2.37k forks source link

[otel] Bugs when handling attributes #2803

Open sk- opened 11 months ago

sk- commented 11 months ago

Expected Behavior

I was reviewing the source code of the OpenTelemetry hooks and found two possible bugs related to how the attributes are being handled.

The first one is that every time a new node is detected, then we attach the pool name to the list of attributes

https://github.com/redis/go-redis/blob/21bd40a47e56e61c0598ea1bdf8e02e67d1aa651/extra/redisotel/metrics.go#L54

The second bug happens in the function reportPoolStats

https://github.com/redis/go-redis/blob/21bd40a47e56e61c0598ea1bdf8e02e67d1aa651/extra/redisotel/metrics.go#L86-L88

In this case depending on whether the initial labels slice has enough capacity we could get unexpected results, as the code it's not copying the values into a new slice.

See for example this code in the playground that shows the bug https://go.dev/play/p/XBHgyYioH4s

Or check this code in the same file, which correctly handles the attributes https://github.com/redis/go-redis/blob/21bd40a47e56e61c0598ea1bdf8e02e67d1aa651/extra/redisotel/metrics.go#L215-L218