Skip to content

Commit

Permalink
feat: cache metrics (OpenAtomFoundation#2318)
Browse files Browse the repository at this point in the history
* cache metrics
  • Loading branch information
dingxiaoshuai123 authored Jan 19, 2024
1 parent c1917c7 commit 5276adb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions tools/pika_exporter/config/info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ stats = true
cpu = true
replication = true
keyspace = true
cache = true

execcount = false
commandstats = false
Expand Down
4 changes: 3 additions & 1 deletion tools/pika_exporter/exporter/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type InfoConfig struct {
Execcount bool `toml:"execcount"`
Commandstats bool `toml:"commandstats"`
Rocksdb bool `toml:"rocksdb"`
Cache bool `toml:"cache"`

Info bool
InfoAll bool
Expand Down Expand Up @@ -70,6 +71,7 @@ func (c *InfoConfig) Display() {
log.Println("Execcount:", c.Execcount)
log.Println("Commandstats:", c.Commandstats)
log.Println("Rocksdb:", c.Rocksdb)
log.Println("Cache:", c.Cache)
log.Println("Info:", c.Info)
log.Println("InfoAll:", c.InfoAll)
}
Expand All @@ -80,7 +82,7 @@ func (c *InfoConfig) CheckInfo() {

if c.Server && c.Data && c.Clients && c.Stats && c.CPU && c.Replication && c.Keyspace {
c.Info = true
if c.Execcount && c.Commandstats && c.Rocksdb {
if c.Execcount && c.Commandstats && c.Rocksdb && c.Cache {
c.InfoAll = true
}
}
Expand Down
87 changes: 87 additions & 0 deletions tools/pika_exporter/exporter/metrics/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.

package metrics

import (
"regexp"
)

func RegisterCache() {
Register(collectCacheMetrics)
}

var collectCacheMetrics = map[string]MetricConfig{
"cache_status": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "cache_status",
Help: "pika serve instance cache status",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "cache_status",
},
},
"cache_db_num": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "cache_db_num",
Help: "pika serve instance cache number",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "cache_db_num",
},
},
"cache_memory": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "cache_memory",
Help: "pika serve instance cache memory usage",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "cache_memory",
},
},
"hits_per_sec": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "hits_per_sec",
Help: "pika serve instance cache hit count per second ",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "hits_per_sec",
},
},
"hitratio_per_second": {
Parser: &regexParser{
name: "hitratio_per_sec",
reg: regexp.MustCompile(`^(?P<hitratio_per_sec>\d+)%$`),
Parser: &normalParser{},
source: "hitratio_per_sec",
},
MetricMeta: &MetaData{
Name: "hitratio_per_sec",
Help: "pika serve instance cache hit rate per second",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "hitratio_per_sec",
},
},
"hitratio": {
Parser: &regexParser{
name: "hitratio_per_sec",
reg: regexp.MustCompile(`^(?P<hitratio_all>\d+)%$`),
Parser: &normalParser{},
source: "hitratio_all",
},
MetricMeta: &MetaData{
Name: "hitratio_all",
Help: "pika serve instance cache hit rate",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias},
ValueName: "hitratio_all",
},
},
}
2 changes: 1 addition & 1 deletion tools/pika_exporter/exporter/metrics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func convertToFloat64(s string) float64 {
s = strings.ToLower(s)

switch s {
case "yes", "up", "online", "true":
case "yes", "up", "online", "true", "ok":
return 1
case "no", "down", "offline", "null", "false":
return 0
Expand Down
7 changes: 4 additions & 3 deletions tools/pika_exporter/exporter/pika.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"sync"
"time"

"github.com/OpenAtomFoundation/pika/tools/pika_exporter/discovery"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"

"github.com/OpenAtomFoundation/pika/tools/pika_exporter/discovery"
"github.com/OpenAtomFoundation/pika/tools/pika_exporter/exporter/metrics"
)

Expand Down Expand Up @@ -241,7 +240,6 @@ func (e *exporter) collectInfo(c *client, ch chan<- prometheus.Metric) error {
if err != nil {
return err
}

collector := metrics.CollectFunc(func(m metrics.Metric) error {
promMetric, err := prometheus.NewConstMetric(
prometheus.NewDesc(prometheus.BuildFQName(e.namespace, "", m.Name), m.Help, m.Labels, nil),
Expand Down Expand Up @@ -328,6 +326,9 @@ func (e *exporter) registerMetrics() {
if config.Rocksdb {
metrics.RegisterRocksDB()
}
if config.Cache {
metrics.RegisterCache()
}
metrics.RegisterBinlog()
}

Expand Down

0 comments on commit 5276adb

Please sign in to comment.