Skip to content

Commit d9c83f4

Browse files
committed
use hmget to fetch counter values
1 parent 414072c commit d9c83f4

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

redis/metrics.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ type MetricsBackend struct {
3131
}
3232

3333
func (m *MetricsBackend) IncrementCounter(ctx context.Context, mt taskqueue.Metric, count int, ts time.Time) error {
34-
roundedTs := ts.Truncate(truncateDur)
35-
roundedTsStr := roundedTs.Format(time.RFC3339)
34+
roundedTs := ts.Truncate(truncateDur).Unix()
3635

3736
hashKey := redisHashKeyCounterMetrics(m.namespace, mt.Name, mt.Labels)
3837
zsetKey := redisZSetKeyCounterMetrics(m.namespace, mt.Name, mt.Labels)
3938

4039
_, err := m.client.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
41-
pipe.HIncrBy(ctx, hashKey, roundedTsStr, int64(count))
42-
pipe.ZAdd(ctx, zsetKey, redis.Z{Score: float64(roundedTs.Unix()), Member: roundedTsStr})
40+
pipe.HIncrBy(ctx, hashKey, strconv.FormatInt(roundedTs, 10), int64(count))
41+
pipe.ZAdd(ctx, zsetKey, redis.Z{Score: float64(roundedTs), Member: roundedTs})
4342
return nil
4443
})
4544

@@ -62,20 +61,31 @@ func (m *MetricsBackend) QueryRangeCounterValues(ctx context.Context, mt taskque
6261

6362
result := taskqueue.MetricRangeValue{Metric: mt}
6463

65-
for _, z := range zz {
64+
timestamps := make([]string, len(zz))
65+
for i, z := range zz {
6666
member, _ := z.Member.(string)
67-
if member == "" {
67+
timestamps[i] = member
68+
}
69+
70+
vals, err := m.client.HMGet(ctx, hashKey, timestamps...).Result()
71+
if err != nil {
72+
return taskqueue.MetricRangeValue{}, err
73+
}
74+
75+
for i, val := range vals {
76+
strVal, ok := val.(string)
77+
if !ok {
6878
continue
6979
}
7080

71-
val, err := m.client.HGet(ctx, hashKey, member).Int()
81+
v, err := strconv.Atoi(strVal)
7282
if err != nil {
7383
continue
7484
}
7585

7686
result.Values = append(result.Values, taskqueue.MetricValue{
77-
TimeStamp: time.Unix(int64(z.Score), 0),
78-
Value: float64(val),
87+
TimeStamp: time.Unix(int64(zz[i].Score), 0),
88+
Value: float64(v),
7989
})
8090
}
8191

0 commit comments

Comments
 (0)