-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
61 lines (50 loc) · 1.27 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"log"
"time"
"github.com/go-kit/kit/metrics"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
type fxMetrics struct {
rate metrics.Gauge
}
// NewMetrics initialises the metrics
func NewMetrics() fxMetrics {
rate := kitprometheus.NewGaugeFrom(prometheus.GaugeOpts{
Namespace: "fx",
Name: "rate",
Help: "fx rate is the exchange rate between the base and quote e.g. USD and GBP.",
//ConstLabels: prometheus.Labels{},
}, []string{"base", "quote"})
return fxMetrics{rate}
}
// collect collects metrics
func (m fxMetrics) collect(appId string) error {
res, err := request(appId)
if err != nil {
return errors.Wrap(err, "requestFixture for data failed")
}
for quoteSymbol, quotePrice := range res.Rates {
// TODO: take base param
m.rate.With("base", "USD", "quote", quoteSymbol).Set(quotePrice)
}
return nil
}
func runCollector(appId string, m fxMetrics, duration time.Duration) chan error {
errs := make(chan error)
ticker := time.NewTicker(duration)
go func() {
for {
log.Println("running collection")
err := m.collect(appId)
log.Println("finished collection")
if err != nil {
errs <- err
}
<-ticker.C
}
}()
return errs
}