forked from prometheus-community/stackdriver_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackdriver_exporter.go
434 lines (370 loc) · 15.4 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// 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"
"log/slog"
"net/http"
"os"
"slices"
"strings"
"github.com/PuerkitoBio/rehttp"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
"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"
"github.com/prometheus-community/stackdriver_exporter/collectors"
"github.com/prometheus-community/stackdriver_exporter/delta"
"github.com/prometheus-community/stackdriver_exporter/utils"
)
var (
// General exporter flags
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9255")
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose Prometheus metrics.",
).Default("/metrics").String()
stackdriverMetricsPath = kingpin.Flag(
"web.stackdriver-telemetry-path", "Path under which to expose Stackdriver metrics.",
).Default("/metrics").String()
projectID = kingpin.Flag(
"google.project-id", "DEPRECATED - Comma seperated list of Google Project IDs. Use 'google.project-ids' instead.",
).String()
projectIDs = kingpin.Flag(
"google.project-ids", "Repeatable flag of Google Project IDs",
).Strings()
projectsFilter = kingpin.Flag(
"google.projects.filter", "Google projects search filter.",
).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", "DEPRECATED - Comma separated Google Stackdriver Monitoring Metric Type prefixes. Use 'monitoring.metrics-prefixes' instead.",
).String()
monitoringMetricsPrefixes = kingpin.Flag(
"monitoring.metrics-prefixes", "Google Stackdriver Monitoring Metric Type prefixes. Repeat this flag to scrape multiple prefixes.",
).Strings()
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()
monitoringMetricsAggregateDeltas = kingpin.Flag(
"monitoring.aggregate-deltas", "If enabled will treat all DELTA metrics as an in-memory counter instead of a gauge",
).Default("false").Bool()
monitoringMetricsDeltasTTL = kingpin.Flag(
"monitoring.aggregate-deltas-ttl", "How long should a delta metric continue to be exported after GCP stops producing a metric",
).Default("30m").Duration()
monitoringDescriptorCacheTTL = kingpin.Flag(
"monitoring.descriptor-cache-ttl", "How long should the metric descriptors for a prefixed be cached for",
).Default("0s").Duration()
monitoringDescriptorCacheOnlyGoogle = kingpin.Flag(
"monitoring.descriptor-cache-only-google", "Only cache descriptors for *.googleapis.com metrics",
).Default("true").Bool()
)
func init() {
prometheus.MustRegister(versioncollector.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 *slog.Logger
projectIDs []string
metricsPrefixes []string
metricsExtraFilters []collectors.MetricFilter
additionalGatherer prometheus.Gatherer
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 *slog.Logger, additionalGatherer prometheus.Gatherer) *handler {
h := &handler{
logger: logger,
projectIDs: projectIDs,
metricsPrefixes: metricPrefixes,
metricsExtraFilters: metricExtraFilters,
additionalGatherer: additionalGatherer,
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,
AggregateDeltas: *monitoringMetricsAggregateDeltas,
DescriptorCacheTTL: *monitoringDescriptorCacheTTL,
DescriptorCacheOnlyGoogle: *monitoringDescriptorCacheOnlyGoogle,
}, h.logger, delta.NewInMemoryCounterStore(h.logger, *monitoringMetricsDeltasTTL), delta.NewInMemoryHistogramStore(h.logger, *monitoringMetricsDeltasTTL))
if err != nil {
h.logger.Error("error creating monitoring collector", "err", err)
os.Exit(1)
}
registry.MustRegister(monitoringCollector)
}
var gatherers prometheus.Gatherer = registry
if h.additionalGatherer != nil {
gatherers = prometheus.Gatherers{
h.additionalGatherer,
registry,
}
}
opts := promhttp.HandlerOpts{ErrorLog: slog.NewLogLogger(h.logger.Handler(), slog.LevelError)}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
return promhttp.HandlerFor(gatherers, opts)
}
// 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 {
for filter, _ := range filters {
if strings.HasPrefix(filter, prefix) {
filteredPrefixes = append(filteredPrefixes, filter)
}
}
}
}
return parseMetricTypePrefixes(filteredPrefixes)
}
func main() {
promslogConfig := &promslog.Config{}
flag.AddFlags(kingpin.CommandLine, promslogConfig)
kingpin.Version(version.Print("stackdriver_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promslog.New(promslogConfig)
if *projectID != "" {
logger.Warn("The google.project-id flag is deprecated and will be replaced by google.project-ids.")
}
if *monitoringMetricsTypePrefixes != "" {
logger.Warn("The monitoring.metrics-type-prefixes flag is deprecated and will be replaced by monitoring.metrics-prefix.")
}
if *monitoringMetricsTypePrefixes == "" && len(*monitoringMetricsPrefixes) == 0 {
logger.Error("At least one GCP monitoring prefix is required.")
os.Exit(1)
}
ctx := context.Background()
var discoveredProjectIDs []string
if len(*projectIDs) == 0 && *projectID == "" && *projectsFilter == "" {
logger.Info("Neither projectIDs nor projectsFilter was provided. Trying to discover it")
var err error
defaultProject, err := getDefaultGCPProject(ctx)
if err != nil {
logger.Error("no explicit projectIDs and error trying to discover default GCloud project", "err", err)
os.Exit(1)
}
discoveredProjectIDs = append(discoveredProjectIDs, *defaultProject)
}
monitoringService, err := createMonitoringService(ctx)
if err != nil {
logger.Error("failed to create monitoring service", "err", err)
os.Exit(1)
}
if *projectsFilter != "" {
projectIDsFromFilter, err := utils.GetProjectIDsFromFilter(ctx, *projectsFilter)
if err != nil {
logger.Error("failed to get project IDs from filter", "err", err)
os.Exit(1)
}
discoveredProjectIDs = append(discoveredProjectIDs, projectIDsFromFilter...)
}
if len(*projectIDs) > 0 {
discoveredProjectIDs = append(discoveredProjectIDs, *projectIDs...)
}
if *projectID != "" {
discoveredProjectIDs = append(discoveredProjectIDs, strings.Split(*projectID, ",")...)
}
var metricsPrefixes []string
if len(*monitoringMetricsPrefixes) > 0 {
metricsPrefixes = append(metricsPrefixes, *monitoringMetricsPrefixes...)
}
if *monitoringMetricsTypePrefixes != "" {
metricsPrefixes = append(metricsPrefixes, strings.Split(*monitoringMetricsTypePrefixes, ",")...)
}
logger.Info(
"Starting stackdriver_exporter",
"version", version.Info(),
"build_context", version.BuildContext(),
"metric_prefixes", fmt.Sprintf("%v", metricsPrefixes),
"extra_filters", strings.Join(*monitoringMetricsExtraFilter, ","),
"projectIDs", fmt.Sprintf("%v", discoveredProjectIDs),
"projectsFilter", *projectsFilter,
)
parsedMetricsPrefixes := parseMetricTypePrefixes(metricsPrefixes)
metricExtraFilters := parseMetricExtraFilters()
// drop duplicate projects
slices.Sort(discoveredProjectIDs)
uniqueProjectIds := slices.Compact(discoveredProjectIDs)
if *metricsPath == *stackdriverMetricsPath {
handler := newHandler(
uniqueProjectIds, parsedMetricsPrefixes, metricExtraFilters, monitoringService, logger, prometheus.DefaultGatherer)
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handler))
} else {
logger.Info("Serving Stackdriver metrics at separate path", "path", *stackdriverMetricsPath)
handler := newHandler(
uniqueProjectIds, parsedMetricsPrefixes, metricExtraFilters, monitoringService, logger, nil)
http.Handle(*stackdriverMetricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handler))
http.Handle(*metricsPath, promhttp.Handler())
}
if *metricsPath != "/" && *metricsPath != "" {
landingConfig := web.LandingConfig{
Name: "Stackdriver Exporter",
Description: "Prometheus Exporter for Google Stackdriver",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: *metricsPath,
Text: "Metrics",
},
},
}
if *metricsPath != *stackdriverMetricsPath {
landingConfig.Links = append(landingConfig.Links,
web.LandingLinks{
Address: *stackdriverMetricsPath,
Text: "Stackdriver Metrics",
},
)
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
logger.Error("error creating landing page", "err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
}
srv := &http.Server{}
if err := web.ListenAndServe(srv, toolkitFlags, logger); err != nil {
logger.Error("Error starting server", "err", err)
os.Exit(1)
}
}
func parseMetricTypePrefixes(inputPrefixes []string) []string {
metricTypePrefixes := []string{}
// Drop duplicate prefixes.
slices.Sort(inputPrefixes)
uniquePrefixes := slices.Compact(inputPrefixes)
// Drop prefixes that start with another existing prefix to avoid error:
// "collected metric xxx was collected before with the same name and label values".
for i, prefix := range uniquePrefixes {
if i != 0 {
previousIndex := len(metricTypePrefixes) - 1
// Drop current prefix if it starts with the previous one.
if strings.HasPrefix(prefix, metricTypePrefixes[previousIndex]) {
continue
}
}
metricTypePrefixes = append(metricTypePrefixes, prefix)
}
return metricTypePrefixes
}
func parseMetricExtraFilters() []collectors.MetricFilter {
var extraFilters []collectors.MetricFilter
for _, ef := range *monitoringMetricsExtraFilter {
targetedMetricPrefix, filterQuery := utils.SplitExtraFilter(ef, ":")
if targetedMetricPrefix != "" {
extraFilter := collectors.MetricFilter{
TargetedMetricPrefix: strings.ToLower(targetedMetricPrefix),
FilterQuery: filterQuery,
}
extraFilters = append(extraFilters, extraFilter)
}
}
return extraFilters
}