Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Exporter for Proxy #2199

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tools/pika_exporter/discovery/codis_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,81 @@ type CodisModelInfo struct {
Servers []CodisServerInfo `json:"servers"`
}

type CodisProxyModelInfo struct {
Id int `json:"id"`
AdminAddr string `json:"admin_addr"`
ProductName string `json:"product_name"`
DataCenter string `json:"data_center"`
}

type CodisGroupInfo struct {
Models []CodisModelInfo `json:"models"`
}

type CodisProxyInfo struct {
Models []CodisProxyModelInfo `json:"models"`
}

type CodisStatsInfo struct {
Group CodisGroupInfo `json:"group"`
Proxy CodisProxyInfo `json:"proxy"`
}

type CodisTopomInfo struct {
Stats CodisStatsInfo `json:"stats"`
}

type RedisInfo struct {
Errors int `json:"errors"`
}

type ProxyOpsInfo struct {
Total int `json:"total"`
Fails int `json:"fails"`
Redis RedisInfo `json:"redis"`
Qps int `json:"qps"`
}

type RowInfo struct {
Utime int64 `json:"utime"`
Stime int64 `json:"stime"`
MaxRss int64 `json:"max_rss"`
IxRss int64 `json:"ix_rss"`
IdRss int64 `json:"id_rss"`
IsRss int64 `json:"is_rss"`
}

type RusageInfo struct {
Now string `json:"now"`
Cpu float64 `json:"cpu"`
Mem float64 `json:"mem"`
Raw RowInfo `json:"raw"`
}

type GeneralInfo struct {
Alloc int64 `json:"alloc"`
Sys int64 `json:"sys"`
Lookups int64 `json:"lookups"`
Mallocs int64 `json:"mallocs"`
Frees int64 `json:"frees"`
}

type HeapInfo struct {
Alloc int64 `json:"alloc"`
Sys int64 `json:"sys"`
Idle int64 `json:"idle"`
Inuse int64 `json:"inuse"`
Objects int64 `json:"objects"`
}

type RunTimeInfo struct {
General GeneralInfo `json:"general"`
Heap HeapInfo `json:"heap"`
}

type ProxyStats struct {
Ops ProxyOpsInfo `json:"ops"`
Rusage RusageInfo `json:"rusage"`
RunTime RunTimeInfo `json:"runtime"`
TimeoutCmdNumber int64 `json:"timeout_cmd_number"`
}
48 changes: 44 additions & 4 deletions tools/pika_exporter/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ type Instance struct {
Alias string
}

type InstanceProxy struct {
ID int
Addr string
ProductName string
}

type Discovery interface {
GetInstances() []Instance
GetInstancesProxy() []InstanceProxy
CheckUpdate(chan int, string)
}

Expand Down Expand Up @@ -58,6 +65,10 @@ func (d *cmdArgsDiscovery) GetInstances() []Instance {
return d.instances
}

func (d *cmdArgsDiscovery) GetInstancesProxy() []InstanceProxy {
return nil
}

func (d *cmdArgsDiscovery) CheckUpdate(chan int, string) {}

type fileDiscovery struct {
Expand Down Expand Up @@ -107,10 +118,15 @@ func (d *fileDiscovery) GetInstances() []Instance {
return d.instances
}

func (d *fileDiscovery) GetInstancesProxy() []InstanceProxy {
return nil
}

func (d *fileDiscovery) CheckUpdate(chan int, string) {}

type codisDiscovery struct {
instances []Instance
instances []Instance
instanceProxy []InstanceProxy
}

func NewCodisDiscovery(url, password, alias string) (*codisDiscovery, error) {
Expand Down Expand Up @@ -155,12 +171,27 @@ func NewCodisDiscovery(url, password, alias string) (*codisDiscovery, error) {
Alias: aliases[i],
}
}
return &codisDiscovery{instances: instances}, nil

instancesproxy := make([]InstanceProxy, len(result.Stats.Proxy.Models))
for i := range result.Stats.Proxy.Models {
instancesproxy[i] = InstanceProxy{
ID: result.Stats.Proxy.Models[i].Id,
Addr: result.Stats.Proxy.Models[i].AdminAddr,
ProductName: result.Stats.Proxy.Models[i].ProductName,
}
}
return &codisDiscovery{
instances: instances,
instanceProxy: instancesproxy,
}, nil
}

func (d *codisDiscovery) GetInstances() []Instance {
return d.instances
}
func (d *codisDiscovery) GetInstancesProxy() []InstanceProxy {
dingxiaoshuai123 marked this conversation as resolved.
Show resolved Hide resolved
return d.instanceProxy
}

func (d *codisDiscovery) CheckUpdate(updatechan chan int, codisaddr string) {
newdis, err := NewCodisDiscovery(codisaddr, "", "")
Expand All @@ -174,7 +205,7 @@ func (d *codisDiscovery) CheckUpdate(updatechan chan int, codisaddr string) {
}

func (d *codisDiscovery) comparedis(new_instance *codisDiscovery) bool {
var addrs []string
var addrs, addrsProxy []string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var (
addrs []string
addrsProxy []string
diff bool
)

var diff bool = false
for _, instance := range new_instance.instances {
addrs = append(addrs, instance.Addr)
Expand All @@ -185,7 +216,16 @@ func (d *codisDiscovery) comparedis(new_instance *codisDiscovery) bool {
return false
}
}
if !diff && len(new_instance.instances) == len(d.instances) {
for _, instance := range new_instance.instanceProxy {
addrsProxy = append(addrsProxy, instance.Addr)
}
for _, instance := range d.instanceProxy {
if !contains(instance.Addr, addrsProxy) {
diff = true
return false
}
}
if !diff && len(new_instance.instances) == len(d.instances) && len(new_instance.instanceProxy) == len(d.instanceProxy) {
return true
}
return false
Expand Down
35 changes: 35 additions & 0 deletions tools/pika_exporter/exporter/future.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,38 @@ func (f *future) Wait() map[futureKey]error {
defer f.Unlock()
return f.m
}

type futureKeyForProxy struct {
addr, instance, ID, productName string
}

type futureForProxy struct {
*sync.Mutex
wait sync.WaitGroup
m map[futureKeyForProxy]error
}

func newFutureForProxy() *futureForProxy {
return &futureForProxy{
Mutex: new(sync.Mutex),
m: make(map[futureKeyForProxy]error),
}
}

func (f *futureForProxy) Add() {
f.wait.Add(1)
}

func (f *futureForProxy) Done(key futureKeyForProxy, val error) {
f.Lock()
defer f.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer func() {
f.Unlock()
f.wait.Done()
}()

f.m[key] = val
f.wait.Done()
}

func (f *futureForProxy) Wait() map[futureKeyForProxy]error {
f.wait.Wait()
f.Lock()
defer f.Unlock()
return f.m
}
13 changes: 13 additions & 0 deletions tools/pika_exporter/exporter/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const (
LabelNameAlias = "alias"
LabelInstanceMode = "instance-mode"
LabelConsensusLevel = "consensus-level"
LabelInstance = "instance"
LabelID = "id"
dingxiaoshuai123 marked this conversation as resolved.
Show resolved Hide resolved
LabelProductName = "product_name"
)

type Describer interface {
Expand Down Expand Up @@ -96,6 +99,7 @@ type MetricConfig struct {
}

var MetricConfigs = make(map[string]MetricConfig)
var MetricConfigsProxy = make(map[string]MetricConfig)

func Register(mcs map[string]MetricConfig) {
for k, mc := range mcs {
Expand All @@ -105,3 +109,12 @@ func Register(mcs map[string]MetricConfig) {
MetricConfigs[k] = mc
}
}

func RegisterProxy(mcs map[string]MetricConfig) {
for k, mc := range mcs {
if _, ok := MetricConfigsProxy[k]; ok {
panic(fmt.Sprintf("register metrics config error. metricConfigProxyName:%s existed", k))
}
MetricConfigsProxy[k] = mc
}
}
25 changes: 25 additions & 0 deletions tools/pika_exporter/exporter/metrics/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package metrics

import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -271,3 +273,26 @@ func convertTimeToUnix(ts string) (int64, error) {
}
return t.Unix(), nil
}

func StructToMap(obj interface{}) (map[string]string, error) {
objValue := reflect.ValueOf(obj)
objType := objValue.Type()

data := make(map[string]string)

for i := 0; i < objValue.NumField(); i++ {
field := objValue.Field(i)
fieldName := objType.Field(i).Name

if field.Kind() == reflect.Struct {
innerData, _ := StructToMap(field.Interface())
for k, v := range innerData {
data[k] = v
}
} else {
data[fieldName] = fmt.Sprintf("%v", field.Interface())
}
}

return data, nil
}
38 changes: 38 additions & 0 deletions tools/pika_exporter/exporter/metrics/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package metrics

func RegisterForProxy() {
RegisterProxy(collectProxyMetrics)
}

var collectProxyMetrics map[string]MetricConfig = map[string]MetricConfig{
"ops_total": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "ops_total",
Help: "proxy total ops",
Type: metricTypeCounter,
Labels: []string{LabelNameAddr, LabelInstance, LabelID, LabelProductName},
ValueName: "Total",
},
},
"ops_fails": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "ops_fails",
Help: "proxy fails counter",
Type: metricTypeCounter,
Labels: []string{LabelNameAddr, LabelInstance, LabelID, LabelProductName},
ValueName: "Fails",
},
},
"qps": {
Parser: &normalParser{},
MetricMeta: &MetaData{
Name: "qps",
Help: "proxy qps",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelInstance, LabelID, LabelProductName},
ValueName: "Qps",
},
},
}
Loading
Loading