@@ -3,15 +3,16 @@ package main
33import (
44 "context"
55 "fmt"
6- "slo/internal/config"
7- "slo/internal/log"
86 "strconv"
97 "strings"
108 "time"
119
1210 "github.com/prometheus/client_golang/api"
1311 v1 "github.com/prometheus/client_golang/api/prometheus/v1"
1412 "github.com/prometheus/common/model"
13+
14+ "slo/internal/config"
15+ "slo/internal/log"
1516)
1617
1718type Estimator struct {
@@ -36,18 +37,23 @@ func getMetric(ctx context.Context, cfg *config.Config, query string) model.Vect
3637 query ,
3738 time .Now (),
3839 )
40+ if err != nil {
41+ log .Panicf ("query failed: %v" , err )
42+ }
3943 if len (warnings ) > 0 {
4044 fmt .Println ("Warnings: " , warnings )
4145 }
4246 vector , ok := result .(model.Vector )
4347 if ! ok || len (vector ) == 0 {
4448 log .Panicf ("no data found for query: %s" , query )
4549 }
50+
4651 return vector
4752}
4853
4954func getMetricValue (ctx context.Context , cfg * config.Config , query string ) float64 {
5055 vector := getMetric (ctx , cfg , query )
56+
5157 return float64 (vector [0 ].Value )
5258}
5359
@@ -56,6 +62,7 @@ func formatNodeID(v model.LabelValue) uint32 {
5662 if err != nil {
5763 log .Panicf ("formatNodeID failed: %v" , err )
5864 }
65+
5966 return uint32 (id )
6067}
6168
@@ -67,23 +74,23 @@ func NewEstimator(ctx context.Context, storage *Storage) *Estimator {
6774 allNodeIDs := make (map [uint32 ]bool )
6875 instanceID := make (map [string ]map [uint32 ]bool )
6976 nodeInstance := make (map [uint32 ]string )
70- //get all node ids
77+ // get all node ids
7178 for _ , v := range vec {
7279 allNodeIDs [formatNodeID (v .Metric ["peer_node_id" ])] = true
7380 }
74- //for target instance, the only absent node id is correct
81+ // for target instance, the only absent node id is correct
7582 for _ , v := range vec {
7683 instance := string (v .Metric ["instance" ])
7784 instanceID [instance ] = make (map [uint32 ]bool )
78- for nodeID , _ := range allNodeIDs {
85+ for nodeID := range allNodeIDs {
7986 instanceID [instance ][nodeID ] = true
8087 }
8188 }
8289 for _ , v := range vec {
8390 instance := string (v .Metric ["instance" ])
8491 instanceID [instance ][formatNodeID (v .Metric ["peer_node_id" ])] = false
8592 }
86- //backwards mapping
93+ // backwards mapping
8794 for instance , nodeIDs := range instanceID {
8895 if strings .Contains (instance , "storage" ) {
8996 continue
@@ -96,32 +103,34 @@ func NewEstimator(ctx context.Context, storage *Storage) *Estimator {
96103 }
97104 e .NodeInstances = nodeInstance
98105 e .NodeRequests = make (map [uint32 ]float64 )
99- //collect counters
100- for nodeID , _ := range e .NodeInstances {
106+ // collect counters
107+ for nodeID := range e .NodeInstances {
101108 e .NodeRequests [nodeID ] = e .NodeRWCounter (ctx , nodeID )
102109 }
103110 e .ClusterCounter = e .ClusterRWCounter (ctx )
111+
104112 return e
105113}
106114
107- func (e * Estimator ) NodeGrpcApiCounter (ctx context.Context , method string , nodeID uint32 ) float64 {
115+ func (e * Estimator ) NodeGrpcAPICounter (ctx context.Context , method string , nodeID uint32 ) float64 {
108116 instance , ok := e .NodeInstances [nodeID ]
109117 if ! ok {
110118 log .Panicf ("no instance found for nodeID: %d" , nodeID )
111119 }
120+
112121 return getMetricValue (ctx , e .cfg , fmt .Sprintf (`api_grpc_request_count{instance="%s",method="%s"}` , instance , method ))
113122}
114123
115- func (e * Estimator ) ClusterGrpcApiCounter (ctx context.Context , method string ) float64 {
124+ func (e * Estimator ) ClusterGrpcAPICounter (ctx context.Context , method string ) float64 {
116125 return getMetricValue (ctx , e .cfg , fmt .Sprintf (`sum(api_grpc_request_count{method="%s"})` , method ))
117126}
118127
119128func (e * Estimator ) NodeRWCounter (ctx context.Context , nodeID uint32 ) float64 {
120- return e .NodeGrpcApiCounter (ctx , "ReadRows" , nodeID ) + e .NodeGrpcApiCounter (ctx , "BulkUpsert" , nodeID )
129+ return e .NodeGrpcAPICounter (ctx , "ReadRows" , nodeID ) + e .NodeGrpcAPICounter (ctx , "BulkUpsert" , nodeID )
121130}
122131
123132func (e * Estimator ) ClusterRWCounter (ctx context.Context ) float64 {
124- return e .ClusterGrpcApiCounter (ctx , "ReadRows" ) + e .ClusterGrpcApiCounter (ctx , "BulkUpsert" )
133+ return e .ClusterGrpcAPICounter (ctx , "ReadRows" ) + e .ClusterGrpcAPICounter (ctx , "BulkUpsert" )
125134}
126135
127136func (e * Estimator ) OnlyThisNode (ctx context.Context , nodeID uint32 ) {
0 commit comments