Skip to content

Commit

Permalink
Revert "Add feature to filter project ID at call-time (#164)" (#316)
Browse files Browse the repository at this point in the history
This reverts commit bc18b73.

Signed-off-by: Ben Rottke <[email protected]>
  • Loading branch information
xairos authored Mar 13, 2024
1 parent bc18b73 commit 205417f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
15 changes: 2 additions & 13 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 every time 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 everytime 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 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>).
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>).


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

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

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

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

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

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

func (h *handler) innerHandler(metricFilters []string, projectFilters map[string]bool) http.Handler {
func (h *handler) innerHandler(filters 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(metricFilters),
MetricTypePrefixes: h.filterMetricTypePrefixes(filters),
ExtraFilters: h.metricsExtraFilters,
RequestInterval: *monitoringMetricsInterval,
RequestOffset: *monitoringMetricsOffset,
Expand Down Expand Up @@ -244,16 +243,13 @@ func (h *handler) innerHandler(metricFilters []string, projectFilters map[string

// filterMetricTypePrefixes filters the initial list of metric type prefixes, with the ones coming from an individual
// prometheus collect request.
func (h *handler) filterMetricTypePrefixes(filters []string) []string {
func (h *handler) filterMetricTypePrefixes(filters map[string]bool) []string {
filteredPrefixes := h.metricsPrefixes
if len(filters) > 0 {
filteredPrefixes = nil
for _, calltimePrefix := range filters {
for _, preconfiguredPrefix := range h.metricsPrefixes {
if strings.HasPrefix(calltimePrefix, preconfiguredPrefix) {
filteredPrefixes = append(filteredPrefixes, calltimePrefix)
break
}
for _, prefix := range h.metricsPrefixes {
if filters[prefix] {
filteredPrefixes = append(filteredPrefixes, prefix)
}
}
}
Expand Down

0 comments on commit 205417f

Please sign in to comment.