Skip to content

Commit 109db73

Browse files
committed
Enhance log directory handling in alert scripts and logging functions
- Updated `alertRules.sh`, `escalation.sh`, and `sendAlert.sh` to handle log directory creation more gracefully, implementing fallback locations if the primary directory cannot be created due to permission issues. - Improved `init_logging` in `loggingFunctions.sh` to attempt directory creation first and only use fallback options if necessary, enhancing logging reliability. - Modified `checkPlanetNotes.sh` to conditionally send alerts based on the availability of the `send_alert` function and the test mode status, improving alert handling in tests. - Updated unit test script `test_collect_cron_metrics.sh` to ensure the log directory is set correctly for testing, enhancing test reliability.
1 parent 12849d2 commit 109db73

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

bin/alerts/alertRules.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ elif [[ -z "${LOG_DIR:-}" ]]; then
3434
export LOG_DIR="${PROJECT_ROOT}/logs"
3535
fi
3636

37-
# Ensure log directory exists
38-
mkdir -p "${LOG_DIR}"
37+
# Ensure log directory exists (handle permission errors gracefully)
38+
mkdir -p "${LOG_DIR}" 2>/dev/null || {
39+
# If we can't create LOG_DIR, try fallback locations
40+
if [[ -n "${TMP_DIR:-}" ]]; then
41+
export LOG_DIR="${TMP_DIR}/logs"
42+
elif [[ -n "${PROJECT_ROOT:-}" ]] && [[ -d "${PROJECT_ROOT}" ]]; then
43+
export LOG_DIR="${PROJECT_ROOT}/tmp/logs"
44+
else
45+
export LOG_DIR="/tmp/osm-notes-monitoring-logs"
46+
fi
47+
mkdir -p "${LOG_DIR}" 2>/dev/null || export LOG_DIR="/tmp"
48+
}
3949

4050
# Initialize logging
4151
init_logging "${LOG_DIR}/alert_rules.log" "alertRules"

bin/alerts/escalation.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ elif [[ -z "${LOG_DIR:-}" ]]; then
3434
export LOG_DIR="${PROJECT_ROOT}/logs"
3535
fi
3636

37-
# Ensure log directory exists
38-
mkdir -p "${LOG_DIR}"
37+
# Ensure log directory exists (handle permission errors gracefully)
38+
mkdir -p "${LOG_DIR}" 2>/dev/null || {
39+
# If we can't create LOG_DIR, try fallback locations
40+
if [[ -n "${TMP_DIR:-}" ]]; then
41+
export LOG_DIR="${TMP_DIR}/logs"
42+
elif [[ -n "${PROJECT_ROOT:-}" ]] && [[ -d "${PROJECT_ROOT}" ]]; then
43+
export LOG_DIR="${PROJECT_ROOT}/tmp/logs"
44+
else
45+
export LOG_DIR="/tmp/osm-notes-monitoring-logs"
46+
fi
47+
mkdir -p "${LOG_DIR}" 2>/dev/null || export LOG_DIR="/tmp"
48+
}
3949

4050
# Only initialize if not in test mode or if script is executed directly
4151
if [[ "${TEST_MODE:-false}" != "true" ]] || [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then

bin/alerts/sendAlert.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ elif [[ -z "${LOG_DIR:-}" ]]; then
3434
export LOG_DIR="${PROJECT_ROOT}/logs"
3535
fi
3636

37-
# Ensure log directory exists
38-
mkdir -p "${LOG_DIR}"
37+
# Ensure log directory exists (handle permission errors gracefully)
38+
mkdir -p "${LOG_DIR}" 2>/dev/null || {
39+
# If we can't create LOG_DIR, try fallback locations
40+
if [[ -n "${TMP_DIR:-}" ]]; then
41+
export LOG_DIR="${TMP_DIR}/logs"
42+
elif [[ -n "${PROJECT_ROOT:-}" ]] && [[ -d "${PROJECT_ROOT}" ]]; then
43+
export LOG_DIR="${PROJECT_ROOT}/tmp/logs"
44+
else
45+
export LOG_DIR="/tmp/osm-notes-monitoring-logs"
46+
fi
47+
mkdir -p "${LOG_DIR}" 2>/dev/null || export LOG_DIR="/tmp"
48+
}
3949

4050
# Only initialize logging if not in test mode
4151
# When TEST_MODE is true, initialization is skipped to avoid database connections

bin/lib/loggingFunctions.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ init_logging() {
4444
local log_dir
4545
log_dir="$(dirname "${LOG_FILE}")"
4646
if [[ ! -d "${log_dir}" ]]; then
47-
# In test mode or if we don't have permissions, use a temporary directory
48-
if [[ "${TEST_MODE:-false}" == "true" ]] || ! mkdir -p "${log_dir}" 2>/dev/null; then
47+
# Try to create the directory first
48+
# Only use fallback if creation fails (e.g., no permissions)
49+
if ! mkdir -p "${log_dir}" 2>/dev/null; then
4950
# Use TMP_DIR if set, otherwise try PROJECT_ROOT/tmp/logs, or use /tmp
5051
local fallback_dir
5152
if [[ -n "${TMP_DIR:-}" ]]; then

bin/monitor/checkPlanetNotes.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,21 @@ run_planet_check() {
105105
local planet_duration_threshold="${INGESTION_PLANET_CHECK_DURATION_THRESHOLD:-600}"
106106
if [[ ${duration} -gt ${planet_duration_threshold} ]]; then
107107
log_warning "${COMPONENT}: Planet check duration (${duration}s) exceeds threshold (${planet_duration_threshold}s)"
108-
send_alert "WARNING" "${COMPONENT}" "Planet Notes check took too long: ${duration}s (threshold: ${planet_duration_threshold}s)"
108+
# Only send alert if send_alert function is available and not mocked
109+
if command -v send_alert >/dev/null 2>&1 && [[ "${TEST_MODE:-false}" != "true" ]]; then
110+
send_alert "WARNING" "${COMPONENT}" "Planet Notes check took too long: ${duration}s (threshold: ${planet_duration_threshold}s)"
111+
fi
109112
fi
110113

111114
return 0
112115
else
113116
log_error "${COMPONENT}: Planet Notes check failed (exit_code: ${exit_code}, duration: ${duration}s)"
114117
record_metric "${COMPONENT}" "planet_check_status" "0" "component=ingestion,check=processCheckPlanetNotes"
115118
record_metric "${COMPONENT}" "planet_check_duration" "${duration}" "component=ingestion,check=processCheckPlanetNotes"
116-
send_alert "ERROR" "${COMPONENT}" "Planet Notes check failed: exit_code=${exit_code}"
119+
# Only send alert if send_alert function is available and not mocked
120+
if command -v send_alert >/dev/null 2>&1 && [[ "${TEST_MODE:-false}" != "true" ]]; then
121+
send_alert "ERROR" "${COMPONENT}" "Planet Notes check failed: exit_code=${exit_code}"
122+
fi
117123
return 1
118124
fi
119125
}

tests/unit/monitor/test_collect_cron_metrics.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ teardown() {
257257
}
258258
export -f record_metric
259259

260+
# Set LOG_DIR to test directory if not already set
261+
export LOG_DIR="${LOG_DIR:-${TEST_LOG_DIR}}"
262+
260263
# Create a mock log file with old execution times
264+
mkdir -p "${LOG_DIR}" 2>/dev/null || export LOG_DIR="${TEST_LOG_DIR}"
261265
mkdir -p "${LOG_DIR}"
262266
echo "cron_etl_last_execution_seconds 3600" >"${LOG_DIR}/cron_metrics.log"
263267

0 commit comments

Comments
 (0)