diff --git a/README.md b/README.md index 3d918f8b..9945f692 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ If you are still using the legacy [Access scopes][access-scopes], the `https://w | `monitoring.metrics-type-prefixes`
`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]) | | `monitoring.metrics-interval`
`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 | | `monitoring.metrics-offset`
`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 | +| `monitoring.filters`| No | Empty list | Formatted string to allow filtering on certain metrics type | | `web.listen-address`
`STACKDRIVER_EXPORTER_WEB_LISTEN_ADDRESS` | No | `:9255` | Address to listen on for web interface and telemetry | | `web.telemetry-path`
`STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH` | No | `/metrics` | Path under which to expose Prometheus metrics | @@ -107,6 +108,15 @@ stackdriver_exporter \ --monitoring.metrics-type-prefixes "compute.googleapis.com/instance/cpu,compute.googleapis.com/instance/disk" ``` +Using extra filters: + +``` +stackdriver_exporter \ + --google.project-id my-test-project \ + --monitoring.metrics-type-prefixes='pubsub.googleapis.com/subscription' \ + --monitoring.metrics-extra-filter='pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match("us-west4.*my-team-subs.*")' +``` + ## Filtering enabled collectors The `stackdriver_exporter` collects all metrics type prefixes by default. diff --git a/collectors/monitoring_collector.go b/collectors/monitoring_collector.go index a7e7cc2c..c5a7c8ed 100644 --- a/collectors/monitoring_collector.go +++ b/collectors/monitoring_collector.go @@ -51,11 +51,20 @@ var ( monitoringDropDelegatedProjects = kingpin.Flag( "monitoring.drop-delegated-projects", "Drop metrics from attached projects and fetch `project_id` only ($STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS).", ).Envar("STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS").Default("false").Bool() + + monitoringMetricsExtraFilter = kingpin.Flag( + "monitoring.filters", "Filters. i.e: pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match(\"my-subs-prefix.*\")").Strings() ) +type MetricFilter struct { + Prefix string + Modifier string +} + type MonitoringCollector struct { projectID string metricsTypePrefixes []string + metricsFilters []MetricFilter metricsInterval time.Duration metricsOffset time.Duration monitoringService *monitoring.Service @@ -145,10 +154,22 @@ func NewMonitoringCollector(projectID string, monitoringService *monitoring.Serv } } } + var extraFilters []MetricFilter + for _, ef := range *monitoringMetricsExtraFilter { + efPrefix, efModifier := utils.GetExtraFilterModifiers(ef, ":") + if efPrefix != "" { + extraFilter := MetricFilter{ + Prefix: efPrefix, + Modifier: efModifier, + } + extraFilters = append(extraFilters, extraFilter) + } + } monitoringCollector := &MonitoringCollector{ projectID: projectID, metricsTypePrefixes: filteredPrefixes, + metricsFilters: extraFilters, metricsInterval: *monitoringMetricsInterval, metricsOffset: *monitoringMetricsOffset, monitoringService: monitoringService, @@ -238,6 +259,13 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri c.projectID, metricDescriptor.Type) } + for _, ef := range c.metricsFilters { + if strings.Contains(metricDescriptor.Type, ef.Prefix) { + filter = fmt.Sprintf("%s AND (%s)", filter, ef.Modifier) + } + } + + level.Debug(c.logger).Log("msg", "retrieving Google Stackdriver Monitoring metrics with filter", "filter", filter) timeSeriesListCall := c.monitoringService.Projects.TimeSeries.List(utils.ProjectResource(c.projectID)). Filter(filter). IntervalStartTime(startTime.Format(time.RFC3339Nano)). diff --git a/utils/utils.go b/utils/utils.go index 49f7b367..9809bdf6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -39,6 +39,14 @@ func NormalizeMetricName(metricName string) string { return strings.Join(normalizedMetricName, "_") } +func GetExtraFilterModifiers(extraFilter string, separator string) (string, string) { + mPrefix := strings.Split(extraFilter, separator) + if mPrefix[0] == extraFilter { + return "", "" + } + return mPrefix[0], strings.Join(mPrefix[1:], "") +} + func ProjectResource(projectID string) string { return "projects/" + projectID }