-
Notifications
You must be signed in to change notification settings - Fork 1
/
anomaly.go
264 lines (248 loc) · 8.29 KB
/
anomaly.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
package reactor
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"strconv"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
"github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/mashiike/aws-cost-anomaly-slack-reactor/internal/costexplorerx"
)
type Anomaly struct {
AccountID string `json:"accountId"`
AnomalyDetailsLink string `json:"anomalyDetailsLink"`
AnomalyEndDate time.Time `json:"anomalyEndDate"`
AnomalyID string `json:"anomalyId"`
AnomalyScore AnomalyScore `json:"anomalyScore"`
AnomalyStartDate time.Time `json:"anomalyStartDate"`
DimensionalValue string `json:"dimensionalValue"`
Impact AnomalyImpact `json:"impact"`
MonitorArn string `json:"monitorArn"`
RootCauses []RootCause `json:"rootCauses"`
SubscriptionID string `json:"subscriptionId"`
SubscriptionName string `json:"subscriptionName"`
}
type AnomalyScore struct {
CurrentScore float64 `json:"currentScore"`
MaxScore float64 `json:"maxScore"`
}
type AnomalyImpact struct {
MaxImpact float64 `json:"maxImpact"`
TotalActualSpend float64 `json:"totalActualSpend"`
TotalExpectedSpend float64 `json:"totalExpectedSpend"`
TotalImpact float64 `json:"totalImpact"`
TotalImpactPercentage float64 `json:"totalImpactPercentage"`
}
type RootCause struct {
LinkedAccount string `json:"linkedAccount"`
LinkedAccountName string `json:"linkedAccountName"`
Region string `json:"region"`
Service string `json:"service"`
UsageType string `json:"usageType"`
}
type Graph struct {
r io.Reader
size int64
}
type DescribeAccountAPIClient interface {
DescribeAccount(ctx context.Context, input *organizations.DescribeAccountInput, optFns ...func(*organizations.Options)) (*organizations.DescribeAccountOutput, error)
}
type GraphGenerator struct {
client costexplorerx.GetCostAndUsageAPIClient
org DescribeAccountAPIClient
cacheDescribeAccountOutput map[string]*organizations.DescribeAccountOutput
cacheDescribeAccountError map[string]error
cacheDescribeAccountMu sync.Mutex
cacheDescribeAccountExpire map[string]time.Time
}
func NewGraphGenerator(client costexplorerx.GetCostAndUsageAPIClient, org DescribeAccountAPIClient) *GraphGenerator {
return &GraphGenerator{
client: client,
org: org,
cacheDescribeAccountOutput: make(map[string]*organizations.DescribeAccountOutput),
cacheDescribeAccountError: make(map[string]error),
cacheDescribeAccountExpire: make(map[string]time.Time),
}
}
func (g *GraphGenerator) describeAccount(ctx context.Context, accountID string) (*organizations.DescribeAccountOutput, error) {
g.cacheDescribeAccountMu.Lock()
defer g.cacheDescribeAccountMu.Unlock()
if expire, ok := g.cacheDescribeAccountExpire[accountID]; ok && time.Now().Before(expire) {
return g.cacheDescribeAccountOutput[accountID], g.cacheDescribeAccountError[accountID]
}
out, err := g.org.DescribeAccount(ctx, &organizations.DescribeAccountInput{AccountId: aws.String(accountID)})
g.cacheDescribeAccountExpire[accountID] = time.Now().Add(1 * time.Hour)
if err != nil {
g.cacheDescribeAccountError[accountID] = err
return nil, err
}
g.cacheDescribeAccountOutput[accountID] = out
return out, nil
}
func (g *GraphGenerator) Generate(ctx context.Context, anomaly Anomaly) ([]*Graph, error) {
graphs := make([]*Graph, 0, len(anomaly.RootCauses))
for _, c := range anomaly.RootCauses {
w, err := g.generate(ctx, anomaly.AnomalyStartDate.AddDate(0, 0, -8), anomaly.AnomalyEndDate.AddDate(0, 0, 8), c)
if err != nil {
return nil, fmt.Errorf("failed to generate graph: %w", err)
}
var buf bytes.Buffer
n, err := w.WriteTo(&buf)
if err != nil {
return nil, fmt.Errorf("failed to write graph: %w", err)
}
graphs = append(graphs, &Graph{r: &buf, size: n})
}
return graphs, nil
}
func firstDayOfNextMonth() time.Time {
now := time.Now()
nextMonth := now.Month() + 1
year := now.Year()
if nextMonth > 12 {
nextMonth = 1
year++
}
return time.Date(year, nextMonth, 1, 0, 0, 0, 0, time.Local)
}
func lastDayOfThisMonth() time.Time {
return firstDayOfNextMonth().Add(-time.Second)
}
func (g *GraphGenerator) generate(ctx context.Context, startAt, endAt time.Time, c RootCause) (io.WriterTo, error) {
costLabel := []string{}
andExpr := []types.Expression{
{
Dimensions: &types.DimensionValues{
Key: types.DimensionRecordType,
Values: []string{"Usage"},
},
},
}
if c.LinkedAccount != "" {
andExpr = append(andExpr, types.Expression{
Dimensions: &types.DimensionValues{
Key: types.DimensionLinkedAccount,
Values: []string{c.LinkedAccount},
},
})
costLabel = append(costLabel, fmt.Sprintf("%s(%s)", c.LinkedAccountName, c.LinkedAccount))
}
if c.Region != "" {
andExpr = append(andExpr, types.Expression{
Dimensions: &types.DimensionValues{
Key: types.DimensionRegion,
Values: []string{c.Region},
},
})
costLabel = append(costLabel, c.Region)
}
if c.Service != "" {
andExpr = append(andExpr, types.Expression{
Dimensions: &types.DimensionValues{
Key: types.DimensionService,
Values: []string{c.Service},
},
})
costLabel = append(costLabel, c.Service)
}
if c.UsageType != "" {
andExpr = append(andExpr, types.Expression{
Dimensions: &types.DimensionValues{
Key: types.DimensionUsageType,
Values: []string{c.UsageType},
},
})
costLabel = append(costLabel, c.UsageType)
}
// for ValidationException: end date past the beginning of next month
if endAt.After(lastDayOfThisMonth()) {
endAt = lastDayOfThisMonth()
}
input := &costexplorer.GetCostAndUsageInput{
Granularity: types.GranularityDaily,
TimePeriod: &types.DateInterval{
Start: aws.String(startAt.Format("2006-01-02")),
End: aws.String(endAt.Format("2006-01-02")),
},
Filter: &types.Expression{
And: andExpr,
},
GroupBy: []types.GroupDefinition{},
Metrics: []string{"NET_UNBLENDED_COST"},
}
slog.Info("get cost and usage", "start_at", startAt, "end_at", endAt, "input", input)
paginator := costexplorerx.NewGetCostAndUsagePaginator(g.client, input)
unit := ""
graph := NewCostGraph()
for paginator.HasMorePages() {
out, err := paginator.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get cost and usage: %w", err)
}
for _, data := range out.ResultsByTime {
date, err := time.Parse("2006-01-02", *data.TimePeriod.Start)
if err != nil {
return nil, fmt.Errorf("failed to parse point date: %w", err)
}
if len(data.Groups) == 0 {
netUnblendedCost, ok := data.Total["NetUnblendedCost"]
if !ok {
return nil, errors.New("NetUnblendedCost not found")
}
cost, err := strconv.ParseFloat(*netUnblendedCost.Amount, 64)
if err != nil {
return nil, err
}
unit = *netUnblendedCost.Unit
graph.AddDataPoint(date, cost, "NetUnblendedCost")
} else {
for _, group := range data.Groups {
var groupLabels []string
for keyIndex, v := range group.Keys {
k := *out.GroupDefinitions[keyIndex].Key
if k != "LINKED_ACCOUNT" {
groupLabels = append(groupLabels, v)
continue
}
desc, err := g.describeAccount(ctx, v)
if err != nil {
slog.Warn("failed to describe account", "account_id", v, "error", err)
groupLabels = append(groupLabels, v)
} else {
groupLabels = append(groupLabels, fmt.Sprintf("%s (%s)", *desc.Account.Name, v))
}
}
l := "(unknown)"
if len(groupLabels) > 0 {
l = strings.Join(groupLabels, ",")
}
netUnblendedCost, ok := group.Metrics["NetUnblendedCost"]
if !ok {
return nil, errors.New("NetUnblendedCost not found")
}
cost, err := strconv.ParseFloat(*netUnblendedCost.Amount, 64)
if err != nil {
return nil, err
}
unit = *netUnblendedCost.Unit
graph.AddDataPoint(date, cost, l)
}
}
}
}
title := strings.Join(costLabel, ",")
slog.InfoContext(ctx, "generate graph", "title", title, "start_at", startAt, "end_at", endAt)
w, err := graph.WriteTo(title, fmt.Sprintf("Cost (%s)", unit))
if err != nil {
return nil, err
}
return w, nil
}