Skip to content

Handle null peerName in GrpcClientObservationConvention - #7381

Merged
jonatan-ivanov merged 1 commit into
micrometer-metrics:1.16.xfrom
GGGGGHT:fix/grpc-peer-name-npe
Apr 7, 2026
Merged

jonatan-ivanov merged 1 commit into
micrometer-metrics:1.16.xfrom
GGGGGHT:fix/grpc-peer-name-npe

Conversation

@GGGGGHT

@GGGGGHT GGGGGHT commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

When NameResolver.getServiceAuthority() returns empty string, the ObservationGrpcClientInterceptor silently catches the URISyntaxException, leaving peerName as null. This caused NPE when
DefaultGrpcClientObservationConvention.getLowCardinalityKeyValues() passed null to ImmutableKeyValue.requireNonNull().

Fix: Add null check for peerName with "UNKNOWN" fallback value, consistent with how other fields (statusCode, peerPort, method, service) are already handled.

Issue: micrometer-metrics/micrometer#7380

@GGGGGHT
GGGGGHT force-pushed the fix/grpc-peer-name-npe branch 2 times, most recently from a2db9f0 to 32a3e7c Compare April 6, 2026 09:18

@shakuzen shakuzen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The change looks good for fixing the issue. I think we should also update the nullability of peerName and the corresponding methods to be accurate, which may have helped catch this. Let's also add a test that would have caught this. Perhaps something like:

      @Test
      void nullPeerNameShouldBeHandledGracefully() {
          DefaultGrpcClientObservationConvention convention = new DefaultGrpcClientObservationConvention();
          GrpcClientObservationContext context = new GrpcClientObservationContext((carrier, key, value) -> {});
          context.setMethodType(MethodDescriptor.MethodType.UNARY);

          // Context is intentionally left without a peerName set
          KeyValues keyValues = convention.getLowCardinalityKeyValues(context);
          assertThat(keyValues).contains(KeyValue.of("net.peer.name", "UNKNOWN"));
      }

@GGGGGHT
GGGGGHT force-pushed the fix/grpc-peer-name-npe branch from c693425 to 5894e1d Compare April 6, 2026 15:49
@jonatan-ivanov jonatan-ivanov changed the title fix: handle null peerName in GrpcClientObservationConvention Handle null peerName in GrpcClientObservationConvention Apr 6, 2026
@jonatan-ivanov

Copy link
Copy Markdown
Member

I think we should also update the nullability of peerName and the corresponding methods to be accurate

I fixed this in 77fffaa.

@jonatan-ivanov

Copy link
Copy Markdown
Member

If we want to fix this in 1.15.x, I think we need to repurpose this PR: a bunch of other fields were fixed with the JSpecify migration in 1.16.x: e45501f#diff-da7cbbc6ec553a89e502113384a8ab257c79d8248e3ff0725173c52960f4f865 both for the client and the server conventions and contexts. This means that the test added in this PR fails on 1.15.x so we either rebase and merge in 1.16.x ~as-is or fix a lot of other things in 1.15.x and add tests.

When NameResolver.getServiceAuthority() returns empty string, the
ObservationGrpcClientInterceptor silently catches
URISyntaxException, leaving peerName null. This caused NPE when
DefaultGrpcClientObservationConvention.getLowCardinalityKeyValues()
passed null to ImmutableKeyValue.requireNonNull().

Fix: Add null check for peerName with "UNKNOWN" fallback value,
consistent with how other fields (statusCode, peerPort, method, service)
are already handled.

Closes micrometer-metricsgh-7380

Co-authored-by: Jonatan Ivanov <[email protected]>
Signed-off-by: zheng2.wang <[email protected]>
@jonatan-ivanov
jonatan-ivanov force-pushed the fix/grpc-peer-name-npe branch from 77fffaa to a2a7b72 Compare April 6, 2026 22:49
@jonatan-ivanov
jonatan-ivanov changed the base branch from main to 1.16.x April 6, 2026 22:49
@jonatan-ivanov

Copy link
Copy Markdown
Member

I rebased this on top of 1.16.x, I think it's mergeable to 1.16.x, as for 1.15.x, I feel that's a slightly bigger change which I think we should handle in a separate PR.

@jonatan-ivanov
jonatan-ivanov merged commit 8c1758b into micrometer-metrics:1.16.x Apr 7, 2026
11 checks passed
@jonatan-ivanov

Copy link
Copy Markdown
Member

@GGGGGHT Thank you for the PR, this is now merged into 1.16.x, you can try this out using 1.16.5-SNAPSHOT (1.16.5 should be released in a week).

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.

ObservationGrpcClientInterceptor throws NPE when NameResolver returns empty authority

3 participants