@@ -418,7 +418,7 @@ function copyFLPConfig() {
418418
419419# get network enrich stage
420420function 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
433446function 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
0 commit comments