Skip to content

Commit

Permalink
NETOBSERV-1672: add cluster & zone to predefined metrics (#665)
Browse files Browse the repository at this point in the history
* NETOBSERV-1672: add cluster & zone to predefined metrics

They're only added when those features are enabled.

* fix Clustername => ClusterName
  • Loading branch information
jotak committed Jun 4, 2024
1 parent c1b4ee0 commit bf5f147
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion controllers/consoleplugin/consoleplugin_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (b *builder) getPromConfig(ctx context.Context) cfg.PrometheusConfig {

allMetricNames := metrics.GetAllNames()
includeList := metrics.GetIncludeList(b.desired)
allMetrics := metrics.GetDefinitions(allMetricNames)
allMetrics := metrics.GetDefinitions(allMetricNames, nil)
for i := range allMetrics {
mSpec := allMetrics[i].Spec
enabled := slices.Contains(includeList, mSpec.MetricName)
Expand Down
6 changes: 3 additions & 3 deletions pkg/dashboards/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestCreateFlowMetricsDashboard_All(t *testing.T) {
assert := assert.New(t)

defs := metrics.GetDefinitions(metrics.GetAllNames())
defs := metrics.GetDefinitions(metrics.GetAllNames(), nil)
js := CreateFlowMetricsDashboards(defs)

d, err := FromBytes([]byte(js["Main"]))
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestCreateFlowMetricsDashboard_All(t *testing.T) {
func TestCreateFlowMetricsDashboard_OnlyNodeIngressBytes(t *testing.T) {
assert := assert.New(t)

defs := metrics.GetDefinitions([]string{"node_ingress_bytes_total"})
defs := metrics.GetDefinitions([]string{"node_ingress_bytes_total"}, nil)
js := CreateFlowMetricsDashboards(defs)

d, err := FromBytes([]byte(js["Main"]))
Expand All @@ -106,7 +106,7 @@ func TestCreateFlowMetricsDashboard_OnlyNodeIngressBytes(t *testing.T) {
func TestCreateFlowMetricsDashboard_DefaultList(t *testing.T) {
assert := assert.New(t)

defs := metrics.GetDefinitions(metrics.DefaultIncludeList)
defs := metrics.GetDefinitions(metrics.DefaultIncludeList, nil)
js := CreateFlowMetricsDashboards(defs)

d, err := FromBytes([]byte(js["Main"]))
Expand Down
32 changes: 26 additions & 6 deletions pkg/metrics/predefined_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"fmt"
"reflect"
"slices"
"strings"

flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
Expand All @@ -23,9 +24,9 @@ const (
var (
latencyBuckets = []string{".005", ".01", ".02", ".03", ".04", ".05", ".075", ".1", ".25", "1"}
mapLabels = map[string][]string{
tagNodes: {"SrcK8S_HostName", "DstK8S_HostName"},
tagNamespaces: {"SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel"},
tagWorkloads: {"SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel", "SrcK8S_OwnerName", "DstK8S_OwnerName", "SrcK8S_OwnerType", "DstK8S_OwnerType", "SrcK8S_Type", "DstK8S_Type"},
tagNodes: {"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_HostName", "DstK8S_HostName"},
tagNamespaces: {"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel"},
tagWorkloads: {"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel", "SrcK8S_OwnerName", "DstK8S_OwnerName", "SrcK8S_OwnerType", "DstK8S_OwnerType", "SrcK8S_Type", "DstK8S_Type"},
}
mapValueFields = map[string]string{
tagBytes: "Bytes",
Expand Down Expand Up @@ -197,18 +198,30 @@ func GetAllNames() []string {
return names
}

func GetDefinitions(names []string) []metricslatest.FlowMetric {
func GetDefinitions(names []string, toRemove []string) []metricslatest.FlowMetric {
ret := []metricslatest.FlowMetric{}
for i := range predefinedMetrics {
for _, name := range names {
if predefinedMetrics[i].MetricName == name {
ret = append(ret, metricslatest.FlowMetric{Spec: predefinedMetrics[i].FlowMetricSpec})
spec := predefinedMetrics[i].FlowMetricSpec
spec.Labels = removeLabels(spec.Labels, toRemove)
ret = append(ret, metricslatest.FlowMetric{Spec: spec})
}
}
}
return ret
}

func removeLabels(initial []string, toRemove []string) []string {
var labels []string
for _, lbl := range initial {
if !slices.Contains(toRemove, lbl) {
labels = append(labels, lbl)
}
}
return labels
}

func GetIncludeList(spec *flowslatest.FlowCollectorSpec) []string {
var list []string
if spec.Processor.Metrics.IncludeList == nil {
Expand Down Expand Up @@ -247,6 +260,13 @@ func removeMetricsByPattern(list []string, search string) []string {

func MergePredefined(fm []metricslatest.FlowMetric, fc *flowslatest.FlowCollectorSpec) []metricslatest.FlowMetric {
names := GetIncludeList(fc)
predefined := GetDefinitions(names)
var toRemove []string
if !helper.IsZoneEnabled(&fc.Processor) {
toRemove = append(toRemove, "SrcK8S_Zone", "DstK8S_Zone")
}
if !helper.IsMultiClusterEnabled(&fc.Processor) {
toRemove = append(toRemove, "K8S_ClusterName")
}
predefined := GetDefinitions(names, toRemove)
return append(predefined, fm...)
}
18 changes: 17 additions & 1 deletion pkg/metrics/predefined_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,23 @@ func TestIncludeExclude(t *testing.T) {
func TestGetDefinitions(t *testing.T) {
assert := assert.New(t)

res := GetDefinitions([]string{"namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"})
res := GetDefinitions([]string{"namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"}, nil)
assert.Len(res, 3)
assert.Equal("node_ingress_bytes_total", res[0].Spec.MetricName)
assert.Equal("Bytes", res[0].Spec.ValueField)
assert.Equal([]string{"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_HostName", "DstK8S_HostName"}, res[0].Spec.Labels)
assert.Equal("namespace_flows_total", res[1].Spec.MetricName)
assert.Empty(res[1].Spec.ValueField)
assert.Equal([]string{"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel"}, res[1].Spec.Labels)
assert.Equal("workload_egress_packets_total", res[2].Spec.MetricName)
assert.Equal("Packets", res[2].Spec.ValueField)
assert.Equal([]string{"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone", "SrcK8S_Namespace", "DstK8S_Namespace", "K8S_FlowLayer", "SrcSubnetLabel", "DstSubnetLabel", "SrcK8S_OwnerName", "DstK8S_OwnerName", "SrcK8S_OwnerType", "DstK8S_OwnerType", "SrcK8S_Type", "DstK8S_Type"}, res[2].Spec.Labels)
}

func TestGetDefinitionsRemoveZoneCluster(t *testing.T) {
assert := assert.New(t)

res := GetDefinitions([]string{"namespace_flows_total", "node_ingress_bytes_total", "workload_egress_packets_total"}, []string{"K8S_ClusterName", "SrcK8S_Zone", "DstK8S_Zone"})
assert.Len(res, 3)
assert.Equal("node_ingress_bytes_total", res[0].Spec.MetricName)
assert.Equal("Bytes", res[0].Spec.ValueField)
Expand Down

0 comments on commit bf5f147

Please sign in to comment.