This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (125 loc) · 3.75 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
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"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/types"
"github.com/drone/drone-go/drone"
)
type CloudwatchClient interface {
PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error)
}
// Ensure required env vars are set
// This doesn't scale well, but we only have a few variables
// Makes it much easier to debug Lambda failures and has the added bonus
// of failing much faster
func verifyEnvVars() error {
for _, v := range []string{
"DRONE_TOKEN",
"DRONE_SERVER",
"CLOUDWATCH_METRICS_NAMESPACE",
} {
if os.Getenv(v) == "" {
fmt.Printf("Required env var '%s' not set\n", v)
return errors.New("Missing env var")
}
}
return nil
}
// Retrieve all builds, pending or running
// We'll filter downstream
func getQueuedBuilds(c drone.Client) []*drone.Stage {
s, err := c.Queue()
if err != nil {
fmt.Printf("Error retrieving build queue %s\n", err.Error())
os.Exit(1)
}
return s
}
func reportBuilds(c drone.Client, cw CloudwatchClient, builds []*drone.Stage) {
// If there aren't any pending builds, exit
// The cloudwatch alarm needs to treat missing data as not breaching
if len(builds) < 1 {
fmt.Println("Build queue is empty")
return
}
// Iterate through pending builds
for _, b := range builds {
// Running builds are good
// A running build doesn't need a new worker node
if b.Status == "pending" {
// Create dimensions array for each queued build
var dimensions []types.Dimension
// b.Labels is a map[string]string representing the builds node labels
for k, v := range b.Labels {
// Build CW metric dimensions using build's node labels
dimensions = append(dimensions, types.Dimension{Name: aws.String(k), Value: aws.String(v)})
}
// Write metric for this queued build to Cloudwatch
putCloudwatchMetric(
cw,
dimensions,
"QueuedBuilds",
)
} else if b.Status == "running" {
// Create dimensions array for each queued build
var dimensions []types.Dimension
// b.Labels is a map[string]string representing the builds node labels
for k, v := range b.Labels {
// Build CW metric dimensions using build's node labels
dimensions = append(dimensions, types.Dimension{Name: aws.String(k), Value: aws.String(v)})
}
putCloudwatchMetric(
cw,
dimensions,
"RunningBuilds",
)
} else {
fmt.Printf("Not putting metric for build with status %s\n", b.Status)
}
}
}
func putCloudwatchMetric(c CloudwatchClient, d []types.Dimension, metricName string) error {
md := []types.MetricDatum{
{
MetricName: aws.String(metricName),
Dimensions: d,
Value: aws.Float64(1.0),
StorageResolution: aws.Int32(60),
Unit: types.StandardUnitCount,
},
}
p := cloudwatch.PutMetricDataInput{
Namespace: aws.String(os.Getenv("CLOUDWATCH_METRICS_NAMESPACE")),
MetricData: md,
}
_, err := c.PutMetricData(context.TODO(), &p)
if err != nil {
fmt.Printf("Error putting metric data - %s\n", err.Error())
return err
} else {
fmt.Println("PutMetric success!")
return nil
}
}
func handler(ctx context.Context, e events.CloudWatchEvent) {
// Verify env vars so we fail fast if any are missing
if err := verifyEnvVars(); err != nil {
os.Exit(1)
}
// Create clients
droneClient := newDroneClient()
cwClient := newCloudwatchClient()
// Get queued builds
builds := getQueuedBuilds(droneClient)
// Report metrics to cloudwatch
reportBuilds(droneClient, cwClient, builds)
}
func main() {
lambda.Start(handler)
}