@@ -2,9 +2,11 @@ package metrics
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "time"
78
9+ "github.com/ViaQ/logerr/v2/kverrors"
810 "github.com/onsi/gomega/gmeasure"
911 "github.com/prometheus/client_golang/api"
1012 v1 "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -19,6 +21,11 @@ const (
1921 ReadRequestPath
2022)
2123
24+ var (
25+ errNilExperiment = errors .New ("error measuring experiment: nil experiment" )
26+ errUnknownPath = errors .New ("error unknown path specified" )
27+ )
28+
2229type Client struct {
2330 api v1.API
2431 timeout time.Duration
@@ -39,20 +46,20 @@ func NewClient(url, token string, timeout time.Duration, cadvisorEnabled bool) (
3946 }
4047
4148 if err := httpConfig .Validate (); err != nil {
42- return nil , fmt . Errorf ( "failed to validate httpConfig: %w" , err )
49+ return nil , kverrors . Wrap ( err , "failed to validate httpConfig" )
4350 }
4451
4552 rt , err := config .NewRoundTripperFromConfig (httpConfig , "benchmarks-metrics" )
4653 if err != nil {
47- return nil , fmt . Errorf ( "failed creating prometheus configuration: %w" , err )
54+ return nil , kverrors . Wrap ( err , "failed creating prometheus configuration" )
4855 }
4956
5057 pc , err := api .NewClient (api.Config {
5158 Address : url ,
5259 RoundTripper : rt ,
5360 })
5461 if err != nil {
55- return nil , fmt . Errorf ( "failed creating prometheus client: %w" , err )
62+ return nil , kverrors . Wrap ( err , "failed creating prometheus client" )
5663 }
5764
5865 return & Client {
@@ -64,12 +71,12 @@ func NewClient(url, token string, timeout time.Duration, cadvisorEnabled bool) (
6471
6572func (c * Client ) Measure (e * gmeasure.Experiment , data Measurement ) error {
6673 if e == nil {
67- return fmt . Errorf ( "error measuring experiment: nil experiment" )
74+ return errNilExperiment
6875 }
6976
7077 value , err := c .executeScalarQuery (data .Query )
7178 if err != nil {
72- return fmt . Errorf ( "error measuring experiment: %s" , err )
79+ return kverrors . Wrap ( err , "error measuring experiment" )
7380 }
7481
7582 e .RecordValue (data .Name , value , data .Unit , data .Annotation , gmeasure .Precision (4 ))
@@ -89,7 +96,7 @@ func (c *Client) MeasureHTTPRequestMetrics(
8996 case ReadRequestPath :
9097 return c .measureCommonRequestMetrics (e , job , HTTPGetMethod , HTTPQueryRangeRoute , HTTPReadPathRoutes , sampleRange , annotation )
9198 default :
92- return fmt . Errorf ( "error unknown path specified: %d " , path )
99+ return kverrors . Wrap ( errUnknownPath , " path" , path )
93100 }
94101}
95102
@@ -106,7 +113,7 @@ func (c *Client) MeasureGRPCRequestMetrics(
106113 case ReadRequestPath :
107114 return c .measureCommonRequestMetrics (e , job , GRPCMethod , GRPCQuerySampleRoute , GRPCReadPathRoutes , sampleRange , annotation )
108115 default :
109- return fmt . Errorf ( "error unknown path specified: %d " , path )
116+ return kverrors . Wrap ( errUnknownPath , " path" , path )
110117 }
111118}
112119
@@ -122,7 +129,7 @@ func (c *Client) MeasureIndexRequestMetrics(
122129 case ReadRequestPath :
123130 return c .Measure (e , RequestIndexRequestRate (IndexReadName , job , ReadOperation , "2.*" , sampleRange ))
124131 default :
125- return fmt . Errorf ( "error unknown path specified: %d " , path )
132+ return kverrors . Wrap ( errUnknownPath , " path" , path )
126133 }
127134}
128135
@@ -225,7 +232,7 @@ func (c *Client) executeScalarQuery(query string) (float64, error) {
225232
226233 res , _ , err := c .api .Query (ctx , query , time .Now ())
227234 if err != nil {
228- return 0.0 , fmt . Errorf ( "failed executing query %q: %w " , query , err )
235+ return 0.0 , kverrors . Wrap ( err , "failed executing query" , " query" , query )
229236 }
230237
231238 switch res .Type () {
@@ -239,7 +246,7 @@ func (c *Client) executeScalarQuery(query string) (float64, error) {
239246 }
240247 return float64 (vec [0 ].Value ), nil
241248 default :
242- return 0.0 , fmt . Errorf ( "failed to parse result for query: %s " , query )
249+ return 0.0 , kverrors . Wrap ( err , "failed to parse result for query" , "query " , query )
243250 }
244251}
245252
0 commit comments