This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
forked from alphagov/paas-metric-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (186 loc) · 5.56 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"crypto/tls"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/alphagov/paas-cf-apps-statsd/metrics"
"github.com/alphagov/paas-cf-apps-statsd/processors"
"github.com/cloudfoundry-community/go-cfclient"
"github.com/cloudfoundry/noaa/consumer"
"github.com/cloudfoundry/sonde-go/events"
"github.com/quipo/statsd"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
apiEndpoint = kingpin.Flag("api-endpoint", "API endpoint").Default("https://api.10.244.0.34.xip.io").OverrideDefaultFromEnvar("API_ENDPOINT").String()
statsdEndpoint = kingpin.Flag("statsd-endpoint", "Statsd endpoint").Default("10.244.11.2:8125").OverrideDefaultFromEnvar("STATSD_ENDPOINT").String()
statsdPrefix = kingpin.Flag("statsd-prefix", "Statsd prefix").Default("mycf.").OverrideDefaultFromEnvar("STATSD_PREFIX").String()
username = kingpin.Flag("username", "UAA username.").Default("").OverrideDefaultFromEnvar("USERNAME").String()
password = kingpin.Flag("password", "UAA password.").Default("").OverrideDefaultFromEnvar("PASSWORD").String()
skipSSLValidation = kingpin.Flag("skip-ssl-validation", "Please don't").Default("false").OverrideDefaultFromEnvar("SKIP_SSL_VALIDATION").Bool()
debug = kingpin.Flag("debug", "Enable debug mode. This disables forwarding to statsd and prints to stdout").Default("false").OverrideDefaultFromEnvar("DEBUG").Bool()
updateFrequency = kingpin.Flag("update-frequency", "The time in seconds, that takes between each apps update call.").Default("300").OverrideDefaultFromEnvar("UPDATE_FREQUENCY").Int64()
metricTemplate = kingpin.Flag("metric-template", "The template that will form a new metric namespace.").Default("{{.Space}}.{{.App}}.{{.Instance}}.{{.Metric}}").OverrideDefaultFromEnvar("METRIC_TEMPLATE").String()
)
type metricProcessor struct {
cfClient *cfclient.Client
cfClientConfig *cfclient.Config
msgChan chan *metrics.Stream
errorChan chan error
watchedApps map[string]*consumer.Consumer
}
func (tr metricProcessor) RefreshAuthToken() (token string, authError error) {
token, err := tr.cfClient.GetToken()
if err != nil {
err := tr.authenticate()
if err != nil {
return "", err
}
return tr.cfClient.GetToken()
}
return token, nil
}
func main() {
kingpin.Parse()
metricProc := &metricProcessor{
cfClientConfig: &cfclient.Config{
ApiAddress: *apiEndpoint,
SkipSslValidation: *skipSSLValidation,
Username: *username,
Password: *password,
},
msgChan: make(chan *metrics.Stream),
errorChan: make(chan error),
watchedApps: make(map[string]*consumer.Consumer),
}
containerMetricProcessor := processors.NewContainerMetricProcessor()
sender := statsd.NewStatsdClient(*statsdEndpoint, *statsdPrefix)
sender.CreateSocket()
var processedMetrics []metrics.Metric
var proc_err error
go func() {
for err := range metricProc.errorChan {
fmt.Fprintf(os.Stderr, "%v\n", err.Error())
}
}()
go func() {
for {
err := metricProc.process(*updateFrequency)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
}()
for wrapper := range metricProc.msgChan {
processedMetrics, proc_err = containerMetricProcessor.Process(wrapper)
if proc_err != nil {
fmt.Fprintf(os.Stderr, "%v\n", proc_err.Error())
} else {
if len(processedMetrics) > 0 {
for _, metric := range processedMetrics {
if *debug {
fmt.Println(metric)
} else {
err := metric.Send(sender)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
}
}
}
processedMetrics = nil
}
}
func (m *metricProcessor) authenticate() (err error) {
client, err := cfclient.NewClient(m.cfClientConfig)
if err != nil {
return err
}
m.cfClient = client
return nil
}
func updateAppSpaceData(app *cfclient.App) error {
if app.SpaceData == (cfclient.SpaceResource{}) {
space, err := app.Space()
if err != nil {
return err
}
org, err := space.Org()
if err != nil {
return err
}
space.OrgData.Entity = org
app.SpaceData.Entity = space
}
return nil
}
func (m *metricProcessor) updateApps() error {
authToken, err := m.cfClient.GetToken()
if err != nil {
return err
}
q := url.Values{}
apps, err := m.cfClient.ListAppsByQuery(q)
if err != nil {
return err
}
runningApps := map[string]bool{}
for _, app := range apps {
runningApps[app.Guid] = true
if _, ok := m.watchedApps[app.Guid]; !ok {
err = updateAppSpaceData(&app)
if err != nil {
return err
}
conn := consumer.New(m.cfClient.Endpoint.DopplerEndpoint, &tls.Config{InsecureSkipVerify: *skipSSLValidation}, nil)
msg, err := conn.Stream(app.Guid, authToken)
go func(currentApp cfclient.App) {
for message := range msg {
stream := metrics.Stream{Msg: message, App: currentApp, Tmpl: *metricTemplate}
if (*message.EventType == events.Envelope_ContainerMetric) {
m.msgChan <- &stream
}
}
}(app)
go func() {
for e := range err {
if e != nil {
m.errorChan <- e
}
}
}()
conn.RefreshTokenFrom(m)
m.watchedApps[app.Guid] = conn
}
}
for appGuid, _ := range m.watchedApps {
if _, ok := runningApps[appGuid]; !ok {
m.watchedApps[appGuid].Close()
delete(m.watchedApps, appGuid)
}
}
return nil
}
func (m *metricProcessor) process(updateFrequency int64) error {
for {
err := m.authenticate()
if err != nil {
return err
}
for {
err := m.updateApps()
if err != nil {
if strings.Contains(err.Error(), `"error":"invalid_token"`) {
break
}
return err
}
time.Sleep(time.Duration(updateFrequency) * time.Second)
}
}
}