OSVauco/infrastructure/04-observability-setup.sh
Chris Christiansen 9fcb9c354a
Some checks are pending
Check Python Version Consistency / Check Python Version (push) Waiting to run
feat(core): Fresh initialization - Deploy v3.6.1 Singularity Architecture
2026-09-03 04:03:09 +00:00

79 lines
2.6 KiB
Bash

#!/usr/bin/env bash
# 04-observability-setup.sh — Configure Cloud Monitoring, alerting, uptime checks
# Idempotent
# Source .env before running: source .env
set -euo pipefail
: "${PROJECT_ID:?Set PROJECT_ID}"
: "${REGION:?Set REGION}"
: "${CLOUD_RUN_SERVICE:?Set CLOUD_RUN_SERVICE}"
: "${ALERT_EMAIL:?Set ALERT_EMAIL}"
echo "=== 04: Setting up observability for ${PROJECT_ID} ==="
bash "$(dirname "$0")/00-authcheck.sh"
gcloud services enable \
monitoring.googleapis.com \
logging.googleapis.com \
cloudtrace.googleapis.com \
--project="${PROJECT_ID}" --quiet
echo "✓ Monitoring APIs enabled"
# 1. Notification channel (email)
CHANNEL_FILE=$(gcloud alpha monitoring channels list \
--filter="type=email AND labels.email_address=${ALERT_EMAIL}" \
--format="value(name)" --project="${PROJECT_ID}" 2>/dev/null | head -1 || true)
if [[ -z "${CHANNEL_FILE}" ]]; then
cat > /tmp/channel.json << EOF
{
"type": "email",
"displayName": "OSVauco Alert Email",
"labels": { "email_address": "${ALERT_EMAIL}" }
}
EOF
NOTIFICATION_CHANNEL=$(gcloud alpha monitoring channels create \
--channel-content-from-file=/tmp/channel.json \
--project="${PROJECT_ID}" \
--format="value(name)")
echo "✓ Notification channel created: ${NOTIFICATION_CHANNEL}"
else
NOTIFICATION_CHANNEL="${CHANNEL_FILE}"
echo "✓ Notification channel already exists: ${NOTIFICATION_CHANNEL}"
fi
# 2. Create log-based metric for agent errors
if ! gcloud logging metrics describe agent-error-count \
--project="${PROJECT_ID}" >/dev/null 2>&1; then
gcloud logging metrics create agent-error-count \
--description="Count of ERROR severity logs from agent" \
--log-filter="resource.type=\"cloud_run_revision\" severity=ERROR" \
--project="${PROJECT_ID}"
echo "✓ Log-based metric 'agent-error-count' created"
else
echo "✓ Log-based metric already exists"
fi
# 3. Uptime check for Cloud Run service
SERVICE_URL=$(gcloud run services describe "${CLOUD_RUN_SERVICE}" \
--region="${REGION}" \
--format="value(status.url)" 2>/dev/null || echo "")
if [[ -n "${SERVICE_URL}" ]]; then
HOST=$(echo "${SERVICE_URL}" | sed 's|https://||')
gcloud monitoring uptime create \
--display-name="${CLOUD_RUN_SERVICE}-uptime" \
--protocol=HTTPS \
--request-path="/health" \
--port=443 \
--monitored-resource-type=uptime_url \
--project="${PROJECT_ID}" \
--hostname="${HOST}" 2>/dev/null || true
echo "✓ Uptime check configured for ${SERVICE_URL}"
fi
echo ""
echo "=== 04: Observability setup COMPLETE ==="
echo " Dashboard: https://console.cloud.google.com/monitoring?project=${PROJECT_ID}"