Skip to content

Commit

Permalink
add support for multiple google project IDs (#105)
Browse files Browse the repository at this point in the history
Signed-off-by: Ross Murray <ross.murray@farfetch.com>

Co-authored-by: Ross Murray <ross.murray@farfetch.com>
  • Loading branch information
rossmmurray and Ross Murray authored Jul 27, 2020
1 parent 7fd73dd commit 42badeb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ If you are still using the legacy [Access scopes][access-scopes], the `https://w

| Flag / Environment Variable | Required | Default | Description |
| --------------------------- | -------- | ------- | ----------- |
| `google.project-id`<br />`STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID` | No | GCloud SDK autodiscovery | Google Project ID |
| `google.project-id`<br />`STACKDRIVER_EXPORTER_GOOGLE_PROJECT_ID` | No | GCloud SDK autodiscovery | Comma seperated list of Google Project IDs |
| `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]) |
| `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 |
| `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 |
Expand Down
24 changes: 14 additions & 10 deletions stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"net/http"
"os"
"strings"

"github.com/PuerkitoBio/rehttp"
"github.com/go-kit/kit/log"
Expand Down Expand Up @@ -46,7 +47,7 @@ var (
).Envar("STACKDRIVER_EXPORTER_WEB_TELEMETRY_PATH").Default("/metrics").String()

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

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

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

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

monitoringCollector, err := collectors.NewMonitoringCollector(projectID, m, filters, logger)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}

registry := prometheus.NewRegistry()
registry.MustRegister(monitoringCollector)

for _, project := range projectIDs {
monitoringCollector, err := collectors.NewMonitoringCollector(project, m, filters, logger)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
registry.MustRegister(monitoringCollector)
}

gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
Expand Down Expand Up @@ -168,7 +171,8 @@ func main() {
os.Exit(1)
}

handlerFunc := newHandler(*projectID, monitoringService, logger)
projectIDs := strings.Split(*projectID, ",")
handlerFunc := newHandler(projectIDs, monitoringService, logger)

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

0 comments on commit 42badeb

Please sign in to comment.