Skip to content

Commit dc04cd8

Browse files
authored
Merge pull request #87 from prometheus-community/superq/collect_param
Add metrics prefix collect URL param
2 parents 9016277 + 6bc4d51 commit dc04cd8

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## master / unreleased
22

3+
* [FEATURE] Add metrics prefix collect URL param
4+
35
## 0.7.0 / 2020-05-01
46

57
* [CHANGE] Remove deprecated `monitoring.New()` use. #76

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ stackdriver_exporter \
107107
--monitoring.metrics-type-prefixes "compute.googleapis.com/instance/cpu,compute.googleapis.com/instance/disk"
108108
```
109109

110+
## Filtering enabled collectors
111+
112+
The `stackdriver_exporter` collects all metrics type prefixes by default.
113+
114+
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>).
115+
116+
117+
```yaml
118+
params:
119+
collect[]:
120+
- compute.googleapis.com/instance/cpu
121+
- compute.googleapis.com/instance/disk
122+
```
123+
110124
## Contributing
111125
112126
Refer to the [contributing guidelines][contributing].

collectors/monitoring_collector.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type MonitoringCollector struct {
7272
monitoringDropDelegatedProjects bool
7373
}
7474

75-
func NewMonitoringCollector(monitoringService *monitoring.Service) (*MonitoringCollector, error) {
75+
func NewMonitoringCollector(monitoringService *monitoring.Service, filters map[string]bool) (*MonitoringCollector, error) {
7676
if *projectID == "" {
7777
return nil, errors.New("Flag `google.project-id` is required")
7878
}
@@ -141,9 +141,20 @@ func NewMonitoringCollector(monitoringService *monitoring.Service) (*MonitoringC
141141
},
142142
)
143143

144+
metricsTypePrefixes := strings.Split(*monitoringMetricsTypePrefixes, ",")
145+
filteredPrefixes := metricsTypePrefixes
146+
if len(filters) > 0 {
147+
filteredPrefixes = nil
148+
for _, prefix := range metricsTypePrefixes {
149+
if filters[prefix] {
150+
filteredPrefixes = append(filteredPrefixes, prefix)
151+
}
152+
}
153+
}
154+
144155
monitoringCollector := &MonitoringCollector{
145156
projectID: *projectID,
146-
metricsTypePrefixes: strings.Split(*monitoringMetricsTypePrefixes, ","),
157+
metricsTypePrefixes: filteredPrefixes,
147158
metricsInterval: *monitoringMetricsInterval,
148159
metricsOffset: *monitoringMetricsOffset,
149160
monitoringService: monitoringService,

stackdriver_exporter.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,34 @@ func createMonitoringService() (*monitoring.Service, error) {
9090
return monitoringService, nil
9191
}
9292

93+
func newHandler(m *monitoring.Service) http.HandlerFunc {
94+
return func(w http.ResponseWriter, r *http.Request) {
95+
collectParams := r.URL.Query()["collect[]"]
96+
97+
// Create filters for "collect[]" query parameters.
98+
filters := make(map[string]bool)
99+
for _, param := range collectParams {
100+
filters[param] = true
101+
}
102+
103+
monitoringCollector, err := collectors.NewMonitoringCollector(m, filters)
104+
if err != nil {
105+
log.Fatal(err)
106+
}
107+
108+
registry := prometheus.NewRegistry()
109+
registry.MustRegister(monitoringCollector)
110+
111+
gatherers := prometheus.Gatherers{
112+
prometheus.DefaultGatherer,
113+
registry,
114+
}
115+
// Delegate http serving to Prometheus client library, which will call collector.Collect.
116+
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
117+
h.ServeHTTP(w, r)
118+
}
119+
}
120+
93121
func main() {
94122
log.AddFlags(kingpin.CommandLine)
95123
kingpin.Version(version.Print("stackdriver_exporter"))
@@ -104,13 +132,9 @@ func main() {
104132
log.Fatal(err)
105133
}
106134

107-
monitoringCollector, err := collectors.NewMonitoringCollector(monitoringService)
108-
if err != nil {
109-
log.Fatal(err)
110-
}
111-
prometheus.MustRegister(monitoringCollector)
135+
handlerFunc := newHandler(monitoringService)
112136

113-
http.Handle(*metricsPath, promhttp.Handler())
137+
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
114138
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
115139
w.Write([]byte(`<html>
116140
<head><title>Stackdriver Exporter</title></head>

0 commit comments

Comments
 (0)