Skip to content

fix: Populate method level attributes in metrics recording#4149

Open
blakeli0 wants to merge 3 commits intomainfrom
merge-apitracercontext
Open

fix: Populate method level attributes in metrics recording#4149
blakeli0 wants to merge 3 commits intomainfrom
merge-apitracercontext

Conversation

@blakeli0
Copy link
Contributor

Populate method level attributes in metrics recording. This is done by overriding newTracer(ApiTracer, ApiTracerContext) and merge the method level ApiTracerContext with the client level ApiTracerContext.

@product-auto-label product-auto-label bot added the size: s Pull request size is small. label Mar 16, 2026
@blakeli0 blakeli0 requested a review from diegomarquezp March 16, 2026 22:07
@gemini-code-assist
Copy link
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 enhances the Golden Signals metrics recording by introducing the capability to incorporate method-level attributes into the tracing context. This change allows for more precise and detailed metrics by merging client-specific and method-specific tracing information, ultimately providing a richer dataset for observability without altering the core metrics collection mechanism.

Highlights

  • Refactored Tracer Context Naming: The apiTracerContext field in GoldenSignalsMetricsTracerFactory was renamed to clientLevelTracerContext to explicitly distinguish between client-level and method-level tracing contexts, improving clarity.
  • Introduced Method-Level Context Merging: A new newTracer overload was added to GoldenSignalsMetricsTracerFactory that accepts a methodLevelTracerContext. This new method merges the client-level context with the provided method-level context, allowing for more granular attribute recording.
  • Enhanced Test Coverage: New unit tests were added to GoldenSignalsMetricsTracerFactoryTest to verify the correct merging of ApiTracerContext instances and to ensure the new newTracer overload behaves as expected, including handling cases where the metrics recorder is null.
Changelog
  • gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracerFactory.java
    • Renamed apiTracerContext field to clientLevelTracerContext for better semantic clarity.
    • Added a new newTracer method overload that accepts an ApiTracerContext for method-level attributes.
    • Implemented logic within the new newTracer method to merge clientLevelTracerContext with the provided methodLevelTracerContext.
    • Updated references to the renamed context field in the constructor and withContext method.
  • gax-java/gax/src/test/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracerFactoryTest.java
    • Added new Mockito imports (RETURNS_DEEP_STUBS, when) to support advanced mocking scenarios.
    • Introduced newTracer_shouldMergeApiTracerContext test to validate the merging behavior of client and method-level contexts.
    • Added newTracer1_createsBaseTracer_ifMetricsRecorderIsNull test to cover the no-op tracer creation for the new newTracer overload when the metrics recorder is absent.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
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 correctly implements the population of method-level attributes for metrics recording by overriding newTracer to merge client-level and method-level ApiTracerContext. The changes look good overall. I've added a few suggestions to improve code maintainability and test robustness by reducing code duplication, improving a test name for clarity, and making a test assertion stronger.

Comment on lines +66 to +70
if (metricsRecorder == null) {
// This should never happen, in case it happens, create a no-op api tracer to not block
// regular requests.
return new BaseApiTracer();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This null check for metricsRecorder is duplicated from the other newTracer method (lines 56-59). To improve maintainability and avoid code duplication, consider extracting this logic into a private helper method. For example:

private ApiTracer createTracerWithContext(ApiTracerContext context) {
  if (metricsRecorder == null) {
    // This should never happen...
    return new BaseApiTracer();
  }
  return new GoldenSignalsMetricsTracer(metricsRecorder, context);
}

Then both newTracer methods can call this helper.

Comment on lines +76 to +78
ApiTracer actual = tracerFactory.newTracer(mock(ApiTracer.class), methodLevelTracerContext);

assertThat(actual).isInstanceOf(GoldenSignalsMetricsTracer.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This test correctly checks that a GoldenSignalsMetricsTracer is created, but it doesn't verify that the merge method was actually called on the clientLevelTracerContext. To make the test more robust, you should add a verification step using Mockito.verify().

You'll also need to add the following import:
import static org.mockito.Mockito.verify;

    ApiTracer actual = tracerFactory.newTracer(mock(ApiTracer.class), methodLevelTracerContext);

    verify(clientLevelTracerContext).merge(methodLevelTracerContext);
    assertThat(actual).isInstanceOf(GoldenSignalsMetricsTracer.class);

}

@Test
void newTracer1_createsBaseTracer_ifMetricsRecorderIsNull() {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The test name newTracer1_createsBaseTracer_ifMetricsRecorderIsNull is a bit confusing due to the 1 suffix. To improve clarity and distinguish it from the existing test for the other newTracer overload (newTracer_createsBaseTracer_ifMetricsRecorderIsNull), consider a more descriptive name like newTracerWithContext_createsBaseTracer_ifMetricsRecorderIsNull.

  void newTracerWithContext_createsBaseTracer_ifMetricsRecorderIsNull() {

@sonarqubecloud
Copy link

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed for 'java_showcase_integration_tests'

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

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

Labels

size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants