@@ -14,7 +14,7 @@ import (
1414 "github.com/cortexproject/cortex/pkg/util/flagext"
1515 "github.com/go-kit/kit/log/level"
1616 "github.com/prometheus/client_golang/prometheus"
17- "github.com/prometheus/client_golang/prometheus/promauto "
17+ "github.com/weaveworks/common/instrument "
1818)
1919
2020// Config says where we can find the ruler configs.
@@ -29,12 +29,12 @@ func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
2929 f .DurationVar (& cfg .ClientTimeout , prefix + "configs.client-timeout" , 5 * time .Second , "Timeout for requests to Weave Cloud configs service." )
3030}
3131
32- var configsRequestDuration = promauto .NewHistogramVec (prometheus.HistogramOpts {
32+ var configsRequestDuration = instrument . NewHistogramCollector ( prometheus .NewHistogramVec (prometheus.HistogramOpts {
3333 Namespace : "cortex" ,
3434 Name : "configs_request_duration_seconds" ,
3535 Help : "Time spent requesting configs." ,
3636 Buckets : prometheus .DefBuckets ,
37- }, []string {"operation" , "status_code" })
37+ }, []string {"operation" , "status_code" }))
3838
3939// Client is what the ruler and altermanger needs from a config store to process rules.
4040type Client interface {
@@ -67,7 +67,12 @@ func (c ConfigDBClient) GetRules(ctx context.Context, since configs.ID) (map[str
6767 suffix = fmt .Sprintf ("?since=%d" , since )
6868 }
6969 endpoint := fmt .Sprintf ("%s/private/api/prom/configs/rules%s" , c .URL .String (), suffix )
70- response , err := doRequest (endpoint , c .Timeout , since , "GetRules" )
70+ var response * ConfigsResponse
71+ err := instrument .CollectedRequest (ctx , "GetRules" , configsRequestDuration , instrument .ErrorCode , func (ctx context.Context ) error {
72+ var err error
73+ response , err = doRequest (endpoint , c .Timeout , since )
74+ return err
75+ })
7176 if err != nil {
7277 return nil , err
7378 }
@@ -88,23 +93,27 @@ func (c ConfigDBClient) GetAlerts(ctx context.Context, since configs.ID) (*Confi
8893 suffix = fmt .Sprintf ("?since=%d" , since )
8994 }
9095 endpoint := fmt .Sprintf ("%s/private/api/prom/configs/alertmanager%s" , c .URL .String (), suffix )
91- return doRequest (endpoint , c .Timeout , since , "GetAlerts" )
96+ var response * ConfigsResponse
97+ err := instrument .CollectedRequest (ctx , "GetAlerts" , configsRequestDuration , instrument .ErrorCode , func (ctx context.Context ) error {
98+ var err error
99+ response , err = doRequest (endpoint , c .Timeout , since )
100+ return err
101+ })
102+ return response , err
92103}
93104
94- func doRequest (endpoint string , timeout time.Duration , since configs.ID , operation string ) (* ConfigsResponse , error ) {
105+ func doRequest (endpoint string , timeout time.Duration , since configs.ID ) (* ConfigsResponse , error ) {
95106 req , err := http .NewRequest ("GET" , endpoint , nil )
96107 if err != nil {
97108 return nil , err
98109 }
99110
100111 client := & http.Client {Timeout : timeout }
101112
102- start := time .Now ()
103113 resp , err := client .Do (req )
104114 if err != nil {
105115 return nil , err
106116 }
107- configsRequestDuration .WithLabelValues (operation , resp .Status ).Observe (time .Since (start ).Seconds ())
108117 defer resp .Body .Close ()
109118
110119 if resp .StatusCode != http .StatusOK {
0 commit comments