From ceeb291e15636aac61ace6b170d8511f80ec2b3b Mon Sep 17 00:00:00 2001 From: crflanigan <69858641+crflanigan@users.noreply.github.com> Date: Wed, 19 Mar 2025 11:22:39 -0500 Subject: [PATCH] feat(outputs.stackdriver): Ensure quota is charged to configured project (#16583) Co-authored-by: root --- plugins/outputs/stackdriver/README.md | 7 +++++++ plugins/outputs/stackdriver/sample.conf | 7 +++++++ plugins/outputs/stackdriver/stackdriver.go | 13 ++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/plugins/outputs/stackdriver/README.md b/plugins/outputs/stackdriver/README.md index 0de36271b..be042ec3d 100644 --- a/plugins/outputs/stackdriver/README.md +++ b/plugins/outputs/stackdriver/README.md @@ -36,6 +36,13 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## GCP Project project = "erudite-bloom-151019" + ## Quota Project + ## Specifies the Google Cloud project that should be billed for metric ingestion. + ## If omitted, the quota is charged to the service account’s default project. + ## This is useful when sending metrics to multiple projects using a single service account. + ## The caller must have the `serviceusage.services.use` permission on the specified project. + # quota_project = "" + ## The namespace for the metric descriptor ## This is optional and users are encouraged to set the namespace as a ## resource label instead. If omitted it is not included in the metric name. diff --git a/plugins/outputs/stackdriver/sample.conf b/plugins/outputs/stackdriver/sample.conf index 79c08da3c..3b27469ff 100644 --- a/plugins/outputs/stackdriver/sample.conf +++ b/plugins/outputs/stackdriver/sample.conf @@ -3,6 +3,13 @@ ## GCP Project project = "erudite-bloom-151019" + ## Quota Project + ## Specifies the Google Cloud project that should be billed for metric ingestion. + ## If omitted, the quota is charged to the service account’s default project. + ## This is useful when sending metrics to multiple projects using a single service account. + ## The caller must have the `serviceusage.services.use` permission on the specified project. + # quota_project = "" + ## The namespace for the metric descriptor ## This is optional and users are encouraged to set the namespace as a ## resource label instead. If omitted it is not included in the metric name. diff --git a/plugins/outputs/stackdriver/stackdriver.go b/plugins/outputs/stackdriver/stackdriver.go index 767da3514..e5604a7e0 100644 --- a/plugins/outputs/stackdriver/stackdriver.go +++ b/plugins/outputs/stackdriver/stackdriver.go @@ -33,6 +33,7 @@ var sampleConfig string // Stackdriver is the Google Stackdriver config info. type Stackdriver struct { Project string `toml:"project"` + QuotaProject string `toml:"quota_project"` Namespace string `toml:"namespace"` ResourceType string `toml:"resource_type"` ResourceLabels map[string]string `toml:"resource_labels"` @@ -136,9 +137,19 @@ func (s *Stackdriver) Connect() error { s.ResourceLabels["project_id"] = s.Project + // Define client options, starting with the user agent + options := []option.ClientOption{ + option.WithUserAgent(internal.ProductToken()), + } + + if s.QuotaProject != "" { + options = append(options, option.WithQuotaProject(s.QuotaProject)) + s.Log.Infof("Using QuotaProject %s for quota attribution", s.QuotaProject) + } + if s.client == nil { ctx := context.Background() - client, err := monitoring.NewMetricClient(ctx, option.WithUserAgent(internal.ProductToken())) + client, err := monitoring.NewMetricClient(ctx, options...) if err != nil { return err }