-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
billing.go
409 lines (350 loc) · 12.7 KB
/
billing.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package billing
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
"github.com/aws/aws-sdk-go-v2/service/costexplorer/costexploreriface"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/metricbeat/mb"
awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/aws"
)
var (
metricsetName = "billing"
regionName = "us-east-1"
labelSeparator = "|"
// This list is from https://github.com/aws/aws-sdk-go-v2/blob/master/service/costexplorer/api_enums.go#L60-L90
supportedDimensionKeys = []string{
"AZ", "INSTANCE_TYPE", "LINKED_ACCOUNT", "OPERATION", "PURCHASE_TYPE",
"REGION", "SERVICE", "USAGE_TYPE", "USAGE_TYPE_GROUP", "RECORD_TYPE",
"OPERATING_SYSTEM", "TENANCY", "SCOPE", "PLATFORM", "SUBSCRIPTION_ID",
"LEGAL_ENTITY_NAME", "DEPLOYMENT_OPTION", "DATABASE_ENGINE",
"CACHE_ENGINE", "INSTANCE_TYPE_FAMILY", "BILLING_ENTITY",
"RESERVATION_ID",
}
dateLayout = "2006-01-02"
)
// init registers the MetricSet with the central registry as soon as the program
// starts. The New function will be called later to instantiate an instance of
// the MetricSet for each host defined in the module's configuration. After the
// MetricSet has been created then Fetch will begin to be called periodically.
func init() {
mb.Registry.MustAddMetricSet(aws.ModuleName, metricsetName, New,
mb.DefaultMetricSet(),
)
}
// MetricSet holds any configuration or state information. It must implement
// the mb.MetricSet interface. And this is best achieved by embedding
// mb.BaseMetricSet because it implements all of the required mb.MetricSet
// interface methods except for Fetch.
type MetricSet struct {
*aws.MetricSet
logger *logp.Logger
CostExplorerConfig CostExplorerConfig `config:"cost_explorer_config"`
}
// Config holds a configuration specific for billing metricset.
type CostExplorerConfig struct {
GroupByDimensionKeys []string `config:"group_by_dimension_keys"`
GroupByTagKeys []string `config:"group_by_tag_keys"`
}
// New creates a new instance of the MetricSet. New is responsible for unpacking
// any MetricSet specific configuration options if there are any.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logger := logp.NewLogger(metricsetName)
metricSet, err := aws.NewMetricSet(base)
if err != nil {
return nil, fmt.Errorf("error creating aws metricset: %w", err)
}
config := struct {
CostExplorerConfig CostExplorerConfig `config:"cost_explorer_config"`
}{}
err = base.Module().UnpackConfig(&config)
if err != nil {
return nil, fmt.Errorf("error unpack raw module config using UnpackConfig: %w", err)
}
logger.Debugf("cost explorer config = %s", config)
return &MetricSet{
MetricSet: metricSet,
logger: logger,
CostExplorerConfig: config.CostExplorerConfig,
}, nil
}
// Validate checks if given dimension keys are supported.
func (c CostExplorerConfig) Validate() error {
for _, key := range c.GroupByDimensionKeys {
supported, _ := aws.StringInSlice(key, supportedDimensionKeys)
if !supported {
return fmt.Errorf("costexplorer GetCostAndUsageRequest does not support dimension key: %s", key)
}
}
return nil
}
// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
// Get startDate and endDate
startDate, endDate := getStartDateEndDate(m.Period)
// Get startTime and endTime
startTime, endTime := aws.GetStartTimeEndTime(m.Period, m.Latency)
// get cost metrics from cost explorer
awsConfig := m.MetricSet.AwsConfig.Copy()
svcCostExplorer := costexplorer.New(awscommon.EnrichAWSConfigWithEndpoint(
m.Endpoint, "monitoring", "", awsConfig))
awsConfig.Region = regionName
svcCloudwatch := cloudwatch.New(awscommon.EnrichAWSConfigWithEndpoint(
m.Endpoint, "monitoring", regionName, awsConfig))
timePeriod := costexplorer.DateInterval{
Start: awssdk.String(startDate),
End: awssdk.String(endDate),
}
var events []mb.Event
// Get estimated charges from CloudWatch
eventsCW := m.getCloudWatchBillingMetrics(svcCloudwatch, startTime, endTime)
events = append(events, eventsCW...)
// Get total cost from Cost Explorer GetCostAndUsage with group by type "DIMENSION" and "TAG"
eventsCE := m.getCostGroupBy(svcCostExplorer, m.CostExplorerConfig.GroupByDimensionKeys, m.CostExplorerConfig.GroupByTagKeys, timePeriod, startDate, endDate)
events = append(events, eventsCE...)
// report events
for _, event := range events {
if reported := report.Event(event); !reported {
m.Logger().Debug("Fetch interrupted, failed to emit event")
return nil
}
}
return nil
}
func (m *MetricSet) getCloudWatchBillingMetrics(
svcCloudwatch cloudwatchiface.ClientAPI,
startTime time.Time,
endTime time.Time) []mb.Event {
var events []mb.Event
namespace := "AWS/Billing"
listMetricsOutput, err := aws.GetListMetricsOutput(namespace, regionName, svcCloudwatch)
if err != nil {
m.Logger().Error(err.Error())
return nil
}
if listMetricsOutput == nil || len(listMetricsOutput) == 0 {
return events
}
metricDataQueriesTotal := constructMetricQueries(listMetricsOutput, m.Period)
metricDataOutput, err := aws.GetMetricDataResults(metricDataQueriesTotal, svcCloudwatch, startTime, endTime)
if err != nil {
err = fmt.Errorf("aws GetMetricDataResults failed with %w, skipping region %s", err, regionName)
m.Logger().Error(err.Error())
return nil
}
// Find a timestamp for all metrics in output
timestamp := aws.FindTimestamp(metricDataOutput)
if timestamp.IsZero() {
return nil
}
for _, output := range metricDataOutput {
if len(output.Values) == 0 {
continue
}
exists, timestampIdx := aws.CheckTimestampInArray(timestamp, output.Timestamps)
if exists {
labels := strings.Split(*output.Label, labelSeparator)
event := aws.InitEvent("", m.AccountName, m.AccountID, timestamp)
event.MetricSetFields.Put(labels[0], output.Values[timestampIdx])
i := 1
for i < len(labels)-1 {
event.MetricSetFields.Put(labels[i], labels[i+1])
i += 2
}
event.Timestamp = endTime
events = append(events, event)
}
}
return events
}
func (m *MetricSet) getCostGroupBy(svcCostExplorer costexploreriface.ClientAPI, groupByDimKeys []string, groupByTags []string, timePeriod costexplorer.DateInterval, startDate string, endDate string) []mb.Event {
var events []mb.Event
groupBys := getGroupBys(groupByTags, groupByDimKeys)
for _, groupBy := range groupBys {
var groupDefs []costexplorer.GroupDefinition
if groupBy.dimension != "" {
groupDefs = append(groupDefs, costexplorer.GroupDefinition{
Key: awssdk.String(groupBy.dimension),
Type: costexplorer.GroupDefinitionTypeDimension,
})
}
if groupBy.tag != "" {
groupDefs = append(groupDefs, costexplorer.GroupDefinition{
Key: awssdk.String(groupBy.tag),
Type: costexplorer.GroupDefinitionTypeTag,
})
}
groupByCostInput := costexplorer.GetCostAndUsageInput{
Granularity: costexplorer.GranularityDaily,
// no permission for "NetAmortizedCost" and "NetUnblendedCost"
Metrics: []string{"AmortizedCost", "BlendedCost",
"NormalizedUsageAmount", "UnblendedCost", "UsageQuantity"},
TimePeriod: &timePeriod,
// Only two values for GroupBy are allowed
GroupBy: groupDefs,
}
groupByCostReq := svcCostExplorer.GetCostAndUsageRequest(&groupByCostInput)
groupByOutput, err := groupByCostReq.Send(context.Background())
if err != nil {
err = fmt.Errorf("costexplorer GetCostAndUsageRequest failed: %w", err)
m.Logger().Errorf(err.Error())
return nil
}
if len(groupByOutput.ResultsByTime) > 0 {
costResultGroups := groupByOutput.ResultsByTime[0].Groups
for _, group := range costResultGroups {
event := m.addCostMetrics(group.Metrics, groupByOutput.GroupDefinitions[0], startDate, endDate)
// generate unique event ID for each event
eventID := startDate + endDate + *groupByOutput.GroupDefinitions[0].Key + string(groupByOutput.GroupDefinitions[0].Type)
for _, key := range group.Keys {
eventID += key
// key value like db.t2.micro or Amazon Simple Queue Service belongs to dimension
if !strings.Contains(key, "$") {
event.MetricSetFields.Put("group_by."+groupBy.dimension, key)
continue
}
// tag key value is separated by $
tagKey, tagValue := parseGroupKey(key)
if tagValue != "" {
event.MetricSetFields.Put("group_by."+tagKey, tagValue)
}
}
t, err := time.Parse(dateLayout, endDate)
if err == nil {
event.Timestamp = t
}
event.ID = generateEventID(eventID)
events = append(events, event)
}
}
}
return events
}
func (m *MetricSet) addCostMetrics(metrics map[string]costexplorer.MetricValue, groupDefinition costexplorer.GroupDefinition, startDate string, endDate string) mb.Event {
event := aws.InitEvent("", m.AccountName, m.AccountID, time.Now())
// add group definition
event.MetricSetFields.Put("group_definition", common.MapStr{
"key": *groupDefinition.Key,
"type": groupDefinition.Type,
})
for metricName, metricValues := range metrics {
cost := metricValues
costFloat, err := strconv.ParseFloat(*cost.Amount, 64)
if err != nil {
err = fmt.Errorf("strconv ParseFloat failed: %w", err)
m.Logger().Errorf(err.Error())
continue
}
value := common.MapStr{
"amount": costFloat,
"unit": &cost.Unit,
}
event.MetricSetFields.Put(metricName, value)
event.MetricSetFields.Put("start_date", startDate)
event.MetricSetFields.Put("end_date", endDate)
}
return event
}
func constructMetricQueries(listMetricsOutput []cloudwatch.Metric, period time.Duration) []cloudwatch.MetricDataQuery {
var metricDataQueries []cloudwatch.MetricDataQuery
metricDataQueryEmpty := cloudwatch.MetricDataQuery{}
for i, listMetric := range listMetricsOutput {
metricDataQuery := createMetricDataQuery(listMetric, i, period)
if metricDataQuery == metricDataQueryEmpty {
continue
}
metricDataQueries = append(metricDataQueries, metricDataQuery)
}
return metricDataQueries
}
func createMetricDataQuery(metric cloudwatch.Metric, index int, period time.Duration) (metricDataQuery cloudwatch.MetricDataQuery) {
statistic := "Maximum"
periodInSeconds := int64(period.Seconds())
id := metricsetName + strconv.Itoa(index)
metricDims := metric.Dimensions
metricName := *metric.MetricName
label := metricName + labelSeparator
for _, dim := range metricDims {
label += *dim.Name + labelSeparator + *dim.Value + labelSeparator
}
metricDataQuery = cloudwatch.MetricDataQuery{
Id: &id,
MetricStat: &cloudwatch.MetricStat{
Period: &periodInSeconds,
Stat: &statistic,
Metric: &metric,
},
Label: &label,
}
return
}
func getStartDateEndDate(period time.Duration) (startDate string, endDate string) {
currentTime := time.Now()
startTime := currentTime.Add(period * -1)
startDate = startTime.Format(dateLayout)
endDate = currentTime.Format(dateLayout)
return
}
func parseGroupKey(groupKey string) (tagKey string, tagValue string) {
keys := strings.Split(groupKey, "$")
if len(keys) == 2 {
tagKey = keys[0]
tagValue = keys[1]
} else if len(keys) > 2 {
tagKey = keys[0]
tagValue = keys[1]
for i := 2; i < len(keys); i++ {
tagValue = tagValue + "$" + keys[i]
}
} else {
tagKey = keys[0]
tagValue = ""
}
return
}
type groupBy struct {
tag string
dimension string
}
func getGroupBys(groupByTags []string, groupByDimKeys []string) []groupBy {
var groupBys []groupBy
if len(groupByTags) == 0 {
groupByTags = []string{""}
}
if len(groupByDimKeys) == 0 {
groupByDimKeys = []string{""}
}
for _, tagKey := range groupByTags {
for _, dimKey := range groupByDimKeys {
groupBy := groupBy{
tag: tagKey,
dimension: dimKey,
}
groupBys = append(groupBys, groupBy)
}
}
return groupBys
}
func generateEventID(eventID string) string {
// create eventID using hash of startDate + endDate + groupDefinitionKey + groupDefinitionType + values
// This will prevent more than one billing metric getting collected in the same day.
h := sha256.New()
h.Write([]byte(eventID))
prefix := hex.EncodeToString(h.Sum(nil))
return prefix[:20]
}