Skip to content

Commit 9e2bd35

Browse files
committed
add includeList option
1 parent 45115e8 commit 9e2bd35

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

docs/netobserv_cli.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ $ oc netobserv metrics [<option>]
185185
|--sports| filter on either of two source ports | -
186186
|--tcp_flags| filter TCP flags | -
187187
|--interfaces| interfaces to monitor | -
188+
|--include_list| list of metric names to generate | namespace_flows_total,node_ingress_bytes_total,node_egress_bytes_total,workload_ingress_bytes_total
188189
|===
189190

190191
.Example running metrics capture for TCP drops

scripts/functions.sh

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ function copyFLPConfig() {
418418

419419
# get network enrich stage
420420
function getNetworkEnrichStage() {
421-
enrichIndex=$("$YQ_BIN" e -oj ".parameters[] | select(.name==\"enrich\") | document_index" "$json")
421+
enrichIndex=$("$YQ_BIN" e -oj ".parameters[] | select(.name==\"enrich\") | path | .[-1]" "$json")
422422
enrichContent=$("$YQ_BIN" e -oj ".parameters[$enrichIndex]" "$json")
423423
enrichJson="${MANIFEST_OUTPUT_PATH}/enrich.json"
424424
echo "$enrichContent" >${enrichJson}
@@ -429,6 +429,19 @@ function overrideNetworkEnrichStage() {
429429
"$YQ_BIN" e -oj --inplace ".parameters[$enrichIndex] = $enrichJsonStr" "$json"
430430
}
431431

432+
# get prometheus stage
433+
function getPromStage() {
434+
promIndex=$("$YQ_BIN" e -oj ".parameters[] | select(.name==\"prometheus\") | path | .[-1]" "$json")
435+
promContent=$("$YQ_BIN" e -oj ".parameters[$promIndex]" "$json")
436+
promJson="${MANIFEST_OUTPUT_PATH}/prom.json"
437+
echo "$promContent" >${promJson}
438+
}
439+
440+
function overridePromStage() {
441+
promJsonStr=$(cat "$promJson")
442+
"$YQ_BIN" e -oj --inplace ".parameters[$promIndex] = $promJsonStr" "$json"
443+
}
444+
432445
# update FLP Config
433446
function updateFLPConfig() {
434447
jsonContent=$(cat "$1")
@@ -631,6 +644,41 @@ function edit_manifest() {
631644
fi
632645
"$YQ_BIN" e --inplace ".spec.template.spec.nodeSelector.\"$key\" |= \"$val\"" "$manifest"
633646
;;
647+
"include_list")
648+
# restrict metrics to matching items
649+
copyFLPConfig "$manifest"
650+
getPromStage
651+
652+
# list all matching metrics separated by new lines first
653+
filteredMetrics=""
654+
IFS=','
655+
for match in $2; do
656+
found=$("$YQ_BIN" -r ".encode.prom.metrics[] | select(.name | contains(\"$match\")).name" "$promJson")
657+
if [ "${#filteredMetrics}" -gt 0 ]; then
658+
filteredMetrics="${filteredMetrics}"$'\n'"${found}"
659+
else
660+
filteredMetrics="$found"
661+
fi
662+
done
663+
664+
# then, format these for YQ filter function
665+
echo "Matching metrics:"
666+
match=""
667+
IFS=$'\n'
668+
for item in $filteredMetrics; do
669+
echo " - $item"
670+
if [ "${#match}" -gt 0 ]; then
671+
match="$match,\"$item\""
672+
else
673+
match="\"$item\""
674+
fi
675+
done
676+
677+
"$YQ_BIN" e --inplace ".encode.prom.metrics | filter(.name == ($match))" "$promJson"
678+
679+
overridePromStage
680+
updateFLPConfig "$json" "$manifest"
681+
;;
634682
esac
635683
}
636684

@@ -679,8 +727,12 @@ function check_args_and_apply() {
679727
*enable_pkt_drop) # Enable packet drop
680728
if [[ "$command" == "flows" || "$command" == "metrics" ]]; then
681729
defaultValue "true"
682-
if [[ "$value" == "true" || "$value" == "false" ]]; then
730+
if [[ "$value" == "true" ]]; then
683731
edit_manifest "pkt_drop_enable" "$value"
732+
includeList="$includeList,workload_egress_bytes_total,namespace_drop_packets_total"
733+
elif [[ "$value" == "false" ]]; then
734+
# nothing to do there
735+
echo
684736
else
685737
echo "invalid value for --enable_pkt_drop"
686738
fi
@@ -692,8 +744,12 @@ function check_args_and_apply() {
692744
*enable_dns) # Enable DNS
693745
if [[ "$command" == "flows" || "$command" == "metrics" ]]; then
694746
defaultValue "true"
695-
if [[ "$value" == "true" || "$value" == "false" ]]; then
747+
if [[ "$value" == "true" ]]; then
696748
edit_manifest "dns_enable" "$value"
749+
includeList="$includeList,namespace_dns_latency_seconds"
750+
elif [[ "$value" == "false" ]]; then
751+
# nothing to do there
752+
echo
697753
else
698754
echo "invalid value for --enable_dns"
699755
fi
@@ -705,8 +761,12 @@ function check_args_and_apply() {
705761
*enable_rtt) # Enable RTT
706762
if [[ "$command" == "flows" || "$command" == "metrics" ]]; then
707763
defaultValue "true"
708-
if [[ "$value" == "true" || "$value" == "false" ]]; then
764+
if [[ "$value" == "true" ]]; then
709765
edit_manifest "rtt_enable" "$value"
766+
includeList="$includeList,namespace_rtt_seconds"
767+
elif [[ "$value" == "false" ]]; then
768+
# nothing to do there
769+
echo
710770
else
711771
echo "invalid value for --enable_rtt"
712772
fi
@@ -718,8 +778,12 @@ function check_args_and_apply() {
718778
*enable_network_events) # Enable Network events monitoring
719779
if [[ "$command" == "flows" || "$command" == "metrics" ]]; then
720780
defaultValue "true"
721-
if [[ "$value" == "true" || "$value" == "false" ]]; then
781+
if [[ "$value" == "true" ]]; then
722782
edit_manifest "network_events_enable" "$value"
783+
includeList="$includeList,namespace_network_policy_events_total"
784+
elif [[ "$value" == "false" ]]; then
785+
# nothing to do there
786+
echo
723787
else
724788
echo "invalid value for --enable_network_events"
725789
fi
@@ -886,6 +950,14 @@ function check_args_and_apply() {
886950
echo "invalid value for --get-subnets"
887951
fi
888952
;;
953+
*include_list) # Restrict metrics capture
954+
if [[ "$command" == "metrics" ]]; then
955+
includeList="$value"
956+
else
957+
echo "--include_list is invalid option for $command"
958+
exit 1
959+
fi
960+
;;
889961
*) # Invalid option
890962
echo "Invalid option: $key" >&2
891963
exit 1
@@ -903,8 +975,10 @@ function check_args_and_apply() {
903975
echo
904976
exit 1
905977
fi
978+
elif [[ "$command" = "metrics" ]]; then
979+
# always restrict generated metrics
980+
edit_manifest "include_list" "$includeList"
906981
fi
907-
908982
yaml="$(cat "$manifest")"
909983
applyYAML "$yaml"
910984
if [[ "$outputYAML" == "false" ]]; then

scripts/generate-doc.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $ oc netobserv flows [<feature_option>] [<command_options>]
6868
features_usage
6969
collector_usage
7070
filters_usage
71-
specific_filters_usage
71+
flowsAndMetrics_filters_usage
7272
echo -e "|==="
7373
# flows example
7474
echo "
@@ -118,7 +118,8 @@ $ oc netobserv metrics [<option>]
118118
| Option | Description | Default"
119119
features_usage
120120
filters_usage
121-
specific_filters_usage
121+
flowsAndMetrics_filters_usage
122+
metrics_options
122123
echo -e "|==="
123124
# Metrics example
124125
echo "

scripts/help.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env bash
22

3+
# metrics includeList
4+
includeList="namespace_flows_total,node_ingress_bytes_total,node_egress_bytes_total,workload_ingress_bytes_total"
5+
36
# display main help
47
function help {
58
echo
@@ -107,11 +110,16 @@ function filters_usage {
107110
echo " --tcp_flags: filter TCP flags (default: n/a)"
108111
}
109112

110-
function specific_filters_usage {
111-
# specific filters
113+
# specific filters for flows and metrics
114+
function flowsAndMetrics_filters_usage {
112115
echo " --interfaces: interfaces to monitor (default: n/a)"
113116
}
114117

118+
# specific filters for metrics
119+
function metrics_options {
120+
echo " --include_list: list of metric names to generate (default: $includeList)"
121+
}
122+
115123
function flows_usage {
116124
echo
117125
echo "Netobserv allows you to capture flows from your cluster."
@@ -124,7 +132,7 @@ function flows_usage {
124132
echo
125133
echo "filters:"
126134
filters_usage
127-
specific_filters_usage
135+
flowsAndMetrics_filters_usage
128136
echo
129137
echo "options:"
130138
collector_usage
@@ -158,9 +166,12 @@ function metrics_usage {
158166
echo
159167
echo "filters:"
160168
filters_usage
161-
specific_filters_usage
169+
flowsAndMetrics_filters_usage
162170
echo "options:"
163171
script_usage
172+
metrics_options
173+
echo
174+
echo "More information, with full list of available metrics: https://github.com/netobserv/network-observability-operator/blob/main/docs/Metrics.md"
164175
}
165176

166177
function follow_usage {

0 commit comments

Comments
 (0)