-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
146 lines (135 loc) · 3.36 KB
/
handler.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
package handler
import (
"net/http"
"os"
"strconv"
"time"
"github.com/atompi/budget_exporter/pkg/options"
csvutil "github.com/atompi/budget_exporter/pkg/util/csv"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
type metrics struct {
total *prometheus.GaugeVec
based *prometheus.GaugeVec
increased *prometheus.GaugeVec
left *prometheus.GaugeVec
}
func RootHandler(opts options.Options) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"metrics_path": opts.Web.Path})
}
}
func MetricsHandler(opts options.ScrapeOptions) gin.HandlerFunc {
reg := prometheus.NewRegistry()
m := newMetrics(reg)
go func() {
for {
records, err := getCSV(opts.Address)
if err != nil {
zap.L().Sugar().Errorf("get csv records failed: %v", err)
time.Sleep(time.Duration(60) * time.Second)
continue
}
for _, record := range *records {
business := record[opts.LabelHeader.Business]
provider := record[opts.LabelHeader.Provider]
total, err := strconv.ParseFloat(record[opts.LabelHeader.Total], 64)
if err != nil {
zap.L().Sugar().Errorf("parse total failed: %v", err)
continue
}
based, err := strconv.ParseFloat(record[opts.LabelHeader.Based], 64)
if err != nil {
zap.L().Sugar().Errorf("parse based failed: %v", err)
continue
}
increased, err := strconv.ParseFloat(record[opts.LabelHeader.Increased], 64)
if err != nil {
zap.L().Sugar().Errorf("parse increased failed: %v", err)
continue
}
left, err := strconv.ParseFloat(record[opts.LabelHeader.Left], 64)
if err != nil {
zap.L().Sugar().Errorf("parse left failed: %v", err)
continue
}
m.total.WithLabelValues(business, provider).Set(total)
m.based.WithLabelValues(business, provider).Set(based)
m.increased.WithLabelValues(business, provider).Set(increased)
m.left.WithLabelValues(business, provider).Set(left)
}
time.Sleep(time.Duration(opts.Interval) * time.Second)
}
}()
h := promhttp.HandlerFor(
reg,
promhttp.HandlerOpts{
Registry: reg,
EnableOpenMetrics: true,
},
)
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func newMetrics(reg *prometheus.Registry) *metrics {
m := &metrics{
total: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "budget_total",
Help: "Budget total",
},
[]string{
"business",
"provider",
},
),
based: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "budget_based",
Help: "Budget based",
},
[]string{
"business",
"provider",
}),
increased: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "budget_increased",
Help: "Budget increased",
},
[]string{
"business",
"provider",
}),
left: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "budget_left",
Help: "Budget left",
},
[]string{
"business",
"provider",
}),
}
reg.MustRegister(m.total, m.based, m.increased, m.left)
return m
}
func getCSV(filePath string) (records *[]map[string]string, err error) {
f, err := os.Open(filePath)
if err != nil {
return
}
defer f.Close()
r := csvutil.BOMAwareCSVReader(f)
res, err := r.ReadAll()
if err != nil {
return
}
data := &res
records, err = csvutil.DataToMap(data)
return
}