@@ -28,6 +28,9 @@ metrics_to_stdout() {
2828 echo " Elapsed time (ms): ${elapsed_time_ms} "
2929 echo " All resources: ${all_resources_count} "
3030 echo " Updated resources: ${updated_resources_count} "
31+ echo " Now (Unix epoch): ${now_unix_epoch} "
32+ echo " now - report_time (s): ${now_minus_report_time_seconds} "
33+ echo " now - last_success (s): ${now_minus_last_success_seconds} "
3134}
3235
3336metrics_to_prometheus () {
@@ -52,6 +55,12 @@ chef_client_run_resources_total${tags} ${all_resources_count}
5255# HELP chef_client_run_updated_resources_total Updated resources in the last Chef run.
5356# TYPE chef_client_run_updated_resources_total gauge
5457chef_client_run_updated_resources_total${tags} ${updated_resources_count}
58+ # HELP chef_client_run_time_since_last_seconds Seconds since the last Chef run report time (now - report_time).
59+ # TYPE chef_client_run_time_since_last_seconds gauge
60+ chef_client_run_time_since_last_seconds${tags} ${now_minus_report_time_seconds}
61+ # HELP chef_client_run_time_since_last_success_seconds Seconds since the last successful Chef run (now - last_success).
62+ # TYPE chef_client_run_time_since_last_success_seconds gauge
63+ chef_client_run_time_since_last_success_seconds${tags} ${now_minus_last_success_seconds}
5564EOF
5665
5766 # Rename the temporary file atomically.
@@ -77,9 +86,29 @@ end_time_unix_epoch="$(iso_to_epoch "$end_time_iso8601")"
7786
7887elapsed_seconds=" $( jq -nr --arg v " $elapsed_time_ms " ' ($v|tonumber) / 1000.0' ) "
7988
89+ # "now" and convenience age metrics
90+ now_unix_epoch=" $( date +%s) "
91+ now_minus_report_time_seconds=" $(( now_unix_epoch - report_time_unix_epoch )) "
92+ now_minus_last_success_seconds=" $(( now_unix_epoch - last_success_unix_epoch )) "
93+
94+ # Guard against missing/zero timestamps producing huge/negative numbers
95+ if [[ " $report_time_unix_epoch " -le 0 ]]; then
96+ now_minus_report_time_seconds=" 0"
97+ fi
98+ if [[ " $last_success_unix_epoch " -le 0 ]]; then
99+ now_minus_last_success_seconds=" 0"
100+ fi
101+ if [[ " $now_minus_report_time_seconds " -lt 0 ]]; then
102+ now_minus_report_time_seconds=" 0"
103+ fi
104+ if [[ " $now_minus_last_success_seconds " -lt 0 ]]; then
105+ now_minus_last_success_seconds=" 0"
106+ fi
107+
108+ tier=" default"
80109if [[ -r " $CONFIG_FILE " ]]; then
81- tier =" $( jq -r ' .tier // empty' " $CONFIG_FILE " ) "
82- [[ -n " $tier " ]] || tier=" default "
110+ t =" $( jq -r ' .tier // empty' " $CONFIG_FILE " ) "
111+ [[ -n " $t " ]] && tier=" $t "
83112fi
84113
85114tags=" {tier=\" $tier \" }"
0 commit comments