Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for multiple google project IDs #105

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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