mirror of
				https://github.com/redis/go-redis.git
				synced 2025-11-04 02:33:24 +03:00 
			
		
		
		
	feat(otel): Add a 'error_type' metrics attribute to separate context errors (#3566)
Co-authored-by: vragov_pf <vragov_pf@magnit.ru>
This commit is contained in:
		@@ -2,6 +2,7 @@ package redisotel
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
@@ -271,9 +272,10 @@ func (mh *metricsHook) DialHook(hook redis.DialHook) redis.DialHook {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		dur := time.Since(start)
 | 
							dur := time.Since(start)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+1)
 | 
							attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
 | 
				
			||||||
		attrs = append(attrs, mh.attrs...)
 | 
							attrs = append(attrs, mh.attrs...)
 | 
				
			||||||
		attrs = append(attrs, statusAttr(err))
 | 
							attrs = append(attrs, statusAttr(err))
 | 
				
			||||||
 | 
							attrs = append(attrs, errorTypeAttribute(err))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		mh.createTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
							mh.createTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
				
			||||||
		return conn, err
 | 
							return conn, err
 | 
				
			||||||
@@ -288,10 +290,11 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		dur := time.Since(start)
 | 
							dur := time.Since(start)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
 | 
							attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+3)
 | 
				
			||||||
		attrs = append(attrs, mh.attrs...)
 | 
							attrs = append(attrs, mh.attrs...)
 | 
				
			||||||
		attrs = append(attrs, attribute.String("type", "command"))
 | 
							attrs = append(attrs, attribute.String("type", "command"))
 | 
				
			||||||
		attrs = append(attrs, statusAttr(err))
 | 
							attrs = append(attrs, statusAttr(err))
 | 
				
			||||||
 | 
							attrs = append(attrs, errorTypeAttribute(err))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
							mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -309,10 +312,11 @@ func (mh *metricsHook) ProcessPipelineHook(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		dur := time.Since(start)
 | 
							dur := time.Since(start)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
 | 
							attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+3)
 | 
				
			||||||
		attrs = append(attrs, mh.attrs...)
 | 
							attrs = append(attrs, mh.attrs...)
 | 
				
			||||||
		attrs = append(attrs, attribute.String("type", "pipeline"))
 | 
							attrs = append(attrs, attribute.String("type", "pipeline"))
 | 
				
			||||||
		attrs = append(attrs, statusAttr(err))
 | 
							attrs = append(attrs, statusAttr(err))
 | 
				
			||||||
 | 
							attrs = append(attrs, errorTypeAttribute(err))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
							mh.useTime.Record(ctx, milliseconds(dur), metric.WithAttributeSet(attribute.NewSet(attrs...)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -330,3 +334,16 @@ func statusAttr(err error) attribute.KeyValue {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return attribute.String("status", "ok")
 | 
						return attribute.String("status", "ok")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func errorTypeAttribute(err error) attribute.KeyValue {
 | 
				
			||||||
 | 
						switch {
 | 
				
			||||||
 | 
						case err == nil:
 | 
				
			||||||
 | 
							return attribute.String("error_type", "none")
 | 
				
			||||||
 | 
						case errors.Is(err, context.Canceled):
 | 
				
			||||||
 | 
							return attribute.String("error_type", "context_canceled")
 | 
				
			||||||
 | 
						case errors.Is(err, context.DeadlineExceeded):
 | 
				
			||||||
 | 
							return attribute.String("error_type", "context_timeout")
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return attribute.String("error_type", "other")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user