From 9b0247595ddef0f50b2604fd0e334e2955d02ee1 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Fri, 6 Sep 2024 11:51:07 -0400 Subject: [PATCH] chore: rename prometheus metrics that are supposed to be gauge (#693) # Description As title. Closes https://github.com/microsoft/retina/issues/237. Also did a bunch of refactoring + removing unused metrics ## Related Issue If this pull request is related to any issue, please mention it here. Additionally, make sure that the issue is assigned to you before submitting this pull request. ## Checklist - [ ] I have read the [contributing documentation](https://retina.sh/docs/contributing). - [ ] I signed and signed-off the commits (`git commit -S -s ...`). See [this documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification) on signing commits. - [ ] I have correctly attributed the author(s) of the code. - [ ] I have tested the changes locally. - [ ] I have followed the project's style guidelines. - [ ] I have updated the documentation, if necessary. - [ ] I have added tests, if applicable. ## Screenshots (if applicable) or Testing Completed Please add any relevant screenshots or GIFs to showcase the changes made. ## Additional Notes Add any additional notes or context about the pull request here. --- Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more information on how to contribute to this project. --- pkg/metrics/interfaces.go | 6 +- pkg/metrics/metrics.go | 76 +++++++------ pkg/metrics/metrics_test.go | 2 +- pkg/metrics/mock_types.go | 102 +++++++++--------- pkg/metrics/types.go | 86 +++++++-------- pkg/module/metrics/dns.go | 2 +- pkg/module/metrics/dns_test.go | 2 +- pkg/module/metrics/drops.go | 10 +- pkg/module/metrics/drops_test.go | 2 +- pkg/module/metrics/forward.go | 10 +- pkg/module/metrics/forward_test.go | 2 +- pkg/module/metrics/latency.go | 32 +++--- pkg/module/metrics/latency_test.go | 14 +-- pkg/module/metrics/tcpflags.go | 2 +- pkg/module/metrics/tcpflags_test.go | 2 +- pkg/module/metrics/tcpretrans.go | 2 +- pkg/module/metrics/types.go | 18 ++-- pkg/plugin/dns/dns_linux_test.go | 4 +- pkg/plugin/dns/types_linux.go | 2 +- pkg/plugin/dropreason/dropreason_linux.go | 4 +- .../dropreason/dropreason_linux_test.go | 4 +- .../infiniband/infiniband_stats_linux.go | 4 +- .../infiniband/infiniband_stats_linux_test.go | 10 +- pkg/plugin/linuxutil/ethtool_stats_linux.go | 2 +- .../linuxutil/ethtool_stats_linux_test.go | 26 +++-- pkg/plugin/linuxutil/netstat_stats_linux.go | 8 +- .../packetforward/packetforward_linux.go | 8 +- .../packetparser/packetparser_linux_test.go | 2 +- .../windows/hnsstats/hnsstats_windows.go | 52 ++++----- pkg/utils/metric_names.go | 58 +++++----- 30 files changed, 271 insertions(+), 283 deletions(-) diff --git a/pkg/metrics/interfaces.go b/pkg/metrics/interfaces.go index 6744d17a22..ddb9a07a5b 100644 --- a/pkg/metrics/interfaces.go +++ b/pkg/metrics/interfaces.go @@ -9,17 +9,17 @@ import ( //go:generate go run go.uber.org/mock/mockgen@v0.4.0 -source=interfaces.go -destination=mock_types.go -package=metrics -type ICounterVec interface { +type CounterVec interface { WithLabelValues(lvs ...string) prometheus.Counter GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error) } -type IGaugeVec interface { +type GaugeVec interface { WithLabelValues(lvs ...string) prometheus.Gauge GetMetricWithLabelValues(lvs ...string) (prometheus.Gauge, error) } -type IHistogramVec interface { +type Histogram interface { Observe(float64) // Keep the Write method for testing purposes. Write(*dto.Metric) error diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 259b06bd6a..70a75bfc9b 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -18,29 +18,29 @@ func InitializeMetrics() { metricsLogger.Warn("Metrics already initialized. Exiting.") return } - DropCounter = exporter.CreatePrometheusGaugeVecForMetric( + DropPacketsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.DropCountTotalName, - dropCountTotalDescription, + utils.DroppedPacketsGaugeName, + dropPacketsGaugeDescription, utils.Reason, utils.Direction) - DropBytesCounter = exporter.CreatePrometheusGaugeVecForMetric( + DropBytesGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.DropBytesTotalName, - dropBytesTotalDescription, + utils.DropBytesGaugeName, + dropBytesGaugeDescription, utils.Reason, utils.Direction) - ForwardCounter = exporter.CreatePrometheusGaugeVecForMetric( + ForwardPacketsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.ForwardCountTotalName, - forwardCountTotalDescription, + utils.ForwardPacketsGaugeName, + forwardPacketsGaugeDescription, utils.Direction) - ForwardBytesCounter = exporter.CreatePrometheusGaugeVecForMetric( + ForwardBytesGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.ForwardBytesTotalName, - forwardBytesTotalDescription, + utils.ForwardBytesGaugeName, + forwardBytesGaugeDescription, utils.Direction) - WindowsCounter = exporter.CreatePrometheusGaugeVecForMetric( + WindowsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, hnsStats, hnsStatsDescription, @@ -49,69 +49,65 @@ func InitializeMetrics() { NodeConnectivityStatusGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, utils.NodeConnectivityStatusName, - nodeConnectivityStatusDescription, + nodeConnectivityStatusGaugeDescription, utils.SourceNodeName, utils.TargetNodeName) NodeConnectivityLatencyGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, utils.NodeConnectivityLatencySecondsName, - nodeConnectivityLatencySecondsDescription, + nodeConnectivityLatencySecondsGaugeDescription, utils.SourceNodeName, utils.TargetNodeName) TCPStateGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.TcpStateGaugeName, + utils.TCPStateGaugeName, tcpStateGaugeDescription, utils.State, ) TCPConnectionRemoteGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.TcpConnectionRemoteGaugeName, + utils.TCPConnectionRemoteGaugeName, tcpConnectionRemoteGaugeDescription, utils.Address, utils.Port, ) - TCPConnectionStats = exporter.CreatePrometheusGaugeVecForMetric( + TCPConnectionStatsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.TcpConnectionStatsName, - tcpConnectionStatsDescription, + utils.TCPConnectionStatsName, + tcpConnectionStatsGaugeDescription, utils.StatName, ) - TCPFlagCounters = exporter.CreatePrometheusGaugeVecForMetric( + + TCPFlagGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.TcpFlagCounters, - tcpFlagCountersDescription, + utils.TCPFlagGauge, + tcpFlagGaugeDescription, utils.Direction, utils.Flag, ) // IP States - IPConnectionStats = exporter.CreatePrometheusGaugeVecForMetric( + IPConnectionStatsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.IpConnectionStatsName, - ipConnectionStatsDescription, + utils.IPConnectionStatsName, + ipConnectionStatsGaugeDescription, utils.StatName, ) // UDP Stats - UDPConnectionStats = exporter.CreatePrometheusGaugeVecForMetric( + UDPConnectionStatsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, - utils.UdpConnectionStatsName, - udpConnectionStatsDescription, + utils.UDPConnectionStatsName, + udpConnectionStatsGaugeDescription, utils.StatName, ) - UDPActiveSocketsCounter = exporter.CreatePrometheusGaugeVecForMetric( - exporter.DefaultRegistry, - utils.UdpActiveSocketsCounterName, - udpActiveSocketsCounterDescription, - ) // Interface Stats - InterfaceStats = exporter.CreatePrometheusGaugeVecForMetric( + InterfaceStatsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, utils.InterfaceStatsName, - interfaceStatsDescription, + interfaceStatsGaugeDescription, utils.InterfaceName, utils.StatName, ) @@ -148,19 +144,19 @@ func InitializeMetrics() { ) // InfiniBand Metrics - InfinibandCounterStats = exporter.CreatePrometheusGaugeVecForMetric( + InfinibandStatsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, utils.InfinibandCounterStatsName, - infinibandCounterStatsDescription, + infinibandStatsGaugeDescription, utils.StatName, utils.Device, utils.Port, ) - InfinibandStatusParams = exporter.CreatePrometheusGaugeVecForMetric( + InfinibandStatusParamsGauge = exporter.CreatePrometheusGaugeVecForMetric( exporter.DefaultRegistry, utils.InfinibandStatusParamsName, - infinibandStatusParamsDescription, + infinibandStatusParamsGaugeDescription, utils.StatName, utils.InterfaceName, ) diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 60a3317b7e..10a8d8012d 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -15,7 +15,7 @@ func TestInitialization_FirstInit(t *testing.T) { InitializeMetrics() // All metrics should be initialized. - objs := []interface{}{DropCounter, DropBytesCounter, ForwardBytesCounter, ForwardCounter, NodeConnectivityStatusGauge, NodeConnectivityLatencyGauge, PluginManagerFailedToReconcileCounter} + objs := []interface{}{DropPacketsGauge, DropBytesGauge, ForwardBytesGauge, ForwardPacketsGauge, NodeConnectivityStatusGauge, NodeConnectivityLatencyGauge, PluginManagerFailedToReconcileCounter} for _, obj := range objs { if obj == nil { t.Fatalf("Expected all metrics to be initialized") diff --git a/pkg/metrics/mock_types.go b/pkg/metrics/mock_types.go index f4423a8082..5d80bb22da 100644 --- a/pkg/metrics/mock_types.go +++ b/pkg/metrics/mock_types.go @@ -17,31 +17,31 @@ import ( gomock "go.uber.org/mock/gomock" ) -// MockICounterVec is a mock of ICounterVec interface. -type MockICounterVec struct { +// MockCounterVec is a mock of CounterVec interface. +type MockCounterVec struct { ctrl *gomock.Controller - recorder *MockICounterVecMockRecorder + recorder *MockCounterVecMockRecorder } -// MockICounterVecMockRecorder is the mock recorder for MockICounterVec. -type MockICounterVecMockRecorder struct { - mock *MockICounterVec +// MockCounterVecMockRecorder is the mock recorder for MockCounterVec. +type MockCounterVecMockRecorder struct { + mock *MockCounterVec } -// NewMockICounterVec creates a new mock instance. -func NewMockICounterVec(ctrl *gomock.Controller) *MockICounterVec { - mock := &MockICounterVec{ctrl: ctrl} - mock.recorder = &MockICounterVecMockRecorder{mock} +// NewMockCounterVec creates a new mock instance. +func NewMockCounterVec(ctrl *gomock.Controller) *MockCounterVec { + mock := &MockCounterVec{ctrl: ctrl} + mock.recorder = &MockCounterVecMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockICounterVec) EXPECT() *MockICounterVecMockRecorder { +func (m *MockCounterVec) EXPECT() *MockCounterVecMockRecorder { return m.recorder } // GetMetricWithLabelValues mocks base method. -func (m *MockICounterVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error) { +func (m *MockCounterVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Counter, error) { m.ctrl.T.Helper() varargs := []any{} for _, a := range lvs { @@ -54,13 +54,13 @@ func (m *MockICounterVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Co } // GetMetricWithLabelValues indicates an expected call of GetMetricWithLabelValues. -func (mr *MockICounterVecMockRecorder) GetMetricWithLabelValues(lvs ...any) *gomock.Call { +func (mr *MockCounterVecMockRecorder) GetMetricWithLabelValues(lvs ...any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetricWithLabelValues", reflect.TypeOf((*MockICounterVec)(nil).GetMetricWithLabelValues), lvs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetricWithLabelValues", reflect.TypeOf((*MockCounterVec)(nil).GetMetricWithLabelValues), lvs...) } // WithLabelValues mocks base method. -func (m *MockICounterVec) WithLabelValues(lvs ...string) prometheus.Counter { +func (m *MockCounterVec) WithLabelValues(lvs ...string) prometheus.Counter { m.ctrl.T.Helper() varargs := []any{} for _, a := range lvs { @@ -72,36 +72,36 @@ func (m *MockICounterVec) WithLabelValues(lvs ...string) prometheus.Counter { } // WithLabelValues indicates an expected call of WithLabelValues. -func (mr *MockICounterVecMockRecorder) WithLabelValues(lvs ...any) *gomock.Call { +func (mr *MockCounterVecMockRecorder) WithLabelValues(lvs ...any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithLabelValues", reflect.TypeOf((*MockICounterVec)(nil).WithLabelValues), lvs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithLabelValues", reflect.TypeOf((*MockCounterVec)(nil).WithLabelValues), lvs...) } -// MockIGaugeVec is a mock of IGaugeVec interface. -type MockIGaugeVec struct { +// MockGaugeVec is a mock of GaugeVec interface. +type MockGaugeVec struct { ctrl *gomock.Controller - recorder *MockIGaugeVecMockRecorder + recorder *MockGaugeVecMockRecorder } -// MockIGaugeVecMockRecorder is the mock recorder for MockIGaugeVec. -type MockIGaugeVecMockRecorder struct { - mock *MockIGaugeVec +// MockGaugeVecMockRecorder is the mock recorder for MockGaugeVec. +type MockGaugeVecMockRecorder struct { + mock *MockGaugeVec } -// NewMockIGaugeVec creates a new mock instance. -func NewMockIGaugeVec(ctrl *gomock.Controller) *MockIGaugeVec { - mock := &MockIGaugeVec{ctrl: ctrl} - mock.recorder = &MockIGaugeVecMockRecorder{mock} +// NewMockGaugeVec creates a new mock instance. +func NewMockGaugeVec(ctrl *gomock.Controller) *MockGaugeVec { + mock := &MockGaugeVec{ctrl: ctrl} + mock.recorder = &MockGaugeVecMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockIGaugeVec) EXPECT() *MockIGaugeVecMockRecorder { +func (m *MockGaugeVec) EXPECT() *MockGaugeVecMockRecorder { return m.recorder } // GetMetricWithLabelValues mocks base method. -func (m *MockIGaugeVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Gauge, error) { +func (m *MockGaugeVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Gauge, error) { m.ctrl.T.Helper() varargs := []any{} for _, a := range lvs { @@ -114,13 +114,13 @@ func (m *MockIGaugeVec) GetMetricWithLabelValues(lvs ...string) (prometheus.Gaug } // GetMetricWithLabelValues indicates an expected call of GetMetricWithLabelValues. -func (mr *MockIGaugeVecMockRecorder) GetMetricWithLabelValues(lvs ...any) *gomock.Call { +func (mr *MockGaugeVecMockRecorder) GetMetricWithLabelValues(lvs ...any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetricWithLabelValues", reflect.TypeOf((*MockIGaugeVec)(nil).GetMetricWithLabelValues), lvs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetricWithLabelValues", reflect.TypeOf((*MockGaugeVec)(nil).GetMetricWithLabelValues), lvs...) } // WithLabelValues mocks base method. -func (m *MockIGaugeVec) WithLabelValues(lvs ...string) prometheus.Gauge { +func (m *MockGaugeVec) WithLabelValues(lvs ...string) prometheus.Gauge { m.ctrl.T.Helper() varargs := []any{} for _, a := range lvs { @@ -132,48 +132,48 @@ func (m *MockIGaugeVec) WithLabelValues(lvs ...string) prometheus.Gauge { } // WithLabelValues indicates an expected call of WithLabelValues. -func (mr *MockIGaugeVecMockRecorder) WithLabelValues(lvs ...any) *gomock.Call { +func (mr *MockGaugeVecMockRecorder) WithLabelValues(lvs ...any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithLabelValues", reflect.TypeOf((*MockIGaugeVec)(nil).WithLabelValues), lvs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithLabelValues", reflect.TypeOf((*MockGaugeVec)(nil).WithLabelValues), lvs...) } -// MockIHistogramVec is a mock of IHistogramVec interface. -type MockIHistogramVec struct { +// MockHistogram is a mock of Histogram interface. +type MockHistogram struct { ctrl *gomock.Controller - recorder *MockIHistogramVecMockRecorder + recorder *MockHistogramMockRecorder } -// MockIHistogramVecMockRecorder is the mock recorder for MockIHistogramVec. -type MockIHistogramVecMockRecorder struct { - mock *MockIHistogramVec +// MockHistogramMockRecorder is the mock recorder for MockHistogram. +type MockHistogramMockRecorder struct { + mock *MockHistogram } -// NewMockIHistogramVec creates a new mock instance. -func NewMockIHistogramVec(ctrl *gomock.Controller) *MockIHistogramVec { - mock := &MockIHistogramVec{ctrl: ctrl} - mock.recorder = &MockIHistogramVecMockRecorder{mock} +// NewMockHistogram creates a new mock instance. +func NewMockHistogram(ctrl *gomock.Controller) *MockHistogram { + mock := &MockHistogram{ctrl: ctrl} + mock.recorder = &MockHistogramMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockIHistogramVec) EXPECT() *MockIHistogramVecMockRecorder { +func (m *MockHistogram) EXPECT() *MockHistogramMockRecorder { return m.recorder } // Observe mocks base method. -func (m *MockIHistogramVec) Observe(arg0 float64) { +func (m *MockHistogram) Observe(arg0 float64) { m.ctrl.T.Helper() m.ctrl.Call(m, "Observe", arg0) } // Observe indicates an expected call of Observe. -func (mr *MockIHistogramVecMockRecorder) Observe(arg0 any) *gomock.Call { +func (mr *MockHistogramMockRecorder) Observe(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Observe", reflect.TypeOf((*MockIHistogramVec)(nil).Observe), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Observe", reflect.TypeOf((*MockHistogram)(nil).Observe), arg0) } // Write mocks base method. -func (m *MockIHistogramVec) Write(arg0 *io_prometheus_client.Metric) error { +func (m *MockHistogram) Write(arg0 *io_prometheus_client.Metric) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Write", arg0) ret0, _ := ret[0].(error) @@ -181,7 +181,7 @@ func (m *MockIHistogramVec) Write(arg0 *io_prometheus_client.Metric) error { } // Write indicates an expected call of Write. -func (mr *MockIHistogramVecMockRecorder) Write(arg0 any) *gomock.Call { +func (mr *MockHistogramMockRecorder) Write(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockIHistogramVec)(nil).Write), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockHistogram)(nil).Write), arg0) } diff --git a/pkg/metrics/types.go b/pkg/metrics/types.go index d61235a970..cff5c28884 100644 --- a/pkg/metrics/types.go +++ b/pkg/metrics/types.go @@ -21,25 +21,24 @@ const ( nodeApiServerHandshakeLatencyHistName = "node_apiserver_handshake_latency_ms" // Metric Descriptions - dropCountTotalDescription = "Total dropped packets" - dropBytesTotalDescription = "Total dropped bytes" - forwardCountTotalDescription = "Total forwarded packets" - forwardBytesTotalDescription = "Total forwarded bytes" - nodeConnectivityStatusDescription = "The last observed status of both ICMP and HTTP connectivity between the current Cilium agent and other Cilium nodes" - nodeConnectivityLatencySecondsDescription = "The last observed latency between the current Cilium agent and other Cilium nodes in seconds" - tcpStateGaugeDescription = "number of active TCP connections by state" - tcpConnectionRemoteGaugeDescription = "number of active TCP connections by remote address" - tcpConnectionStatsDescription = "TCP connections Statistics" - tcpFlagCountersDescription = "TCP counters by flag" - ipConnectionStatsDescription = "IP connections Statistics" - udpConnectionStatsDescription = "UDP connections Statistics" - udpActiveSocketsCounterDescription = "number of active UDP sockets" - interfaceStatsDescription = "Interface Statistics" - nodeApiServerHandshakeLatencyDesc = "Histogram depicting latency of the TCP handshake between nodes and Kubernetes API server measured in milliseconds" - dnsRequestCounterDescription = "DNS requests by statistics" - dnsResponseCounterDescription = "DNS responses by statistics" - infinibandCounterStatsDescription = "InfiniBand Counter Statistics" - infinibandStatusParamsDescription = "InfiniBand Status Parameters" + dropPacketsGaugeDescription = "Total dropped packets" + dropBytesGaugeDescription = "Total dropped bytes" + forwardPacketsGaugeDescription = "Total forwarded packets" + forwardBytesGaugeDescription = "Total forwarded bytes" + nodeConnectivityStatusGaugeDescription = "The last observed status of both ICMP and HTTP connectivity between the current Cilium agent and other Cilium nodes" + nodeConnectivityLatencySecondsGaugeDescription = "The last observed latency between the current Cilium agent and other Cilium nodes in seconds" + tcpStateGaugeDescription = "Number of active TCP connections by state" + tcpConnectionRemoteGaugeDescription = "Number of active TCP connections by remote address" + tcpConnectionStatsGaugeDescription = "TCP connections statistics" + tcpFlagGaugeDescription = "TCP gauges by flag" + ipConnectionStatsGaugeDescription = "IP connections statistics" + udpConnectionStatsGaugeDescription = "UDP connections statistics" + interfaceStatsGaugeDescription = "Interface statistics" + nodeAPIServerHandshakeLatencyDesc = "Histogram depicting latency of the TCP handshake between nodes and Kubernetes API server measured in milliseconds" + dnsRequestCounterDescription = "DNS requests by statistics" + dnsResponseCounterDescription = "DNS responses by statistics" + infinibandStatsGaugeDescription = "InfiniBand statistics gauge" + infinibandStatusParamsGaugeDescription = "InfiniBand Status Parameters gauge" // Control plane metrics pluginManagerFailedToReconcileCounterDescription = "Number of times the plugin manager failed to reconcile the plugins" @@ -51,46 +50,45 @@ var ( // Prevent re-initialization isInitialized bool - // Common counters across os distributions - DropCounter IGaugeVec - DropBytesCounter IGaugeVec - ForwardCounter IGaugeVec - ForwardBytesCounter IGaugeVec + // Common gauges across os distributions + DropPacketsGauge GaugeVec + DropBytesGauge GaugeVec + ForwardPacketsGauge GaugeVec + ForwardBytesGauge GaugeVec - WindowsCounter IGaugeVec + WindowsGauge GaugeVec // Common gauges across os distributions - NodeConnectivityStatusGauge IGaugeVec - NodeConnectivityLatencyGauge IGaugeVec + NodeConnectivityStatusGauge GaugeVec + NodeConnectivityLatencyGauge GaugeVec // TCP Stats - TCPStateGauge IGaugeVec - TCPConnectionRemoteGauge IGaugeVec - TCPConnectionStats IGaugeVec - TCPFlagCounters IGaugeVec + TCPStateGauge GaugeVec + TCPConnectionRemoteGauge GaugeVec + TCPConnectionStatsGauge GaugeVec + TCPFlagGauge GaugeVec // IP States - IPConnectionStats IGaugeVec + IPConnectionStatsGauge GaugeVec // UDP Stats - UDPConnectionStats IGaugeVec - UDPActiveSocketsCounter IGaugeVec + UDPConnectionStatsGauge GaugeVec // Interface Stats - InterfaceStats IGaugeVec + InterfaceStatsGauge GaugeVec metricsLogger *log.ZapLogger // Control Plane Metrics - PluginManagerFailedToReconcileCounter ICounterVec - LostEventsCounter ICounterVec + PluginManagerFailedToReconcileCounter CounterVec + LostEventsCounter CounterVec // DNS Metrics. - DNSRequestCounter ICounterVec - DNSResponseCounter ICounterVec + DNSRequestCounter CounterVec + DNSResponseCounter CounterVec - InfinibandCounterStats IGaugeVec - InfinibandStatusParams IGaugeVec + InfinibandStatsGauge GaugeVec + InfinibandStatusParamsGauge GaugeVec ) func ToPrometheusType(metric interface{}) prometheus.Collector { @@ -98,12 +96,10 @@ func ToPrometheusType(metric interface{}) prometheus.Collector { return nil } switch m := metric.(type) { - case IGaugeVec: + case GaugeVec: return m.(*prometheus.GaugeVec) - case ICounterVec: + case CounterVec: return m.(*prometheus.CounterVec) - case IHistogramVec: - return m.(prometheus.Histogram) default: metricsLogger.Error("error converting unknown metric type", zap.Any("metric", m)) return nil diff --git a/pkg/module/metrics/dns.go b/pkg/module/metrics/dns.go index 6949136804..003d14cb3d 100644 --- a/pkg/module/metrics/dns.go +++ b/pkg/module/metrics/dns.go @@ -31,7 +31,7 @@ var ( type DNSMetrics struct { baseMetricObject - dnsMetrics metricsinit.ICounterVec + dnsMetrics metricsinit.CounterVec metricName string } diff --git a/pkg/module/metrics/dns_test.go b/pkg/module/metrics/dns_test.go index 452f8400da..7e15fedf4a 100644 --- a/pkg/module/metrics/dns_test.go +++ b/pkg/module/metrics/dns_test.go @@ -302,7 +302,7 @@ func TestProcessLocalCtx(t *testing.T) { m := NewMockContextOptionsInterface(ctrl) //nolint:typecheck m.EXPECT().getLocalCtxValues(tt.input).Return(tt.output).Times(1) - mockCV := metrics.NewMockICounterVec(ctrl) + mockCV := metrics.NewMockCounterVec(ctrl) if tt.metricsUpdate { mockCV.EXPECT().WithLabelValues(tt.expectedLabels).Return(c).Times(1) } diff --git a/pkg/module/metrics/drops.go b/pkg/module/metrics/drops.go index 833f9c2d20..cf5db7d7a1 100644 --- a/pkg/module/metrics/drops.go +++ b/pkg/module/metrics/drops.go @@ -25,7 +25,7 @@ const ( type DropCountMetrics struct { baseMetricObject - dropMetric metrics.IGaugeVec + dropMetric metrics.GaugeVec metricName string } @@ -43,13 +43,13 @@ func NewDropCountMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogge func (d *DropCountMetrics) Init(metricName string) { switch metricName { - case utils.DropCountTotalName: + case utils.DroppedPacketsGaugeName: d.dropMetric = exporter.CreatePrometheusGaugeVecForMetric( exporter.AdvancedRegistry, TotalDropCountName, TotalDropCountDesc, d.getLabels()...) - case utils.DropBytesTotalName: + case utils.DropBytesGaugeName: d.dropMetric = exporter.CreatePrometheusGaugeVecForMetric( exporter.AdvancedRegistry, TotalDropBytesName, @@ -163,9 +163,9 @@ func (d *DropCountMetrics) processLocalCtxFlow(flow *v1.Flow) { func (d *DropCountMetrics) update(fl *v1.Flow, labels []string) { switch d.metricName { - case utils.DropCountTotalName: + case utils.DroppedPacketsGaugeName: d.dropMetric.WithLabelValues(labels...).Inc() - case utils.DropBytesTotalName: + case utils.DropBytesGaugeName: d.dropMetric.WithLabelValues(labels...).Add(float64(utils.PacketSize(fl))) } } diff --git a/pkg/module/metrics/drops_test.go b/pkg/module/metrics/drops_test.go index 4b9ecddaa6..bc2e9f707f 100644 --- a/pkg/module/metrics/drops_test.go +++ b/pkg/module/metrics/drops_test.go @@ -277,7 +277,7 @@ func TestNewDrop(t *testing.T) { } else { assert.NotNil(t, f, "drp[] metrics should not be nil Test Name: %s", tc.name) } - dropMock := metricsinit.NewMockIGaugeVec(ctrl) //nolint:typecheck + dropMock := metricsinit.NewMockGaugeVec(ctrl) //nolint:typecheck f.dropMetric = dropMock diff --git a/pkg/module/metrics/forward.go b/pkg/module/metrics/forward.go index e16710172a..7263ed0b7c 100644 --- a/pkg/module/metrics/forward.go +++ b/pkg/module/metrics/forward.go @@ -27,7 +27,7 @@ const ( type ForwardMetrics struct { baseMetricObject - forwardMetric metricsinit.IGaugeVec + forwardMetric metricsinit.GaugeVec // bytesMetric metricsinit.IGaugeVec metricName string } @@ -46,13 +46,13 @@ func NewForwardCountMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLo func (f *ForwardMetrics) Init(metricName string) { switch metricName { - case utils.ForwardCountTotalName: + case utils.ForwardPacketsGaugeName: f.forwardMetric = exporter.CreatePrometheusGaugeVecForMetric( exporter.AdvancedRegistry, TotalCountName, TotalCountDesc, f.getLabels()...) - case utils.ForwardBytesTotalName: + case utils.ForwardBytesGaugeName: f.forwardMetric = exporter.CreatePrometheusGaugeVecForMetric( exporter.AdvancedRegistry, TotalBytesName, @@ -163,9 +163,9 @@ func (f *ForwardMetrics) processLocalCtxFlow(flow *v1.Flow) { func (f *ForwardMetrics) update(fl *v1.Flow, labels []string) { switch f.metricName { - case utils.ForwardCountTotalName: + case utils.ForwardPacketsGaugeName: f.forwardMetric.WithLabelValues(labels...).Inc() - case utils.ForwardBytesTotalName: + case utils.ForwardBytesGaugeName: f.forwardMetric.WithLabelValues(labels...).Add(float64(utils.PacketSize(fl))) } } diff --git a/pkg/module/metrics/forward_test.go b/pkg/module/metrics/forward_test.go index ff23c78239..9d48f30577 100644 --- a/pkg/module/metrics/forward_test.go +++ b/pkg/module/metrics/forward_test.go @@ -290,7 +290,7 @@ func TestNewForward(t *testing.T) { assert.NotNil(t, f, "forward metrics should not be nil Test Name: %s", tc.name) } - forwardMock := metricsinit.NewMockIGaugeVec(ctrl) //nolint:typecheck + forwardMock := metricsinit.NewMockGaugeVec(ctrl) //nolint:typecheck f.forwardMetric = forwardMock diff --git a/pkg/module/metrics/latency.go b/pkg/module/metrics/latency.go index 0be7ac94d3..3810054d04 100644 --- a/pkg/module/metrics/latency.go +++ b/pkg/module/metrics/latency.go @@ -63,9 +63,9 @@ type LatencyMetrics struct { l *log.ZapLogger apiServerIps map[string]struct{} cache *ttlcache.Cache[key, *val] - nodeApiServerLatency metricsinit.IHistogramVec - nodeApiServerHandshakeLatency metricsinit.IHistogramVec - noResponseMetric metricsinit.ICounterVec + nodeAPIServerLatency metricsinit.Histogram + nodeAPIServerHandshakeLatency metricsinit.Histogram + noResponseMetric metricsinit.CounterVec callbackId string mu sync.RWMutex } @@ -83,8 +83,8 @@ func NewLatencyMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, } switch ctxOptions.MetricName { - case utils.NodeApiServerLatencyName: - lm.nodeApiServerLatency = exporter.CreatePrometheusHistogramWithLinearBucketsForMetric( + case utils.NodeAPIServerLatencyName: + lm.nodeAPIServerLatency = exporter.CreatePrometheusHistogramWithLinearBucketsForMetric( exporter.AdvancedRegistry, apiserverLatencyName, apiserverLatencyDesc, @@ -92,8 +92,8 @@ func NewLatencyMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, width, count, ) - case utils.NodeApiServerTcpHandshakeLatencyName: - lm.nodeApiServerHandshakeLatency = exporter.CreatePrometheusHistogramWithLinearBucketsForMetric( + case utils.NodeAPIServerTCPHandshakeLatencyName: + lm.nodeAPIServerHandshakeLatency = exporter.CreatePrometheusHistogramWithLinearBucketsForMetric( exporter.AdvancedRegistry, apiServerHandshakeLatencyName, apiServerHandshakeLatencyDesc, @@ -101,7 +101,7 @@ func NewLatencyMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, width, count, ) - case utils.NoResponseFromApiServerName: + case utils.NoResponseFromAPIServerName: lm.noResponseMetric = exporter.CreatePrometheusCounterVecForMetric( exporter.AdvancedRegistry, noResponseFromNodeAPIServerName, @@ -150,11 +150,11 @@ func (lm *LatencyMetrics) Init(metricName string) { } func (lm *LatencyMetrics) Clean() { - if lm.nodeApiServerLatency != nil { - exporter.UnregisterMetric(exporter.AdvancedRegistry, metricsinit.ToPrometheusType(lm.nodeApiServerLatency)) + if lm.nodeAPIServerLatency != nil { + exporter.UnregisterMetric(exporter.AdvancedRegistry, metricsinit.ToPrometheusType(lm.nodeAPIServerLatency)) } - if lm.nodeApiServerLatency != nil { - exporter.UnregisterMetric(exporter.AdvancedRegistry, metricsinit.ToPrometheusType(lm.nodeApiServerHandshakeLatency)) + if lm.nodeAPIServerLatency != nil { + exporter.UnregisterMetric(exporter.AdvancedRegistry, metricsinit.ToPrometheusType(lm.nodeAPIServerHandshakeLatency)) } if lm.noResponseMetric != nil { exporter.UnregisterMetric(exporter.AdvancedRegistry, metricsinit.ToPrometheusType(lm.noResponseMetric)) @@ -287,16 +287,16 @@ func (lm *LatencyMetrics) calculateLatency(f *flow.Flow) { latency := math.Round(float64(f.Time.Nanos-item.Value().t) / float64(1000000)) // Log continuous latency. - if lm.nodeApiServerLatency != nil { - lm.nodeApiServerLatency.Observe(latency) + if lm.nodeAPIServerLatency != nil { + lm.nodeAPIServerLatency.Observe(latency) } // Determine if this is the first reply packet, and if so, log handshake latency. prevFlowflags := item.Value().flags curFlowflags := f.L4.GetTCP().Flags - if lm.nodeApiServerHandshakeLatency != nil && prevFlowflags != nil && prevFlowflags.SYN && curFlowflags != nil && curFlowflags.SYN && curFlowflags.ACK { + if lm.nodeAPIServerHandshakeLatency != nil && prevFlowflags != nil && prevFlowflags.GetSYN() && curFlowflags != nil && curFlowflags.GetSYN() && curFlowflags.GetACK() { // This is the first reply packet. - lm.nodeApiServerHandshakeLatency.Observe(latency) + lm.nodeAPIServerHandshakeLatency.Observe(latency) } // Delete the entry from cache. Calculate latency for the first reply packet only. lm.cache.Delete(k) diff --git a/pkg/module/metrics/latency_test.go b/pkg/module/metrics/latency_test.go index 96bb63b06d..dc9d88827b 100644 --- a/pkg/module/metrics/latency_test.go +++ b/pkg/module/metrics/latency_test.go @@ -49,13 +49,13 @@ func TestNewLatencyMetrics(t *testing.T) { for _, option := range options[2:] { lm = NewLatencyMetrics(option, l, "") } - if lm.nodeApiServerLatency == nil { + if lm.nodeAPIServerLatency == nil { t.Errorf("LatencyMetrics.nodeApiServerLatency should be initialized") } if lm.noResponseMetric == nil { t.Errorf("LatencyMetrics.noResponseMetric should be initialized") } - if lm.nodeApiServerHandshakeLatency == nil { + if lm.nodeAPIServerHandshakeLatency == nil { t.Errorf("LatencyMetrics.nodeApiServerHandshakeLatency should be initialized") } @@ -99,18 +99,18 @@ func TestProcessFlow(t *testing.T) { lm.apiServerIps[apiSeverIp.String()] = struct{}{} // Set mock nodeApiServerLatency. - mHist := metrics.NewMockIHistogramVec(ctrl) + mHist := metrics.NewMockHistogram(ctrl) mHist.EXPECT().Observe(float64(1)).Return().Times(2) - lm.nodeApiServerLatency = mHist + lm.nodeAPIServerLatency = mHist // Set mock nodeApiServerHandshakeLatency. - mHist2 := metrics.NewMockIHistogramVec(ctrl) + mHist2 := metrics.NewMockHistogram(ctrl) mHist2.EXPECT().Observe(float64(1)).Return().Times(1) - lm.nodeApiServerHandshakeLatency = mHist2 + lm.nodeAPIServerHandshakeLatency = mHist2 // Test No response metric. c := prometheus.NewCounter(prometheus.CounterOpts{}) - mNoResponse := metrics.NewMockICounterVec(ctrl) + mNoResponse := metrics.NewMockCounterVec(ctrl) mNoResponse.EXPECT().WithLabelValues("no_response").Return(c).Times(1) lm.noResponseMetric = mNoResponse diff --git a/pkg/module/metrics/tcpflags.go b/pkg/module/metrics/tcpflags.go index af19db37a6..02c8eb539a 100644 --- a/pkg/module/metrics/tcpflags.go +++ b/pkg/module/metrics/tcpflags.go @@ -25,7 +25,7 @@ const ( type TCPMetrics struct { baseMetricObject - tcpFlagsMetrics metricsinit.IGaugeVec + tcpFlagsMetrics metricsinit.GaugeVec } func NewTCPMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, isLocalContext enrichmentContext) *TCPMetrics { diff --git a/pkg/module/metrics/tcpflags_test.go b/pkg/module/metrics/tcpflags_test.go index 2ea452d1db..8b845df6bb 100644 --- a/pkg/module/metrics/tcpflags_test.go +++ b/pkg/module/metrics/tcpflags_test.go @@ -479,7 +479,7 @@ func TestNewTCPMetrics(t *testing.T) { assert.NotNil(t, tcp, "forward metrics should not be nil Test Name: %s", tc.name) } - tcpFlagMockMetrics := metricsinit.NewMockIGaugeVec(ctrl) //nolint:staticcheck + tcpFlagMockMetrics := metricsinit.NewMockGaugeVec(ctrl) //nolint:staticcheck tcp.tcpFlagsMetrics = tcpFlagMockMetrics testmetric := prometheus.NewGauge(prometheus.GaugeOpts{ diff --git a/pkg/module/metrics/tcpretrans.go b/pkg/module/metrics/tcpretrans.go index a463476b4b..3e307eabf4 100644 --- a/pkg/module/metrics/tcpretrans.go +++ b/pkg/module/metrics/tcpretrans.go @@ -25,7 +25,7 @@ const ( type TCPRetransMetrics struct { baseMetricObject - tcpRetransMetrics metricsinit.IGaugeVec + tcpRetransMetrics metricsinit.GaugeVec } func NewTCPRetransMetrics(ctxOptions *api.MetricsContextOptions, fl *log.ZapLogger, isLocalContext enrichmentContext) *TCPRetransMetrics { diff --git a/pkg/module/metrics/types.go b/pkg/module/metrics/types.go index 7784ef4219..41d923a661 100644 --- a/pkg/module/metrics/types.go +++ b/pkg/module/metrics/types.go @@ -336,19 +336,19 @@ func DefaultCtxOptions() []string { func DefaultMetrics() []string { return []string{ // forward - utils.ForwardCountTotalName, - utils.ForwardBytesTotalName, + utils.ForwardPacketsGaugeName, + utils.ForwardBytesGaugeName, // drop - utils.DropCountTotalName, - utils.DropBytesTotalName, + utils.DroppedPacketsGaugeName, + utils.DropBytesGaugeName, // tcp flags - utils.TcpFlagCounters, + utils.TCPFlagGauge, // tcp retransmissions - utils.TcpRetransCount, + utils.TCPRetransCount, // latency - utils.NodeApiServerLatencyName, - utils.NodeApiServerTcpHandshakeLatencyName, - utils.NoResponseFromApiServerName, + utils.NodeAPIServerLatencyName, + utils.NodeAPIServerTCPHandshakeLatencyName, + utils.NoResponseFromAPIServerName, // dns utils.DNSRequestCounterName, utils.DNSResponseCounterName, diff --git a/pkg/plugin/dns/dns_linux_test.go b/pkg/plugin/dns/dns_linux_test.go index 648d714437..7c84f95518 100644 --- a/pkg/plugin/dns/dns_linux_test.go +++ b/pkg/plugin/dns/dns_linux_test.go @@ -140,7 +140,7 @@ func TestRequestEventHandler(t *testing.T) { c := prometheus.NewCounter(prometheus.CounterOpts{}) // Basic metrics. - mockCV := metrics.NewMockICounterVec(ctrl) + mockCV := metrics.NewMockCounterVec(ctrl) mockCV.EXPECT().WithLabelValues(event.QType, event.DNSName).Return(c).Times(1) before := value(c) metrics.DNSRequestCounter = mockCV @@ -190,7 +190,7 @@ func TestResponseEventHandler(t *testing.T) { // Basic metrics. c := prometheus.NewCounter(prometheus.CounterOpts{}) - mockCV := metrics.NewMockICounterVec(ctrl) + mockCV := metrics.NewMockCounterVec(ctrl) mockCV.EXPECT().WithLabelValues(event.Rcode, event.QType, event.DNSName, "1.1.1.1,2.2.2.2", "2").Return(c).Times(1) before := value(c) metrics.DNSResponseCounter = mockCV diff --git a/pkg/plugin/dns/types_linux.go b/pkg/plugin/dns/types_linux.go index 48cf0d11b6..dd5a05077c 100644 --- a/pkg/plugin/dns/types_linux.go +++ b/pkg/plugin/dns/types_linux.go @@ -16,7 +16,7 @@ const ( Name api.PluginName = "dns" ) -var m metrics.ICounterVec +var m metrics.CounterVec type dns struct { cfg *kcfg.Config diff --git a/pkg/plugin/dropreason/dropreason_linux.go b/pkg/plugin/dropreason/dropreason_linux.go index b5872bb717..f8a4440cb6 100644 --- a/pkg/plugin/dropreason/dropreason_linux.go +++ b/pkg/plugin/dropreason/dropreason_linux.go @@ -487,8 +487,8 @@ func (dr *dropReason) Stop() error { } func (dr *dropReason) dropMetricAdd(reason string, direction string, count float64, bytes float64) { - metrics.DropCounter.WithLabelValues(reason, direction).Set(float64(count)) - metrics.DropBytesCounter.WithLabelValues(reason, direction).Set(float64(bytes)) + metrics.DropPacketsGauge.WithLabelValues(reason, direction).Set(float64(count)) + metrics.DropBytesGauge.WithLabelValues(reason, direction).Set(float64(bytes)) } // Helper functions. diff --git a/pkg/plugin/dropreason/dropreason_linux_test.go b/pkg/plugin/dropreason/dropreason_linux_test.go index a954453e4e..d2a1bf0423 100644 --- a/pkg/plugin/dropreason/dropreason_linux_test.go +++ b/pkg/plugin/dropreason/dropreason_linux_test.go @@ -135,11 +135,11 @@ func TestProcessMapValue(t *testing.T) { direction := testMetricKey.getDirection() dropCount := &dto.Metric{} - err := metrics.DropCounter.WithLabelValues(reason, direction).Write(dropCount) + err := metrics.DropPacketsGauge.WithLabelValues(reason, direction).Write(dropCount) require.Nil(t, err, "Expected no error but got: %w", err) dropBytes := &dto.Metric{} - err = metrics.DropBytesCounter.WithLabelValues(reason, direction).Write(dropBytes) + err = metrics.DropBytesGauge.WithLabelValues(reason, direction).Write(dropBytes) require.Nil(t, err, "Expected no error but got: %w", err) dropCountValue := *dropCount.Gauge.Value diff --git a/pkg/plugin/infiniband/infiniband_stats_linux.go b/pkg/plugin/infiniband/infiniband_stats_linux.go index f7363e7603..02558ff389 100644 --- a/pkg/plugin/infiniband/infiniband_stats_linux.go +++ b/pkg/plugin/infiniband/infiniband_stats_linux.go @@ -133,11 +133,11 @@ func (ir *InfinibandReader) readStatusParamStats(fsys fs.FS) error { func (ir *InfinibandReader) updateMetrics() { // Adding counter stats for counter, val := range ir.counterStats { - metrics.InfinibandCounterStats.WithLabelValues(counter.Name, counter.Device, counter.Port).Set(float64(val)) + metrics.InfinibandStatsGauge.WithLabelValues(counter.Name, counter.Device, counter.Port).Set(float64(val)) } // Adding status params for statusParam, val := range ir.statusParamStats { - metrics.InfinibandStatusParams.WithLabelValues(statusParam.Name, statusParam.Iface).Set(float64(val)) + metrics.InfinibandStatusParamsGauge.WithLabelValues(statusParam.Name, statusParam.Iface).Set(float64(val)) } } diff --git a/pkg/plugin/infiniband/infiniband_stats_linux_test.go b/pkg/plugin/infiniband/infiniband_stats_linux_test.go index 053d5b84ea..f15cf6955e 100644 --- a/pkg/plugin/infiniband/infiniband_stats_linux_test.go +++ b/pkg/plugin/infiniband/infiniband_stats_linux_test.go @@ -13,8 +13,8 @@ import ( ) var ( - MockGaugeVec *metrics.MockIGaugeVec - MockCounterVec *metrics.MockICounterVec + MockGaugeVec *metrics.MockGaugeVec + MockCounterVec *metrics.MockCounterVec ) func TestNewInfinibandReader(t *testing.T) { @@ -30,9 +30,9 @@ func InitalizeMetricsForTesting(ctrl *gomock.Controller) { metricsLogger := log.Logger().Named("metrics") metricsLogger.Info("Initializing metrics for testing") - MockGaugeVec = metrics.NewMockIGaugeVec(ctrl) - metrics.InfinibandCounterStats = MockGaugeVec //nolint:typecheck // no type check - metrics.InfinibandStatusParams = MockGaugeVec + MockGaugeVec = metrics.NewMockGaugeVec(ctrl) + metrics.InfinibandStatsGauge = MockGaugeVec //nolint:typecheck // no type check + metrics.InfinibandStatusParamsGauge = MockGaugeVec } //nolint:testifylint // not making linter changes to preserve exact behavior diff --git a/pkg/plugin/linuxutil/ethtool_stats_linux.go b/pkg/plugin/linuxutil/ethtool_stats_linux.go index 882bcc48e4..3f0625e5a0 100644 --- a/pkg/plugin/linuxutil/ethtool_stats_linux.go +++ b/pkg/plugin/linuxutil/ethtool_stats_linux.go @@ -114,7 +114,7 @@ func (er *EthtoolReader) updateMetrics() { // retrive interfacename and statname from ethStats for ifName, stats := range er.data.stats { for statName, statVal := range stats { - metrics.InterfaceStats.WithLabelValues(ifName, statName).Set(float64(statVal)) + metrics.InterfaceStatsGauge.WithLabelValues(ifName, statName).Set(float64(statVal)) } } } diff --git a/pkg/plugin/linuxutil/ethtool_stats_linux_test.go b/pkg/plugin/linuxutil/ethtool_stats_linux_test.go index 97ea262929..e5213b3782 100644 --- a/pkg/plugin/linuxutil/ethtool_stats_linux_test.go +++ b/pkg/plugin/linuxutil/ethtool_stats_linux_test.go @@ -14,8 +14,8 @@ import ( ) var ( - MockGaugeVec *metrics.MockIGaugeVec - MockCounterVec *metrics.MockICounterVec + MockGaugeVec *metrics.MockGaugeVec + MockCounterVec *metrics.MockCounterVec ) var ( @@ -174,22 +174,20 @@ func InitalizeMetricsForTesting(ctrl *gomock.Controller) { metricsLogger := log.Logger().Named("metrics") metricsLogger.Info("Initializing metrics for testing") - MockCounterVec = metrics.NewMockICounterVec(ctrl) - MockGaugeVec = metrics.NewMockIGaugeVec(ctrl) //nolint:typecheck + MockCounterVec = metrics.NewMockCounterVec(ctrl) + MockGaugeVec = metrics.NewMockGaugeVec(ctrl) - metrics.DropCounter = MockGaugeVec - metrics.DropBytesCounter = MockGaugeVec - metrics.ForwardBytesCounter = MockGaugeVec - metrics.ForwardCounter = MockGaugeVec + metrics.DropPacketsGauge = MockGaugeVec + metrics.DropBytesGauge = MockGaugeVec + metrics.ForwardBytesGauge = MockGaugeVec + metrics.ForwardPacketsGauge = MockGaugeVec metrics.NodeConnectivityStatusGauge = MockGaugeVec metrics.NodeConnectivityLatencyGauge = MockGaugeVec metrics.TCPStateGauge = MockGaugeVec metrics.TCPConnectionRemoteGauge = MockGaugeVec - metrics.TCPConnectionStats = MockGaugeVec - metrics.TCPFlagCounters = MockGaugeVec - metrics.IPConnectionStats = MockGaugeVec - metrics.UDPConnectionStats = MockGaugeVec - metrics.UDPActiveSocketsCounter = MockGaugeVec - metrics.InterfaceStats = MockGaugeVec + metrics.TCPConnectionStatsGauge = MockGaugeVec + metrics.IPConnectionStatsGauge = MockGaugeVec + metrics.UDPConnectionStatsGauge = MockGaugeVec + metrics.InterfaceStatsGauge = MockGaugeVec metrics.PluginManagerFailedToReconcileCounter = MockCounterVec } diff --git a/pkg/plugin/linuxutil/netstat_stats_linux.go b/pkg/plugin/linuxutil/netstat_stats_linux.go index f3fb79ffec..5b71ebf029 100644 --- a/pkg/plugin/linuxutil/netstat_stats_linux.go +++ b/pkg/plugin/linuxutil/netstat_stats_linux.go @@ -233,17 +233,17 @@ func (nr *NetstatReader) updateMetrics() { } // Adding TCP Connection Stats for statName, val := range nr.connStats.TcpExt { - metrics.TCPConnectionStats.WithLabelValues(statName).Set(float64(val)) + metrics.TCPConnectionStatsGauge.WithLabelValues(statName).Set(float64(val)) } // Adding IP Stats for statName, val := range nr.connStats.IpExt { - metrics.IPConnectionStats.WithLabelValues(statName).Set(float64(val)) + metrics.IPConnectionStatsGauge.WithLabelValues(statName).Set(float64(val)) } // Adding MPTCP Stats for statName, val := range nr.connStats.MPTcpExt { - metrics.TCPConnectionStats.WithLabelValues(statName).Set(float64(val)) + metrics.TCPConnectionStatsGauge.WithLabelValues(statName).Set(float64(val)) } // TCP COnnection State and remote addr metrics @@ -267,7 +267,7 @@ func (nr *NetstatReader) updateMetrics() { } // UDP COnnection State metrics - metrics.UDPConnectionStats.WithLabelValues(utils.Active).Set(float64(nr.connStats.UdpSockets.totalActiveSockets)) + metrics.UDPConnectionStatsGauge.WithLabelValues(utils.Active).Set(float64(nr.connStats.UdpSockets.totalActiveSockets)) } func validateRemoteAddr(addr string) bool { diff --git a/pkg/plugin/packetforward/packetforward_linux.go b/pkg/plugin/packetforward/packetforward_linux.go index 4a2c04bdfd..a1405ee98c 100644 --- a/pkg/plugin/packetforward/packetforward_linux.go +++ b/pkg/plugin/packetforward/packetforward_linux.go @@ -80,12 +80,12 @@ func processMapValue(m IMap, key uint32) (uint64, uint64, error) { func updateMetrics(data *PacketForwardData) { // Add the packet count metrics. - metrics.ForwardCounter.WithLabelValues(ingressLabel).Set(float64(data.ingressCountTotal)) - metrics.ForwardCounter.WithLabelValues(egressLabel).Set(float64(data.egressCountTotal)) + metrics.ForwardPacketsGauge.WithLabelValues(ingressLabel).Set(float64(data.ingressCountTotal)) + metrics.ForwardPacketsGauge.WithLabelValues(egressLabel).Set(float64(data.egressCountTotal)) // Add the packet bytes metrics. - metrics.ForwardBytesCounter.WithLabelValues(ingressLabel).Set(float64(data.ingressBytesTotal)) - metrics.ForwardBytesCounter.WithLabelValues(egressLabel).Set(float64(data.egressBytesTotal)) + metrics.ForwardBytesGauge.WithLabelValues(ingressLabel).Set(float64(data.ingressBytesTotal)) + metrics.ForwardBytesGauge.WithLabelValues(egressLabel).Set(float64(data.egressBytesTotal)) } // Plugin API implementation for packet forward. diff --git a/pkg/plugin/packetparser/packetparser_linux_test.go b/pkg/plugin/packetparser/packetparser_linux_test.go index 630a8e8eb2..95251f9365 100644 --- a/pkg/plugin/packetparser/packetparser_linux_test.go +++ b/pkg/plugin/packetparser/packetparser_linux_test.go @@ -335,7 +335,7 @@ func TestReadDataPodLevelEnabled(t *testing.T) { recordsChannel: make(chan perf.Record, buffer), } - mICounterVec := metrics.NewMockICounterVec(ctrl) + mICounterVec := metrics.NewMockCounterVec(ctrl) mICounterVec.EXPECT().WithLabelValues(gomock.Any()).Return(prometheus.NewCounter(prometheus.CounterOpts{})).AnyTimes() metrics.LostEventsCounter = mICounterVec diff --git a/pkg/plugin/windows/hnsstats/hnsstats_windows.go b/pkg/plugin/windows/hnsstats/hnsstats_windows.go index 67ba87e08a..09ca584f96 100644 --- a/pkg/plugin/windows/hnsstats/hnsstats_windows.go +++ b/pkg/plugin/windows/hnsstats/hnsstats_windows.go @@ -147,50 +147,50 @@ func pullHnsStats(ctx context.Context, h *hnsstats) error { func notifyHnsStats(h *hnsstats, stats *HnsStatsData) { // hns signals - metrics.ForwardCounter.WithLabelValues(ingressLabel).Set(float64(stats.hnscounters.PacketsReceived)) + metrics.ForwardPacketsGauge.WithLabelValues(ingressLabel).Set(float64(stats.hnscounters.PacketsReceived)) h.l.Debug("emitting packets received count metric", zap.Uint64(PacketsReceived, stats.hnscounters.PacketsReceived)) - metrics.ForwardCounter.WithLabelValues(egressLabel).Set(float64(stats.hnscounters.PacketsSent)) + metrics.ForwardPacketsGauge.WithLabelValues(egressLabel).Set(float64(stats.hnscounters.PacketsSent)) h.l.Debug("emitting packets sent count metric", zap.Uint64(PacketsSent, stats.hnscounters.PacketsSent)) - metrics.ForwardBytesCounter.WithLabelValues(egressLabel).Set(float64(stats.hnscounters.BytesSent)) + metrics.ForwardBytesGauge.WithLabelValues(egressLabel).Set(float64(stats.hnscounters.BytesSent)) h.l.Debug("emitting bytes sent count metric", zap.Uint64(BytesSent, stats.hnscounters.BytesSent)) - metrics.ForwardBytesCounter.WithLabelValues(ingressLabel).Set(float64(stats.hnscounters.BytesReceived)) + metrics.ForwardBytesGauge.WithLabelValues(ingressLabel).Set(float64(stats.hnscounters.BytesReceived)) h.l.Debug("emitting bytes received count metric", zap.Uint64(BytesReceived, stats.hnscounters.BytesReceived)) - metrics.WindowsCounter.WithLabelValues(PacketsReceived).Set(float64(stats.hnscounters.PacketsReceived)) - metrics.WindowsCounter.WithLabelValues(PacketsSent).Set(float64(stats.hnscounters.PacketsSent)) + metrics.WindowsGauge.WithLabelValues(PacketsReceived).Set(float64(stats.hnscounters.PacketsReceived)) + metrics.WindowsGauge.WithLabelValues(PacketsSent).Set(float64(stats.hnscounters.PacketsSent)) - metrics.DropCounter.WithLabelValues(utils.Endpoint, egressLabel).Set(float64(stats.hnscounters.DroppedPacketsOutgoing)) - metrics.DropCounter.WithLabelValues(utils.Endpoint, ingressLabel).Set(float64(stats.hnscounters.DroppedPacketsIncoming)) + metrics.DropPacketsGauge.WithLabelValues(utils.Endpoint, egressLabel).Set(float64(stats.hnscounters.DroppedPacketsOutgoing)) + metrics.DropPacketsGauge.WithLabelValues(utils.Endpoint, ingressLabel).Set(float64(stats.hnscounters.DroppedPacketsIncoming)) if stats.vfpCounters == nil { h.l.Warn("will not record some metrics since VFP port counters failed to be set") return } - metrics.DropCounter.WithLabelValues(utils.AclRule, ingressLabel).Set(float64(stats.vfpCounters.In.DropCounters.AclDropPacketCount)) - metrics.DropCounter.WithLabelValues(utils.AclRule, egressLabel).Set(float64(stats.vfpCounters.Out.DropCounters.AclDropPacketCount)) + metrics.DropPacketsGauge.WithLabelValues(utils.AclRule, ingressLabel).Set(float64(stats.vfpCounters.In.DropCounters.AclDropPacketCount)) + metrics.DropPacketsGauge.WithLabelValues(utils.AclRule, egressLabel).Set(float64(stats.vfpCounters.Out.DropCounters.AclDropPacketCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.ResetCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ResetCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.ClosedFin).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ClosedFinCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.ResetSyn).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ResetSynCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.TcpHalfOpenTimeouts).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TcpHalfOpenTimeoutsCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.Verified).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.VerifiedCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.TimedOutCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TimedOutCount)) - metrics.TCPConnectionStats.WithLabelValues(utils.TimeWaitExpiredCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TimeWaitExpiredCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.ResetCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ResetCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.ClosedFin).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ClosedFinCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.ResetSyn).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.ResetSynCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.TcpHalfOpenTimeouts).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TcpHalfOpenTimeoutsCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.Verified).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.VerifiedCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.TimedOutCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TimedOutCount)) + metrics.TCPConnectionStatsGauge.WithLabelValues(utils.TimeWaitExpiredCount).Set(float64(stats.vfpCounters.In.TcpCounters.ConnectionCounters.TimeWaitExpiredCount)) // TCP Flag counters - metrics.TCPFlagCounters.WithLabelValues(ingressLabel, utils.SYN).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.SynPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(ingressLabel, utils.SYNACK).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.SynAckPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(ingressLabel, utils.FIN).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.FinPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(ingressLabel, utils.RST).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.RstPacketCount)) - - metrics.TCPFlagCounters.WithLabelValues(egressLabel, utils.SYN).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.SynPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(egressLabel, utils.SYNACK).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.SynAckPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(egressLabel, utils.FIN).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.FinPacketCount)) - metrics.TCPFlagCounters.WithLabelValues(egressLabel, utils.RST).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.RstPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(ingressLabel, utils.SYN).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.SynPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(ingressLabel, utils.SYNACK).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.SynAckPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(ingressLabel, utils.FIN).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.FinPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(ingressLabel, utils.RST).Set(float64(stats.vfpCounters.In.TcpCounters.PacketCounters.RstPacketCount)) + + metrics.TCPFlagGauge.WithLabelValues(egressLabel, utils.SYN).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.SynPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(egressLabel, utils.SYNACK).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.SynAckPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(egressLabel, utils.FIN).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.FinPacketCount)) + metrics.TCPFlagGauge.WithLabelValues(egressLabel, utils.RST).Set(float64(stats.vfpCounters.Out.TcpCounters.PacketCounters.RstPacketCount)) } func (h *hnsstats) Start(ctx context.Context) error { diff --git a/pkg/utils/metric_names.go b/pkg/utils/metric_names.go index 3db7daaa3e..8060b05243 100644 --- a/pkg/utils/metric_names.go +++ b/pkg/utils/metric_names.go @@ -11,24 +11,23 @@ package utils const ( // Common Counters across os distributions (should these be asynch or synch) // make sure IsMetric is updated if you add a new metric here - DropCountTotalName = "drop_count" - DropBytesTotalName = "drop_bytes" - ForwardCountTotalName = "forward_count" - ForwardBytesTotalName = "forward_bytes" - TcpStateGaugeName = "tcp_state" - TcpConnectionRemoteGaugeName = "tcp_connection_remote" - TcpConnectionStatsName = "tcp_connection_stats" - TcpFlagCounters = "tcp_flag_counters" - TcpRetransCount = "tcp_retransmission_count" - IpConnectionStatsName = "ip_connection_stats" - UdpConnectionStatsName = "udp_connection_stats" - UdpActiveSocketsCounterName = "udp_active_sockets" + DroppedPacketsGaugeName = "drop_count" + DropBytesGaugeName = "drop_bytes" + ForwardPacketsGaugeName = "forward_count" + ForwardBytesGaugeName = "forward_bytes" + TCPStateGaugeName = "tcp_state" + TCPConnectionRemoteGaugeName = "tcp_connection_remote" + TCPConnectionStatsName = "tcp_connection_stats" + TCPFlagGauge = "tcp_flag_gauges" + TCPRetransCount = "tcp_retransmission_count" + IPConnectionStatsName = "ip_connection_stats" + UDPConnectionStatsName = "udp_connection_stats" InterfaceStatsName = "interface_stats" DNSRequestCounterName = "dns_request_count" DNSResponseCounterName = "dns_response_count" - NodeApiServerLatencyName = "node_apiserver_latency" - NodeApiServerTcpHandshakeLatencyName = "node_apiserver_handshake_latency" - NoResponseFromApiServerName = "node_apiserver_no_response" + NodeAPIServerLatencyName = "node_apiserver_latency" + NodeAPIServerTCPHandshakeLatencyName = "node_apiserver_handshake_latency" + NoResponseFromAPIServerName = "node_apiserver_no_response" InfinibandCounterStatsName = "infiniband_counter_stats" InfinibandStatusParamsName = "infiniband_status_params" @@ -41,25 +40,24 @@ const ( func IsAdvancedMetric(name string) bool { switch name { case - DropCountTotalName, - DropBytesTotalName, - ForwardCountTotalName, - ForwardBytesTotalName, + DroppedPacketsGaugeName, + DropBytesGaugeName, + ForwardPacketsGaugeName, + ForwardBytesGaugeName, NodeConnectivityStatusName, NodeConnectivityLatencySecondsName, - TcpStateGaugeName, - TcpConnectionRemoteGaugeName, - TcpConnectionStatsName, - TcpFlagCounters, - TcpRetransCount, - IpConnectionStatsName, - UdpConnectionStatsName, - UdpActiveSocketsCounterName, + TCPStateGaugeName, + TCPConnectionRemoteGaugeName, + TCPConnectionStatsName, + TCPFlagGauge, + TCPRetransCount, + IPConnectionStatsName, + UDPConnectionStatsName, DNSRequestCounterName, DNSResponseCounterName, - NodeApiServerLatencyName, - NodeApiServerTcpHandshakeLatencyName, - NoResponseFromApiServerName: + NodeAPIServerLatencyName, + NodeAPIServerTCPHandshakeLatencyName, + NoResponseFromAPIServerName: return true default: return false