Skip to content

Commit 882f61d

Browse files
authored
add monitoring.filters flag (#133)
* add monitoring.filters flag Signed-off-by: vrunoa <vruno.alassia@gmail.com>
1 parent b79175b commit 882f61d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ If you are still using the legacy [Access scopes][access-scopes], the `https://w
6767
| `monitoring.metrics-type-prefixes`<br />`STACKDRIVER_EXPORTER_MONITORING_METRICS_TYPE_PREFIXES` | Yes | | Comma separated Google Stackdriver Monitoring Metric Type prefixes (see [example][metrics-prefix-example] and [available metrics][metrics-list]) |
6868
| `monitoring.metrics-interval`<br />`STACKDRIVER_EXPORTER_MONITORING_METRICS_INTERVAL` | No | `5m` | Metric's timestamp interval to request from the Google Stackdriver Monitoring Metrics API. Only the most recent data point is used |
6969
| `monitoring.metrics-offset`<br />`STACKDRIVER_EXPORTER_MONITORING_METRICS_OFFSET` | No | `0s` | Offset (into the past) for the metric's timestamp interval to request from the Google Stackdriver Monitoring Metrics API, to handle latency in published metrics |
70+
| `monitoring.filters`| No | Empty list | Formatted string to allow filtering on certain metrics type |
7071
| `web.listen-address`<br />`STACKDRIVER_EXPORTER_WEB_LISTEN_ADDRESS` | No | `:9255` | Address to listen on for web interface and telemetry |
7172
| `web.telemetry-path`<br />`STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH` | No | `/metrics` | Path under which to expose Prometheus metrics |
7273

@@ -107,6 +108,15 @@ stackdriver_exporter \
107108
--monitoring.metrics-type-prefixes "compute.googleapis.com/instance/cpu,compute.googleapis.com/instance/disk"
108109
```
109110

111+
Using extra filters:
112+
113+
```
114+
stackdriver_exporter \
115+
--google.project-id my-test-project \
116+
--monitoring.metrics-type-prefixes='pubsub.googleapis.com/subscription' \
117+
--monitoring.metrics-extra-filter='pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match("us-west4.*my-team-subs.*")'
118+
```
119+
110120
## Filtering enabled collectors
111121

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

collectors/monitoring_collector.go

+28
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ var (
5151
monitoringDropDelegatedProjects = kingpin.Flag(
5252
"monitoring.drop-delegated-projects", "Drop metrics from attached projects and fetch `project_id` only ($STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS).",
5353
).Envar("STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS").Default("false").Bool()
54+
55+
monitoringMetricsExtraFilter = kingpin.Flag(
56+
"monitoring.filters", "Filters. i.e: pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match(\"my-subs-prefix.*\")").Strings()
5457
)
5558

59+
type MetricFilter struct {
60+
Prefix string
61+
Modifier string
62+
}
63+
5664
type MonitoringCollector struct {
5765
projectID string
5866
metricsTypePrefixes []string
67+
metricsFilters []MetricFilter
5968
metricsInterval time.Duration
6069
metricsOffset time.Duration
6170
monitoringService *monitoring.Service
@@ -145,10 +154,22 @@ func NewMonitoringCollector(projectID string, monitoringService *monitoring.Serv
145154
}
146155
}
147156
}
157+
var extraFilters []MetricFilter
158+
for _, ef := range *monitoringMetricsExtraFilter {
159+
efPrefix, efModifier := utils.GetExtraFilterModifiers(ef, ":")
160+
if efPrefix != "" {
161+
extraFilter := MetricFilter{
162+
Prefix: efPrefix,
163+
Modifier: efModifier,
164+
}
165+
extraFilters = append(extraFilters, extraFilter)
166+
}
167+
}
148168

149169
monitoringCollector := &MonitoringCollector{
150170
projectID: projectID,
151171
metricsTypePrefixes: filteredPrefixes,
172+
metricsFilters: extraFilters,
152173
metricsInterval: *monitoringMetricsInterval,
153174
metricsOffset: *monitoringMetricsOffset,
154175
monitoringService: monitoringService,
@@ -238,6 +259,13 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri
238259
c.projectID,
239260
metricDescriptor.Type)
240261
}
262+
for _, ef := range c.metricsFilters {
263+
if strings.Contains(metricDescriptor.Type, ef.Prefix) {
264+
filter = fmt.Sprintf("%s AND (%s)", filter, ef.Modifier)
265+
}
266+
}
267+
268+
level.Debug(c.logger).Log("msg", "retrieving Google Stackdriver Monitoring metrics with filter", "filter", filter)
241269
timeSeriesListCall := c.monitoringService.Projects.TimeSeries.List(utils.ProjectResource(c.projectID)).
242270
Filter(filter).
243271
IntervalStartTime(startTime.Format(time.RFC3339Nano)).

utils/utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func NormalizeMetricName(metricName string) string {
3939
return strings.Join(normalizedMetricName, "_")
4040
}
4141

42+
func GetExtraFilterModifiers(extraFilter string, separator string) (string, string) {
43+
mPrefix := strings.Split(extraFilter, separator)
44+
if mPrefix[0] == extraFilter {
45+
return "", ""
46+
}
47+
return mPrefix[0], strings.Join(mPrefix[1:], "")
48+
}
49+
4250
func ProjectResource(projectID string) string {
4351
return "projects/" + projectID
4452
}

0 commit comments

Comments
 (0)