Skip to content

Commit 42badeb

Browse files
rossmmurrayRoss Murray
andauthored
add support for multiple google project IDs (#105)
Signed-off-by: Ross Murray <[email protected]> Co-authored-by: Ross Murray <[email protected]>
1 parent 7fd73dd commit 42badeb

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ If you are still using the legacy [Access scopes][access-scopes], the `https://w
6363

6464
| Flag / Environment Variable | Required | Default | Description |
6565
| --------------------------- | -------- | ------- | ----------- |
66-
| `google.project-id`<br />`STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID` | No | GCloud SDK autodiscovery | Google Project ID |
66+
| `google.project-id`<br />`STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID` | No | GCloud SDK autodiscovery | Comma seperated list of Google Project IDs |
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 |

stackdriver_exporter.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"net/http"
1919
"os"
20+
"strings"
2021

2122
"github.com/PuerkitoBio/rehttp"
2223
"github.com/go-kit/kit/log"
@@ -46,7 +47,7 @@ var (
4647
).Envar("STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH").Default("/metrics").String()
4748

4849
projectID = kingpin.Flag(
49-
"google.project-id", "Google Project ID ($STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID).",
50+
"google.project-id", "Comma seperated list of Google Project IDs ($STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID).",
5051
).Envar("STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID").String()
5152

5253
stackdriverMaxRetries = kingpin.Flag(
@@ -108,7 +109,7 @@ func createMonitoringService(ctx context.Context) (*monitoring.Service, error) {
108109
return monitoringService, nil
109110
}
110111

111-
func newHandler(projectID string, m *monitoring.Service, logger log.Logger) http.HandlerFunc {
112+
func newHandler(projectIDs []string, m *monitoring.Service, logger log.Logger) http.HandlerFunc {
112113
return func(w http.ResponseWriter, r *http.Request) {
113114
collectParams := r.URL.Query()["collect"]
114115

@@ -118,14 +119,16 @@ func newHandler(projectID string, m *monitoring.Service, logger log.Logger) http
118119
filters[param] = true
119120
}
120121

121-
monitoringCollector, err := collectors.NewMonitoringCollector(projectID, m, filters, logger)
122-
if err != nil {
123-
level.Error(logger).Log("err", err)
124-
os.Exit(1)
125-
}
126-
127122
registry := prometheus.NewRegistry()
128-
registry.MustRegister(monitoringCollector)
123+
124+
for _, project := range projectIDs {
125+
monitoringCollector, err := collectors.NewMonitoringCollector(project, m, filters, logger)
126+
if err != nil {
127+
level.Error(logger).Log("err", err)
128+
os.Exit(1)
129+
}
130+
registry.MustRegister(monitoringCollector)
131+
}
129132

130133
gatherers := prometheus.Gatherers{
131134
prometheus.DefaultGatherer,
@@ -168,7 +171,8 @@ func main() {
168171
os.Exit(1)
169172
}
170173

171-
handlerFunc := newHandler(*projectID, monitoringService, logger)
174+
projectIDs := strings.Split(*projectID, ",")
175+
handlerFunc := newHandler(projectIDs, monitoringService, logger)
172176

173177
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
174178
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)