8
8
"fmt"
9
9
"time"
10
10
11
- "github.com/OdyseeTeam/odysee-api/internal/metrics"
12
11
"github.com/OdyseeTeam/odysee-api/internal/monitor"
13
12
"github.com/OdyseeTeam/odysee-api/pkg/rpcerrors"
14
13
@@ -35,9 +34,9 @@ type QueryCache struct {
35
34
}
36
35
37
36
func NewQueryCache (store cache.CacheInterface [any ]) * QueryCache {
38
- m := marshaler .New (store )
37
+ marshal := marshaler .New (store )
39
38
return & QueryCache {
40
- cache : m ,
39
+ cache : marshal ,
41
40
singleflight : & singleflight.Group {},
42
41
}
43
42
}
@@ -49,31 +48,34 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached
49
48
Params : query .Params (),
50
49
}
51
50
52
- ctx , cancel := context .WithTimeout (context .Background (), 1000 * time .Millisecond )
51
+ ctx , cancel := context .WithTimeout (context .Background (), 5000 * time .Millisecond )
53
52
defer cancel ()
54
53
54
+ start := time .Now ()
55
+
55
56
hit , err := c .cache .Get (ctx , cacheReq , & CachedResponse {})
56
57
if err != nil {
57
58
if ! errors .Is (err , & store.NotFound {}) {
58
- metrics . SturdyQueryCacheErrorCount . WithLabelValues ( cacheReq .Method ). Inc ( )
59
+ ObserveQueryCacheOperation ( CacheOperationGet , CacheResultError , cacheReq .Method , start )
59
60
return nil , fmt .Errorf ("error during cache.get: %w" , err )
60
61
}
61
62
62
- metrics . SturdyQueryCacheMissCount . WithLabelValues ( cacheReq .Method ). Inc ( )
63
+ ObserveQueryCacheOperation ( CacheOperationGet , CacheResultMiss , cacheReq .Method , start )
63
64
64
65
if getter == nil {
65
66
log .Warnf ("nil getter provided for %s" , cacheReq .Method )
66
- metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
67
67
return nil , nil
68
68
}
69
69
70
+ log .Infof ("cache miss for %s, key=%s, duration=%.2fs" , cacheReq .Method , cacheReq .GetCacheKey (), time .Since (start ).Seconds ())
70
71
// Cold object retrieval after cache miss
71
- log . Infof ( "cold object retrieval for %s [%s]" , cacheReq . Method , cacheReq . GetCacheKey () )
72
+ start := time . Now ( )
72
73
obj , err , _ := c .singleflight .Do (cacheReq .GetCacheKey (), getter )
73
74
if err != nil {
74
- metrics . SturdyQueryCacheErrorCount . WithLabelValues ( cacheReq .Method ). Inc ( )
75
+ ObserveQueryRetrievalDuration ( CacheResultError , cacheReq .Method , start )
75
76
return nil , fmt .Errorf ("error calling getter: %w" , err )
76
77
}
78
+ ObserveQueryRetrievalDuration (CacheResultSuccess , cacheReq .Method , start )
77
79
78
80
res , ok := obj .(* jsonrpc.RPCResponse )
79
81
if ! ok {
@@ -84,27 +86,29 @@ func (c *QueryCache) Retrieve(query *Query, getter func() (any, error)) (*Cached
84
86
if res .Error != nil {
85
87
log .Debugf ("rpc error received (%s), not caching" , cacheReq .Method )
86
88
} else {
89
+ start := time .Now ()
87
90
err = c .cache .Set (
88
91
ctx , cacheReq , cacheResp ,
89
92
store .WithExpiration (cacheReq .Expiration ()),
90
93
store .WithTags (cacheReq .Tags ()),
91
94
)
92
95
if err != nil {
93
- metrics . SturdyQueryCacheErrorCount . WithLabelValues ( cacheReq .Method ). Inc ( )
96
+ ObserveQueryCacheOperation ( CacheOperationSet , CacheResultError , cacheReq .Method , start )
94
97
monitor .ErrorToSentry (fmt .Errorf ("error during cache.set: %w" , err ), map [string ]string {"method" : cacheReq .Method })
95
- return cacheResp , fmt .Errorf ("error during cache.set: %w" , err )
98
+ log .Warnf ("error during cache.set: %s" , err )
99
+ return cacheResp , nil
96
100
}
101
+ ObserveQueryCacheOperation (CacheOperationSet , CacheResultSuccess , cacheReq .Method , start )
97
102
}
98
103
99
104
return cacheResp , nil
100
105
}
101
- log .Debugf ("cache hit for %s [%s]" , cacheReq .Method , cacheReq .GetCacheKey ())
106
+ log .Infof ("cache hit for %s, key=%s, duration=%.2fs" , cacheReq .Method , cacheReq .GetCacheKey (), time .Since (start ).Seconds ())
107
+ ObserveQueryCacheOperation (CacheOperationGet , CacheResultHit , cacheReq .Method , start )
102
108
cacheResp , ok := hit .(* CachedResponse )
103
109
if ! ok {
104
- metrics .SturdyQueryCacheErrorCount .WithLabelValues (cacheReq .Method ).Inc ()
105
110
return nil , errors .New ("unknown cache object retrieved" )
106
111
}
107
- metrics .SturdyQueryCacheHitCount .WithLabelValues (cacheReq .Method ).Inc ()
108
112
return cacheResp , nil
109
113
}
110
114
0 commit comments