fix(telemetry): allow GCP project override#2960
fix(telemetry): allow GCP project override#2960Deeven-Seru wants to merge 1 commit intogoogleapis:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new --telemetry-gcp-project flag and logic to resolve the Google Cloud project ID from environment variables for telemetry exports. It updates the OpenTelemetry initialization across the codebase and adds a helper to provide descriptive hints when GCP configuration is missing. Review feedback recommends using %w for error wrapping to preserve the error chain and suggests optimizing the project ID resolution by performing the lookup once at the start of the setup process.
| if strings.Contains(message, "no project found") || strings.Contains(message, "project") { | ||
| projectHint := "Set --telemetry-gcp-project or GOOGLE_CLOUD_PROJECT (or GCLOUD_PROJECT), and ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | ||
| if telemetryGCPProject != "" { | ||
| projectHint = "Ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | ||
| } | ||
| return fmt.Errorf("%s %s", message, projectHint) |
There was a problem hiding this comment.
The error wrapping should use %w to preserve the error chain, allowing callers to inspect the original error if needed. Additionally, the condition strings.Contains(message, "no project found") is redundant as it is covered by strings.Contains(message, "project"). To align with the PR description's goal of improving guidance for missing Application Default Credentials (ADC), consider also checking for "credentials".
| if strings.Contains(message, "no project found") || strings.Contains(message, "project") { | |
| projectHint := "Set --telemetry-gcp-project or GOOGLE_CLOUD_PROJECT (or GCLOUD_PROJECT), and ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | |
| if telemetryGCPProject != "" { | |
| projectHint = "Ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | |
| } | |
| return fmt.Errorf("%s %s", message, projectHint) | |
| if strings.Contains(message, "project") || strings.Contains(message, "credentials") { | |
| projectHint := "Set --telemetry-gcp-project or GOOGLE_CLOUD_PROJECT (or GCLOUD_PROJECT), and ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | |
| if telemetryGCPProject != "" { | |
| projectHint = "Ensure GOOGLE_APPLICATION_CREDENTIALS points to valid ADC credentials." | |
| } | |
| return fmt.Errorf("%w: %s", err, projectHint) | |
| } |
| if projectID := resolveGCPProjectID(telemetryGCPProject); projectID != "" { | ||
| gcpExporterOpts = append(gcpExporterOpts, texporter.WithProjectID(projectID)) | ||
| } |
There was a problem hiding this comment.
To improve efficiency and avoid redundant environment variable lookups, consider resolving the project ID once at the beginning of SetupOTel and passing the resolved value to newTracerProvider and newMeterProvider instead of calling resolveGCPProjectID in both functions. This aligns with the general repository principle of minimizing redundant parsing/lookups.
| if telemetryGCPProject != "" { | ||
| return telemetryGCPProject | ||
| } | ||
| if projectID := os.Getenv("GOOGLE_CLOUD_PROJECT"); projectID != "" { |
There was a problem hiding this comment.
Not sure if we want to read from env var from src code. Will circle back to you on this. Thank you for the contribution!!
feat(telemetry): Add GCP project override for local runs
Summary
This PR introduces a
--telemetry-gcp-projectflag to allow explicit specification of the Google Cloud Project ID for telemetry when running locally.Previously, local runs relying on
--telemetry-gcpcould fail if Application Default Credentials (ADC) or project configuration were missing or misconfigured. This change improves reliability and developer experience by enabling manual project overrides and clearer error guidance.Ref: Local
--telemetry-gcpfailureFixes: #2285
Changes
New Features
--telemetry-gcp-projectCLI flagFallback Behavior
If
--telemetry-gcp-projectis not provided, the system now falls back to:GOOGLE_CLOUD_PROJECTGCLOUD_PROJECTThis ensures compatibility with standard Google Cloud environment configurations.
Telemetry Integration Updates
This guarantees consistent project attribution across telemetry signals.
Error Handling Improvements
Improved guidance when:
New error messages now:
Example Usage
app --telemetry-gcp \ --telemetry-gcp-project=my-dev-project