Skip to content

Commit b58fdbd

Browse files
Improve metrics
1 parent 9e75379 commit b58fdbd

10 files changed

Lines changed: 95 additions & 37 deletions

File tree

codis/pkg/proxy/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ type Stats struct {
558558
PrimaryOnly bool `json:"primary_only"`
559559
} `json:"backend"`
560560

561-
Runtime *RuntimeStats `json:"runtime,omitempty"`
562-
TimeoutCmdNumber int64 `json:"timeout_cmd_number"`
561+
Runtime *RuntimeStats `json:"runtime,omitempty"`
562+
SlowCmdCount int64 `json:"slow_cmd_count"` // Cumulative count of slow log
563563
}
564564

565565
type RuntimeStats struct {
@@ -668,6 +668,6 @@ func (p *Proxy) Stats(flags StatsFlags) *Stats {
668668
stats.Runtime.NumCgoCall = runtime.NumCgoCall()
669669
stats.Runtime.MemOffheap = unsafe2.OffheapBytes()
670670
}
671-
stats.TimeoutCmdNumber = TimeoutCmdNumberInSecond.Int64()
671+
stats.SlowCmdCount = SlowCmdCount.Int64()
672672
return stats
673673
}

codis/pkg/proxy/session.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,14 @@ func (s *Session) loopWriter(tasks *RequestChan) (err error) {
236236
} else {
237237
s.incrOpStats(r, resp.Type)
238238
}
239+
nowTime := time.Now().UnixNano()
240+
duration := int64((nowTime - r.ReceiveTime) / 1e3)
241+
s.updateMaxDelay(duration, r)
239242
if fflush {
240243
s.flushOpStats(false)
241244
}
242-
nowTime := time.Now().UnixNano()
243-
duration := int64((nowTime - r.ReceiveTime) / 1e3)
244245
if duration >= s.config.SlowlogLogSlowerThan {
245-
TimeoutCmdNumber.Incr()
246+
SlowCmdCount.Incr()
246247
//client -> proxy -> server -> porxy -> client
247248
//Record the waiting time from receiving the request from the client to sending it to the backend server
248249
//the waiting time from sending the request to the backend server to receiving the response from the server
@@ -759,3 +760,10 @@ func (s *Session) handlePConfig(r *Request) error {
759760
}
760761
return nil
761762
}
763+
764+
func (s *Session) updateMaxDelay(duration int64, r *Request) {
765+
e := s.getOpStats(r.OpStr)
766+
if duration > e.maxDelay.Int64() {
767+
e.maxDelay.Set(duration)
768+
}
769+
}

codis/pkg/proxy/stats.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import (
1414
"pika/codis/v2/pkg/utils/sync2/atomic2"
1515
)
1616

17-
var (
18-
TimeoutCmdNumber atomic2.Int64
19-
TimeoutCmdNumberInSecond atomic2.Int64
20-
)
17+
var SlowCmdCount atomic2.Int64 // Cumulative count of slow log
2118

2219
type opStats struct {
2320
opstr string
@@ -27,14 +24,16 @@ type opStats struct {
2724
redis struct {
2825
errors atomic2.Int64
2926
}
27+
maxDelay atomic2.Int64
3028
}
3129

3230
func (s *opStats) OpStats() *OpStats {
3331
o := &OpStats{
34-
OpStr: s.opstr,
35-
Calls: s.calls.Int64(),
36-
Usecs: s.nsecs.Int64() / 1e3,
37-
Fails: s.fails.Int64(),
32+
OpStr: s.opstr,
33+
Calls: s.calls.Int64(),
34+
Usecs: s.nsecs.Int64() / 1e3,
35+
Fails: s.fails.Int64(),
36+
MaxDelay: s.maxDelay.Int64(),
3837
}
3938
if o.Calls != 0 {
4039
o.UsecsPercall = o.Usecs / o.Calls
@@ -50,6 +49,7 @@ type OpStats struct {
5049
UsecsPercall int64 `json:"usecs_percall"`
5150
Fails int64 `json:"fails"`
5251
RedisErrType int64 `json:"redis_errtype"`
52+
MaxDelay int64 `json:"max_delay"`
5353
}
5454

5555
var cmdstats struct {
@@ -67,8 +67,7 @@ var cmdstats struct {
6767

6868
func init() {
6969
cmdstats.opmap = make(map[string]*opStats, 128)
70-
TimeoutCmdNumber.Set(0)
71-
TimeoutCmdNumberInSecond.Set(0)
70+
SlowCmdCount.Set(0)
7271
go func() {
7372
for {
7473
start := time.Now()
@@ -77,9 +76,17 @@ func init() {
7776
delta := cmdstats.total.Int64() - total
7877
normalized := math.Max(0, float64(delta)) * float64(time.Second) / float64(time.Since(start))
7978
cmdstats.qps.Set(int64(normalized + 0.5))
79+
}
80+
}()
8081

81-
TimeoutCmdNumberInSecond.Swap(TimeoutCmdNumber.Int64())
82-
TimeoutCmdNumber.Set(0)
82+
go func() {
83+
for {
84+
time.Sleep(15 * time.Second)
85+
cmdstats.Lock()
86+
for _, s := range cmdstats.opmap {
87+
s.maxDelay = 0
88+
}
89+
cmdstats.Unlock()
8390
}
8491
}()
8592
}
@@ -175,6 +182,18 @@ func incrOpStats(e *opStats) {
175182
s.redis.errors.Add(n)
176183
cmdstats.redis.errors.Add(n)
177184
}
185+
186+
for {
187+
oldValue := s.maxDelay
188+
if e.maxDelay > oldValue {
189+
if s.maxDelay.CompareAndSwap(oldValue.Int64(), e.maxDelay.Int64()) {
190+
e.maxDelay.Set(0)
191+
break
192+
}
193+
} else {
194+
break
195+
}
196+
}
178197
}
179198

180199
var sessions struct {

include/pika_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ class PikaServer : public pstd::noncopyable {
327327
uint32_t SlowlogLen();
328328
void SlowlogObtain(int64_t number, std::vector<SlowlogEntry>* slowlogs);
329329
void SlowlogPushEntry(const PikaCmdArgsType& argv, int64_t time, int64_t duration);
330+
uint64_t SlowlogCount();
330331

331332
/*
332333
* Statistic used
@@ -680,6 +681,7 @@ class PikaServer : public pstd::noncopyable {
680681
* Slowlog used
681682
*/
682683
uint64_t slowlog_entry_id_ = 0;
684+
uint64_t slowlog_counter_ = 0;
683685
std::shared_mutex slowlog_protector_;
684686
std::list<SlowlogEntry> slowlog_list_;
685687

src/pika_admin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ void InfoCmd::InfoStats(std::string& info) {
10561056
tmp_stream << "is_slots_migrating:" << (is_migrating ? "Yes, " : "No, ") << start_migration_time_str << ", "
10571057
<< (is_migrating ? (current_time_s - start_migration_time) : (end_migration_time - start_migration_time))
10581058
<< "\r\n";
1059-
1059+
tmp_stream << "slow_logs_count:" << g_pika_server->SlowlogCount() << "\r\n";
10601060
info.append(tmp_stream.str());
10611061
}
10621062

src/pika_server.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,17 @@ void PikaServer::SlowlogPushEntry(const PikaCmdArgsType& argv, int64_t time, int
11981198
entry.start_time = time;
11991199
entry.duration = duration;
12001200
slowlog_list_.push_front(entry);
1201+
slowlog_counter_++;
12011202
}
12021203

12031204
SlowlogTrim();
12041205
}
12051206

1207+
uint64_t PikaServer::SlowlogCount() {
1208+
std::shared_lock l(slowlog_protector_);
1209+
return slowlog_counter_;
1210+
}
1211+
12061212
void PikaServer::ResetStat() {
12071213
statistic_.server_stat.accumulative_connections.store(0);
12081214
statistic_.server_stat.qps.querynum.store(0);

tools/pika_exporter/discovery/codis_dashboard.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type CmdInfo struct {
4949
Calls int64 `json:"calls"`
5050
Usecs_percall int64 `json:"usecs_percall"`
5151
Fails int64 `json:"fails"`
52+
MaxDelay int64 `json:"max_delay"`
5253
}
5354

5455
type ProxyOpsInfo struct {
@@ -97,9 +98,9 @@ type RunTimeInfo struct {
9798
}
9899

99100
type ProxyStats struct {
100-
Online bool `json:"online"`
101-
Ops ProxyOpsInfo `json:"ops"`
102-
Rusage RusageInfo `json:"rusage"`
103-
RunTime RunTimeInfo `json:"runtime"`
104-
TimeoutCmdNumber int64 `json:"timeout_cmd_number"`
101+
Online bool `json:"online"`
102+
Ops ProxyOpsInfo `json:"ops"`
103+
Rusage RusageInfo `json:"rusage"`
104+
RunTime RunTimeInfo `json:"runtime"`
105+
SlowCmdCount int64 `json:"slow_cmd_count"`
105106
}

tools/pika_exporter/exporter/metrics/parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ func (p *proxyParser) Parse(m MetricMeta, c Collector, opt ParseOption) {
304304
metric.Value = convertToFloat64(strconv.FormatInt(v[1], 10))
305305
case "fails":
306306
metric.Value = convertToFloat64(strconv.FormatInt(v[2], 10))
307+
case "max_delay":
308+
metric.Value = convertToFloat64(strconv.FormatInt(v[3], 10))
307309
}
308310

309311
if err := c.Collect(metric); err != nil {

tools/pika_exporter/exporter/metrics/proxy.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ func RegisterForProxy() {
66
}
77

88
var collectProxyMetrics map[string]MetricConfig = map[string]MetricConfig{
9-
"ops_total": {
9+
"total_ops": {
1010
Parser: &normalParser{},
1111
MetricMeta: &MetaData{
12-
Name: "ops_total",
12+
Name: "total_ops",
1313
Help: "proxy total ops",
1414
Type: metricTypeCounter,
1515
Labels: []string{LabelNameAddr, LabelID, LabelProductName},
1616
ValueName: "ops_total",
1717
},
1818
},
19-
"ops_fails": {
19+
"total_ops_fails": {
2020
Parser: &normalParser{},
2121
MetricMeta: &MetaData{
22-
Name: "ops_fails",
22+
Name: "total_ops_fails",
2323
Help: "proxy fails counter",
2424
Type: metricTypeCounter,
2525
Labels: []string{LabelNameAddr, LabelID, LabelProductName},
@@ -66,23 +66,23 @@ var collectProxyMetrics map[string]MetricConfig = map[string]MetricConfig{
6666
ValueName: "online",
6767
},
6868
},
69-
"timeout_cmd_number": {
69+
"total_slow_cmd": {
7070
Parser: &normalParser{},
7171
MetricMeta: &MetaData{
72-
Name: "timeout_cmd_number",
73-
Help: "The number of commands recorded in the slow log within the last second",
74-
Type: metricTypeGauge,
72+
Name: "total_slow_cmd",
73+
Help: "The number of commands recorded in the slow log",
74+
Type: metricTypeCounter,
7575
Labels: []string{LabelNameAddr, LabelID, LabelProductName},
76-
ValueName: "timeout_cmd_number",
76+
ValueName: "slow_cmd_count",
7777
},
7878
},
7979
}
8080

8181
var collectPorxyCmdMetrics map[string]MetricConfig = map[string]MetricConfig{
82-
"calls": {
82+
"total_calls": {
8383
Parser: &proxyParser{},
8484
MetricMeta: &MetaData{
85-
Name: "calls",
85+
Name: "total_calls",
8686
Help: "the number of cmd calls",
8787
Type: metricTypeCounter,
8888
Labels: []string{LabelNameAddr, LabelID, LabelProductName, LabelOpstr},
@@ -99,14 +99,24 @@ var collectPorxyCmdMetrics map[string]MetricConfig = map[string]MetricConfig{
9999
ValueName: "usecs_percall",
100100
},
101101
},
102-
"fails": {
102+
"total_fails": {
103103
Parser: &proxyParser{},
104104
MetricMeta: &MetaData{
105-
Name: "fails",
105+
Name: "total_fails",
106106
Help: "the number of cmd fail",
107107
Type: metricTypeCounter,
108108
Labels: []string{LabelNameAddr, LabelID, LabelProductName, LabelOpstr},
109109
ValueName: "fails",
110110
},
111111
},
112+
"max_delay": {
113+
Parser: &proxyParser{},
114+
MetricMeta: &MetaData{
115+
Name: "max_delay",
116+
Help: "The maximum time consumed by this command since the last collection.",
117+
Type: metricTypeGauge,
118+
Labels: []string{LabelNameAddr, LabelID, LabelProductName, LabelOpstr},
119+
ValueName: "max_delay",
120+
},
121+
},
112122
}

tools/pika_exporter/exporter/metrics/stats.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,14 @@ var collectStatsMetrics = map[string]MetricConfig{
158158
Labels: []string{LabelNameAddr, LabelNameAlias, "is_compact", "compact_cron", "compact_interval"},
159159
},
160160
},
161+
"total_slow_log": {
162+
Parser: &normalParser{},
163+
MetricMeta: &MetaData{
164+
Name: "total_slow_log",
165+
Help: "pika serve instance total count of slow log",
166+
Type: metricTypeCounter,
167+
Labels: []string{LabelNameAddr, LabelNameAlias},
168+
ValueName: "slow_logs_count",
169+
},
170+
},
161171
}

0 commit comments

Comments
 (0)