Skip to content

Commit 67abf61

Browse files
committed
NETOBSERV-1935: enable metrics from list/nested fields
1 parent 44eeb60 commit 67abf61

File tree

13 files changed

+74
-8
lines changed

13 files changed

+74
-8
lines changed

apis/flowmetrics/v1alpha1/flowmetric_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ type FlowMetricSpec struct {
9393
// +optional
9494
Labels []string `json:"labels"`
9595

96+
// `flatten` is a list of list-type fields that must be flattened, such as Interfaces and NetworkEvents. Flattened fields generate one metric per item in that field.
97+
// For instance, when flattening `Interfaces` on a bytes counter, a flow having Interfaces [br-ex, ens5] increases one counter for `br-ex` and another for `ens5`.
98+
// +optional
99+
Flatten []string `json:"flatten"`
100+
96101
// Set the `remap` property to use different names for the generated metric labels than the flow fields. Use the origin flow fields as keys, and the desired label names as values.
97102
// +optional
98103
Remap map[string]string `json:"remap"`

apis/flowmetrics/v1alpha1/flowmetric_webhook.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ func validateFlowMetric(_ context.Context, fMetric *FlowMetric) (admission.Warni
100100
fmt.Sprintf("invalid label name: %s", fMetric.Spec.Labels)))
101101
}
102102

103+
labelsMap := make(map[string]any, len(fMetric.Spec.Labels))
104+
for _, label := range fMetric.Spec.Labels {
105+
labelsMap[label] = nil
106+
}
107+
103108
// Only fields defined as Labels are valid for remapping
104109
if len(fMetric.Spec.Remap) != 0 {
105-
labelsMap := make(map[string]any, len(fMetric.Spec.Labels))
106-
for _, label := range fMetric.Spec.Labels {
107-
labelsMap[label] = nil
108-
}
109110
var invalidMapping []string
110111
for toRemap := range fMetric.Spec.Remap {
111112
if _, ok := labelsMap[toRemap]; !ok {
@@ -117,6 +118,20 @@ func validateFlowMetric(_ context.Context, fMetric *FlowMetric) (admission.Warni
117118
fmt.Sprintf("some fields defined for remapping are not defined as labels: %v", invalidMapping)))
118119
}
119120
}
121+
122+
// Only fields defined as Labels are valid for flattening
123+
if len(fMetric.Spec.Flatten) != 0 {
124+
var invalidFlatten []string
125+
for _, toFlatten := range fMetric.Spec.Flatten {
126+
if _, ok := labelsMap[toFlatten]; !ok {
127+
invalidFlatten = append(invalidFlatten, toFlatten)
128+
}
129+
}
130+
if len(invalidFlatten) > 0 {
131+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "flatten"), fMetric.Spec.Flatten,
132+
fmt.Sprintf("some fields defined for flattening are not defined as labels: %v", invalidFlatten)))
133+
}
134+
}
120135
}
121136

122137
if fMetric.Spec.ValueField != "" {

apis/flowmetrics/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/flows.netobserv.io_flowmetrics.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ spec:
196196
- matchType
197197
type: object
198198
type: array
199+
flatten:
200+
description: |-
201+
`flatten` is a list of list-type fields that must be flattened, such as Interfaces and NetworkEvents. Flattened fields generate one metric per item in that field.
202+
For instance, when flattening `Interfaces` on a bytes counter, a flow having Interfaces [br-ex, ens5] increases one counter for `br-ex` and another for `ens5`.
203+
items:
204+
type: string
205+
type: array
199206
labels:
200207
description: |-
201208
`labels` is a list of fields that should be used as Prometheus labels, also known as dimensions.

config/crd/bases/flows.netobserv.io_flowmetrics.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ spec:
186186
- matchType
187187
type: object
188188
type: array
189+
flatten:
190+
description: |-
191+
`flatten` is a list of list-type fields that must be flattened, such as Interfaces and NetworkEvents. Flattened fields generate one metric per item in that field.
192+
For instance, when flattening `Interfaces` on a bytes counter, a flow having Interfaces [br-ex, ens5] increases one counter for `br-ex` and another for `ens5`.
193+
items:
194+
type: string
195+
type: array
189196
labels:
190197
description: |-
191198
`labels` is a list of fields that should be used as Prometheus labels, also known as dimensions.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Count flows per network interface
2+
# More examples in https://github.com/netobserv/network-observability-operator/tree/main/config/samples/flowmetrics
3+
apiVersion: flows.netobserv.io/v1alpha1
4+
kind: FlowMetric
5+
metadata:
6+
name: per-interface
7+
namespace: netobserv
8+
spec:
9+
metricName: per_interface
10+
type: Counter
11+
labels: [Interfaces]
12+
flatten: [Interfaces]
13+
remap:
14+
Interfaces: interface

controllers/flp/flp_pipeline_builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func flowMetricToFLP(flowMetric *metricslatest.FlowMetricSpec) (*api.MetricsItem
270270
Filters: []api.MetricsFilter{},
271271
Labels: flowMetric.Labels,
272272
Remap: flowMetric.Remap,
273+
Flatten: flowMetric.Flatten,
273274
ValueKey: flowMetric.ValueField,
274275
}
275276
for _, f := range metrics.GetFilters(flowMetric) {

docs/FlowMetric.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ be used to eliminate duplicates: `Duplicate != "true"` and `FlowDirection = "0"`
151151
Refer to the documentation for the list of available fields: https://docs.openshift.com/container-platform/latest/observability/network_observability/json-flows-format-reference.html.<br/>
152152
</td>
153153
<td>false</td>
154+
</tr><tr>
155+
<td><b>flatten</b></td>
156+
<td>[]string</td>
157+
<td>
158+
`flatten` is a list of list-type fields that must be flattened, such as Interfaces and NetworkEvents. Flattened fields generate one metric per item in that field.
159+
For instance, when flattening `Interfaces` on a bytes counter, a flow having Interfaces [br-ex, ens5] increases one counter for `br-ex` and another for `ens5`.<br/>
160+
</td>
161+
<td>false</td>
154162
</tr><tr>
155163
<td><b>labels</b></td>
156164
<td>[]string</td>

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ require (
8888
)
8989

9090
replace github.com/prometheus/common v0.55.0 => github.com/netobserv/prometheus-common v0.55.0-netobserv
91+
92+
replace github.com/netobserv/flowlogs-pipeline => github.com/jotak/flowlogs-pipeline v0.0.0-20241113134338-68583d85dd5e

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
5656
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
5757
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
5858
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
59+
github.com/jotak/flowlogs-pipeline v0.0.0-20241113134338-68583d85dd5e h1:+Yn7FfcXQPdb99vl13e8Tppyeq0snWMz+zJH/Q2NMX8=
60+
github.com/jotak/flowlogs-pipeline v0.0.0-20241113134338-68583d85dd5e/go.mod h1:wnCpWttAFkLSSxOcfCkd9zA5pwV/1OcxS5tAfAxNWEc=
5961
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
6062
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
6163
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -88,8 +90,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
8890
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
8991
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
9092
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
91-
github.com/netobserv/flowlogs-pipeline v1.6.1-crc0.0.20240920063618-2ea1a7ce77b8 h1:Wumfysno927/yCK8RAG/1AtTDpQQzSn9PkoGBLu2Khk=
92-
github.com/netobserv/flowlogs-pipeline v1.6.1-crc0.0.20240920063618-2ea1a7ce77b8/go.mod h1:VVHQK3sEL91gl6CQkrcE+RAi9vMat5gaVaZLqPDoKlQ=
9393
github.com/netobserv/prometheus-common v0.55.0-netobserv h1:Fapr74g0S3gRh/kTTyv9Ytm4DJJfFuUTEToiU/np9eg=
9494
github.com/netobserv/prometheus-common v0.55.0-netobserv/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
9595
github.com/onsi/ginkgo/v2 v2.20.2 h1:7NVCeyIWROIAheY21RLS+3j2bb52W0W82tkberYytp4=

0 commit comments

Comments
 (0)