Skip to content

feat(prometheus): add ContextLabels to ClientMetrics - #798

Merged
johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
ArtARTs36:main
Sep 5, 2025
Merged

feat(prometheus): add ContextLabels to ClientMetrics#798
johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
ArtARTs36:main

Conversation

@ArtARTs36

@ArtARTs36 ArtARTs36 commented Sep 4, 2025

Copy link
Copy Markdown
Contributor

This PR implements #783 and adds methods for adding labels from context.

Changes

  • Add WithClientContextLabels option to ClientMetrics, similar WithContextLabels
  • Changed internal clientMetricsConfig to retain the ability to pass options WithContextLabels, WithClientHandlingTimeHistogram, WithClientStreamRecvHistogram, WithClientStreamSendHistogram in any order. Histograms that are created in options must receive label names that are also created in options.

Verification

  1. Init ClientMetrics with label names
clMetrics := grpcprom.NewClientMetrics(grpcprom.WithClientContextLabels("user_id"))
  1. Create gRPC client with interceptor and WithLabelsFromContext
labelsFromContext := func(ctx context.Context) prometheus.Labels {
	return prometheus.Labels{"user_id": "admin"}
}
client, err := grpc.NewClient(targetGRPCAddr, grpc.WithChainUnaryInterceptor(
	clMetrics.UnaryClientInterceptor(
		grpcprom.WithLabelsFromContext(labelsFromContext), 
	)))
  1. Send request and given new label in counter
grpc_client_handled_total{grpc_code="OK",grpc_method="Ping",grpc_service="testing.testpb.v1.TestService",grpc_type="unary",user_id="admin"} 12

@johanbrandhorst johanbrandhorst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This looks great, thank you! I'm not sure about the test failure, that doesn't look like it has anything to do with this PR, does it fail locally for you too?

@ArtARTs36

ArtARTs36 commented Sep 5, 2025

Copy link
Copy Markdown
Contributor Author

This passed for me locally. I suggest restarting this in GitHub Actions.
image

@johanbrandhorst
johanbrandhorst merged commit 2dc9821 into grpc-ecosystem:main Sep 5, 2025
4 of 5 checks passed
@johanbrandhorst

Copy link
Copy Markdown
Collaborator

Good enough for me, it looks like a flaky test

@johanbrandhorst

Copy link
Copy Markdown
Collaborator

Thanks for your contribution!

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

Labels

None yet

2 participants