@@ -11,7 +11,7 @@ import (
11
11
"go.opentelemetry.io/otel/attribute"
12
12
"go.opentelemetry.io/otel/metric"
13
13
"go.opentelemetry.io/otel/metric/instrument"
14
- "go.opentelemetry.io/otel/metric/instrument/syncint64 "
14
+ "go.opentelemetry.io/otel/metric/instrument/syncfloat64 "
15
15
)
16
16
17
17
// InstrumentMetrics starts reporting OpenTelemetry Metrics.
@@ -64,6 +64,12 @@ func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error {
64
64
return nil
65
65
case * redis.Ring :
66
66
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
+
67
73
if err := reportPoolStats (rdb , conf ); err != nil {
68
74
otel .Handle (err )
69
75
}
@@ -134,9 +140,9 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
134
140
func (ctx context.Context ) {
135
141
stats := rdb .PoolStats ()
136
142
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 ... )
140
146
141
147
usage .Observe (ctx , int64 (stats .IdleConns ), idleAttrs ... )
142
148
usage .Observe (ctx , int64 (stats .TotalConns - stats .IdleConns ), usedAttrs ... )
@@ -147,7 +153,7 @@ func reportPoolStats(rdb *redis.Client, conf *config) error {
147
153
}
148
154
149
155
func addMetricsHook (rdb * redis.Client , conf * config ) error {
150
- createTime , err := conf .meter .SyncInt64 ().Histogram (
156
+ createTime , err := conf .meter .SyncFloat64 ().Histogram (
151
157
"db.client.connections.create_time" ,
152
158
instrument .WithDescription ("The time it took to create a new connection." ),
153
159
instrument .WithUnit ("ms" ),
@@ -156,7 +162,7 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
156
162
return err
157
163
}
158
164
159
- useTime , err := conf .meter .SyncInt64 ().Histogram (
165
+ useTime , err := conf .meter .SyncFloat64 ().Histogram (
160
166
"db.client.connections.use_time" ,
161
167
instrument .WithDescription ("The time between borrowing a connection and returning it to the pool." ),
162
168
instrument .WithUnit ("ms" ),
@@ -168,13 +174,15 @@ func addMetricsHook(rdb *redis.Client, conf *config) error {
168
174
rdb .AddHook (& metricsHook {
169
175
createTime : createTime ,
170
176
useTime : useTime ,
177
+ attrs : conf .attrs ,
171
178
})
172
179
return nil
173
180
}
174
181
175
182
type metricsHook struct {
176
- createTime syncint64.Histogram
177
- useTime syncint64.Histogram
183
+ createTime syncfloat64.Histogram
184
+ useTime syncfloat64.Histogram
185
+ attrs []attribute.KeyValue
178
186
}
179
187
180
188
var _ redis.Hook = (* metricsHook )(nil )
@@ -185,7 +193,7 @@ func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
185
193
186
194
conn , err := hook (ctx , network , addr )
187
195
188
- mh .createTime .Record (ctx , time .Since (start ). Milliseconds () )
196
+ mh .createTime .Record (ctx , milliseconds ( time .Since (start )), mh . attrs ... )
189
197
return conn , err
190
198
}
191
199
}
@@ -196,8 +204,14 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
196
204
197
205
err := hook (ctx , cmd )
198
206
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 ... )
201
215
202
216
return err
203
217
}
@@ -211,13 +225,23 @@ func (mh *metricsHook) ProcessPipelineHook(
211
225
212
226
err := hook (ctx , cmds )
213
227
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 ... )
216
236
217
237
return err
218
238
}
219
239
}
220
240
241
+ func milliseconds (d time.Duration ) float64 {
242
+ return float64 (d ) / float64 (time .Millisecond )
243
+ }
244
+
221
245
func statusAttr (err error ) attribute.KeyValue {
222
246
if err != nil {
223
247
return attribute .String ("status" , "error" )
0 commit comments