|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +JOB=${1:-} |
| 6 | +INSTANCE=${2:-} |
| 7 | +METRICS_DIR=${3:-metrics} |
| 8 | + |
| 9 | +if [[ -z "$JOB" || -z "$INSTANCE" ]]; then |
| 10 | + echo "Usage: $0 <job> <instance> [metrics_dir]" |
| 11 | + echo " job - GitHub workflow name" |
| 12 | + echo " instance - GitHub run ID" |
| 13 | + echo " metrics_dir - (optional) Directory containing JSON metrics files (default: metrics)" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +OUTPUT_FILE="$METRICS_DIR/prometheus.txt" |
| 18 | + |
| 19 | +if [[ ! -d "$METRICS_DIR" ]]; then |
| 20 | + echo "Metrics directory '$METRICS_DIR' does not exist." |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +shopt -s nullglob |
| 25 | +FILES=("$METRICS_DIR"/*.json) |
| 26 | + |
| 27 | +# Filter out the output file from being processed |
| 28 | +FILES=("${FILES[@]/$OUTPUT_FILE}") |
| 29 | + |
| 30 | +if [[ ${#FILES[@]} -eq 0 ]]; then |
| 31 | + echo "No JSON files found in '$METRICS_DIR'." |
| 32 | + exit 0 |
| 33 | +fi |
| 34 | + |
| 35 | +> "$OUTPUT_FILE" |
| 36 | + |
| 37 | +for file in "${FILES[@]}"; do |
| 38 | + if [[ ! -s "$file" ]]; then |
| 39 | + echo "Skipping empty file: $file" |
| 40 | + continue |
| 41 | + fi |
| 42 | + |
| 43 | + filename=$(basename "$file") |
| 44 | + scenario_id="${filename%%-*}" |
| 45 | + scenario_name=$(jq -r '.scenarioName // empty' "$file") |
| 46 | + data_length=$(jq '.data | length' "$file") |
| 47 | + |
| 48 | + echo "# Metrics from $filename" >> "$OUTPUT_FILE" |
| 49 | + |
| 50 | + if [[ "$data_length" -eq 0 ]]; then |
| 51 | + echo "e2e_cpu_seconds_total{scenario_name=\"$scenario_name\",scenario_id=\"$scenario_id\",job=\"$JOB\",instance=\"$INSTANCE\"} 0" >> "$OUTPUT_FILE" |
| 52 | + echo "e2e_memory_rss_bytes{scenario_name=\"$scenario_name\",scenario_id=\"$scenario_id\",job=\"$JOB\",instance=\"$INSTANCE\"} 0" >> "$OUTPUT_FILE" |
| 53 | + else |
| 54 | + jq -c '.data[]' "$file" | while read -r entry; do |
| 55 | + timestamp=$(echo "$entry" | jq -r '.timestamp') |
| 56 | + cpu=$(echo "$entry" | jq -r '.cpu') |
| 57 | + memory=$(echo "$entry" | jq -r '.memory') |
| 58 | + |
| 59 | + echo "e2e_cpu_seconds_total{scenario_name=\"$scenario_name\",scenario_id=\"$scenario_id\",job=\"$JOB\",instance=\"$INSTANCE\",timestamp=\"$timestamp\"} $cpu" >> "$OUTPUT_FILE" |
| 60 | + echo "e2e_memory_rss_bytes{scenario_name=\"$scenario_name\",scenario_id=\"$scenario_id\",job=\"$JOB\",instance=\"$INSTANCE\",timestamp=\"$timestamp\"} $memory" >> "$OUTPUT_FILE" |
| 61 | + done |
| 62 | + fi |
| 63 | +done |
| 64 | + |
| 65 | +echo "Metrics converted to Prometheus format: $OUTPUT_FILE" |
0 commit comments