Skip to content

Commit 39411f6

Browse files
authored
Merge pull request #40 from alexey-medvedchikov/drop-delegated-projects-flag
Add option to drop metrics from delegated projects
2 parents 7028a8d + 0ac6073 commit 39411f6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

collectors/monitoring_collector.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type MonitoringCollector struct {
2828
lastScrapeTimestampMetric prometheus.Gauge
2929
lastScrapeDurationSecondsMetric prometheus.Gauge
3030
collectorFillMissingLabels bool
31+
monitoringDropDelegatedProjects bool
3132
}
3233

3334
func NewMonitoringCollector(
@@ -37,6 +38,7 @@ func NewMonitoringCollector(
3738
metricsOffset time.Duration,
3839
monitoringService *monitoring.Service,
3940
collectorFillMissingLabels bool,
41+
monitoringDropDelegatedProjects bool,
4042
) (*MonitoringCollector, error) {
4143
apiCallsTotalMetric := prometheus.NewCounter(
4244
prometheus.CounterOpts{
@@ -111,6 +113,7 @@ func NewMonitoringCollector(
111113
lastScrapeTimestampMetric: lastScrapeTimestampMetric,
112114
lastScrapeDurationSecondsMetric: lastScrapeDurationSecondsMetric,
113115
collectorFillMissingLabels: collectorFillMissingLabels,
116+
monitoringDropDelegatedProjects: monitoringDropDelegatedProjects,
114117
}
115118

116119
return monitoringCollector, nil
@@ -271,6 +274,21 @@ func (c *MonitoringCollector) reportTimeSeriesMetrics(
271274
labelValues = append(labelValues, value)
272275
}
273276

277+
if c.monitoringDropDelegatedProjects {
278+
dropDelegatedProject := false
279+
280+
for idx, val := range labelKeys {
281+
if val == "project_id" && labelValues[idx] != c.projectID {
282+
dropDelegatedProject = true
283+
break
284+
}
285+
}
286+
287+
if dropDelegatedProject {
288+
continue
289+
}
290+
}
291+
274292
switch timeSeries.MetricKind {
275293
case "GAUGE":
276294
metricValueType = prometheus.GaugeValue

stackdriver_exporter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var (
2323
"google.project-id", "Google Project ID ($STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID).",
2424
).Envar("STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID").Required().String()
2525

26+
monitoringDropDelegatedProjects = kingpin.Flag(
27+
"monitoring.drop-delegated-projects", "Drop metrics from attached projects and fetch `project_id` only ($STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS).",
28+
).Envar("STACKDRIVER_EXPORTER_DROP_DELEGATED_PROJECTS").Default("false").Bool()
29+
2630
monitoringMetricsTypePrefixes = kingpin.Flag(
2731
"monitoring.metrics-type-prefixes", "Comma separated Google Stackdriver Monitoring Metric Type prefixes ($STACKDRIVER_EXPORTER_MONITORING_METRICS_TYPE_PREFIXES).",
2832
).Envar("STACKDRIVER_EXPORTER_MONITORING_METRICS_TYPE_PREFIXES").Required().String()
@@ -123,7 +127,7 @@ func main() {
123127
os.Exit(1)
124128
}
125129

126-
monitoringCollector, err := collectors.NewMonitoringCollector(*projectID, metricsTypePrefixes, *monitoringMetricsInterval, *monitoringMetricsOffset, monitoringService, *collectorFillMissingLabels)
130+
monitoringCollector, err := collectors.NewMonitoringCollector(*projectID, metricsTypePrefixes, *monitoringMetricsInterval, *monitoringMetricsOffset, monitoringService, *collectorFillMissingLabels, *monitoringDropDelegatedProjects)
127131
if err != nil {
128132
log.Error(err)
129133
os.Exit(1)

0 commit comments

Comments
 (0)