-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
298 lines (277 loc) · 7.83 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
defaultPort = "9122"
weatherAPIEndpoint = "https://api.weather.com/v2/pws/observations/current?stationId=%s&format=json&apiKey=%s&units=m&numericPrecision=decimal"
)
var (
apiKey = os.Getenv("WU_API_KEY")
)
func newWeatherMetrics() map[string]*prometheus.GaugeVec {
labels := []string{"stationID", "neighborhood", "softwareType", "country"}
return map[string]*prometheus.GaugeVec{
"temperature": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_temp",
Help: "Air temperature in degrees Celsius",
},
labels,
),
"dewpoint": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_dewpt",
Help: "Dew point temperature in degrees Celsius",
},
labels,
),
"humidity": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_humidity",
Help: "Relative humidity in percentage",
},
labels,
),
"pressure": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_pressure",
Help: "Atmospheric pressure at sea level in hectopascals",
},
labels,
),
"windspeed": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_windSpeed",
Help: "Wind speed in meters per second",
},
labels,
),
"winddirection": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_windDir",
Help: "Wind direction in degrees",
},
labels,
),
"windgust": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_windGust",
Help: "Wind gust speed in meters per second",
},
labels,
),
"precipitation_rate": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_precipRate",
Help: "Precipitation rate in millimeters per hour",
},
labels,
),
"precipitation_total": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_precipTotal",
Help: "Total accumulated precipitation in millimeters",
},
labels,
),
"uv_index": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_uv",
Help: "Ultraviolet Index",
},
labels,
),
"solar_radiation": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_solarRadiation",
Help: "Solar radiation in watts per square meter",
},
labels,
),
"epoch": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_epoch",
Help: "Epoch time in seconds",
},
labels,
),
"visibility": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_visibility",
Help: "Visibility in meters",
},
labels,
),
"soil_temperature": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_soilTemp",
Help: "Soil temperature in degrees Celsius",
},
labels,
),
"soil_moisture": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_soilMoisture",
Help: "Soil moisture in percentage",
},
labels,
),
"windchill": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_windChill",
Help: "Wind chill temperature in degrees Celsius",
},
labels,
),
"elevation": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_elevation",
Help: "Elevation in meters",
},
labels,
),
"latitude": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_latitude",
Help: "Latitude",
},
labels,
),
"longitude": prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wunderground_longitude",
Help: "Longitude",
},
labels,
),
}
}
type WeatherObservation struct {
Observations []struct {
StationID string `json:"stationID"`
ObsTimeUTC string `json:"obsTimeUtc"`
ObsTimeLocal string `json:"obsTimeLocal"`
Neighborhood string `json:"neighborhood"`
SoftwareType string `json:"softwareType"`
Country string `json:"country"`
SolarRadiation float64 `json:"solarRadiation"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
RealtimeFrequency interface{} `json:"realtimeFrequency"`
Epoch int `json:"epoch"`
UV float64 `json:"uv"`
WindDir int `json:"winddir"`
Humidity float64 `json:"humidity"`
QCStatus int `json:"qcStatus"`
Metric struct {
Temp float64 `json:"temp"`
HeatIndex float64 `json:"heatIndex"`
DewPt float64 `json:"dewpt"`
WindChill float64 `json:"windChill"`
WindSpeed float64 `json:"windSpeed"`
WindGust float64 `json:"windGust"`
Pressure float64 `json:"pressure"`
PrecipRate float64 `json:"precipRate"`
PrecipTotal float64 `json:"precipTotal"`
Elev float64 `json:"elev"`
} `json:"metric"`
} `json:"observations"`
}
type WeatherData struct {
StationID string
Epoch int
Latitude float64
Longitude float64
Elevation float64
Neighborhood string
SoftwareType string
Country string
Sensors map[string]float64
}
func fetchWeatherData(stationID string) (WeatherData, error) {
url := fmt.Sprintf(weatherAPIEndpoint, stationID, apiKey)
resp, err := http.Get(url)
if err != nil {
return WeatherData{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return WeatherData{}, err
}
if resp.StatusCode != http.StatusOK {
return WeatherData{}, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
var weatherObservation WeatherObservation
err = json.Unmarshal(body, &weatherObservation)
if err != nil {
return WeatherData{}, err
}
obs := weatherObservation.Observations[0]
data := WeatherData{
StationID: stationID,
Epoch: obs.Epoch,
Latitude: obs.Lat,
Longitude: obs.Lon,
Elevation: obs.Metric.Elev,
Neighborhood: obs.Neighborhood,
SoftwareType: obs.SoftwareType,
Country: obs.Country,
Sensors: map[string]float64{
"temperature": obs.Metric.Temp,
"dewpoint": obs.Metric.DewPt,
"humidity": obs.Humidity,
"pressure": obs.Metric.Pressure,
"windspeed": obs.Metric.WindSpeed,
"winddirection": float64(obs.WindDir),
"windgust": obs.Metric.WindGust,
"precipitation_rate": obs.Metric.PrecipRate,
"precipitation_total": obs.Metric.PrecipTotal,
"uv_index": obs.UV,
"solar_radiation": obs.SolarRadiation,
},
}
return data, nil
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP)
router.HandleFunc("/scrape", func(w http.ResponseWriter, r *http.Request) {
stationID := r.URL.Query().Get("station_id")
if stationID == "" {
http.Error(w, "station_id query parameter is required", http.StatusBadRequest)
return
}
weatherData, err := fetchWeatherData(stationID)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to fetch weather data: %s", err), http.StatusInternalServerError)
return
}
registry := prometheus.NewRegistry()
weatherMetrics := newWeatherMetrics()
for _, metric := range weatherMetrics {
registry.MustRegister(metric)
}
for sensor, value := range weatherData.Sensors {
if metric, ok := weatherMetrics[sensor]; ok {
metric.WithLabelValues(stationID, weatherData.Neighborhood, weatherData.SoftwareType, weatherData.Country).Set(value)
}
}
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}).ServeHTTP(w, r)
})
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}