Skip to content

fix(telemetry): allow GCP project override#2960

Open
Deeven-Seru wants to merge 1 commit intogoogleapis:mainfrom
Deeven-Seru:fix-2285-telemetry-gcp-project
Open

fix(telemetry): allow GCP project override#2960
Deeven-Seru wants to merge 1 commit intogoogleapis:mainfrom
Deeven-Seru:fix-2285-telemetry-gcp-project

Conversation

@Deeven-Seru
Copy link
Copy Markdown
Contributor

feat(telemetry): Add GCP project override for local runs

Summary

This PR introduces a --telemetry-gcp-project flag to allow explicit specification of the Google Cloud Project ID for telemetry when running locally.

Previously, local runs relying on --telemetry-gcp could 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-gcp failure
Fixes: #2285


Changes

New Features

  • Add --telemetry-gcp-project CLI flag
    • Allows explicit override of the GCP project ID for telemetry exporters
    • Useful for local development and multi-project environments

Fallback Behavior

If --telemetry-gcp-project is not provided, the system now falls back to:

  1. GOOGLE_CLOUD_PROJECT
  2. GCLOUD_PROJECT

This ensures compatibility with standard Google Cloud environment configurations.

Telemetry Integration Updates

  • Pass resolved project ID into:
    • GCP Trace exporter
    • GCP Metric exporter

This guarantees consistent project attribution across telemetry signals.

Error Handling Improvements

Improved guidance when:

  • Application Default Credentials (ADC) are missing
  • Project ID cannot be resolved

New error messages now:

  • Clearly state the missing configuration
  • Suggest actionable fixes
  • Reference the new override flag

Example Usage

app --telemetry-gcp \
    --telemetry-gcp-project=my-dev-project

@Deeven-Seru Deeven-Seru requested a review from a team as a code owner April 6, 2026 10:50
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +218 to +223
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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".

Suggested change
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)
}

Comment on lines +135 to +137
if projectID := resolveGCPProjectID(telemetryGCPProject); projectID != "" {
gcpExporterOpts = append(gcpExporterOpts, texporter.WithProjectID(projectID))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ADC Env Var required for GCP Telemetry when running locally

3 participants