Skip to content

Commit

Permalink
chore: coding style, add code for prometheus (#4002)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Mar 13, 2024
1 parent 09d7af7 commit cc2a7e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
46 changes: 23 additions & 23 deletions core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,13 @@ func (s *Redis) BitOpAnd(destKey string, keys ...string) (int64, error) {
}

// BitOpAndCtx is redis bit operation (and) command implementation.
func (s *Redis) BitOpAndCtx(ctx context.Context, destKey string, keys ...string) (val int64, err error) {
func (s *Redis) BitOpAndCtx(ctx context.Context, destKey string, keys ...string) (int64, error) {
conn, err := getRedis(s)
if err != nil {
return
return 0, err
}

val, err = conn.BitOpAnd(ctx, destKey, keys...).Result()
return
return conn.BitOpAnd(ctx, destKey, keys...).Result()
}

// BitOpNot is redis bit operation (not) command implementation.
Expand All @@ -190,14 +189,13 @@ func (s *Redis) BitOpNot(destKey, key string) (int64, error) {
}

// BitOpNotCtx is redis bit operation (not) command implementation.
func (s *Redis) BitOpNotCtx(ctx context.Context, destKey, key string) (val int64, err error) {
func (s *Redis) BitOpNotCtx(ctx context.Context, destKey, key string) (int64, error) {
conn, err := getRedis(s)
if err != nil {
return
return 0, err
}

val, err = conn.BitOpNot(ctx, destKey, key).Result()
return
return conn.BitOpNot(ctx, destKey, key).Result()
}

// BitOpOr is redis bit operation (or) command implementation.
Expand Down Expand Up @@ -463,7 +461,8 @@ func (s *Redis) GeoAdd(key string, geoLocation ...*GeoLocation) (int64, error) {
}

// GeoAddCtx is the implementation of redis geoadd command.
func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) (int64, error) {
func (s *Redis) GeoAddCtx(ctx context.Context, key string, geoLocation ...*GeoLocation) (
int64, error) {
conn, err := getRedis(s)
if err != nil {
return 0, err
Expand All @@ -478,7 +477,8 @@ func (s *Redis) GeoDist(key, member1, member2, unit string) (float64, error) {
}

// GeoDistCtx is the implementation of redis geodist command.
func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) (float64, error) {
func (s *Redis) GeoDistCtx(ctx context.Context, key, member1, member2, unit string) (
float64, error) {
conn, err := getRedis(s)
if err != nil {
return 0, err
Expand Down Expand Up @@ -821,7 +821,7 @@ func (s *Redis) HmsetCtx(ctx context.Context, key string, fieldsAndValues map[st

// Hscan is the implementation of redis hscan command.
func (s *Redis) Hscan(key string, cursor uint64, match string, count int64) (
keys []string, cur uint64, err error) {
[]string, uint64, error) {
return s.HscanCtx(context.Background(), key, cursor, match, count)
}

Expand Down Expand Up @@ -1256,7 +1256,7 @@ func (s *Redis) SaddCtx(ctx context.Context, key string, values ...any) (int, er
}

// Scan is the implementation of redis scan command.
func (s *Redis) Scan(cursor uint64, match string, count int64) (keys []string, cur uint64, err error) {
func (s *Redis) Scan(cursor uint64, match string, count int64) ([]string, uint64, error) {
return s.ScanCtx(context.Background(), cursor, match, count)
}

Expand Down Expand Up @@ -1293,7 +1293,7 @@ func (s *Redis) SetBitCtx(ctx context.Context, key string, offset int64, value i

// Sscan is the implementation of redis sscan command.
func (s *Redis) Sscan(key string, cursor uint64, match string, count int64) (
keys []string, cur uint64, err error) {
[]string, uint64, error) {
return s.SscanCtx(context.Background(), key, cursor, match, count)
}

Expand Down Expand Up @@ -1344,7 +1344,8 @@ func (s *Redis) ScriptRun(script *Script, keys []string, args ...any) (any, erro
}

// ScriptRunCtx is the implementation of *redis.Script run command.
func (s *Redis) ScriptRunCtx(ctx context.Context, script *Script, keys []string, args ...any) (any, error) {
func (s *Redis) ScriptRunCtx(ctx context.Context, script *Script, keys []string,
args ...any) (any, error) {
conn, err := getRedis(s)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1638,8 +1639,7 @@ func (s *Redis) Zadd(key string, score int64, value string) (bool, error) {
}

// ZaddCtx is the implementation of redis zadd command.
func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (
val bool, err error) {
func (s *Redis) ZaddCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
return s.ZaddFloatCtx(ctx, key, float64(score), value)
}

Expand Down Expand Up @@ -1668,13 +1668,12 @@ func (s *Redis) ZaddFloatCtx(ctx context.Context, key string, score float64, val
}

// Zaddnx is the implementation of redis zadd nx command.
func (s *Redis) Zaddnx(key string, score int64, value string) (val bool, err error) {
func (s *Redis) Zaddnx(key string, score int64, value string) (bool, error) {
return s.ZaddnxCtx(context.Background(), key, score, value)
}

// ZaddnxCtx is the implementation of redis zadd nx command.
func (s *Redis) ZaddnxCtx(ctx context.Context, key string, score int64, value string) (
val bool, err error) {
func (s *Redis) ZaddnxCtx(ctx context.Context, key string, score int64, value string) (bool, error) {
return s.ZaddnxFloatCtx(ctx, key, float64(score), value)
}

Expand Down Expand Up @@ -1822,7 +1821,7 @@ func (s *Redis) ZscoreByFloatCtx(ctx context.Context, key, value string) (float6

// Zscan is the implementation of redis zscan command.
func (s *Redis) Zscan(key string, cursor uint64, match string, count int64) (
keys []string, cur uint64, err error) {
[]string, uint64, error) {
return s.ZscanCtx(context.Background(), key, cursor, match, count)
}

Expand Down Expand Up @@ -1987,7 +1986,7 @@ func (s *Redis) ZrevrangeWithScores(key string, start, stop int64) ([]Pair, erro
// ZRevRangeWithScoresCtx is the implementation of redis zrevrange command with scores.
// Deprecated: use ZrevrangeWithScoresCtx instead.
func (s *Redis) ZRevRangeWithScoresCtx(ctx context.Context, key string, start, stop int64) (
val []Pair, err error) {
[]Pair, error) {
return s.ZrevrangeWithScoresCtx(ctx, key, start, stop)
}

Expand Down Expand Up @@ -2021,7 +2020,7 @@ func (s *Redis) ZrevrangeWithScoresByFloat(key string, start, stop int64) ([]Flo
// ZRevRangeWithScoresByFloatCtx is the implementation of redis zrevrange command with scores by float.
// Deprecated: use ZrevrangeWithScoresByFloatCtx instead.
func (s *Redis) ZRevRangeWithScoresByFloatCtx(ctx context.Context, key string, start, stop int64) (
val []FloatPair, err error) {
[]FloatPair, error) {
return s.ZrevrangeWithScoresByFloatCtx(ctx, key, start, stop)
}

Expand Down Expand Up @@ -2066,7 +2065,8 @@ func (s *Redis) ZrangebyscoreWithScoresCtx(ctx context.Context, key string, star
}

// ZrangebyscoreWithScoresByFloat is the implementation of redis zrangebyscore command with scores by float.
func (s *Redis) ZrangebyscoreWithScoresByFloat(key string, start, stop float64) ([]FloatPair, error) {
func (s *Redis) ZrangebyscoreWithScoresByFloat(key string, start, stop float64) (
[]FloatPair, error) {
return s.ZrangebyscoreWithScoresByFloatCtx(context.Background(), key, start, stop)
}

Expand Down
9 changes: 5 additions & 4 deletions rest/handler/prometheushandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
Subsystem: "requests",
Name: "duration_ms",
Help: "http server requests duration(ms).",
Labels: []string{"path", "method"},
Labels: []string{"path", "method", "code"},
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 750, 1000},
})

Expand All @@ -26,7 +26,7 @@ var (
Subsystem: "requests",
Name: "code_total",
Help: "http server requests error count.",
Labels: []string{"path", "code", "method"},
Labels: []string{"path", "method", "code"},
})
)

Expand All @@ -37,8 +37,9 @@ func PrometheusHandler(path, method string) func(http.Handler) http.Handler {
startTime := timex.Now()
cw := response.NewWithCodeResponseWriter(w)
defer func() {
metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method)
metricServerReqCodeTotal.Inc(path, strconv.Itoa(cw.Code), method)
code := strconv.Itoa(cw.Code)
metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method, code)
metricServerReqCodeTotal.Inc(path, method, code)
}()

next.ServeHTTP(cw, r)
Expand Down

0 comments on commit cc2a7e9

Please sign in to comment.