File: client_options.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-middleware 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,464 kB
  • sloc: makefile: 107; sh: 9
file content (77 lines) | stat: -rw-r--r-- 2,822 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package prometheus

import (
	"github.com/prometheus/client_golang/prometheus"
)

type clientMetricsConfig struct {
	counterOpts counterOptions
	// clientHandledHistogram can be nil.
	clientHandledHistogram *prometheus.HistogramVec
	// clientStreamRecvHistogram can be nil.
	clientStreamRecvHistogram *prometheus.HistogramVec
	// clientStreamSendHistogram can be nil.
	clientStreamSendHistogram *prometheus.HistogramVec
}

type ClientMetricsOption func(*clientMetricsConfig)

func (c *clientMetricsConfig) apply(opts []ClientMetricsOption) {
	for _, o := range opts {
		o(c)
	}
}

func WithClientCounterOptions(opts ...CounterOption) ClientMetricsOption {
	return func(o *clientMetricsConfig) {
		o.counterOpts = opts
	}
}

// WithClientHandlingTimeHistogram turns on recording of handling time of RPCs.
// Histogram metrics can be very expensive for Prometheus to retain and query.
func WithClientHandlingTimeHistogram(opts ...HistogramOption) ClientMetricsOption {
	return func(o *clientMetricsConfig) {
		o.clientHandledHistogram = prometheus.NewHistogramVec(
			histogramOptions(opts).apply(prometheus.HistogramOpts{
				Name:    "grpc_client_handling_seconds",
				Help:    "Histogram of response latency (seconds) of the gRPC until it is finished by the application.",
				Buckets: prometheus.DefBuckets,
			}),
			[]string{"grpc_type", "grpc_service", "grpc_method"},
		)
	}
}

// WithClientStreamRecvHistogram turns on recording of single message receive time of streaming RPCs.
// Histogram metrics can be very expensive for Prometheus to retain and query.
func WithClientStreamRecvHistogram(opts ...HistogramOption) ClientMetricsOption {
	return func(o *clientMetricsConfig) {
		o.clientStreamRecvHistogram = prometheus.NewHistogramVec(
			histogramOptions(opts).apply(prometheus.HistogramOpts{
				Name:    "grpc_client_msg_recv_handling_seconds",
				Help:    "Histogram of response latency (seconds) of the gRPC single message receive.",
				Buckets: prometheus.DefBuckets,
			}),
			[]string{"grpc_type", "grpc_service", "grpc_method"},
		)
	}
}

// WithClientStreamSendHistogram turns on recording of single message send time of streaming RPCs.
// Histogram metrics can be very expensive for Prometheus to retain and query.
func WithClientStreamSendHistogram(opts ...HistogramOption) ClientMetricsOption {
	return func(o *clientMetricsConfig) {
		o.clientStreamSendHistogram = prometheus.NewHistogramVec(
			histogramOptions(opts).apply(prometheus.HistogramOpts{
				Name:    "grpc_client_msg_send_handling_seconds",
				Help:    "Histogram of response latency (seconds) of the gRPC single message send.",
				Buckets: prometheus.DefBuckets,
			}),
			[]string{"grpc_type", "grpc_service", "grpc_method"},
		)
	}
}