-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (110 loc) · 3.48 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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"strings"
"github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/gin-gonic/gin"
"github.com/kelseyhightower/envconfig"
)
type TargetMode string
var IPAddress TargetMode = "ip-address"
var HostName TargetMode = "host-name"
type AppConfig struct {
BuildkiteToken string `split_words:"true" required:"true"`
BuildkiteOrg string `split_words:"true" required:"true"`
TargetMode TargetMode `split_words:"true" default:"ip-address"`
TargetPorts string `split_words:"true" required:"true"`
}
func main() {
log.Println("Starting prometheus-buildkite-sd")
var appConfig AppConfig
envconfig.MustProcess("", &appConfig)
// log useful information on server start
log.Println("config -> org", appConfig.BuildkiteOrg)
log.Println("config -> target-mode", appConfig.TargetMode)
r := gin.Default()
r.GET("/", buildkiteServiceDiscoveryHandler(appConfig))
r.Run()
}
type Target string
type TargetResult struct {
Targets []Target `json:"targets"`
Labels map[string]string
}
func buildkiteServiceDiscoveryHandler(appConfig AppConfig) func(c *gin.Context) {
// TODO: while testing this out,
// a single host machine could host multiple agents
// so test that case also
return func(c *gin.Context) {
log.Println("start of root request")
config, err := buildkite.NewTokenConfig(appConfig.BuildkiteToken, false)
if err != nil {
c.Error(err)
}
buildkiteClient := buildkite.NewClient(config.Client())
var allAgents []buildkite.Agent
for currentPage, lastPage := 1, 0; (currentPage == 1) || (currentPage <= lastPage); {
agents, resp, err := buildkiteClient.Agents.List(appConfig.BuildkiteOrg, &buildkite.AgentListOptions{
ListOptions: buildkite.ListOptions{
Page: currentPage,
PerPage: 100,
},
})
if err != nil {
c.Error(err)
}
lastPage = resp.LastPage
currentPage++
allAgents = append(allAgents, agents...)
}
type PromSDEntry struct {
Targets []string `json:"targets"`
Labels map[string]string `json:"labels"`
}
var sdEntries []PromSDEntry
for _, agent := range allAgents {
entry := PromSDEntry{
Labels: map[string]string{
"buildkite_agent_host_name": *agent.Hostname,
// TODO: see if you could filter by connected state in buildkite api itself
},
}
if appConfig.TargetMode == IPAddress {
entry.Targets = []string{
*agent.IPAddress,
}
} else if appConfig.TargetMode == HostName {
entry.Targets = []string{
*agent.Hostname,
}
} else {
c.Error(errors.New("unknown TARGET_MODE, please set that env to ip-address or host-name"))
}
for _, port := range strings.Split(appConfig.TargetPorts, ",") {
for idx := range entry.Targets {
entry.Targets[idx] = fmt.Sprintf("%s:%s", entry.Targets[idx], port)
}
}
if agent.Job != nil {
// A job is actively running on the agent
// So add metadata related to it
if agent.Job.ID != nil {
entry.Labels["buildkite_job_id"] = *agent.Job.ID
}
if agent.Job.StepKey != nil {
entry.Labels["buildkite_job_key"] = *agent.Job.StepKey
}
}
// TODO: agent.Metadata is an array of string. It could contain two queues with different vaules
// at that point, the agent is supposed to be serving both of the queues at the same time
// so have separate SD entries for them
sdEntries = append(sdEntries, entry)
}
// TODO: handle c.Error
c.JSON(http.StatusOK, sdEntries)
log.Println("end of root request")
}
}