-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprometheus.go
More file actions
66 lines (57 loc) · 1.34 KB
/
prometheus.go
File metadata and controls
66 lines (57 loc) · 1.34 KB
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
62
63
64
65
66
package main
import (
"io"
"log"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
type vattenfallCollector struct {
prices *prometheus.Desc
regions []string
tz *time.Location
}
func NewVattenfallCollector(regions []string, tz *time.Location) *vattenfallCollector {
return &vattenfallCollector{
prices: prometheus.NewDesc(
"energy_price_per_kwh",
"Energy price per kWh for a region",
[]string{"region"}, prometheus.Labels{"currency": "SEK", "country": "SE"},
),
regions: regions,
tz: tz,
}
}
func (v *vattenfallCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- v.prices
}
func (v *vattenfallCollector) Collect(ch chan<- prometheus.Metric) {
for _, r := range v.regions {
now := time.Now().In(v.tz)
data, err := fetch(now, r)
if err != nil {
log.Println(err)
}
for _, entry := range data {
if entry.Timestamp.Day() != now.Day() {
continue
}
if entry.Timestamp.Hour() == now.Hour() {
ch <- prometheus.MustNewConstMetric(v.prices,
prometheus.GaugeValue, entry.Value, entry.Region)
}
}
}
}
func WriteMetricsTo(w io.Writer, g prometheus.Gatherer) error {
mfs, err := g.Gather()
if err != nil {
return err
}
for _, mf := range mfs {
if _, err := expfmt.MetricFamilyToText(w, mf); err != nil {
return err
}
}
return nil
}