-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
172 lines (141 loc) · 3.91 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
package main
import (
"fmt"
"log"
"net/url"
"sync"
"github.com/anodot/anodot-common/pkg/metrics3"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
//"github.com/aws/aws-lambda-go/lambda"
)
const metricsPerSecond = 1000
var schemaIds map[string]string
var accountId string
func SetAccountId(in []metrics3.AnodotMetrics30) []metrics3.AnodotMetrics30 {
for _, m := range in {
if accountId != "" {
m.Dimensions["account_id"] = accountId
}
}
return in
}
func SendMetrics(metrics []metrics3.AnodotMetrics30, submiter *metrics3.Anodot30Client) error {
var previousIndex int = 0
var index int = 0
var totalCount int = 0
metrics = SetAccountId(metrics)
for index < len(metrics) {
previousIndex = index
index = index + metricsPerSecond
if index > len(metrics) {
index = len(metrics)
}
var metricsSlice []metrics3.AnodotMetrics30 = metrics[previousIndex:index]
err := SubmitMetrics(*submiter, metricsSlice)
if err != nil {
log.Printf("Retry sending metrics")
err := SubmitMetrics(*submiter, metricsSlice)
if err != nil {
return err
}
}
totalCount = totalCount + len(metricsSlice)
}
log.Printf("Metrics pushed total count %d \n", totalCount)
return nil
}
func SubmitMetrics(client metrics3.Anodot30Client, metrics []metrics3.AnodotMetrics30) error {
respSubmit, err := client.SubmitMetrics(metrics)
if err != nil {
log.Printf("submition failed: %v", err)
if respSubmit != nil {
log.Printf("http status: %v", respSubmit.HttpResponse.Status)
}
return err
}
if respSubmit.HasErrors() {
log.Printf("submition failed: %s", respSubmit.ErrorMessage())
log.Printf("http status: %v", respSubmit.HttpResponse.Status)
return fmt.Errorf(respSubmit.ErrorMessage())
}
return nil
}
func LambdaHandler() {
var wg sync.WaitGroup
schemaIds = make(map[string]string, 0)
c, err := GetConfig()
if err != nil {
log.Fatalf("Could not parse config: %v", err)
}
accountId = c.AccountId
ml := &SyncMetricList{
metrics: make([]metrics3.AnodotMetrics30, 0),
}
el := &ErrorList{
errors: make([]error, 0),
}
session := session.Must(session.NewSession(&aws.Config{Region: aws.String(c.Region)}))
cloudwatchSvc := cloudwatch.New(session)
url, err := url.Parse(c.AnodotUrl)
if err != nil {
log.Fatalf("Could not parse Anodot url: %v", err)
}
client, err := metrics3.NewAnodot30Client(*url, &c.AccessKey, &c.AnodotToken, nil)
if err != nil {
log.Fatalf("failed to create anodot30 client: %v", err)
}
sm := SchemasManager{*client}
schemas, err := GetSchemasFromConfig(c)
if err != nil {
log.Fatalf("failed to get metrics schemas: %v", err)
}
respGetschemas, err := client.GetSchemas()
if err != nil {
log.Fatalf("failed to fetch metrics schemas: %v", err)
}
if respGetschemas.HasErrors() {
log.Fatalf(respGetschemas.ErrorMessage())
}
err = sm.UpdateSchemas(schemas, respGetschemas.Schemas)
if err != nil {
log.Fatal(err)
}
// fetch schemas again after schema update
respGetschemas, err = client.GetSchemas()
if err != nil {
log.Fatalf("failed to fetch metrics schemas: %v", err)
}
if respGetschemas.HasErrors() {
log.Fatalf(respGetschemas.ErrorMessage())
}
for _, schema := range respGetschemas.Schemas {
for _, service := range GetSupportedService() {
if schema.Name == schemaName(accountId, service) {
schemaIds[service] = schema.Id
}
}
}
Handle(c.RegionsConfigs[c.Region], &wg, session, cloudwatchSvc, ml, el)
wg.Wait()
if len(el.errors) > 0 {
for _, e := range el.errors {
log.Printf("ERROR occured: %v", e)
}
log.Fatalf("exiting...")
}
if len(ml.metrics) > 0 {
log.Printf("Total fetched metrics count %d", len(ml.metrics))
err := SendMetrics(ml.metrics, client)
if err != nil {
log.Fatalf("Failed to send metrics")
}
} else {
log.Print("No any metrics to push ")
}
}
func main() {
lambda.Start(LambdaHandler)
}