Skip to content

Commit e213eed

Browse files
authored
Merge pull request #4 from TheMeier/http_client_metrics
feat: add metrics for http client
2 parents 82d3bd9 + 0c4f895 commit e213eed

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

downdetector-exporter.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/url"
99
"os"
@@ -355,8 +355,17 @@ func workHorse(companyIDs string, searchString string) {
355355

356356
func getHTTPClient(proxyURLStr string) *http.Client {
357357

358-
var transport *http.Transport
359-
transport = http.DefaultTransport.(*http.Transport).Clone()
358+
var (
359+
httpRequestsTotal = prometheus.NewCounterVec(
360+
prometheus.CounterOpts{
361+
Name: "client_api_requests_total",
362+
Help: "Total number of client requests made.",
363+
},
364+
[]string{"method", "code"},
365+
)
366+
)
367+
prometheus.MustRegister(httpRequestsTotal)
368+
transport := http.DefaultTransport.(*http.Transport).Clone()
360369
// tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
361370

362371
if proxyURLStr != "" {
@@ -368,9 +377,11 @@ func getHTTPClient(proxyURLStr string) *http.Client {
368377
transport.Proxy = http.ProxyURL(proxyURL)
369378
}
370379

380+
roundTripper := promhttp.InstrumentRoundTripperCounter(httpRequestsTotal, transport)
381+
371382
//adding the Transport object to the http Client
372383
client := &http.Client{
373-
Transport: transport,
384+
Transport: roundTripper,
374385
Timeout: 60 * time.Second,
375386
}
376387
return client
@@ -391,14 +402,14 @@ func initToken() {
391402
res, err := httpClient.Do(req)
392403
if res.StatusCode != 200 {
393404
// return if we weren't successful - we have tokenGraceSeconds to retry
394-
body, _ := ioutil.ReadAll(res.Body)
405+
body, _ := io.ReadAll(res.Body)
395406
level.Warn(lg).Log("msg", fmt.Sprintf("Error response code: %d - %s", res.StatusCode, body))
396407
return
397408
}
398409
defer res.Body.Close()
399410

400411
// read body from response
401-
body, err := ioutil.ReadAll(res.Body)
412+
body, err := io.ReadAll(res.Body)
402413
if err != nil {
403414
// return if we weren't successful - we have tokenGraceSeconds to retry
404415
level.Warn(lg).Log("msg", fmt.Sprintf("Couldn't read in body: %s", err.Error()))
@@ -441,14 +452,14 @@ func getMetrics(companyIDs string, searchString string) {
441452
res, err := httpClient.Do(req)
442453
if res.StatusCode != 200 {
443454
// return if we weren't successful
444-
body, _ := ioutil.ReadAll(res.Body)
455+
body, _ := io.ReadAll(res.Body)
445456
level.Warn(lg).Log("msg", fmt.Sprintf("Could not get metrics: %d - %s", res.StatusCode, body))
446457
return
447458
}
448459
defer res.Body.Close()
449460

450461
// read body from response
451-
body, err := ioutil.ReadAll(res.Body)
462+
body, err := io.ReadAll(res.Body)
452463
if err != nil {
453464
// return if we weren't successful - we have tokenGraceSeconds to retry
454465
level.Warn(lg).Log("msg", fmt.Sprintf("Couldn't read in body: %s", err.Error()))
@@ -591,6 +602,6 @@ func systemAlive(listenAddress string, metricsPath string) {
591602
level.Warn(lg).Log("msg", fmt.Sprintf("liveness check failed: %s", err.Error()))
592603
}
593604
// Read all away or else we'll run out of sockets sooner or later
594-
_, _ = ioutil.ReadAll(res.Body)
605+
_, _ = io.ReadAll(res.Body)
595606
defer res.Body.Close()
596607
}

0 commit comments

Comments
 (0)