forked from thomersch/openweathermap_exporter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
144 lines (122 loc) · 3.78 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"log"
"net/http"
"time"
owm "github.com/briandowns/openweathermap"
"github.com/caarlos0/env"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Config stores the parameters used to fetch the data
type Config struct {
pollingInterval time.Duration
requestTimeout time.Duration
APIKey string `env:"OWM_API_KEY"` // APIKey delivered by Openweathermap
Location string `env:"OWM_LOCATION" envDefault:"Lille,FR"`
Duration int `env:"OWM_DURATION" envDefault:"5"`
}
func loadMetrics(ctx context.Context, location string) <-chan error {
errC := make(chan error)
go func() {
c := time.Tick(cfg.pollingInterval)
for {
select {
case <-ctx.Done():
return // returning not to leak the goroutine
case <-c:
client := &http.Client{
Timeout: cfg.requestTimeout,
}
w, err := owm.NewCurrent("C", "FR", cfg.APIKey, owm.WithHttpClient(client)) // (internal - OpenWeatherMap reference for kelvin) with English output
if err != nil {
errC <- err
continue
}
err = w.CurrentByName(location)
if err != nil {
errC <- err
continue
}
temp.WithLabelValues(location).Set(w.Main.Temp)
pressure.WithLabelValues(location).Set(w.Main.Pressure)
humidity.WithLabelValues(location).Set(float64(w.Main.Humidity))
wind.WithLabelValues(location).Set(w.Wind.Speed)
clouds.WithLabelValues(location).Set(float64(w.Clouds.All))
rain.WithLabelValues(location).Set(w.Rain.ThreeH)
var scraped_weather = w.Weather[0].Description
if scraped_weather == last_weather {
weather.WithLabelValues(location, scraped_weather).Set(1)
} else {
weather.WithLabelValues(location, scraped_weather).Set(1)
weather.WithLabelValues(location, last_weather).Set(0)
last_weather = scraped_weather
}
log.Println(w.Weather[0].Description)
log.Println("scraping OK for ", location)
}
}
}()
return errC
}
var (
cfg = Config{
pollingInterval: 5 * time.Second,
requestTimeout: 1 * time.Second,
}
temp = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "temperature_celsius",
Help: "Temperature in °C",
}, []string{"location"})
pressure = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "pressure_hpa",
Help: "Atmospheric pressure in hPa",
}, []string{"location"})
humidity = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "humidity_percent",
Help: "Humidity in Percent",
}, []string{"location"})
wind = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "wind_mps",
Help: "Wind speed in m/s",
}, []string{"location"})
clouds = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "cloudiness_percent",
Help: "Cloudiness in Percent",
}, []string{"location"})
rain = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "rain",
Help: "Rain contents 3h",
}, []string{"location"})
weather = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "openweathermap",
Name: "weather",
Help: "The weather label.",
}, []string{"location", "weather"})
last_weather = ""
)
func main() {
env.Parse(&cfg)
prometheus.Register(temp)
prometheus.Register(pressure)
prometheus.Register(humidity)
prometheus.Register(wind)
prometheus.Register(clouds)
prometheus.Register(weather)
errC := loadMetrics(context.TODO(), cfg.Location)
go func() {
for err := range errC {
log.Println(err)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}