|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | +) |
| 6 | + |
| 7 | +const ( |
| 8 | + metricsNamespace = "cloudflared" |
| 9 | + rpcSubsystem = "rpc" |
| 10 | +) |
| 11 | + |
| 12 | +// CloudflaredServer operation labels |
| 13 | +// CloudflaredServer is an extension of SessionManager with additional methods, but it's helpful |
| 14 | +// to visualize it separately in the metrics since they are technically different client/servers. |
| 15 | +const ( |
| 16 | + Cloudflared = "cloudflared" |
| 17 | +) |
| 18 | + |
| 19 | +// ConfigurationManager operation labels |
| 20 | +const ( |
| 21 | + ConfigurationManager = "config" |
| 22 | + |
| 23 | + OperationUpdateConfiguration = "update_configuration" |
| 24 | +) |
| 25 | + |
| 26 | +// SessionManager operation labels |
| 27 | +const ( |
| 28 | + SessionManager = "session" |
| 29 | + |
| 30 | + OperationRegisterUdpSession = "register_udp_session" |
| 31 | + OperationUnregisterUdpSession = "unregister_udp_session" |
| 32 | +) |
| 33 | + |
| 34 | +// RegistrationServer operation labels |
| 35 | +const ( |
| 36 | + Registration = "registration" |
| 37 | + |
| 38 | + OperationRegisterConnection = "register_connection" |
| 39 | + OperationUnregisterConnection = "unregister_connection" |
| 40 | + OperationUpdateLocalConfiguration = "update_local_configuration" |
| 41 | +) |
| 42 | + |
| 43 | +type rpcMetrics struct { |
| 44 | + serverOperations *prometheus.CounterVec |
| 45 | + serverFailures *prometheus.CounterVec |
| 46 | + serverOperationsLatency *prometheus.HistogramVec |
| 47 | + |
| 48 | + ClientOperations *prometheus.CounterVec |
| 49 | + ClientFailures *prometheus.CounterVec |
| 50 | + ClientOperationsLatency *prometheus.HistogramVec |
| 51 | +} |
| 52 | + |
| 53 | +var CapnpMetrics *rpcMetrics = &rpcMetrics{ |
| 54 | + serverOperations: prometheus.NewCounterVec( |
| 55 | + prometheus.CounterOpts{ |
| 56 | + Namespace: metricsNamespace, |
| 57 | + Subsystem: rpcSubsystem, |
| 58 | + Name: "server_operations", |
| 59 | + Help: "Number of rpc methods by handler served", |
| 60 | + }, |
| 61 | + []string{"handler", "method"}, |
| 62 | + ), |
| 63 | + serverFailures: prometheus.NewCounterVec( |
| 64 | + prometheus.CounterOpts{ |
| 65 | + Namespace: metricsNamespace, |
| 66 | + Subsystem: rpcSubsystem, |
| 67 | + Name: "server_failures", |
| 68 | + Help: "Number of rpc methods failures by handler served", |
| 69 | + }, |
| 70 | + []string{"handler", "method"}, |
| 71 | + ), |
| 72 | + serverOperationsLatency: prometheus.NewHistogramVec( |
| 73 | + prometheus.HistogramOpts{ |
| 74 | + Namespace: metricsNamespace, |
| 75 | + Subsystem: rpcSubsystem, |
| 76 | + Name: "server_latency_secs", |
| 77 | + Help: "Latency of rpc methods by handler served", |
| 78 | + // Bucket starts at 50ms, each bucket grows by a factor of 3, up to 5 buckets and is expressed as seconds: |
| 79 | + // 50ms, 150ms, 450ms, 1350ms, 4050ms |
| 80 | + Buckets: prometheus.ExponentialBuckets(0.05, 3, 5), |
| 81 | + }, |
| 82 | + []string{"handler", "method"}, |
| 83 | + ), |
| 84 | + ClientOperations: prometheus.NewCounterVec( |
| 85 | + prometheus.CounterOpts{ |
| 86 | + Namespace: metricsNamespace, |
| 87 | + Subsystem: rpcSubsystem, |
| 88 | + Name: "client_operations", |
| 89 | + Help: "Number of rpc methods by handler requested", |
| 90 | + }, |
| 91 | + []string{"handler", "method"}, |
| 92 | + ), |
| 93 | + ClientFailures: prometheus.NewCounterVec( |
| 94 | + prometheus.CounterOpts{ |
| 95 | + Namespace: metricsNamespace, |
| 96 | + Subsystem: rpcSubsystem, |
| 97 | + Name: "client_failures", |
| 98 | + Help: "Number of rpc method failures by handler requested", |
| 99 | + }, |
| 100 | + []string{"handler", "method"}, |
| 101 | + ), |
| 102 | + ClientOperationsLatency: prometheus.NewHistogramVec( |
| 103 | + prometheus.HistogramOpts{ |
| 104 | + Namespace: metricsNamespace, |
| 105 | + Subsystem: rpcSubsystem, |
| 106 | + Name: "client_latency_secs", |
| 107 | + Help: "Latency of rpc methods by handler requested", |
| 108 | + // Bucket starts at 50ms, each bucket grows by a factor of 3, up to 5 buckets and is expressed as seconds: |
| 109 | + // 50ms, 150ms, 450ms, 1350ms, 4050ms |
| 110 | + Buckets: prometheus.ExponentialBuckets(0.05, 3, 5), |
| 111 | + }, |
| 112 | + []string{"handler", "method"}, |
| 113 | + ), |
| 114 | +} |
| 115 | + |
| 116 | +func ObserveServerHandler(inner func() error, handler, method string) error { |
| 117 | + defer CapnpMetrics.serverOperations.WithLabelValues(handler, method).Inc() |
| 118 | + timer := prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) { |
| 119 | + CapnpMetrics.serverOperationsLatency.WithLabelValues(handler, method).Observe(s) |
| 120 | + })) |
| 121 | + defer timer.ObserveDuration() |
| 122 | + |
| 123 | + err := inner() |
| 124 | + if err != nil { |
| 125 | + CapnpMetrics.serverFailures.WithLabelValues(handler, method).Inc() |
| 126 | + } |
| 127 | + return err |
| 128 | +} |
| 129 | + |
| 130 | +func NewClientOperationLatencyObserver(server string, method string) *prometheus.Timer { |
| 131 | + return prometheus.NewTimer(prometheus.ObserverFunc(func(s float64) { |
| 132 | + CapnpMetrics.ClientOperationsLatency.WithLabelValues(server, method).Observe(s) |
| 133 | + })) |
| 134 | +} |
| 135 | + |
| 136 | +func init() { |
| 137 | + prometheus.MustRegister(CapnpMetrics.serverOperations) |
| 138 | + prometheus.MustRegister(CapnpMetrics.serverFailures) |
| 139 | + prometheus.MustRegister(CapnpMetrics.serverOperationsLatency) |
| 140 | + prometheus.MustRegister(CapnpMetrics.ClientOperations) |
| 141 | + prometheus.MustRegister(CapnpMetrics.ClientFailures) |
| 142 | + prometheus.MustRegister(CapnpMetrics.ClientOperationsLatency) |
| 143 | +} |
0 commit comments