@@ -8,6 +8,13 @@ import (
8
8
"github.com/redis/go-redis/v9/internal"
9
9
)
10
10
11
+ // contextKey is a custom type for context keys to avoid collisions
12
+ type contextKey string
13
+
14
+ const (
15
+ startTimeKey contextKey = "start_time"
16
+ )
17
+
11
18
// MetricsHook collects metrics about notification processing.
12
19
type MetricsHook struct {
13
20
NotificationCounts map [string ]int64
@@ -30,15 +37,15 @@ func (mh *MetricsHook) PreHook(ctx context.Context, notificationType string, not
30
37
31
38
// Store start time in context for duration calculation
32
39
startTime := time .Now ()
33
- _ = context .WithValue (ctx , "start_time" , startTime ) // Context not used further
40
+ _ = context .WithValue (ctx , startTimeKey , startTime ) // Context not used further
34
41
35
42
return notification , true
36
43
}
37
44
38
45
// PostHook records processing completion and any errors.
39
46
func (mh * MetricsHook ) PostHook (ctx context.Context , notificationType string , notification []interface {}, result error ) {
40
47
// Calculate processing duration
41
- if startTime , ok := ctx .Value ("start_time" ).(time.Time ); ok {
48
+ if startTime , ok := ctx .Value (startTimeKey ).(time.Time ); ok {
42
49
duration := time .Since (startTime )
43
50
mh .ProcessingTimes [notificationType ] = duration
44
51
}
0 commit comments