Merge remote-tracking branch 'fork/add_labels' into add_labels
Some checks failed
autoapprove / autoapprove (pull_request) Successful in 18s
codeql / analyze (go) (pull_request) Failing after 1m15s
prbuild / test (pull_request) Failing after 5m26s
prbuild / lint (pull_request) Successful in 9m54s
automerge / automerge (pull_request) Failing after 12m54s
dependabot-automerge / automerge (pull_request) Failing after 12m53s

This commit is contained in:
Денис Евстигнеев 2024-11-26 13:15:25 +03:00
commit a718c1dc61

View File

@ -2,6 +2,7 @@ package redis
import ( import (
"context" "context"
"errors"
"reflect" "reflect"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -130,6 +131,8 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOpti
b := r.pool.Get() b := r.pool.Get()
defer r.pool.Put(b) defer r.pool.Put(b)
options := store.NewExistsOptions(opts...) options := store.NewExistsOptions(opts...)
labels := make([]string, 0, 6)
labels = append(labels, "name", options.Name, "statement", "exists")
timeout := r.opts.Timeout timeout := r.opts.Timeout
if options.Timeout > 0 { if options.Timeout > 0 {
@ -144,21 +147,22 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOpti
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key) rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
ts := time.Now() ts := time.Now()
val, err := r.cli.Exists(ctx, rkey).Result() val, err := r.cli.Exists(ctx, rkey).Result()
setSpanError(ctx, err) setSpanError(ctx, err)
te := time.Since(ts) te := time.Since(ts)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds())
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
if err == goredis.Nil || (err == nil && val == 0) { r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() if errors.Is(err, goredis.Nil) || (err == nil && val == 0) {
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
return store.ErrNotFound return store.ErrNotFound
} else if err == nil { } else if err == nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
} else if err != nil { } else if err != nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
return err return err
} }
@ -176,6 +180,8 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
defer r.pool.Put(b) defer r.pool.Put(b)
options := store.NewReadOptions(opts...) options := store.NewReadOptions(opts...)
labels := make([]string, 0, 6)
labels = append(labels, "name", options.Name, "statement", "read")
timeout := r.opts.Timeout timeout := r.opts.Timeout
if options.Timeout > 0 { if options.Timeout > 0 {
@ -190,21 +196,21 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...s
rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key) rkey := r.getKey(b, r.opts.Namespace, options.Namespace, key)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
ts := time.Now() ts := time.Now()
buf, err := r.cli.Get(ctx, rkey).Bytes() buf, err := r.cli.Get(ctx, rkey).Bytes()
setSpanError(ctx, err) setSpanError(ctx, err)
te := time.Since(ts) te := time.Since(ts)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
if err == goredis.Nil || (err == nil && buf == nil) { if errors.Is(err, goredis.Nil) || (err == nil && buf == nil) {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
return store.ErrNotFound return store.ErrNotFound
} else if err == nil { } else if err == nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
} else if err != nil { } else if err != nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
return err return err
} }
@ -399,6 +405,8 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
defer r.pool.Put(b) defer r.pool.Put(b)
options := store.NewDeleteOptions(opts...) options := store.NewDeleteOptions(opts...)
labels := make([]string, 0, 6)
labels = append(labels, "name", options.Name, "statement", "delete")
timeout := r.opts.Timeout timeout := r.opts.Timeout
if options.Timeout > 0 { if options.Timeout > 0 {
@ -411,21 +419,22 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOpti
defer cancel() defer cancel()
} }
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
ts := time.Now() ts := time.Now()
err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err() err := r.cli.Del(ctx, r.getKey(b, r.opts.Namespace, options.Namespace, key)).Err()
te := time.Since(ts) te := time.Since(ts)
setSpanError(ctx, err) setSpanError(ctx, err)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec()
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
if err == goredis.Nil { r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() if errors.Is(err, goredis.Nil) {
r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
return store.ErrNotFound return store.ErrNotFound
} else if err == nil { } else if err == nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
} else if err != nil { } else if err != nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
return err return err
} }
@ -532,6 +541,8 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
defer r.pool.Put(b) defer r.pool.Put(b)
options := store.NewWriteOptions(opts...) options := store.NewWriteOptions(opts...)
labels := make([]string, 0, 6)
labels = append(labels, "name", options.Name, "statement", "write")
timeout := r.opts.Timeout timeout := r.opts.Timeout
if options.Timeout > 0 { if options.Timeout > 0 {
@ -560,22 +571,22 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
} }
} }
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Inc() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Inc()
ts := time.Now() ts := time.Now()
err := r.cli.Set(ctx, rkey, buf, options.TTL).Err() err := r.cli.Set(ctx, rkey, buf, options.TTL).Err()
te := time.Since(ts) te := time.Since(ts)
setSpanError(ctx, err) setSpanError(ctx, err)
r.opts.Meter.Counter(semconv.StoreRequestInflight, "name", options.Name).Dec() r.opts.Meter.Counter(semconv.StoreRequestInflight, labels...).Dec()
r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Summary(semconv.StoreRequestLatencyMicroseconds, labels...).Update(te.Seconds())
r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, "name", options.Name).Update(te.Seconds()) r.opts.Meter.Histogram(semconv.StoreRequestDurationSeconds, labels...).Update(te.Seconds())
if err == goredis.Nil { if errors.Is(err, goredis.Nil) {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "miss").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "miss")...).Inc()
return store.ErrNotFound return store.ErrNotFound
} else if err == nil { } else if err == nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "hit").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "hit")...).Inc()
} else if err != nil { } else if err != nil {
r.opts.Meter.Counter(semconv.StoreRequestTotal, "name", options.Name, "status", "failure").Inc() r.opts.Meter.Counter(semconv.StoreRequestTotal, append(labels, "status", "failure")...).Inc()
return err return err
} }