-
Notifications
You must be signed in to change notification settings - Fork 15
/
compute.go
205 lines (162 loc) · 4.21 KB
/
compute.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
package processor
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/prometheus/client_golang/prometheus"
"github.com/st8ed/aws-cost-exporter/pkg/state"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
_ "github.com/mithrandie/csvq-driver"
)
func Compute(ctx context.Context, config *state.Config, registry *prometheus.Registry, logger log.Logger) error {
if err := updateSymlinks(config); err != nil {
return err
}
db, err := sql.Open("csvq", config.RepositoryPath)
if err != nil {
return err
} else {
level.Debug(logger).Log("msg", "Opened database", "repository", config.RepositoryPath)
}
defer func() {
if err := db.Close(); err != nil {
level.Warn(logger).Log("msg", "Unable to close database", "err", err)
}
level.Debug(logger).Log("msg", "Closed database")
}()
items, err := os.ReadDir(config.QueriesPath)
if err != nil {
return err
}
for _, item := range items {
if item.IsDir() {
continue
}
// TODO: Basic file validation (extension)
query, err := os.ReadFile(filepath.Join(config.QueriesPath, item.Name()))
if err != nil {
return err
}
level.Debug(logger).Log("msg", "Running query", "name", item.Name())
rows, err := db.QueryContext(ctx, string(query))
if err != nil {
return err
}
level.Debug(logger).Log("msg", "Updating metrics registry")
err = ingestMetrics(registry, rows)
if err != nil {
return err
}
}
return nil
}
func ingestMetrics(registry *prometheus.Registry, rows *sql.Rows) error {
columns, err := rows.Columns()
if err != nil {
return err
}
if len(columns) == 0 {
return errors.New("malformed query: there are no columns in result set")
}
labelNames := make([]string, 0)
labelValuePtrs := make([]*string, 0)
metricNames := make([]string, 0)
metricValuePtrs := make([]*float64, 0)
rowValuePtrs := make([]interface{}, len(columns))
for i, column := range columns {
if strings.HasPrefix(column, "metric_") {
// FIXME: Check metric name using regexp
value := new(float64)
metricNames = append(metricNames, strings.TrimPrefix(column, "metric_"))
metricValuePtrs = append(metricValuePtrs, value)
rowValuePtrs[i] = value
} else {
value := new(string)
labelNames = append(labelNames, column)
labelValuePtrs = append(labelValuePtrs, value)
rowValuePtrs[i] = value
}
}
gauges := make([]*prometheus.GaugeVec, len(metricNames))
for i, name := range metricNames {
gauges[i] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: fmt.Sprintf("aws_report_%s", name),
},
labelNames,
)
if err := registry.Register(gauges[i]); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
gauges[i] = are.ExistingCollector.(*prometheus.GaugeVec)
} else {
return err
}
}
}
for rows.Next() {
if err := rows.Scan(rowValuePtrs...); err != nil {
if err == sql.ErrNoRows {
break
} else {
return err
}
} else {
labelValues := make([]string, len(labelValuePtrs))
for i := range labelValuePtrs {
labelValues[i] = *labelValuePtrs[i]
}
for i, gauge := range gauges {
gauge.WithLabelValues(labelValues...).Set(*metricValuePtrs[i])
}
}
}
return nil
}
func updateSymlinks(config *state.Config) error {
items, err := os.ReadDir(filepath.Join(config.RepositoryPath, "data"))
if err != nil {
return err
}
names := make([]string, 0)
for _, item := range items {
if item.IsDir() {
continue
}
names = append(names, item.Name())
}
sort.Sort(sort.Reverse(sort.StringSlice(names)))
for i, name := range names {
var source string
if i == 0 {
source = "report-current.csv"
} else {
source = fmt.Sprintf("report-%d.csv", i)
}
source = filepath.Join(config.RepositoryPath, source)
target := filepath.Join(".", "data", name)
if err := symlink(source, target); err != nil {
return err
}
}
return nil
}
// Atomically overwrite symbolic link
func symlink(source string, target string) error {
sourceTmp := source + ".tmp"
if err := os.Remove(sourceTmp); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.Symlink(target, sourceTmp); err != nil {
return err
}
if err := os.Rename(sourceTmp, source); err != nil {
return err
}
return nil
}