3 # Copyright 2023 Nordix Foundation.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 set -o errexit # Exit on most errors
19 set -o nounset # Disallow expansion of unset variables
20 set -o pipefail # Use last non-zero exit code in a pipeline
21 #set -o xtrace # Uncomment for debugging
23 function script_usage() {
26 -h|--help Displays this help
27 -u|--metrics-url=URL URL to Prometheus metrics
28 Default: http://localhost:8887/manage/prometheus
29 -o|--output=FILE Path to output file
30 Default: metrics-reports/metrics-[timestamp].tsv
34 function parse_params() {
35 # Set parameter defaults
36 METRICS_URL="http://localhost:8887/manage/prometheus"
37 OUTFILE="metrics-reports/metrics-$(date --iso-8601=seconds).tsv"
38 TEMP_DIR="/tmp/cps-metrics"
42 while [[ $# -gt 0 ]]; do
59 echo "Invalid parameter was provided: $param" >&2
67 function generate_report() {
68 # Create needed directories.
69 mkdir -p $TEMP_DIR "$(dirname "$OUTFILE")"
71 # Scrape raw metrics (suppress progress meter).
72 curl --fail --silent --show-error --output $TEMP_DIR/metrics-raw.txt "$METRICS_URL"
74 # Remove comments, sort by name, and separate by tabs.
75 grep --invert-match "^#" $TEMP_DIR/metrics-raw.txt | sort | sed 's/,[}]/}\t/' >$TEMP_DIR/metrics-all.txt
77 # Extract useful metrics.
78 grep -E "^cps_|^spring_data_" $TEMP_DIR/metrics-all.txt >$TEMP_DIR/metrics-cps.txt
80 # Extract into columns.
81 grep "_count" $TEMP_DIR/metrics-cps.txt | sed 's/_count//' | cut -f 1 >$TEMP_DIR/column1.txt
82 grep "_count" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column2.txt
83 grep "_sum" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column3.txt
84 grep "_max" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column4.txt
86 # Combine columns into report.
87 paste $TEMP_DIR/column{1,2,3,4}.txt >$TEMP_DIR/report.txt
89 # Sort by Sum (column 3), descending.
90 sort --general-numeric-sort --reverse --field-separator=$'\t' --key=3 $TEMP_DIR/report.txt >$TEMP_DIR/report-sorted.txt
92 # Compile final report, with column headers.
93 echo -e "Method\tCount\tSum\tMax" >"$OUTFILE"
94 cat $TEMP_DIR/report-sorted.txt >>"$OUTFILE"
96 # Output path to generated file
101 rm -f $TEMP_DIR/* && rmdir $TEMP_DIR 2>/dev/null
103 # Set up the cleanup function to be triggered upon script exit