-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
110 lines (90 loc) · 2.71 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
_ "time/tzdata"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type regionFlag []string
func (r *regionFlag) String() string {
return strings.Join(*r, ", ")
}
func (r *regionFlag) Set(value string) error {
*r = append(*r, value)
return nil
}
func main() {
var regions regionFlag
flag.Var(®ions, "region", "region to query for, SN1-4, can be passed multiple times")
txtfile := flag.String("output.file", "", "write metrics to specified file (must have .prom extension)")
addr := flag.String("output.http", "", "host:port to listen on for HTTP scrapes")
showVersion := flag.Bool("version", false, "show version and build info")
flag.Parse()
if *showVersion {
fmt.Fprintf(os.Stdout, "{\"version\": \"%s\", \"commit\": \"%s\", \"date\": \"%s\"}\n", Version(), Commit(), Timestamp())
os.Exit(0)
}
if len(regions) == 0 {
log.Fatalln("need at least one region")
}
loc, err := time.LoadLocation("Europe/Stockholm")
if err != nil {
log.Fatalln(err)
}
c := prometheus.NewPedanticRegistry()
c.MustRegister(NewVattenfallCollector(regions, loc))
if *txtfile == "" && *addr == "" {
WriteMetricsTo(os.Stdout, c)
os.Exit(0)
}
if *txtfile != "" {
if elems := strings.Split(*txtfile, "."); elems[len(elems)-1] != "prom" {
log.Fatalln("filename must end with .prom extension:", *txtfile)
}
err := prometheus.WriteToTextfile(*txtfile, c)
if err != nil {
log.Fatalln(err)
}
os.Exit(0)
}
if *addr != "" {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
p := prometheus.NewPedanticRegistry()
p.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
p.MustRegister(collectors.NewGoCollector())
listener, err := net.Listen("tcp", *addr)
if err != nil {
log.Fatalln(err)
}
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(p, promhttp.HandlerOpts{}))
mux.Handle("/prices", promhttp.InstrumentMetricHandler(p, promhttp.HandlerFor(c, promhttp.HandlerOpts{})))
mux.Handle("/forecast", promhttp.InstrumentMetricHandler(p, http.HandlerFunc(forecastHandler(loc, regions))))
h := &http.Server{
Handler: mux,
}
go func() {
if err := h.Serve(listener); err != http.ErrServerClosed {
log.Fatalln(err.Error())
}
}()
log.Println("exporter listening on:", listener.Addr().String())
<-ctx.Done()
t, tc := context.WithTimeout(context.Background(), 5*time.Second)
defer tc()
h.Shutdown(t)
log.Println("exporter shutdown completed")
}
}