forked from prometheus-community/stackdriver_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackdriver_exporter.go
193 lines (161 loc) · 7.14 KB
/
stackdriver_exporter.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
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/rehttp"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/monitoring/v3"
"google.golang.org/api/option"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/prometheus-community/stackdriver_exporter/collectors"
)
var (
listenAddress = kingpin.Flag(
"web.listen-address", "Address to listen on for web interface and telemetry ($STACKDRIVER_EXPORTER_WEB_LISTEN_ADDRESS).",
).Envar("STACKDRIVER_EXPORTER_WEB_LISTEN_ADDRESS").Default(":9255").String()
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose Prometheus metrics ($STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH).",
).Envar("STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH").Default("/metrics").String()
projectID = kingpin.Flag(
"google.project-id", "Comma seperated list of Google Project IDs ($STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID).",
).Envar("STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID").String()
stackdriverMaxRetries = kingpin.Flag(
"stackdriver.max-retries", "Max number of retries that should be attempted on 503 errors from stackdriver. ($STACKDRIVER_EXPORTER_MAX_RETRIES)",
).Envar("STACKDRIVER_EXPORTER_MAX_RETRIES").Default("0").Int()
stackdriverHttpTimeout = kingpin.Flag(
"stackdriver.http-timeout", "How long should stackdriver_exporter wait for a result from the Stackdriver API ($STACKDRIVER_EXPORTER_HTTP_TIMEOUT)",
).Envar("STACKDRIVER_EXPORTER_HTTP_TIMEOUT").Default("10s").Duration()
stackdriverMaxBackoffDuration = kingpin.Flag(
"stackdriver.max-backoff", "Max time between each request in an exp backoff scenario ($STACKDRIVER_EXPORTER_MAX_BACKOFF_DURATION)",
).Envar("STACKDRIVER_EXPORTER_MAX_BACKOFF_DURATION").Default("5s").Duration()
stackdriverBackoffJitterBase = kingpin.Flag(
"stackdriver.backoff-jitter", "The amount of jitter to introduce in a exp backoff scenario ($STACKDRIVER_EXPORTER_BACKODFF_JITTER_BASE)",
).Envar("STACKDRIVER_EXPORTER_BACKODFF_JITTER_BASE").Default("1s").Duration()
stackdriverRetryStatuses = kingpin.Flag(
"stackdriver.retry-statuses", "The HTTP statuses that should trigger a retry ($STACKDRIVER_EXPORTER_RETRY_STATUSES)",
).Envar("STACKDRIVER_EXPORTER_RETRY_STATUSES").Default("503").Ints()
)
func init() {
prometheus.MustRegister(version.NewCollector("stackdriver_exporter"))
}
func getDefaultGCPProject(ctx context.Context) (*string, error) {
credentials, err := google.FindDefaultCredentials(ctx, compute.ComputeScope)
if err != nil {
return nil, err
}
if credentials.ProjectID == "" {
return nil, fmt.Errorf("unable to identify the gcloud project. Got empty string")
}
return &credentials.ProjectID, nil
}
func createMonitoringService(ctx context.Context) (*monitoring.Service, error) {
googleClient, err := google.DefaultClient(ctx, monitoring.MonitoringReadScope)
if err != nil {
return nil, fmt.Errorf("Error creating Google client: %v", err)
}
googleClient.Timeout = *stackdriverHttpTimeout
googleClient.Transport = rehttp.NewTransport(
googleClient.Transport, // need to wrap DefaultClient transport
rehttp.RetryAll(
rehttp.RetryMaxRetries(*stackdriverMaxRetries),
rehttp.RetryStatuses(*stackdriverRetryStatuses...)), // Cloud support suggests retrying on 503 errors
rehttp.ExpJitterDelay(*stackdriverBackoffJitterBase, *stackdriverMaxBackoffDuration), // Set timeout to <10s as that is prom default timeout
)
monitoringService, err := monitoring.NewService(ctx, option.WithHTTPClient(googleClient))
if err != nil {
return nil, fmt.Errorf("Error creating Google Stackdriver Monitoring service: %v", err)
}
return monitoringService, nil
}
func newHandler(projectIDs []string, m *monitoring.Service, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
collectParams := r.URL.Query()["collect"]
// Create filters for "collect" query parameters.
filters := make(map[string]bool)
for _, param := range collectParams {
filters[param] = true
}
registry := prometheus.NewRegistry()
for _, project := range projectIDs {
monitoringCollector, err := collectors.NewMonitoringCollector(project, m, filters, logger)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
registry.MustRegister(monitoringCollector)
}
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
func main() {
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("stackdriver_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
ctx := context.Background()
if *projectID == "" {
level.Info(logger).Log("msg", "no projectID was provided. Trying to discover it")
var err error
projectID, err = getDefaultGCPProject(ctx)
if err != nil {
level.Error(logger).Log("msg", "no explicit projectID and error trying to discover default GCloud project", "err", err)
os.Exit(1)
}
}
level.Info(logger).Log("msg", "Starting stackdriver_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
level.Info(logger).Log("msg", "Using Google Cloud Project ID", "projectID", *projectID)
monitoringService, err := createMonitoringService(ctx)
if err != nil {
level.Error(logger).Log("msg", "failed to create monitoring service", "err", err)
os.Exit(1)
}
projectIDs := strings.Split(*projectID, ",")
handlerFunc := newHandler(projectIDs, monitoringService, logger)
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Stackdriver Exporter</title></head>
<body>
<h1>Stackdriver Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
level.Info(logger).Log("msg", "Listening on", "address", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
}