Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-1447: Added a check to log a warning if some labels are used in metric defi… #629

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions apis/flowmetrics/v1alpha1/flowmetric_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func (r *FlowMetricWebhook) ValidateDelete(_ context.Context, _ runtime.Object)
return nil, nil
}

func checkFlowMetricCartinality(fMetric *FlowMetric) {
for _, label := range fMetric.Spec.Labels {
if helper.LabelIsHighCardinality(label) {
//No warning level logging as info since it is not an error
flowmetriclog.Info("Warning: metric label has high cardinality, please limit it by using some filters", "labelName", label)
return
OlivierCazade marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func validateFlowMetric(_ context.Context, fMetric *FlowMetric) error {
var str []string
var allErrs field.ErrorList
Expand Down Expand Up @@ -95,5 +105,6 @@ func validateFlowMetric(_ context.Context, fMetric *FlowMetric) error {
schema.GroupKind{Group: GroupVersion.Group, Kind: FlowMetric{}.Kind},
fMetric.Name, allErrs)
}
checkFlowMetricCartinality(fMetric)
return nil
}
18 changes: 14 additions & 4 deletions controllers/consoleplugin/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/netobserv/flowlogs-pipeline/pkg/api"
flowslatest "github.com/netobserv/network-observability-operator/apis/flowcollector/v1beta2"
"gopkg.in/yaml.v2"
)

type ServerConfig struct {
Expand Down Expand Up @@ -109,8 +110,17 @@ type PluginConfig struct {
}

//go:embed static-frontend-config.yaml
var staticFrontendConfig []byte

func LoadStaticFrontendConfig() []byte {
return staticFrontendConfig
var rawStaticFrontendConfig []byte
var staticFrontendConfig *FrontendConfig

func LoadStaticFrontendConfig() (FrontendConfig, error) {
if staticFrontendConfig == nil {
cfg := FrontendConfig{}
err := yaml.Unmarshal(rawStaticFrontendConfig, &cfg)
if err != nil {
return cfg, err
}
staticFrontendConfig = &cfg
}
return *staticFrontendConfig, nil
}
3 changes: 2 additions & 1 deletion controllers/consoleplugin/consoleplugin_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ func (b *builder) configMap() (*corev1.ConfigMap, string, error) {
b.setLokiConfig(&config.Loki)

// configure frontend from embedded static file
err := yaml.Unmarshal(cfg.LoadStaticFrontendConfig(), &config.Frontend)
var err error
config.Frontend, err = cfg.LoadStaticFrontendConfig()
if err != nil {
return nil, "", err
}
Expand Down
6 changes: 2 additions & 4 deletions controllers/consoleplugin/consoleplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,7 @@ func TestHTTPClientConfig(t *testing.T) {
}

func TestNoMissingFields(t *testing.T) {
var cfg config.FrontendConfig
err := yaml.Unmarshal(config.LoadStaticFrontendConfig(), &cfg)
cfg, err := config.LoadStaticFrontendConfig()
assert.NoError(t, err)

hasField := func(name string) bool {
Expand Down Expand Up @@ -537,8 +536,7 @@ func TestNoMissingFields(t *testing.T) {
}

func TestFieldsCardinalityWarns(t *testing.T) {
var cfg config.FrontendConfig
err := yaml.Unmarshal(config.LoadStaticFrontendConfig(), &cfg)
cfg, err := config.LoadStaticFrontendConfig()
assert.NoError(t, err)

allowed := []config.CardinalityWarn{config.CardinalityWarnAvoid, config.CardinalityWarnCareful, config.CardinalityWarnFine}
Expand Down
18 changes: 15 additions & 3 deletions pkg/helper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/netobserv/network-observability-operator/controllers/consoleplugin/config"

"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -93,13 +92,12 @@ func UnstructuredDuration(in *metav1.Duration) string {
}

func FindFilter(labels []string, isNumber bool) bool {
var cfg config.FrontendConfig
type filter struct {
exists bool
isNum bool
}

err := yaml.Unmarshal(config.LoadStaticFrontendConfig(), &cfg)
cfg, err := config.LoadStaticFrontendConfig()
if err != nil {
return false
}
Expand All @@ -124,3 +122,17 @@ func FindFilter(labels []string, isNumber bool) bool {

return true
}

func LabelIsHighCardinality(label string) bool {
frontendCfg, err := config.LoadStaticFrontendConfig()
if err != nil {
return false
}
for _, cfgLabel := range frontendCfg.Fields {
if label == cfgLabel.Name {
return cfgLabel.CardinalityWarn == config.CardinalityWarnCareful ||
cfgLabel.CardinalityWarn == config.CardinalityWarnAvoid
}
}
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works for me thank you!!

}
7 changes: 7 additions & 0 deletions pkg/helper/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,10 @@ func TestCRDDefault(t *testing.T) {
assert.Equal(t, "app: netobserv-flowcollector\n", GetFieldDefaultString([]string{"spec", "processor", "debug"}, "lokiStaticLabels"))

}

func TestLabelCardinality(t *testing.T) {
assert.True(t, LabelIsHighCardinality("SrcK8S_Name"))
assert.True(t, LabelIsHighCardinality("DstK8S_Name"))
assert.True(t, LabelIsHighCardinality("TimeReceived"))
assert.False(t, LabelIsHighCardinality("SrcK8S_OwnerName"))
}