forked from buildkite/buildkite-agent-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.go
79 lines (64 loc) · 1.59 KB
/
lambda.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/buildkite/buildkite-metrics/backend"
"github.com/buildkite/buildkite-metrics/collector"
"github.com/buildkite/buildkite-metrics/version"
"github.com/buildkite/go-buildkite/buildkite"
"github.com/eawsy/aws-lambda-go/service/lambda/runtime"
)
func handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
org := os.Getenv("BUILDKITE_ORG")
token := os.Getenv("BUILDKITE_TOKEN")
backendOpt := os.Getenv("BUILDKITE_BACKEND")
queue := os.Getenv("BUILDKITE_QUEUE")
quiet := os.Getenv("BUILDKITE_QUIET")
if quiet == "1" || quiet == "false" {
log.SetOutput(ioutil.Discard)
}
config, err := buildkite.NewTokenConfig(token, false)
if err != nil {
return nil, err
}
client := buildkite.NewClient(config.Client())
t := time.Now()
client.UserAgent = fmt.Sprintf(
"%s buildkite-metrics/%s buildkite-metrics-lambda queue=%q",
client.UserAgent, version.Version, queue,
)
col := collector.New(client, collector.Opts{
OrgSlug: org,
})
if queue != "" {
col.Queue = queue
}
var bk backend.Backend
if backendOpt == "statsd" {
bk, err = backend.NewStatsDBackend(os.Getenv("STATSD_HOST"), strings.ToLower(os.Getenv("STATSD_TAGS")) == "true")
if err != nil {
return nil, err
}
} else {
bk = &backend.CloudWatchBackend{}
}
res, err := col.Collect()
if err != nil {
return nil, err
}
res.Dump()
err = bk.Collect(res)
if err != nil {
return nil, err
}
log.Printf("Finished in %s", time.Now().Sub(t))
return "", nil
}
func init() {
runtime.HandleFunc(handle)
}