Skip to content

fix(telemetry): resolve GCP project ID from gcloud for trace exporter#23942

Open
Gitanaskhan26 wants to merge 6 commits intogoogle-gemini:mainfrom
Gitanaskhan26:fix/trace-export-cli-auth-project-id
Open

fix(telemetry): resolve GCP project ID from gcloud for trace exporter#23942
Gitanaskhan26 wants to merge 6 commits intogoogle-gemini:mainfrom
Gitanaskhan26:fix/trace-export-cli-auth-project-id

Conversation

@Gitanaskhan26
Copy link
Copy Markdown

@Gitanaskhan26 Gitanaskhan26 commented Mar 26, 2026

Summary

Fixes trace exporting failure when useCliAuth is enabled. The TraceExporter's internal GoogleAuth incorrectly resolves the GCP project ID for authorized_user credentials, causing traces to target the wrong project (e.g., the OAuth client's project 681255809395) while logs and metrics export correctly.

Details

When useCliAuth is enabled and no explicit GOOGLE_CLOUD_PROJECT / OTLP_GOOGLE_CLOUD_PROJECT env var is set, the GCP project ID passed to all three exporters is undefined. Each exporter then resolves the project internally:

  • Logs (@google-cloud/logging): Uses google-gax which adds extra fallback logic for project resolution.
  • Metrics (googleapis): Uses REST clients with broader auth handling.
  • Traces (@google-cloud/opentelemetry-cloud-trace-exporter): Creates a raw GoogleAuth with authorized_user credentials. Since UserRefreshClient has no projectId, GoogleAuth.getProjectId() falls through to gcloud config config-helper or the metadata server and may resolve to the wrong project.

This PR adds a resolveGcpProjectId() helper in sdk.ts that resolves the project ID with a fallback to gcloud config get-value project, ensuring all three exporters always receive the correct project ID explicitly.

Related Issues

Fixes #21969

How to Validate

  1. Enable telemetry with useCliAuth: true and target: gcp in settings.
  2. Ensure gcloud config get-value project returns your GCP project ID.
  3. Do NOT set GOOGLE_CLOUD_PROJECT or OTLP_GOOGLE_CLOUD_PROJECT env vars.
  4. Run the CLI and verify traces are exported successfully to your GCP project.
  5. Also verify that setting OTLP_GOOGLE_CLOUD_PROJECT still takes priority over the gcloud fallback.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@Gitanaskhan26 Gitanaskhan26 requested a review from a team as a code owner March 26, 2026 20:53
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 26, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue in GCP telemetry exporting where trace data was being sent to an incorrect project when using CLI authentication. By introducing a robust project ID resolution mechanism that leverages environment variables and the gcloud CLI, it ensures that all telemetry exporters consistently target the intended GCP project, thereby improving the reliability and accuracy of telemetry data collection.

Highlights

  • GCP Project ID Resolution: Implemented a new resolveGcpProjectId() helper function to correctly determine the GCP project ID for telemetry exporters. This function prioritizes environment variables (OTLP_GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_PROJECT) and falls back to querying the gcloud CLI configuration.
  • Trace Exporter Fix: Resolved an issue where the OpenTelemetry Cloud Trace Exporter incorrectly identified the GCP project ID when useCliAuth was enabled, causing traces to be sent to the wrong project.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 the resolveGcpProjectId function to the telemetry SDK to ensure consistent GCP project ID resolution. The function prioritizes environment variables (OTLP_GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_PROJECT) and falls back to querying the gcloud CLI configuration if they are unset. Corresponding updates were made to initializeTelemetry to use this logic, and new unit tests were added to cover various resolution scenarios. I have no feedback to provide.

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. area/enterprise Issues related to Telemetry, Policy, Quota / Licensing labels Mar 26, 2026
@Gitanaskhan26 Gitanaskhan26 force-pushed the fix/trace-export-cli-auth-project-id branch from 8fab13c to e50cbac Compare March 26, 2026 21:29
When useCliAuth is enabled, the TraceExporter's internal GoogleAuth
resolves the project ID incorrectly for authorized_user credentials,
causing traces to target the wrong GCP project while logs and metrics
work correctly.

Add resolveGcpProjectId() that falls back to 'gcloud config get-value
project' when OTLP_GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_PROJECT env
vars are not set, ensuring all three exporters receive the correct
project ID explicitly.

Fixes google-gemini#21969
@Gitanaskhan26 Gitanaskhan26 force-pushed the fix/trace-export-cli-auth-project-id branch from e9d46fd to e7956cc Compare March 26, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/enterprise Issues related to Telemetry, Policy, Quota / Licensing priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Traces exporting doesn't work with CliAuth

1 participant