From 245f6346d81df43d25e5f94278b0ce13d2f03ade Mon Sep 17 00:00:00 2001 From: Tom Hadlaw Date: Thu, 26 Oct 2023 22:53:39 -0700 Subject: [PATCH] metrics: initialize metrics for several existing metrics. Using previous commits, initializes metrics to zero for various existing metrics. Signed-off-by: Tom Hadlaw --- pkg/metrics/metric/metric.go | 2 + pkg/metrics/metrics.go | 102 ++++++++++++++++++++++++++++++----- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/pkg/metrics/metric/metric.go b/pkg/metrics/metric/metric.go index fb2414514dd3d..c08e9aae4a0c5 100644 --- a/pkg/metrics/metric/metric.go +++ b/pkg/metrics/metric/metric.go @@ -265,6 +265,8 @@ func NewValues(vs ...string) Values { return vals } +// Labels is a slice of labels that represents a label set for a vector type +// metric. type Labels []Label func (lbls Labels) labelNames() []string { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index eb2a1ce2178f1..d02253daa2fe3 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -20,6 +20,7 @@ import ( "github.com/cilium/cilium/api/v1/models" "github.com/cilium/cilium/pkg/metrics/metric" "github.com/cilium/cilium/pkg/promise" + "github.com/cilium/cilium/pkg/source" "github.com/cilium/cilium/pkg/time" "github.com/cilium/cilium/pkg/version" ) @@ -96,12 +97,22 @@ const ( // Labels + // LabelValueFalse is the string value for true metric label values. + LabelValueTrue = "true" + + // LabelValueFalse is the string value for false metric label values. + LabelValueFalse = "false" + // LabelValueOutcomeSuccess is used as a successful outcome of an operation LabelValueOutcomeSuccess = "success" // LabelValueOutcomeFail is used as an unsuccessful outcome of an operation LabelValueOutcomeFail = "fail" + // LabelValueOutcomeFailure is used as an unsuccessful outcome of an operation. + // NOTE: This should only be used for existing metrics, new metrics should use LabelValueOutcomeFail. + LabelValueOutcomeFailure = "failure" + // LabelDropReason is used to describe reason for dropping a packets/bytes LabelDropReason = "reason" @@ -145,6 +156,8 @@ const ( // LabelPolicySource is the label used to see the enforcement status LabelPolicySource = "source" + LabelSource = "source" + // LabelScope is the label used to defined multiples scopes in the same // metric. For example, one counter may measure a metric over the scope of // the entire event (scope=global), or just part of an event @@ -224,6 +237,12 @@ const ( LabelLocationRemoteIntraCluster = "remote_intra_cluster" LabelLocationRemoteInterCluster = "remote_inter_cluster" + // Rule label is a label for a L7 rule name. + LabelL7Rule = "rule" + + // LabelL7ProxyType is the label for denoting a L7 proxy type. + LabelL7ProxyType = "proxy_type" + // LabelType is the label for type in general (e.g. endpoint, node) LabelType = "type" LabelPeerEndpoint = "endpoint" @@ -238,6 +257,9 @@ const ( ) var ( + // LabelValuesBool is metric label value set for boolean type. + LabelValuesBool = metric.NewValues(LabelValueTrue, LabelValueFalse) + // Namespace is used to scope metrics from cilium. It is prepended to metric // names and separated with a '_' Namespace = CiliumAgentNamespace @@ -718,13 +740,18 @@ func NewLegacyMetrics() *LegacyMetrics { Help: "Duration of processed API calls labeled by path, method and return code.", }, []string{LabelPath, LabelMethod, LabelAPIReturnCode}), - EndpointRegenerationTotal: metric.NewCounterVec(metric.CounterOpts{ + EndpointRegenerationTotal: metric.NewCounterVecWithLabels(metric.CounterOpts{ ConfigName: Namespace + "_endpoint_regenerations_total", Namespace: Namespace, Name: "endpoint_regenerations_total", Help: "Count of all endpoint regenerations that have completed, tagged by outcome", - }, []string{"outcome"}), + }, metric.Labels{ + { + Name: LabelOutcome, + Values: metric.NewValues(LabelValueOutcomeSuccess, LabelValueOutcomeFailure), + }, + }), EndpointStateCount: metric.NewGaugeVec(metric.GaugeOpts{ ConfigName: Namespace + "_endpoint_state", @@ -771,13 +798,18 @@ func NewLegacyMetrics() *LegacyMetrics { Help: "Highest policy revision number in the agent", }), - PolicyChangeTotal: metric.NewCounterVec(metric.CounterOpts{ + PolicyChangeTotal: metric.NewCounterVecWithLabels(metric.CounterOpts{ ConfigName: Namespace + "_policy_change_total", Namespace: Namespace, Name: "policy_change_total", Help: "Number of policy changes by outcome", - }, []string{"outcome"}), + }, metric.Labels{ + { + Name: LabelOutcome, + Values: metric.NewValues(LabelValueOutcomeSuccess, LabelValueOutcomeFailure), + }, + }), PolicyEndpointStatus: metric.NewGaugeVec(metric.GaugeOpts{ ConfigName: Namespace + "_policy_endpoint_enforcement_status", @@ -787,13 +819,18 @@ func NewLegacyMetrics() *LegacyMetrics { Help: "Number of endpoints labeled by policy enforcement status", }, []string{LabelPolicyEnforcement}), - PolicyImplementationDelay: metric.NewHistogramVec(metric.HistogramOpts{ + PolicyImplementationDelay: metric.NewHistogramVecWithLabels(metric.HistogramOpts{ ConfigName: Namespace + "_policy_implementation_delay", Namespace: Namespace, Name: "policy_implementation_delay", Help: "Time between a policy change and it being fully deployed into the datapath", - }, []string{LabelPolicySource}), + }, metric.Labels{ + { + Name: LabelPolicySource, + Values: metric.NewValues(string(source.Kubernetes), string(source.CustomResource), string(source.LocalAPI)), + }, + }), CIDRGroupsReferenced: metric.NewGauge(metric.GaugeOpts{ ConfigName: Namespace + "cidrgroups_referenced", @@ -844,12 +881,21 @@ func NewLegacyMetrics() *LegacyMetrics { Help: "Number of redirects installed for endpoints, labeled by protocol", }, []string{LabelProtocolL7}), - ProxyPolicyL7Total: metric.NewCounterVec(metric.CounterOpts{ + ProxyPolicyL7Total: metric.NewCounterVecWithLabels(metric.CounterOpts{ ConfigName: Namespace + "_policy_l7_total", Namespace: Namespace, Name: "policy_l7_total", Help: "Number of total proxy requests handled", - }, []string{"rule", "proxy_type"}), + }, metric.Labels{ + { + Name: LabelL7Rule, + Values: metric.NewValues("received", "forwarded", "denied", "parse_errors"), + }, + { + Name: LabelL7ProxyType, + Values: metric.NewValues("fqdn", "envoy"), + }, + }), ProxyUpstreamTime: metric.NewHistogramVec(metric.HistogramOpts{ ConfigName: Namespace + "_proxy_upstream_reply_seconds", @@ -965,19 +1011,51 @@ func NewLegacyMetrics() *LegacyMetrics { Help: "Number of times that Cilium has started a subprocess, labeled by subsystem", }, []string{LabelSubsystem}), - KubernetesEventProcessed: metric.NewCounterVec(metric.CounterOpts{ + KubernetesEventProcessed: metric.NewCounterVecWithLabels(metric.CounterOpts{ ConfigName: Namespace + "_kubernetes_events_total", Namespace: Namespace, Name: "kubernetes_events_total", Help: "Number of Kubernetes events processed labeled by scope, action and execution result", - }, []string{LabelScope, LabelAction, LabelStatus}), + }, + metric.Labels{ + { + Name: LabelScope, + Values: metric.NewValues("CiliumNetworkPolicy", "CiliumClusterwideNetworkPolicy", "NetworkPolicy"), + }, + { + Name: LabelAction, + Values: metric.NewValues("update", "delete"), + }, + { + Name: LabelStatus, + Values: metric.NewValues("success", "failed"), + }, + }, + ), - KubernetesEventReceived: metric.NewCounterVec(metric.CounterOpts{ + KubernetesEventReceived: metric.NewCounterVecWithLabels(metric.CounterOpts{ ConfigName: Namespace + "_kubernetes_events_received_total", Namespace: Namespace, Name: "kubernetes_events_received_total", Help: "Number of Kubernetes events received labeled by scope, action, valid data and equalness", - }, []string{LabelScope, LabelAction, "valid", "equal"}), + }, metric.Labels{ + { + Name: LabelScope, + Values: metric.NewValues("CiliumNetworkPolicy", "CiliumClusterwideNetworkPolicy", "NetworkPolicy"), + }, + { + Name: LabelAction, + Values: metric.NewValues("update", "delete"), + }, + { + Name: "valid", + Values: LabelValuesBool, + }, + { + Name: "equal", + Values: LabelValuesBool, + }, + }), KubernetesAPIInteractions: metric.NewHistogramVec(metric.HistogramOpts{ ConfigName: Namespace + "_" + SubsystemK8sClient + "_api_latency_time_seconds",