@@ -9,12 +9,14 @@ import (
99
1010type clientMetricsConfig struct {
1111 counterOpts counterOptions
12- // clientHandledHistogram can be nil.
13- clientHandledHistogram * prometheus.HistogramVec
14- // clientStreamRecvHistogram can be nil.
15- clientStreamRecvHistogram * prometheus.HistogramVec
16- // clientStreamSendHistogram can be nil.
17- clientStreamSendHistogram * prometheus.HistogramVec
12+ // clientHandledHistogramFn can be nil.
13+ clientHandledHistogramFn func () * prometheus.HistogramVec
14+ // clientStreamRecvHistogramFn can be nil.
15+ clientStreamRecvHistogramFn func () * prometheus.HistogramVec
16+ // clientStreamSendHistogramFn can be nil.
17+ clientStreamSendHistogramFn func () * prometheus.HistogramVec
18+ // contextLabels defines the names of dynamic labels to be extracted from context
19+ contextLabels []string
1820}
1921
2022type ClientMetricsOption func (* clientMetricsConfig )
@@ -35,43 +37,67 @@ func WithClientCounterOptions(opts ...CounterOption) ClientMetricsOption {
3537// Histogram metrics can be very expensive for Prometheus to retain and query.
3638func WithClientHandlingTimeHistogram (opts ... HistogramOption ) ClientMetricsOption {
3739 return func (o * clientMetricsConfig ) {
38- o .clientHandledHistogram = prometheus .NewHistogramVec (
39- histogramOptions (opts ).apply (& prometheus.HistogramOpts {
40- Name : "grpc_client_handling_seconds" ,
41- Help : "Histogram of response latency (seconds) of the gRPC until it is finished by the application." ,
42- Buckets : prometheus .DefBuckets ,
43- }),
44- []string {"grpc_type" , "grpc_service" , "grpc_method" },
45- )
40+ o .clientHandledHistogramFn = func () * prometheus.HistogramVec {
41+ defaultLabels := []string {"grpc_type" , "grpc_service" , "grpc_method" }
42+ allLabels := append (defaultLabels , o .contextLabels ... )
43+
44+ return prometheus .NewHistogramVec (
45+ histogramOptions (opts ).apply (& prometheus.HistogramOpts {
46+ Name : "grpc_client_handling_seconds" ,
47+ Help : "Histogram of response latency (seconds) of the gRPC until it is finished by the application." ,
48+ Buckets : prometheus .DefBuckets ,
49+ }),
50+ allLabels ,
51+ )
52+ }
4653 }
4754}
4855
4956// WithClientStreamRecvHistogram turns on recording of single message receive time of streaming RPCs.
5057// Histogram metrics can be very expensive for Prometheus to retain and query.
5158func WithClientStreamRecvHistogram (opts ... HistogramOption ) ClientMetricsOption {
5259 return func (o * clientMetricsConfig ) {
53- o .clientStreamRecvHistogram = prometheus .NewHistogramVec (
54- histogramOptions (opts ).apply (& prometheus.HistogramOpts {
55- Name : "grpc_client_msg_recv_handling_seconds" ,
56- Help : "Histogram of response latency (seconds) of the gRPC single message receive." ,
57- Buckets : prometheus .DefBuckets ,
58- }),
59- []string {"grpc_type" , "grpc_service" , "grpc_method" },
60- )
60+ o .clientStreamRecvHistogramFn = func () * prometheus.HistogramVec {
61+ defaultLabels := []string {"grpc_type" , "grpc_service" , "grpc_method" }
62+ allLabels := append (defaultLabels , o .contextLabels ... )
63+
64+ return prometheus .NewHistogramVec (
65+ histogramOptions (opts ).apply (& prometheus.HistogramOpts {
66+ Name : "grpc_client_msg_recv_handling_seconds" ,
67+ Help : "Histogram of response latency (seconds) of the gRPC single message receive." ,
68+ Buckets : prometheus .DefBuckets ,
69+ }),
70+ allLabels ,
71+ )
72+ }
6173 }
6274}
6375
6476// WithClientStreamSendHistogram turns on recording of single message send time of streaming RPCs.
6577// Histogram metrics can be very expensive for Prometheus to retain and query.
6678func WithClientStreamSendHistogram (opts ... HistogramOption ) ClientMetricsOption {
6779 return func (o * clientMetricsConfig ) {
68- o .clientStreamSendHistogram = prometheus .NewHistogramVec (
69- histogramOptions (opts ).apply (& prometheus.HistogramOpts {
70- Name : "grpc_client_msg_send_handling_seconds" ,
71- Help : "Histogram of response latency (seconds) of the gRPC single message send." ,
72- Buckets : prometheus .DefBuckets ,
73- }),
74- []string {"grpc_type" , "grpc_service" , "grpc_method" },
75- )
80+ o .clientStreamSendHistogramFn = func () * prometheus.HistogramVec {
81+ defaultLabels := []string {"grpc_type" , "grpc_service" , "grpc_method" }
82+ allLabels := append (defaultLabels , o .contextLabels ... )
83+
84+ return prometheus .NewHistogramVec (
85+ histogramOptions (opts ).apply (& prometheus.HistogramOpts {
86+ Name : "grpc_client_msg_send_handling_seconds" ,
87+ Help : "Histogram of response latency (seconds) of the gRPC single message send." ,
88+ Buckets : prometheus .DefBuckets ,
89+ }),
90+ allLabels ,
91+ )
92+ }
93+ }
94+ }
95+
96+ // WithClientContextLabels configures the server metrics to include dynamic labels extracted from context.
97+ // The provided label names will be added to all server metrics as dynamic labels.
98+ // Use WithLabelsFromContext in the interceptor options to specify how to extract these labels from context.
99+ func WithClientContextLabels (labelNames ... string ) ClientMetricsOption {
100+ return func (o * clientMetricsConfig ) {
101+ o .contextLabels = labelNames
76102 }
77103}
0 commit comments