Skip to content

Commit 7f48276

Browse files
pvragovvragov_pf
andauthored
feat(otel): Add a 'error_type' metrics attribute to separate context errors (#3566)
Co-authored-by: vragov_pf <[email protected]>
1 parent 5771fa4 commit 7f48276

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

extra/redisotel/metrics.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redisotel
22

33
import (
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, errorTypeAttribute(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, errorTypeAttribute(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, errorTypeAttribute(err))
316320

317321
mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
318322

@@ -330,3 +334,16 @@ func statusAttr(err error) attribute.KeyValue {
330334
}
331335
return attribute.String("status", "ok")
332336
}
337+
338+
func errorTypeAttribute(err error) attribute.KeyValue {
339+
switch {
340+
case err == nil:
341+
return attribute.String("error_type", "none")
342+
case errors.Is(err, context.Canceled):
343+
return attribute.String("error_type", "context_canceled")
344+
case errors.Is(err, context.DeadlineExceeded):
345+
return attribute.String("error_type", "context_timeout")
346+
default:
347+
return attribute.String("error_type", "other")
348+
}
349+
}

0 commit comments

Comments
 (0)