@@ -2,6 +2,7 @@ package redisotel
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net"
78 "sync"
@@ -271,9 +272,10 @@ func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
271272
272273 dur := time.Since(start)
273274
274- attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+1 )
275+ attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2 )
275276 attrs = append(attrs, mh.attrs...)
276277 attrs = append(attrs, statusAttr(err))
278+ attrs = append(attrs, cancelledAttr(err))
277279
278280 mh.createTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
279281 return conn, err
@@ -288,10 +290,11 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
288290
289291 dur := time.Since(start)
290292
291- attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2 )
293+ attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+3 )
292294 attrs = append(attrs, mh.attrs...)
293295 attrs = append(attrs, attribute.String("type", "command"))
294296 attrs = append(attrs, statusAttr(err))
297+ attrs = append(attrs, cancelledAttr(err))
295298
296299 mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
297300
@@ -309,10 +312,11 @@ func (mh *metricsHook) ProcessPipelineHook(
309312
310313 dur := time.Since(start)
311314
312- attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2 )
315+ attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+3 )
313316 attrs = append(attrs, mh.attrs...)
314317 attrs = append(attrs, attribute.String("type", "pipeline"))
315318 attrs = append(attrs, statusAttr(err))
319+ attrs = append(attrs, cancelledAttr(err))
316320
317321 mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
318322
@@ -330,3 +334,10 @@ func statusAttr(err error) attribute.KeyValue {
330334 }
331335 return attribute.String("status", "ok")
332336}
337+
338+ func cancelledAttr(err error) attribute.KeyValue {
339+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
340+ return attribute.Bool("canceled", true)
341+ }
342+ return attribute.Bool("canceled", false)
343+ }
0 commit comments