@@ -11,7 +11,7 @@ import (
1111 "go.opentelemetry.io/otel/attribute"
1212 "go.opentelemetry.io/otel/metric"
1313 "go.opentelemetry.io/otel/metric/instrument"
14- "go.opentelemetry.io/otel/metric/instrument/syncint64 "
14+ "go.opentelemetry.io/otel/metric/instrument/syncfloat64 "
1515)
1616
1717// InstrumentMetrics starts reporting OpenTelemetry Metrics.
@@ -64,6 +64,12 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
6464 return nil
6565 case * redis.Ring :
6666 rdb .OnNewNode (func (rdb * redis.Client ) {
67+ if conf .poolName == "" {
68+ opt := rdb .Options ()
69+ conf .poolName = opt .Addr
70+ }
71+ conf .attrs = append (conf .attrs , attribute .String ("pool.name" , conf .poolName ))
72+
6773 if err := reportPoolStats (rdb , conf ); err != nil {
6874 otel .Handle (err )
6975 }
@@ -134,9 +140,9 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
134140 func (ctx context.Context ) {
135141 stats := rdb .PoolStats ()
136142
137- idleMax .Observe (ctx , int64 (redisConf .MinIdleConns ) )
138- idleMin .Observe (ctx , int64 (redisConf .MaxIdleConns ) )
139- connsMax .Observe (ctx , int64 (redisConf .PoolSize ))
143+ idleMax .Observe (ctx , int64 (redisConf .MaxIdleConns ), labels ... )
144+ idleMin .Observe (ctx , int64 (redisConf .MinIdleConns ), labels ... )
145+ connsMax .Observe (ctx , int64 (redisConf .PoolSize ), labels ... )
140146
141147 usage .Observe (ctx , int64 (stats .IdleConns ), idleAttrs ... )
142148 usage .Observe (ctx , int64 (stats .TotalConns - stats .IdleConns ), usedAttrs ... )
@@ -147,7 +153,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
147153}
148154
149155func addMetricsHook (rdb * redis.Client , conf * config ) error {
150- createTime , err := conf .meter .SyncInt64 ().Histogram (
156+ createTime , err := conf .meter .SyncFloat64 ().Histogram (
151157 "db.client.connections.create_time" ,
152158 instrument .WithDescription ("The time it took to create a new connection." ),
153159 instrument .WithUnit ("ms" ),
@@ -156,7 +162,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
156162 return err
157163 }
158164
159- useTime , err := conf .meter .SyncInt64 ().Histogram (
165+ useTime , err := conf .meter .SyncFloat64 ().Histogram (
160166 "db.client.connections.use_time" ,
161167 instrument .WithDescription ("The time between borrowing a connection and returning it to the pool." ),
162168 instrument .WithUnit ("ms" ),
@@ -168,13 +174,15 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
168174 rdb .AddHook (& metricsHook {
169175 createTime : createTime ,
170176 useTime : useTime ,
177+ attrs : conf .attrs ,
171178 })
172179 return nil
173180}
174181
175182type metricsHook struct {
176- createTime syncint64.Histogram
177- useTime syncint64.Histogram
183+ createTime syncfloat64.Histogram
184+ useTime syncfloat64.Histogram
185+ attrs []attribute.KeyValue
178186}
179187
180188var _ redis.Hook = (* metricsHook )(nil )
@@ -185,7 +193,7 @@ func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
185193
186194 conn , err := hook (ctx , network , addr )
187195
188- mh .createTime .Record (ctx , time .Since (start ). Milliseconds () )
196+ mh .createTime .Record (ctx , milliseconds ( time .Since (start )), mh . attrs ... )
189197 return conn , err
190198 }
191199}
@@ -196,8 +204,14 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
196204
197205 err := hook (ctx , cmd )
198206
199- dur := time .Since (start ).Milliseconds ()
200- mh .useTime .Record (ctx , dur , attribute .String ("type" , "command" ), statusAttr (err ))
207+ dur := time .Since (start )
208+
209+ attrs := make ([]attribute.KeyValue , 0 , len (mh .attrs )+ 2 )
210+ attrs = append (attrs , mh .attrs ... )
211+ attrs = append (attrs , attribute .String ("type" , "command" ))
212+ attrs = append (attrs , statusAttr (err ))
213+
214+ mh .useTime .Record (ctx , milliseconds (dur ), attrs ... )
201215
202216 return err
203217 }
@@ -211,13 +225,23 @@ func (mh *metricsHook) ProcessPipelineHook(
211225
212226 err := hook (ctx , cmds )
213227
214- dur := time .Since (start ).Milliseconds ()
215- mh .useTime .Record (ctx , dur , attribute .String ("type" , "pipeline" ), statusAttr (err ))
228+ dur := time .Since (start )
229+
230+ attrs := make ([]attribute.KeyValue , 0 , len (mh .attrs )+ 2 )
231+ attrs = append (attrs , mh .attrs ... )
232+ attrs = append (attrs , attribute .String ("type" , "pipeline" ))
233+ attrs = append (attrs , statusAttr (err ))
234+
235+ mh .useTime .Record (ctx , milliseconds (dur ), attrs ... )
216236
217237 return err
218238 }
219239}
220240
241+ func milliseconds (d time.Duration ) float64 {
242+ return float64 (d ) / float64 (time .Millisecond )
243+ }
244+
221245func statusAttr (err error ) attribute.KeyValue {
222246 if err != nil {
223247 return attribute .String ("status" , "error" )
0 commit comments