Skip to content

fix: use dynamic tracer name instead of hardcoded gax-java#12190

Open
diegomarquezp wants to merge 14 commits intomainfrom
observability/tracing/tracer-name
Open

fix: use dynamic tracer name instead of hardcoded gax-java#12190
diegomarquezp wants to merge 14 commits intomainfrom
observability/tracing/tracer-name

Conversation

@diegomarquezp
Copy link
Copy Markdown
Contributor

Description

This PR updates SpanTracerFactory to dynamically initialize OpenTelemetry tracers using artifact metadata (artifactName and version) rather than defaulting to a hardcoded "gax-java" instrumentation scope.

Previously, traces initiated by downstream libraries (like gapic-showcase) would incorrectly report their scope as gax-java. This update ensures that the tracer name accurately reflects the invoking context (e.g., com.google.cloud:gapic-showcase), significantly improving observability and root-cause analysis in distributed tracing environments.

Key Changes

  • Modified SpanTracerFactory to construct the OpenTelemetry metric tracer lazily from ApiTracerContext metadata when withContext is invoked.
  • Implemented a fallback strategy returning a BaseApiTracer (no-op) when the active tracer hasn't been properly initialized, avoiding NullPointerExceptions during client configuration.
  • Updated SpanTracerFactoryTest to mock OpenTelemetry testing the null-tracer degradation logic.
  • Updated ITOtelTracing in java-showcase to assert that spans log the exact metadata injected from LibraryMetadata.

Refactored SpanTracerFactory to dynamically initialize tracers with
metadata containing the artifact name and version when withContext
is invoked. Handled the null-tracer fallback strategy by returning a
BaseApiTracer when tracer is null. Updated unit and integration tests.
@diegomarquezp diegomarquezp requested a review from a team as a code owner March 24, 2026 20:07
@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 significantly enhances the observability of distributed tracing by ensuring that OpenTelemetry tracers accurately report the originating library's metadata, such as its artifact name and version. This change moves away from a generic, hardcoded tracer name to a dynamic one, which will greatly improve the precision of root-cause analysis and overall trace clarity in complex distributed systems. Additionally, it introduces a robust fallback for uninitialized tracers, improving client stability.

Highlights

  • Dynamic OpenTelemetry Tracer Initialization: The SpanTracerFactory now dynamically initializes OpenTelemetry tracers using the artifactName and version from LibraryMetadata when withContext is invoked, replacing the previously hardcoded 'gax-java' instrumentation scope.
  • No-Op Tracer Fallback: A fallback mechanism has been implemented to return a BaseApiTracer (no-op) if the tracer has not been properly initialized, preventing NullPointerExceptions during client configuration.
  • Updated Test Coverage: Unit tests in SpanTracerFactoryTest were updated to mock OpenTelemetry and verify the new null-tracer degradation logic. Integration tests in ITOtelTracing were refactored to use EchoClient and assert that spans correctly log the dynamically injected metadata.
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.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@diegomarquezp diegomarquezp marked this pull request as draft March 24, 2026 20:14
@diegomarquezp diegomarquezp marked this pull request as ready for review March 24, 2026 21:31
@diegomarquezp diegomarquezp added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 26, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 26, 2026
@diegomarquezp diegomarquezp added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 26, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 26, 2026
return new BaseApiTracerFactory();
}
Tracer newTracer = openTelemetry.getTracer(metadata.artifactName(), metadata.version());
if (newTracer == null) {
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.

In what case this could return null? I think openTelemetry api in general is safe to use. But if we are not sure, it's always good to add a null check as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The docs don't mention a possible null return. I think you are right. I removed this check.

}
ApiTracerContext mergedContext = this.apiTracerContext.merge(context);
LibraryMetadata metadata = mergedContext.libraryMetadata();
if (metadata == null || metadata.isEmpty()) {
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.

In addition, I think we should skip if artifactName is null but version is populated. But it version is null but artifact name is not, maybe we should still create one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

return new BaseApiTracerFactory();
}
ApiTracerContext mergedContext = this.apiTracerContext.merge(context);
LibraryMetadata metadata = mergedContext.libraryMetadata();
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.

Since metadata comes from the Library level context, I think we can do the check before merging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Checking on original context.

@blakeli0 blakeli0 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 27, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 27, 2026
@diegomarquezp diegomarquezp added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 27, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 27, 2026
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.

4 participants