Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 9f01dca

Browse files
Maximilian WilhelmBarbarossaTM
Maximilian Wilhelm
authored andcommitted
Always expose rpki_refresh metrics for sucessful http calls
For successful HTTP calls there were cases where no 'rpki_refresh' metric was exposed, as FetchFile() return'ed without the bool return value set to true. As the bool return value mainly seems to indicate that a file was successfully fetch from an HTTP URL, the same behavior can be achieve by using the HTTP status code to expose the metric. This also contains some drive-by clean-ups. Signed-off-by: Maximilian Wilhelm <[email protected]>
1 parent 46b68be commit 9f01dca

File tree

3 files changed

+50
-52
lines changed

3 files changed

+50
-52
lines changed

cmd/gortr/gortr.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ import (
1212
"errors"
1313
"flag"
1414
"fmt"
15-
rtr "github.com/cloudflare/gortr/lib"
16-
"github.com/cloudflare/gortr/prefixfile"
17-
"github.com/cloudflare/gortr/utils"
18-
"github.com/prometheus/client_golang/prometheus"
19-
"github.com/prometheus/client_golang/prometheus/promhttp"
20-
log "github.com/sirupsen/logrus"
21-
"golang.org/x/crypto/ssh"
2215
"io/ioutil"
2316
"net/http"
2417
"os"
@@ -28,6 +21,14 @@ import (
2821
"sync"
2922
"syscall"
3023
"time"
24+
25+
rtr "github.com/cloudflare/gortr/lib"
26+
"github.com/cloudflare/gortr/prefixfile"
27+
"github.com/cloudflare/gortr/utils"
28+
"github.com/prometheus/client_golang/prometheus"
29+
"github.com/prometheus/client_golang/prometheus/promhttp"
30+
log "github.com/sirupsen/logrus"
31+
"golang.org/x/crypto/ssh"
3132
)
3233

3334
const (
@@ -244,14 +245,12 @@ func (s *state) updateFile(file string) error {
244245
log.Debugf("Refreshing cache from %s", file)
245246

246247
s.lastts = time.Now().UTC()
247-
data, code, lastrefresh, err := s.fetchConfig.FetchFile(file)
248+
data, code, err := s.fetchConfig.FetchFile(file)
248249
if err != nil {
249250
return err
250251
}
251-
if lastrefresh {
252-
LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
253-
}
254252
if code != -1 {
253+
LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
255254
RefreshStatusCode.WithLabelValues(file, fmt.Sprintf("%d", code)).Inc()
256255
}
257256

@@ -368,15 +367,14 @@ func (s *state) updateFile(file string) error {
368367

369368
func (s *state) updateSlurm(file string) error {
370369
log.Debugf("Refreshing slurm from %v", file)
371-
data, code, lastrefresh, err := s.fetchConfig.FetchFile(file)
370+
data, code, err := s.fetchConfig.FetchFile(file)
372371
if err != nil {
373372
return err
374373
}
375-
if lastrefresh {
376-
LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
377-
}
374+
378375
if code != -1 {
379376
RefreshStatusCode.WithLabelValues(file, fmt.Sprintf("%d", code)).Inc()
377+
LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
380378
}
381379

382380
buf := bytes.NewBuffer(data)

cmd/rtrmon/rtrmon.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
"errors"
88
"flag"
99
"fmt"
10-
rtr "github.com/cloudflare/gortr/lib"
11-
"github.com/cloudflare/gortr/prefixfile"
12-
"github.com/cloudflare/gortr/utils"
13-
"github.com/prometheus/client_golang/prometheus"
14-
"github.com/prometheus/client_golang/prometheus/promhttp"
15-
log "github.com/sirupsen/logrus"
16-
"golang.org/x/crypto/ssh"
1710
"io/ioutil"
1811
"net"
1912
"net/http"
@@ -22,6 +15,14 @@ import (
2215
"runtime"
2316
"sync"
2417
"time"
18+
19+
rtr "github.com/cloudflare/gortr/lib"
20+
"github.com/cloudflare/gortr/prefixfile"
21+
"github.com/cloudflare/gortr/utils"
22+
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/promhttp"
24+
log "github.com/sirupsen/logrus"
25+
"golang.org/x/crypto/ssh"
2526
)
2627

2728
const (
@@ -264,7 +265,7 @@ func (c *Client) Start(id int, ch chan int) {
264265
}
265266
} else {
266267
log.Infof("%d: Fetching %s", c.id, c.Path)
267-
data, _, _, err := c.FetchConfig.FetchFile(c.Path)
268+
data, _, err := c.FetchConfig.FetchFile(c.Path)
268269
if err != nil {
269270
log.Error(err)
270271
continue

utils/utils.go

+28-29
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"net"
88
"net/http"
99
"os"
10-
"time"
1110
"sync"
11+
"time"
1212
)
1313

1414
type FetchConfig struct {
@@ -22,9 +22,9 @@ type FetchConfig struct {
2222

2323
func NewFetchConfig() *FetchConfig {
2424
return &FetchConfig{
25-
etags: make(map[string]string),
25+
etags: make(map[string]string),
2626
etagsLock: &sync.RWMutex{},
27-
Mime: "application/json",
27+
Mime: "application/json",
2828
}
2929
}
3030

@@ -45,11 +45,13 @@ func (e IdenticalEtag) Error() string {
4545
return fmt.Sprintf("File %s is identical according to Etag: %s", e.File, e.Etag)
4646
}
4747

48-
func (c *FetchConfig) FetchFile(file string) ([]byte, int, bool, error) {
48+
func (c *FetchConfig) FetchFile(file string) ([]byte, int, error) {
4949
var f io.Reader
5050
var err error
51-
if len(file) > 8 && (file[0:7] == "http://" || file[0:8] == "https://") {
5251

52+
status_code := -1
53+
54+
if len(file) > 8 && (file[0:7] == "http://" || file[0:8] == "https://") {
5355
// Copying base of DefaultTransport from https://golang.org/src/net/http/transport.go
5456
// There is a proposal for a Clone of
5557
tr := &http.Transport{
@@ -65,6 +67,7 @@ func (c *FetchConfig) FetchFile(file string) ([]byte, int, bool, error) {
6567
ExpectContinueTimeout: 1 * time.Second,
6668
ProxyConnectHeader: map[string][]string{},
6769
}
70+
6871
// Keep User-Agent in proxy request
6972
tr.ProxyConnectHeader.Set("User-Agent", c.UserAgent)
7073

@@ -84,61 +87,57 @@ func (c *FetchConfig) FetchFile(file string) ([]byte, int, bool, error) {
8487

8588
proxyurl, err := http.ProxyFromEnvironment(req)
8689
if err != nil {
87-
return nil, -1, false, err
90+
return nil, -1, err
8891
}
8992
proxyreq := http.ProxyURL(proxyurl)
9093
tr.Proxy = proxyreq
9194

92-
if err != nil {
93-
return nil, -1, false, err
94-
}
95-
9695
fhttp, err := client.Do(req)
9796
if err != nil {
98-
return nil, -1, false, err
97+
return nil, -1, err
9998
}
10099
if fhttp.Body != nil {
101100
defer fhttp.Body.Close()
102101
}
103102
defer client.CloseIdleConnections()
104-
//RefreshStatusCode.WithLabelValues(file, fmt.Sprintf("%d", fhttp.StatusCode)).Inc()
105103

106104
if fhttp.StatusCode == 304 {
107-
//LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
108-
return nil, fhttp.StatusCode, true, HttpNotModified{
105+
return nil, fhttp.StatusCode, HttpNotModified{
109106
File: file,
110107
}
111-
} else if fhttp.StatusCode != 200 {
108+
}
109+
110+
if fhttp.StatusCode != 200 {
112111
c.etagsLock.Lock()
113112
delete(c.etags, file)
114113
c.etagsLock.Unlock()
115-
return nil, fhttp.StatusCode, true, fmt.Errorf("HTTP %s", fhttp.Status)
116114
}
117-
//LastRefresh.WithLabelValues(file).Set(float64(s.lastts.UnixNano() / 1e9))
118-
119-
f = fhttp.Body
120115

121116
newEtag := fhttp.Header.Get("ETag")
122-
123-
if !c.EnableEtags || newEtag == "" || newEtag != c.etags[file] { // check lock here
124-
c.etagsLock.Lock()
125-
c.etags[file] = newEtag
126-
c.etagsLock.Unlock()
127-
} else {
128-
return nil, fhttp.StatusCode, true, IdenticalEtag{
117+
if c.EnableEtags && newEtag != "" && newEtag == c.etags[file] {
118+
return nil, fhttp.StatusCode, IdenticalEtag{
129119
File: file,
130120
Etag: newEtag,
131121
}
132122
}
123+
124+
c.etagsLock.Lock()
125+
c.etags[file] = newEtag
126+
c.etagsLock.Unlock()
127+
128+
f = fhttp.Body
129+
status_code = fhttp.StatusCode
133130
} else {
134131
f, err = os.Open(file)
135132
if err != nil {
136-
return nil, -1, false, err
133+
return nil, -1, err
137134
}
138135
}
136+
139137
data, err := ioutil.ReadAll(f)
140138
if err != nil {
141-
return nil, -1, false, err
139+
return nil, -1, err
142140
}
143-
return data, -1, false, nil
141+
142+
return data, status_code, nil
144143
}

0 commit comments

Comments
 (0)