Check Spring Actuator Age

I’m going to use the Spring’s Actuator endpoint on /no-auth/actuator/info to parse the date of the last service’s git commit and print a simple report with the age or my services.

$ curl --location --request GET production.myhost.com/myservice/no-auth/actuator/info|jq

returns

{
  "git": {
    "branch": "main",
    "commit": {
      "id": "effd243",
      "time": "20220704-1031"
    }
  },
  "build": {
    "artifact": "myservice",
    "name": "myservice",
    "time": "2022-07-04T13:31:22.477Z",
    "version": "0.0.1-SNAPSHOT",
    "group": "com.alros.myservice"
  }
}

add jq -r '.git.commit.time' and the line will just return the commit time.

The date can be parsed with

date -jf '%Y%m%d-%H%M' +%s ${commit_time}

Extracting the info for a service will be

commit_time=`curl --location --request GET ${base_url}/${service}/no-auth/actuator/info 2>/dev/null|jq -r '.git.commit.time'`
now=`date +%s`
commit=`date -jf '%Y%m%d-%H%M' +%s ${commit_time}`
age=`echo "(${now}-${commit})/3600"|bc`

Then it’s simply a matter of iterating over multiple services

get_server_status() {
  base_url='production.myhost.com'
  for service in 'myservice1' 'myservice2' 'myservice3'
  do
    commit_time=`curl --location --request GET ${base_url}/${service}/no-auth/actuator/info 2>/dev/null|jq -r '.git.commit.time'`
    now=`date +%s`
    commit=`date -jf '%Y%m%d-%H%M' +%s ${commit_time}`
    age=`echo "(${now}-${commit})/3600"|bc`
    printf "%30s %s %s %s hours\n" ${service} ${commit_time} ${age}
  done
}

Add a parameter to switch environment (base_url)

get_server_status() {
  if [ -z "$1" ]; then base_url='production.myhost.com'
  elif [ "$1" = 'development' ]; then base_url='development.myhost.com'
  elif [ "$1" = 'staging' ]; then base_url='staging.myhost.com'
  elif [ "$1" = 'production' ]; then base_url='production.myhost.com'
  else echo 'valid options are development, staging, production' && return -1
  fi
  printf "for %s\n" ${base_url}
  for service in 'myservice1' 'myservice2' 'myservice3'
  do
    commit_time=`curl --location --request GET ${base_url}/${service}/no-auth/actuator/info 2>/dev/null|jq -r '.git.commit.time'`
    now=`date +%s`
    commit=`date -jf '%Y%m%d-%H%M' +%s ${commit_time}`
    age=`echo "(${now}-${commit})/3600"|bc`
    printf "%30s %s %s %s hours\n" ${service} ${commit_time} ${age}
  done
}

Final touch: a bit of visualization

get_server_status() {
  if [ -z "$1" ]; then base_url='production.myhost.com'
  elif [ "$1" = 'development' ]; then base_url='development.myhost.com'
  elif [ "$1" = 'staging' ]; then base_url='staging.myhost.com'
  elif [ "$1" = 'production' ]; then base_url='production.myhost.com'
  else echo 'valid options are development, staging, production' && return -1
  fi
  printf "for %s\n" ${base_url}
  for service in 'myservice1' 'myservice2' 'myservice3'
  do
    commit_time=`curl --location --request GET ${base_url}/${service}/no-auth/actuator/info 2>/dev/null|jq -r '.git.commit.time'`
    now=`date +%s`
    commit=`date -jf '%Y%m%d-%H%M' +%s ${commit_time}`
    age=`echo "(${now}-${commit})/3600"|bc`
    if [ $age -lt 4 ]; then emoji='🐣'
    elif [ $age -lt 24 ]; then emoji='🐥'
    elif [ $age -lt 168 ]; then emoji='🐓'
    else emoji='🍗'
    fi
    printf "%30s %s %s %s hours\n" ${service} ${emoji} ${commit_time} ${age}
  done
}

output:

$ get_server_status
for production.myhost.com
                    myservice1 🐓 20220702-1621 68 hours
                    myservice2 🐥 20220704-1335 22 hours
                    myservice3 🍗 20220530-1517 861 hours
Tags: Spring DevOps