Skip to content

Commit bc18b73

Browse files
authored
Add feature to filter project ID at call-time (#164)
Signed-off-by: Ben Kochie <[email protected]>
1 parent 43caa51 commit bc18b73

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/prometheus-community/stackdriver_exporter)
88
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
99

10-
A [Prometheus][prometheus] exporter for [Google Stackdriver Monitoring][stackdriver] metrics. It acts as a proxy that requests Stackdriver API for the metric's time-series everytime prometheus scrapes it.
10+
A [Prometheus][prometheus] exporter for [Google Stackdriver Monitoring][stackdriver] metrics. It acts as a proxy that requests Stackdriver API for the metric's time-series every time prometheus scrapes it.
1111

1212
## Installation
1313

@@ -167,7 +167,7 @@ stackdriver_exporter \
167167

168168
The `stackdriver_exporter` collects all metrics type prefixes by default.
169169

170-
For advanced uses, the collection can be filtered by using a repeatable URL param called `collect`. In the Prometheus configuration you can use you can use this syntax under the [scrape config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#<scrape_config>).
170+
For advanced uses, the metric prefixes can be filtered by using a repeatable URL param called `collect`. In the Prometheus configuration you can use this syntax under the [scrape config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#<scrape_config>).
171171

172172

173173
```yaml
@@ -177,6 +177,17 @@ params:
177177
- compute.googleapis.com/instance/disk
178178
```
179179
180+
The GCP projects to collect metrics from can also be filtered, using the `project_ids` URL param:
181+
```yaml
182+
params:
183+
project_ids:
184+
- project-a
185+
- project-b
186+
collect:
187+
- compute.googleapis.com/instance/cpu
188+
- compute.googleapis.com/instance/disk
189+
```
190+
180191
### What to know about Aggregating DELTA Metrics
181192

182193
Treating DELTA Metrics as a gauge produces data which is wildly inaccurate/not very useful (see https://github.com/prometheus-community/stackdriver_exporter/issues/116). However, aggregating the DELTA metrics overtime is not a perfect solution and is intended to produce data which mirrors GCP's data as close as possible.

stackdriver_exporter.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,14 @@ type handler struct {
180180

181181
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
182182
collectParams := r.URL.Query()["collect"]
183-
filters := make(map[string]bool)
184-
for _, param := range collectParams {
185-
filters[param] = true
186-
}
183+
projectIDsParam := r.URL.Query()["project_ids"]
187184

188-
if len(filters) > 0 {
189-
h.innerHandler(filters).ServeHTTP(w, r)
190-
return
185+
projectFilters := make(map[string]bool)
186+
for _, projID := range projectIDsParam {
187+
projectFilters[projID] = true
191188
}
192189

193-
h.handler.ServeHTTP(w, r)
190+
h.innerHandler(collectParams, projectFilters).ServeHTTP(w, r)
194191
}
195192

196193
func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters []collectors.MetricFilter, m *monitoring.Service, logger log.Logger, additionalGatherer prometheus.Gatherer) *handler {
@@ -203,16 +200,20 @@ func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters
203200
m: m,
204201
}
205202

206-
h.handler = h.innerHandler(nil)
203+
h.handler = h.innerHandler(nil, nil)
207204
return h
208205
}
209206

210-
func (h *handler) innerHandler(filters map[string]bool) http.Handler {
207+
func (h *handler) innerHandler(metricFilters []string, projectFilters map[string]bool) http.Handler {
211208
registry := prometheus.NewRegistry()
212209

213210
for _, project := range h.projectIDs {
211+
if len(projectFilters) > 0 && !projectFilters[project] {
212+
continue
213+
}
214+
214215
monitoringCollector, err := collectors.NewMonitoringCollector(project, h.m, collectors.MonitoringCollectorOptions{
215-
MetricTypePrefixes: h.filterMetricTypePrefixes(filters),
216+
MetricTypePrefixes: h.filterMetricTypePrefixes(metricFilters),
216217
ExtraFilters: h.metricsExtraFilters,
217218
RequestInterval: *monitoringMetricsInterval,
218219
RequestOffset: *monitoringMetricsOffset,
@@ -243,13 +244,16 @@ func (h *handler) innerHandler(filters map[string]bool) http.Handler {
243244

244245
// filterMetricTypePrefixes filters the initial list of metric type prefixes, with the ones coming from an individual
245246
// prometheus collect request.
246-
func (h *handler) filterMetricTypePrefixes(filters map[string]bool) []string {
247+
func (h *handler) filterMetricTypePrefixes(filters []string) []string {
247248
filteredPrefixes := h.metricsPrefixes
248249
if len(filters) > 0 {
249250
filteredPrefixes = nil
250-
for _, prefix := range h.metricsPrefixes {
251-
if filters[prefix] {
252-
filteredPrefixes = append(filteredPrefixes, prefix)
251+
for _, calltimePrefix := range filters {
252+
for _, preconfiguredPrefix := range h.metricsPrefixes {
253+
if strings.HasPrefix(calltimePrefix, preconfiguredPrefix) {
254+
filteredPrefixes = append(filteredPrefixes, calltimePrefix)
255+
break
256+
}
253257
}
254258
}
255259
}

0 commit comments

Comments
 (0)