-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
303 lines (263 loc) · 9.53 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
299
300
301
302
303
package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/krateoplatformops/finops-prometheus-exporter-generic/internal/utils"
"k8s.io/client-go/rest"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
finopsdatatypes "github.com/krateoplatformops/finops-data-types/api/v1"
"github.com/krateoplatformops/finops-prometheus-exporter-generic/internal/helpers/kube/endpoints"
"github.com/krateoplatformops/finops-prometheus-exporter-generic/internal/helpers/kube/httpcall"
)
type recordGaugeCombo struct {
record []string
gauge prometheus.Gauge
}
func ParseConfigFile(file string) (finopsdatatypes.ExporterScraperConfig, *httpcall.Endpoint, error) {
fileReader, err := os.OpenFile(file, os.O_RDONLY, 0600)
if err != nil {
return finopsdatatypes.ExporterScraperConfig{}, &httpcall.Endpoint{}, err
}
defer fileReader.Close()
data, err := io.ReadAll(fileReader)
if err != nil {
return finopsdatatypes.ExporterScraperConfig{}, &httpcall.Endpoint{}, err
}
parse := finopsdatatypes.ExporterScraperConfig{}
err = yaml.Unmarshal(data, &parse)
if err != nil {
return finopsdatatypes.ExporterScraperConfig{}, &httpcall.Endpoint{}, err
}
regex, _ := regexp.Compile("<.*?>")
rc, _ := rest.InClusterConfig()
endpoint, err := endpoints.Resolve(context.Background(), endpoints.ResolveOptions{
RESTConfig: rc,
API: &parse.Spec.ExporterConfig.API,
})
if err != nil {
return finopsdatatypes.ExporterScraperConfig{}, &httpcall.Endpoint{}, err
}
newURL := endpoint.ServerURL
toReplaceRange := regex.FindStringIndex(newURL)
for toReplaceRange != nil {
// Use the indexes of the match of the regex to replace the URL with the value of the additional variable from the config file
// The replacement has +1/-1 on the indexes to remove the < and > from the string to use as key in the config map
// If the replacement contains ONLY uppercase letters, it is taken from environment variables
varToReplace := parse.Spec.ExporterConfig.AdditionalVariables[newURL[toReplaceRange[0]+1:toReplaceRange[1]-1]]
if varToReplace == strings.ToUpper(varToReplace) {
varToReplace = os.Getenv(varToReplace)
}
newURL = strings.Replace(newURL, newURL[toReplaceRange[0]:toReplaceRange[1]], varToReplace, -1)
toReplaceRange = regex.FindStringIndex(newURL)
}
endpoint.ServerURL = newURL
newURL = parse.Spec.ExporterConfig.API.Path
toReplaceRange = regex.FindStringIndex(newURL)
for toReplaceRange != nil {
// Use the indexes of the match of the regex to replace the URL with the value of the additional variable from the config file
// The replacement has +1/-1 on the indexes to remove the < and > from the string to use as key in the config map
// If the replacement contains ONLY uppercase letters, it is taken from environment variables
varToReplace := parse.Spec.ExporterConfig.AdditionalVariables[newURL[toReplaceRange[0]+1:toReplaceRange[1]-1]]
if varToReplace == strings.ToUpper(varToReplace) {
varToReplace = os.Getenv(varToReplace)
}
newURL = strings.Replace(newURL, newURL[toReplaceRange[0]:toReplaceRange[1]], varToReplace, -1)
toReplaceRange = regex.FindStringIndex(newURL)
}
parse.Spec.ExporterConfig.API.Path = newURL
return parse, endpoint, nil
}
func makeAPIRequest(config finopsdatatypes.ExporterScraperConfig, endpoint *httpcall.Endpoint, fileName string) {
log.Logger.Info().Msgf("Request URL: %s", endpoint.ServerURL)
httpClient, err := httpcall.HTTPClientForEndpoint(endpoint)
if err != nil {
fatal(err)
}
res, err := httpcall.Do(context.TODO(), httpClient, httpcall.Options{
API: &config.Spec.ExporterConfig.API,
Endpoint: endpoint,
})
if err != nil {
fatal(err)
}
defer res.Body.Close()
if res.StatusCode == 202 {
res.Body.Close()
secondsToSleep, _ := strconv.ParseInt(res.Header.Get("Retry-after"), 10, 64)
time.Sleep(time.Duration(secondsToSleep) * time.Second)
res, err = http.Get(res.Header.Get("Location"))
fatal(err)
var data map[string]string
err = json.NewDecoder(res.Body).Decode(&data)
fatal(err)
res.Body.Close()
res, err = http.Get(data["downloadUrl"])
fatal(err)
}
data, err := io.ReadAll(res.Body)
fatal(err)
log.Logger.Info().Msg("Trying to parse data as JSON")
jsonDataParsed, err := utils.TryParseResponseAsFocusJSON(utils.TrapBOM(data))
if err != nil {
err = os.WriteFile(fmt.Sprintf("/temp/%s.dat", fileName), utils.TrapBOM(data), 0644)
fatal(err)
} else {
err = os.WriteFile(fmt.Sprintf("/temp/%s.dat", fileName), jsonDataParsed, 0644)
if err != nil {
fatal(err)
}
}
}
func getRecordsFromFile(fileName string) [][]string {
file, err := os.Open(fmt.Sprintf("/temp/%s.dat", fileName))
fatal(err)
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
fatal(err)
return records
}
func updatedMetrics(config finopsdatatypes.ExporterScraperConfig, endpoint *httpcall.Endpoint, useConfig bool, registry *prometheus.Registry, prometheusMetrics map[string]recordGaugeCombo) {
for {
attemptResourceExportersInThisIteration := true
fileName := ""
if config.Spec.ExporterConfig.Provider.Name != "" {
fileName = config.Spec.ExporterConfig.Provider.Name
} else {
fileName = "download"
}
if useConfig {
makeAPIRequest(config, endpoint, fileName)
}
records := getRecordsFromFile(fileName)
resourceTypes := []string{}
if config.Spec.ExporterConfig.Provider.Name != "" {
var err error
resourceTypes, err = utils.InitializeResourcesWithProvider(config)
if err != nil {
log.Logger.Warn().Err(err).Msg("error while retrieving provider name, continuing...")
attemptResourceExportersInThisIteration = false
}
}
// Obtain various indexes
// BilledCost for value of metric
// ResourceType to check if additional exporters need to be started
billedCostIndex, err := utils.GetIndexOf(records, "BilledCost")
if err != nil {
log.Logger.Warn().Err(err).Msg("error while selecting column BilledCost, retrying...")
continue
}
resourceTypeIndex := -1
if attemptResourceExportersInThisIteration {
resourceTypeIndex, err = utils.GetIndexOf(records, "ResourceType")
if err != nil {
log.Logger.Warn().Err(err).Msg("error while selecting column ResourceType, retrying...")
continue
}
}
notFound := true
for i, record := range records {
// Skip header line
if i == 0 {
continue
}
if attemptResourceExportersInThisIteration {
for _, resourceName := range resourceTypes {
if record[resourceTypeIndex] == resourceName {
resourceIdIndex, err := utils.GetIndexOf(records, "ResourceId")
if err != nil {
log.Logger.Warn().Err(err).Msg("error while selecting column ResourceId, retrying...")
continue
}
found := false
for _, elem := range utils.ResourceIdTypeComboList {
if strings.EqualFold(record[resourceIdIndex], elem.ResourceId) {
found = true
}
}
if !found {
utils.ResourceIdTypeComboList = append(utils.ResourceIdTypeComboList, utils.ResourceIdTypeCombo{ResourceId: record[resourceIdIndex], ResourceType: resourceName})
}
}
}
}
notFound = true
if _, ok := prometheusMetrics[strings.Join(record, " ")]; ok {
metricValue, err := strconv.ParseFloat(record[billedCostIndex], 64)
fatal(err)
prometheusMetrics[strings.Join(record, " ")].gauge.Set(metricValue)
notFound = false
}
if notFound {
labels := prometheus.Labels{}
for j, value := range record {
if strings.Contains(records[0][j], "x_") {
continue
}
if !strings.Contains(records[0][j], "Tags") {
labels[records[0][j]] = value
} else {
replacer := strings.NewReplacer("{", "", "}", "", "=", ":", ",", ";", "\"", "")
labels[records[0][j]] = replacer.Replace(value)
}
}
newMetricsRow := promauto.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("billed_cost_%s_%d", strings.ReplaceAll(config.Spec.ExporterConfig.Provider.Name, "-", "_"), i),
ConstLabels: labels,
})
metricValue, err := strconv.ParseFloat(records[i][billedCostIndex], 64)
fatal(err)
newMetricsRow.Set(metricValue)
prometheusMetrics[strings.Join(record, " ")] = recordGaugeCombo{record: record, gauge: newMetricsRow}
registry.MustRegister(newMetricsRow)
}
}
if attemptResourceExportersInThisIteration {
err = utils.StartNewExporters(config, endpoint)
if err != nil {
log.Logger.Warn().Err(err).Msg("error while starting resource exporters, continuing...")
}
}
time.Sleep(time.Duration(config.Spec.ExporterConfig.PollingIntervalHours) * time.Hour)
}
}
func main() {
var err error
config := finopsdatatypes.ExporterScraperConfig{}
endpoint := &httpcall.Endpoint{}
useConfig := true
if len(os.Args) <= 1 {
config, endpoint, err = ParseConfigFile("/config/config.yaml")
if err != nil {
log.Logger.Error().Err(err).Msg("error while parsing configuration, exiting")
return
}
} else {
useConfig = false
config.Spec.ExporterConfig.Provider.Name = os.Args[1]
config.Spec.ExporterConfig.PollingIntervalHours = 1
}
registry := prometheus.NewRegistry()
go updatedMetrics(config, endpoint, useConfig, registry, map[string]recordGaugeCombo{})
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
http.Handle("/metrics", handler)
http.ListenAndServe(":2112", nil)
}
func fatal(err error) {
if err != nil {
log.Logger.Warn().Err(err).Msg("an error has occured, continuing...")
}
}