mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 04:45:49 +00:00
fix power api install
This commit is contained in:
parent
8ea0831d9e
commit
21bda69797
1 changed files with 26 additions and 57 deletions
83
.github/workflows/pipeline.yml
vendored
83
.github/workflows/pipeline.yml
vendored
|
@ -6,15 +6,10 @@ on:
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ pipeline-optimization ]
|
branches: [ pipeline-optimization ]
|
||||||
|
|
||||||
# Add concurrency limit to prevent resource contention
|
|
||||||
concurrency:
|
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-with-metrics:
|
build-with-metrics:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 60 # Add overall job timeout
|
timeout-minutes: 60
|
||||||
|
|
||||||
services:
|
services:
|
||||||
prometheus:
|
prometheus:
|
||||||
|
@ -26,7 +21,6 @@ jobs:
|
||||||
--health-interval 10s
|
--health-interval 10s
|
||||||
--health-timeout 5s
|
--health-timeout 5s
|
||||||
--health-retries 3
|
--health-retries 3
|
||||||
--health-start-period 10s
|
|
||||||
|
|
||||||
pushgateway:
|
pushgateway:
|
||||||
image: prom/pushgateway:latest
|
image: prom/pushgateway:latest
|
||||||
|
@ -37,14 +31,11 @@ jobs:
|
||||||
--health-interval 10s
|
--health-interval 10s
|
||||||
--health-timeout 5s
|
--health-timeout 5s
|
||||||
--health-retries 3
|
--health-retries 3
|
||||||
--health-start-period 10s
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
|
||||||
fetch-depth: 0 # Fetch all history for better metrics
|
|
||||||
|
|
||||||
# Installation and setup of monitoring tools with error handling
|
# Installation and setup of monitoring tools
|
||||||
- name: Setup monitoring tools
|
- name: Setup monitoring tools
|
||||||
id: setup-monitoring
|
id: setup-monitoring
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
|
@ -56,39 +47,18 @@ jobs:
|
||||||
sudo apt-get install -y powerstat linux-tools-common linux-tools-generic || (echo "Failed to install powerstat and linux tools" && exit 1)
|
sudo apt-get install -y powerstat linux-tools-common linux-tools-generic || (echo "Failed to install powerstat and linux tools" && exit 1)
|
||||||
echo "::endgroup::"
|
echo "::endgroup::"
|
||||||
|
|
||||||
echo "::group::Installing PowerAPI"
|
|
||||||
sudo snap install powerapi || (echo "Failed to install PowerAPI" && exit 1)
|
|
||||||
echo "::endgroup::"
|
|
||||||
|
|
||||||
echo "::group::Setting up node exporter"
|
echo "::group::Setting up node exporter"
|
||||||
curl -L --retry 3 https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o node_exporter.tar.gz || (echo "Failed to download node exporter" && exit 1)
|
curl -L --retry 3 https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz -o node_exporter.tar.gz || (echo "Failed to download node exporter" && exit 1)
|
||||||
tar xvfz node_exporter.tar.gz || (echo "Failed to extract node exporter" && exit 1)
|
tar xvfz node_exporter.tar.gz || (echo "Failed to extract node exporter" && exit 1)
|
||||||
echo "::endgroup::"
|
echo "::endgroup::"
|
||||||
|
|
||||||
# Start monitoring tools with improved configuration and error handling
|
# Start monitoring tools with improved configuration
|
||||||
- name: Start monitoring
|
- name: Start monitoring
|
||||||
id: start-monitoring
|
id: start-monitoring
|
||||||
timeout-minutes: 2
|
timeout-minutes: 2
|
||||||
run: |
|
run: |
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
# Start PowerAPI with retry mechanism
|
|
||||||
max_retries=3
|
|
||||||
retry_count=0
|
|
||||||
while [ $retry_count -lt $max_retries ]; do
|
|
||||||
if sudo powerapi --pid $$ --frequency 1000 --output prometheus --pushgateway-url http://localhost:9091/metrics/job/powerapi & then
|
|
||||||
echo "POWERAPI_PID=$!" >> $GITHUB_ENV
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
retry_count=$((retry_count+1))
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ $retry_count -eq $max_retries ]; then
|
|
||||||
echo "Failed to start PowerAPI after $max_retries attempts"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Start node exporter with health check
|
# Start node exporter with health check
|
||||||
./node_exporter-*/node_exporter --web.listen-address=":9100" &
|
./node_exporter-*/node_exporter --web.listen-address=":9100" &
|
||||||
echo "NODE_EXPORTER_PID=$!" >> $GITHUB_ENV
|
echo "NODE_EXPORTER_PID=$!" >> $GITHUB_ENV
|
||||||
|
@ -112,7 +82,7 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
start_time=$(date +%s%N)
|
start_time=$(date +%s%N)
|
||||||
./mvnw -B verify -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
|
./mvnw -B verify
|
||||||
build_status=$?
|
build_status=$?
|
||||||
end_time=$(date +%s%N)
|
end_time=$(date +%s%N)
|
||||||
echo "BUILD_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
echo "BUILD_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
||||||
|
@ -120,12 +90,12 @@ jobs:
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
id: test
|
id: test
|
||||||
if: success() || failure() # Run even if build fails
|
if: success() || failure()
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
run: |
|
run: |
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
start_time=$(date +%s%N)
|
start_time=$(date +%s%N)
|
||||||
./mvnw test -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn
|
./mvnw test
|
||||||
test_status=$?
|
test_status=$?
|
||||||
end_time=$(date +%s%N)
|
end_time=$(date +%s%N)
|
||||||
echo "TEST_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
echo "TEST_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
||||||
|
@ -160,7 +130,6 @@ jobs:
|
||||||
start_time=$(date +%s%N)
|
start_time=$(date +%s%N)
|
||||||
kubectl apply -f k8s/ || (echo "Failed to apply Kubernetes manifests" && exit 1)
|
kubectl apply -f k8s/ || (echo "Failed to apply Kubernetes manifests" && exit 1)
|
||||||
|
|
||||||
# Wait for deployment with proper error handling
|
|
||||||
if ! kubectl wait --for=condition=ready pod -l app=petclinic --timeout=180s; then
|
if ! kubectl wait --for=condition=ready pod -l app=petclinic --timeout=180s; then
|
||||||
echo "::error::Deployment failed - collecting debug information"
|
echo "::error::Deployment failed - collecting debug information"
|
||||||
kubectl describe pods -l app=petclinic
|
kubectl describe pods -l app=petclinic
|
||||||
|
@ -171,14 +140,14 @@ jobs:
|
||||||
end_time=$(date +%s%N)
|
end_time=$(date +%s%N)
|
||||||
echo "DEPLOY_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
echo "DEPLOY_TIME=$((($end_time - $start_time)/1000000))" >> $GITHUB_ENV
|
||||||
|
|
||||||
# Export metrics with improved error handling
|
# Export metrics with proper function definition
|
||||||
- name: Export metrics to Prometheus
|
- name: Export metrics to Prometheus
|
||||||
if: always() # Run even if previous steps failed
|
if: always()
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
run: |
|
run: |
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
# Function to safely export metric
|
# Define the export_metric function
|
||||||
export_metric() {
|
export_metric() {
|
||||||
local metric_name=$1
|
local metric_name=$1
|
||||||
local metric_value=$2
|
local metric_value=$2
|
||||||
|
@ -198,22 +167,27 @@ jobs:
|
||||||
export_metric "pipeline_docker_build_duration_ms" "${DOCKER_BUILD_TIME}" "docker-build"
|
export_metric "pipeline_docker_build_duration_ms" "${DOCKER_BUILD_TIME}" "docker-build"
|
||||||
export_metric "pipeline_deploy_duration_ms" "${DEPLOY_TIME}" "deploy"
|
export_metric "pipeline_deploy_duration_ms" "${DEPLOY_TIME}" "deploy"
|
||||||
|
|
||||||
# Export power consumption metrics with error handling
|
# Collect additional resource metrics with function definition
|
||||||
if [ -f energy_metrics.csv ]; then
|
|
||||||
while IFS=, read -r timestamp watts; do
|
|
||||||
export_metric "power_consumption_watts" "$watts" "power" || continue
|
|
||||||
done < energy_metrics.csv
|
|
||||||
else
|
|
||||||
echo "::warning::energy_metrics.csv not found"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Collect additional resource metrics
|
|
||||||
- name: Collect resource metrics
|
- name: Collect resource metrics
|
||||||
if: always()
|
if: always()
|
||||||
timeout-minutes: 2
|
timeout-minutes: 2
|
||||||
run: |
|
run: |
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
|
||||||
|
# Define the export_metric function again since it's a new shell context
|
||||||
|
export_metric() {
|
||||||
|
local metric_name=$1
|
||||||
|
local metric_value=$2
|
||||||
|
local stage=$3
|
||||||
|
|
||||||
|
if [ -n "$metric_value" ]; then
|
||||||
|
echo "${metric_name}{stage=\"${stage}\",project=\"petclinic\"} ${metric_value}" | \
|
||||||
|
curl --retry 3 --retry-delay 2 --max-time 10 --silent --show-error \
|
||||||
|
--data-binary @- http://localhost:9091/metrics/job/petclinic-pipeline || \
|
||||||
|
echo "::warning::Failed to export ${metric_name} for ${stage}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Memory usage metric with error handling
|
# Memory usage metric with error handling
|
||||||
mem_usage=$(free -b | grep Mem: | awk '{print $3}') || echo "::warning::Failed to collect memory usage"
|
mem_usage=$(free -b | grep Mem: | awk '{print $3}') || echo "::warning::Failed to collect memory usage"
|
||||||
if [ -n "$mem_usage" ]; then
|
if [ -n "$mem_usage" ]; then
|
||||||
|
@ -226,7 +200,7 @@ jobs:
|
||||||
export_metric "pipeline_cpu_usage_percent" "$cpu_usage" "cpu"
|
export_metric "pipeline_cpu_usage_percent" "$cpu_usage" "cpu"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Stop monitoring tools and collect metrics
|
# Collect final metrics
|
||||||
- name: Collect final metrics
|
- name: Collect final metrics
|
||||||
if: always()
|
if: always()
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
|
@ -236,11 +210,7 @@ jobs:
|
||||||
# End timestamp
|
# End timestamp
|
||||||
date +%s%N > pipeline_end_time.txt
|
date +%s%N > pipeline_end_time.txt
|
||||||
|
|
||||||
# Stop monitoring processes safely
|
# Stop node exporter
|
||||||
if [ -n "$POWERAPI_PID" ]; then
|
|
||||||
sudo kill $POWERAPI_PID || echo "::warning::Failed to stop PowerAPI"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$NODE_EXPORTER_PID" ]; then
|
if [ -n "$NODE_EXPORTER_PID" ]; then
|
||||||
kill $NODE_EXPORTER_PID || echo "::warning::Failed to stop node exporter"
|
kill $NODE_EXPORTER_PID || echo "::warning::Failed to stop node exporter"
|
||||||
fi
|
fi
|
||||||
|
@ -268,7 +238,6 @@ jobs:
|
||||||
with:
|
with:
|
||||||
name: pipeline-metrics
|
name: pipeline-metrics
|
||||||
path: |
|
path: |
|
||||||
energy_metrics.csv
|
|
||||||
system_metrics.txt
|
system_metrics.txt
|
||||||
memory_metrics.txt
|
memory_metrics.txt
|
||||||
disk_metrics.txt
|
disk_metrics.txt
|
||||||
|
|
Loading…
Reference in a new issue