-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp.go
65 lines (55 loc) · 1.95 KB
/
esp.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
package main
import (
"context"
"os"
"time"
"github.com/jedipunkz/esp/cloudwatch"
"github.com/jedipunkz/esp/ecs"
log "github.com/sirupsen/logrus"
)
func main() {
for {
ctx := context.Background()
client, err := ecs.NewClientToMetadataEndpoint()
if err != nil {
log.Printf("error creating client: %s", err)
}
taskMetadata, err := client.RetriveTaskMetadata(ctx)
if err != nil {
log.Printf("error retrieving task metadata: %s", err)
}
containersMetadata, err := client.RetriveContainersMetadata(ctx)
if err != nil {
log.Printf("error retrieving task stats metadata: %s", err)
}
containerName := os.Getenv("CONTAINER_NAME")
if containerName == "" {
log.Fatal("error retrieving container name from environment variable")
}
for _, container := range taskMetadata.Containers {
if container.Name == containerName {
s := containersMetadata[container.DockerID]
cpuUsage := ((float64(s.CPUStats.CPUUsage.TotalUsage) - float64(s.PreCPUStats.CPUUsage.TotalUsage)) /
(float64(s.CPUStats.SystemCPUUsage) - float64(s.PreCPUStats.SystemCPUUsage))) *
float64(s.CPUStats.OnlineCPUs) * 100
_, err = cloudwatch.PutMetricData(cpuUsage)
if err != nil {
log.Printf("Error putting metric data: %s", err)
}
log.SetFormatter(&log.JSONFormatter{})
log.WithFields(log.Fields{
"CPUStats.CPUStats.TotalUsage": s.CPUStats.CPUUsage.TotalUsage,
"PreCPUStats.CPUUsage.TotalUsage": s.PreCPUStats.CPUUsage.TotalUsage,
"CPUStats.SystemCPUUsage": s.CPUStats.SystemCPUUsage,
"PreCPUStats.SystemCPUUsage": s.PreCPUStats.SystemCPUUsage,
"CPUStats.OnlineCPUs": s.CPUStats.OnlineCPUs,
"CPUUsage": cpuUsage,
"ContainerName": container.Name,
"Namespace": os.Getenv("NAMESPACE"),
"REGION": os.Getenv("REGION"),
}).Info("ESP Stats")
}
}
time.Sleep(time.Second * 1)
}
}