/
main.go
155 lines (144 loc) · 4.55 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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/maja42/goval"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
flagPath = flag.String("p", "/metrics", "HTTP path where to expose metrics to")
flagListen = flag.String("l", ":9106", "Address to listen to")
flagAPIURL = flag.String("A", "http://localhost:8080", "URL of the PUN API endpoint")
flagCompoundMetric = flag.String("C", "", "Custom metric. If empty, no custom metric is exported. A custom metric based on PUN or the monthly average. Example: \"monthly_cost=MPUN/1000+0.08\". You can use PUN (latest PUN) and MPUN (monthly average)")
flagSleepInterval = flag.Duration("i", time.Minute, "Interval between speedtest executions, expressed as a Go duration string")
)
func splitLabelExpression(labelExpression string) (string, string, error) {
parts := strings.SplitN(labelExpression, "=", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("expected 2 expression components, got %d", len(parts))
}
return parts[0], parts[1], nil
}
func main() {
flag.Parse()
if *flagAPIURL == "" {
log.Fatal("API URL cannot be empty")
}
u, err := url.Parse(*flagAPIURL)
if err != nil {
log.Fatalf("Invalid API URL: %v", err)
}
if u.Scheme == "" || u.Host == "" {
log.Fatalf("Scheme or host cannot be empty in API URL")
}
var (
eval *goval.Evaluator
custom_name, custom_expr string
)
if *flagCompoundMetric != "" {
custom_name, custom_expr, err = splitLabelExpression(*flagCompoundMetric)
if err != nil {
log.Fatalf("Failed to split label from expression: %v", err)
}
eval = goval.NewEvaluator()
}
punGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mercatoelettrico_pun",
Help: "PUN - Prezzo Unico Nazionale for the Italian Mercato Elettrico",
},
[]string{},
)
if err := prometheus.Register(punGauge); err != nil {
log.Fatalf("Failed to register PUN gauge: %v", err)
}
punMonthlyAvgGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mercatoelettrico_pun_month_average",
Help: "PUN - Current month's average for Prezzo Unico Nazionale for the Italian Mercato Elettrico",
},
[]string{},
)
if err := prometheus.Register(punMonthlyAvgGauge); err != nil {
log.Fatalf("Failed to register PUN monthly average gauge: %v", err)
}
var punCustomGauge *prometheus.GaugeVec
if eval != nil {
log.Printf("Creating custom gauge `%s` with formula `%s`", custom_name, custom_expr)
punCustomGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "mercatoelettrico_" + custom_name,
Help: "PUN - Custom metric using Prezzo Unico Nazionale - formula: " + custom_expr,
},
[]string{},
)
if err := prometheus.Register(punCustomGauge); err != nil {
log.Fatalf("Failed to register PUN custom gauge: %v", err)
}
}
getPun := func(endpoint string) (float64, error) {
resp, err := http.Get(endpoint)
if err != nil {
return 0, fmt.Errorf("GET failed: %w", err)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("HTTP body read failed: %w", err)
}
if err := resp.Body.Close(); err != nil {
log.Printf("Warning: failed to close HTTP body: %v", err)
}
return strconv.ParseFloat(string(data), 64)
}
go func() {
firstrun := true
for {
if !firstrun {
log.Printf("Sleeping %s...", *flagSleepInterval)
time.Sleep(*flagSleepInterval)
}
firstrun = false
// export PUN
log.Printf("Fetching PUN value...")
pun, err := getPun(*flagAPIURL)
if err != nil {
log.Printf("Failed to fetch PUN value: %v", err)
} else {
punGauge.WithLabelValues().Set(pun)
}
// export monthly PUN average
log.Printf("Fetching PUN monthly average value...")
punavg, err := getPun(*flagAPIURL + "/month")
if err != nil {
log.Printf("Failed to fetch PUN monthly average value: %v", err)
} else {
punMonthlyAvgGauge.WithLabelValues().Set(punavg)
}
if eval != nil {
// export custom metric
log.Printf("Computing custom metric `%s`", custom_name)
variables := map[string]interface{}{
"PUN": pun,
"MPUN": punavg,
}
custom_metric, err := eval.Evaluate(custom_expr, variables, nil)
if err != nil {
log.Printf("Failed to evaluate custom metric: %v", err)
} else {
punCustomGauge.WithLabelValues().Set(custom_metric.(float64))
}
}
}
}()
http.Handle(*flagPath, promhttp.Handler())
log.Printf("Starting server on %s", *flagListen)
log.Fatal(http.ListenAndServe(*flagListen, nil))
}