forked from prometheus-community/stackdriver_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackdriver_exporter.go
More file actions
293 lines (244 loc) · 10 KB
/
Copy pathstackdriver_exporter.go
File metadata and controls
293 lines (244 loc) · 10 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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/log"
"github.com/go-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"
"github.com/prometheus-community/stackdriver_exporter/utils"
)
var (
// General exporter flags
listenAddress = kingpin.Flag(
"web.listen-address", "Address to listen on for web interface and telemetry.",
).Default(":9255").String()
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose Prometheus metrics.",
).Default("/metrics").String()
projectID = kingpin.Flag(
"google.project-id", "Comma seperated list of Google Project IDs.",
).String()
stackdriverMaxRetries = kingpin.Flag(
"stackdriver.max-retries", "Max number of retries that should be attempted on 503 errors from stackdriver.",
).Default("0").Int()
stackdriverHttpTimeout = kingpin.Flag(
"stackdriver.http-timeout", "How long should stackdriver_exporter wait for a result from the Stackdriver API.",
).Default("10s").Duration()
stackdriverMaxBackoffDuration = kingpin.Flag(
"stackdriver.max-backoff", "Max time between each request in an exp backoff scenario.",
).Default("5s").Duration()
stackdriverBackoffJitterBase = kingpin.Flag(
"stackdriver.backoff-jitter", "The amount of jitter to introduce in a exp backoff scenario.",
).Default("1s").Duration()
stackdriverRetryStatuses = kingpin.Flag(
"stackdriver.retry-statuses", "The HTTP statuses that should trigger a retry.",
).Default("503").Ints()
// Monitoring collector flags
monitoringMetricsTypePrefixes = kingpin.Flag(
"monitoring.metrics-type-prefixes", "Comma separated Google Stackdriver Monitoring Metric Type prefixes.",
).Required().String()
monitoringMetricsInterval = kingpin.Flag(
"monitoring.metrics-interval", "Interval to request the Google Stackdriver Monitoring Metrics for. Only the most recent data point is used.",
).Default("5m").Duration()
monitoringMetricsOffset = kingpin.Flag(
"monitoring.metrics-offset", "Offset for the Google Stackdriver Monitoring Metrics interval into the past.",
).Default("0s").Duration()
monitoringMetricsIngestDelay = kingpin.Flag(
"monitoring.metrics-ingest-delay", "Offset for the Google Stackdriver Monitoring Metrics interval into the past by the ingest delay from the metric's metadata.",
).Default("false").Bool()
collectorFillMissingLabels = kingpin.Flag(
"collector.fill-missing-labels", "Fill missing metrics labels with empty string to avoid label dimensions inconsistent failure.",
).Default("true").Bool()
monitoringDropDelegatedProjects = kingpin.Flag(
"monitoring.drop-delegated-projects", "Drop metrics from attached projects and fetch `project_id` only.",
).Default("false").Bool()
monitoringMetricsExtraFilter = kingpin.Flag(
"monitoring.filters", "Filters. i.e: pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match(\"my-subs-prefix.*\")").Strings()
)
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
}
type handler struct {
handler http.Handler
logger log.Logger
projectIDs []string
metricsPrefixes []string
metricsExtraFilters []collectors.MetricFilter
m *monitoring.Service
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
collectParams := r.URL.Query()["collect"]
filters := make(map[string]bool)
for _, param := range collectParams {
filters[param] = true
}
if len(filters) > 0 {
h.innerHandler(filters).ServeHTTP(w, r)
return
}
h.handler.ServeHTTP(w, r)
}
func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters []collectors.MetricFilter, m *monitoring.Service, logger log.Logger) *handler {
h := &handler{
logger: logger,
projectIDs: projectIDs,
metricsPrefixes: metricPrefixes,
metricsExtraFilters: metricExtraFilters,
m: m,
}
h.handler = h.innerHandler(nil)
return h
}
func (h *handler) innerHandler(filters map[string]bool) http.Handler {
registry := prometheus.NewRegistry()
for _, project := range h.projectIDs {
monitoringCollector, err := collectors.NewMonitoringCollector(project, h.m, collectors.MonitoringCollectorOptions{
MetricTypePrefixes: h.filterMetricTypePrefixes(filters),
ExtraFilters: h.metricsExtraFilters,
RequestInterval: *monitoringMetricsInterval,
RequestOffset: *monitoringMetricsOffset,
IngestDelay: *monitoringMetricsIngestDelay,
FillMissingLabels: *collectorFillMissingLabels,
DropDelegatedProjects: *monitoringDropDelegatedProjects,
}, h.logger)
if err != nil {
level.Error(h.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.
return promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
}
// filterMetricTypePrefixes filters the initial list of metric type prefixes, with the ones coming from an individual
// prometheus collect request.
func (h *handler) filterMetricTypePrefixes(filters map[string]bool) []string {
filteredPrefixes := h.metricsPrefixes
if len(filters) > 0 {
filteredPrefixes = nil
for _, prefix := range h.metricsPrefixes {
if filters[prefix] {
filteredPrefixes = append(filteredPrefixes, prefix)
}
}
}
return filteredPrefixes
}
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, ",")
metricsTypePrefixes := strings.Split(*monitoringMetricsTypePrefixes, ",")
metricExtraFilters := parseMetricExtraFilters()
handler := newHandler(projectIDs, metricsTypePrefixes, metricExtraFilters, monitoringService, logger)
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handler))
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)
}
}
func parseMetricExtraFilters() []collectors.MetricFilter {
var extraFilters []collectors.MetricFilter
for _, ef := range *monitoringMetricsExtraFilter {
efPrefix, efModifier := utils.GetExtraFilterModifiers(ef, ":")
if efPrefix != "" {
extraFilter := collectors.MetricFilter{
Prefix: efPrefix,
Modifier: efModifier,
}
extraFilters = append(extraFilters, extraFilter)
}
}
return extraFilters
}