Skip to content

Commit

Permalink
Add feature to filter project ID at call-time (#164)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
xairos committed Mar 11, 2024
1 parent 43caa51 commit bc18b73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/prometheus-community/stackdriver_exporter)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

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.
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.

## Installation

Expand Down Expand Up @@ -167,7 +167,7 @@ stackdriver_exporter \

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

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>).
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>).


```yaml
Expand All @@ -177,6 +177,17 @@ params:
- compute.googleapis.com/instance/disk
```

The GCP projects to collect metrics from can also be filtered, using the `project_ids` URL param:
```yaml
params:
project_ids:
- project-a
- project-b
collect:
- compute.googleapis.com/instance/cpu
- compute.googleapis.com/instance/disk
```

### What to know about Aggregating DELTA Metrics

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.
Expand Down
34 changes: 19 additions & 15 deletions stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,14 @@ type handler struct {

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
}
projectIDsParam := r.URL.Query()["project_ids"]

if len(filters) > 0 {
h.innerHandler(filters).ServeHTTP(w, r)
return
projectFilters := make(map[string]bool)
for _, projID := range projectIDsParam {
projectFilters[projID] = true
}

h.handler.ServeHTTP(w, r)
h.innerHandler(collectParams, projectFilters).ServeHTTP(w, r)
}

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

h.handler = h.innerHandler(nil)
h.handler = h.innerHandler(nil, nil)
return h
}

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

for _, project := range h.projectIDs {
if len(projectFilters) > 0 && !projectFilters[project] {
continue
}

monitoringCollector, err := collectors.NewMonitoringCollector(project, h.m, collectors.MonitoringCollectorOptions{
MetricTypePrefixes: h.filterMetricTypePrefixes(filters),
MetricTypePrefixes: h.filterMetricTypePrefixes(metricFilters),
ExtraFilters: h.metricsExtraFilters,
RequestInterval: *monitoringMetricsInterval,
RequestOffset: *monitoringMetricsOffset,
Expand Down Expand Up @@ -243,13 +244,16 @@ func (h *handler) innerHandler(filters map[string]bool) http.Handler {

// 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 {
func (h *handler) filterMetricTypePrefixes(filters []string) []string {
filteredPrefixes := h.metricsPrefixes
if len(filters) > 0 {
filteredPrefixes = nil
for _, prefix := range h.metricsPrefixes {
if filters[prefix] {
filteredPrefixes = append(filteredPrefixes, prefix)
for _, calltimePrefix := range filters {
for _, preconfiguredPrefix := range h.metricsPrefixes {
if strings.HasPrefix(calltimePrefix, preconfiguredPrefix) {
filteredPrefixes = append(filteredPrefixes, calltimePrefix)
break
}
}
}
}
Expand Down

0 comments on commit bc18b73

Please sign in to comment.