Skip to content

Conversation

ArtARTs36
Copy link
Contributor

@ArtARTs36 ArtARTs36 commented Sep 4, 2025

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

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

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
Copy link
Contributor Author

ArtARTs36 commented Sep 5, 2025

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
Collaborator

Good enough for me, it looks like a flaky test

@johanbrandhorst
Copy link
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants