This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
138 lines (118 loc) · 6.3 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
package main
import (
"log"
"strings"
"time"
"github.com/alphagov/paas-metric-exporter/app"
"github.com/alphagov/paas-metric-exporter/metrics"
"github.com/alphagov/paas-metric-exporter/processors"
"github.com/alphagov/paas-metric-exporter/senders"
"github.com/cloudfoundry-community/go-cfclient"
sonde_events "github.com/cloudfoundry/sonde-go/events"
quipo_statsd "github.com/quipo/statsd"
"gopkg.in/alecthomas/kingpin.v2"
"os"
)
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()
clientID = kingpin.Flag("client-id", "UAA client ID.").Default("").OverrideDefaultFromEnvar("CLIENT_ID").String()
clientSecret = kingpin.Flag("client-secret", "UAA client secret.").Default("").OverrideDefaultFromEnvar("CLIENT_SECRET").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 prometheus 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(senders.DefaultTemplate).OverrideDefaultFromEnvar("METRIC_TEMPLATE").String()
metricWhitelist = kingpin.Flag("metric-whitelist", "Comma separated metric name prefixes to enable.").Default("").OverrideDefaultFromEnvar("METRIC_WHITELIST").String()
prometheusBindPort = kingpin.Flag("prometheus-bind-port", "The port to bind to for prometheus metrics.").Default("8080").OverrideDefaultFromEnvar("PORT").Int()
enableStatsd = kingpin.Flag("enable-statsd", "Enable the statsd sender.").Default("true").OverrideDefaultFromEnvar("ENABLE_STATSD").Bool()
enablePrometheus = kingpin.Flag("enable-prometheus", "Enable the prometheus sender.").Default("false").OverrideDefaultFromEnvar("ENABLE_PROMETHEUS").Bool()
enableLoggregator = kingpin.Flag("enable-loggregator", "Enable the Loggregator sender.").Default("false").OverrideDefaultFromEnvar("ENABLE_LOGGREGATOR").Bool()
enableLocking = kingpin.Flag("enable-locking", "Enable locking via Locket.").Default("false").OverrideDefaultFromEnvar("ENABLE_LOCKING").Bool()
locketAddress = kingpin.Flag("locket-address", "address:port of Locket server.").Default("127.0.0.1:8891").OverrideDefaultFromEnvar("LOCKET_API_LOCATION").String()
locketCACert = kingpin.Flag("locket-ca-cert", "File path to Locket CA certificate.").Default("").OverrideDefaultFromEnvar("LOCKET_CA_CERT").String()
locketClientCert = kingpin.Flag("locket-client-cert", "File path to Locket client certificate.").Default("").OverrideDefaultFromEnvar("LOCKET_CLIENT_CERT").String()
locketClientKey = kingpin.Flag("locket-client-key", "File path to Locket client key.").Default("").OverrideDefaultFromEnvar("LOCKET_CLIENT_KEY").String()
)
func normalizePrefix(prefix string) string {
prefix = strings.TrimRight(strings.TrimSpace(prefix), ".")
if prefix == "" {
return prefix
}
return prefix + "."
}
func normalizeWhitelist(csv string) []string {
list := strings.Split(csv, ",")
whitelist := make([]string, len(list))
for i, val := range list {
whitelist[i] = strings.TrimSpace(val)
}
return whitelist
}
func main() {
kingpin.Parse()
*statsdPrefix = normalizePrefix(*statsdPrefix)
log.SetFlags(0)
config := &app.Config{
CFClientConfig: &cfclient.Config{
ApiAddress: *apiEndpoint,
SkipSslValidation: *skipSSLValidation,
Username: *username,
Password: *password,
ClientID: *clientID,
ClientSecret: *clientSecret,
},
CFAppUpdateFrequency: time.Duration(*updateFrequency) * time.Second,
Whitelist: normalizeWhitelist(*metricWhitelist),
Template: *metricTemplate,
EnablePrometheus: *enablePrometheus,
PrometheusPort: *prometheusBindPort,
}
locketConfig := app.NewLocketConfig(locketAddress, locketCACert, locketClientCert, locketClientKey)
config.ClientLocketConfig = locketConfig
processors := map[sonde_events.Envelope_EventType]processors.Processor{
sonde_events.Envelope_ContainerMetric: &processors.ContainerMetricProcessor{},
sonde_events.Envelope_LogMessage: &processors.LogMessageProcessor{},
sonde_events.Envelope_HttpStartStop: &processors.HttpStartStopProcessor{},
}
var metricSenders []metrics.Sender
if *debug {
debugSender, err := senders.NewDebugSender(*statsdPrefix, config.Template)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
metricSenders = append(metricSenders, debugSender)
} else {
if *enableStatsd {
client := quipo_statsd.NewStatsdClient(*statsdEndpoint, *statsdPrefix)
client.CreateSocket()
statsDSender, err := senders.NewStatsdSender(client, config.Template)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
metricSenders = append(metricSenders, statsDSender)
}
if *enablePrometheus {
metricSenders = append(metricSenders, senders.NewPrometheusSender())
}
if *enableLoggregator {
loggregatorSender, err := senders.NewLoggregatorSender(
app.DefaultLoggregatorConfig.MetronURL,
app.DefaultLoggregatorConfig.CACertPath,
app.DefaultLoggregatorConfig.ClientCertPath,
app.DefaultLoggregatorConfig.ClientKeyPath,
)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
metricSenders = append(metricSenders, loggregatorSender)
}
}
app := app.NewApplication(config, processors, metricSenders)
app.Start(*enableLocking)
}