-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprometheus.go
105 lines (88 loc) · 2.29 KB
/
prometheus.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
type result struct {
Main struct {
Temp float64
Pressure float64
Humidity float64
}
Wind struct {
Speed float64
}
Clouds struct {
All float64
}
}
var (
temp = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "temperature_celsius",
Help: "Temperature in °C",
}, []string{"location"})
pressure = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "pressure_hpa",
Help: "Atmospheric pressure in hPa",
}, []string{"location"})
humidity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "humidity_percent",
Help: "Humidity in Percent",
}, []string{"location"})
wind = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "wind_mps",
Help: "Wind speed in m/s",
}, []string{"location"})
clouds = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "cloudiness_percent",
Help: "Cloudiness in Percent",
}, []string{"location"})
)
type Exporter struct {
Location string
APIKey string
}
func (e *Exporter) owmData() (result, error) {
var res result
url := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", e.Location, e.APIKey)
fmt.Println(url)
resp, err := http.Get(url)
if err != nil {
return res, err
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&res)
return res, err
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
temp.Describe(ch)
pressure.Describe(ch)
humidity.Describe(ch)
wind.Describe(ch)
clouds.Describe(ch)
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
res, err := e.owmData()
if err != nil {
log.Printf("could not get results from OpenWeatherMap: %v", err)
return
}
temp.WithLabelValues(e.Location).Set(res.Main.Temp)
temp.Collect(ch)
pressure.WithLabelValues(e.Location).Set(res.Main.Pressure)
pressure.Collect(ch)
humidity.WithLabelValues(e.Location).Set(res.Main.Humidity)
humidity.Collect(ch)
wind.WithLabelValues(e.Location).Set(res.Wind.Speed)
wind.Collect(ch)
clouds.WithLabelValues(e.Location).Set(res.Clouds.All)
clouds.Collect(ch)
}