Skip to content

Instantly share code, notes, and snippets.

@pcreux
Created January 26, 2022 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/c52319dfd61fc65eead7921235efcd7a to your computer and use it in GitHub Desktop.
Save pcreux/c52319dfd61fc65eead7921235efcd7a to your computer and use it in GitHub Desktop.
datadog + heroku postgres integration for more than 1 database

Datadog documentation shows a code sample to monitor one postgres DB on Heroku at https://docs.datadoghq.com/agent/basic_agent_usage/heroku/

if [ -n "$DATABASE_URL" ]; then
  POSTGREGEX='^postgres://([^:]+):([^@]+)@([^:]+):([^/]+)/(.*)$'
  if [[ $DATABASE_URL =~ $POSTGREGEX ]]; then
    sed -i "s/<YOUR HOSTNAME>/${BASH_REMATCH[3]}/" "$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
    sed -i "s/<YOUR USERNAME>/${BASH_REMATCH[1]}/" "$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
    sed -i "s/<YOUR PASSWORD>/${BASH_REMATCH[2]}/" "$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
    sed -i "s/<YOUR PORT>/${BASH_REMATCH[4]}/" "$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
    sed -i "s/<YOUR DBNAME>/${BASH_REMATCH[5]}/" "$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
  fi
fi

with

init_config:

instances:
  - host: <YOUR HOSTNAME>
    port: <YOUR PORT>
    username: <YOUR USERNAME>
    password: <YOUR PASSWORD>
    dbname: <YOUR DBNAME>
    ssl: True

Here is an updated version to monitor all DBs associated to an application.

# datadog/prerun.sh

# ...

monitor_database() {
  POSTGREGEX='^postgres://([^:]+):([^@]+)@([^:]+):([^/]+)/(.*)$'
  YAML_FILE="$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
  DB_URL=$1

  if [[ $DB_URL =~ $POSTGREGEX ]]; then
    echo "  - host: ${BASH_REMATCH[3]}"     >> $YAML_FILE
    echo "    port: ${BASH_REMATCH[4]}"     >> $YAML_FILE
    echo "    username: ${BASH_REMATCH[1]}" >> $YAML_FILE
    echo "    password: ${BASH_REMATCH[2]}" >> $YAML_FILE
    echo "    dbname: ${BASH_REMATCH[5]}"   >> $YAML_FILE
    echo "    ssl: True"                    >> $YAML_FILE
  fi
}

# Update the Postgres configuration defined at datadog/conf.d/postgres.yaml
# using the Heroku application environment variable.
# Note: During the dyno start up, the YAML file will be copied to
# $DD_CONF_DIR/conf.d/postgres.d/conf.yaml
env | while IFS= read -r line; do
  ENV_VAR_NAME=${line%%=*}
  ENV_VAR_VALUE=${line#*=}
  if [[ $ENV_VAR_NAME =~ 'HEROKU_POSTGRESQL_' ]]; then
    monitor_database $ENV_VAR_VALUE
  fi
fi
done

# ...
# datadog/conf.d/postgres.yaml

init_config:

# `datadog/prerun.sh` will inject instances below on boot
instances:
init_config:
# `datadog/prerun.sh` will inject instances below on boot
instances:
monitor_database() {
POSTGREGEX='^postgres://([^:]+):([^@]+)@([^:]+):([^/]+)/(.*)$'
YAML_FILE="$DD_CONF_DIR/conf.d/postgres.d/conf.yaml"
DB_URL=$1
if [[ $DB_URL =~ $POSTGREGEX ]]; then
echo " - host: ${BASH_REMATCH[3]}" >> $YAML_FILE
echo " port: ${BASH_REMATCH[4]}" >> $YAML_FILE
echo " username: ${BASH_REMATCH[1]}" >> $YAML_FILE
echo " password: ${BASH_REMATCH[2]}" >> $YAML_FILE
echo " dbname: ${BASH_REMATCH[5]}" >> $YAML_FILE
echo " ssl: True" >> $YAML_FILE
fi
}
# Update the Postgres configuration defined at datadog/conf.d/postgres.yaml
# using the Heroku application environment variable.
# Note: During the dyno start up, the YAML file will be copied to
# $DD_CONF_DIR/conf.d/postgres.d/conf.yaml
env | while IFS= read -r line; do
ENV_VAR_NAME=${line%%=*}
ENV_VAR_VALUE=${line#*=}
if [[ $ENV_VAR_NAME =~ 'HEROKU_POSTGRESQL_' ]]; then
monitor_database $ENV_VAR_VALUE
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment