pk-fxq
Last Updated: April 20, 2020
·
10
· rafaelcgo

Heroku: Ruby Language Metrics (Public Beta) on Stack: Container

Here at Pier we use Heroku for hosting some of our applications and we would like to have the Language Runtime Metrics that they provide in their metrics dashboard for each app. You can read the docs at: https://devcenter.heroku.com/articles/language-runtime-metrics-ruby#getting-started

Done everything according to the article but the metrics won't show up.
After contacting the support I've found out that "stack: container" won't have the language metrics because buildpacks aren't available at containerized apps.
Right now there is a note about this, at the time there wasn't one.

This process does not work with container based-applications because they do not use buildpacks. Currently container-based applications are not supported in this beta.

After speaking with Heroku support we found a way to make it work.

  1. First of all, we’re using an alpine docker image.
  2. We added bash and curl to our Dockerfile (apk add bash curl)
  3. We copied the heroku-metrics-daemon.sh (from https://github.com/heroku/heroku-buildpack-metrics) to /etc/profile.d/ to run as a profile.d script on the boot of the heroku dyno.
  4. We needed to edit the .sh file. 5 We removed the --warning=no-unknown-keyword flag from the tar command. Apparently it’s not compatible with the alpine tar, only GNU tar (you could also add GNU tar to your image). 6 We had problems with the array parsing/syntax in the script (even with bash installed and with #!/bin/bash). So we removed the option to use AGENTMON_DEBUG from the script.

The final script is as follows:

#!/bin/bash

# From: https://github.com/heroku/heroku-buildpack-metrics/blob/master/.profile.d/heroku-metrics-daemon.sh
setup_metrics() {
    # don't do anything if we don't have a metrics url.
    if [[ -z "$HEROKU_METRICS_URL" ]] || [[ "${DYNO}" = run\.* ]]; then
        return 0
    fi

    STARTTIME=$(date +%s)
    BUILD_DIR=/tmp

    DOWNLOAD_URL=$(curl --retry 3 -s https://agentmon-releases.s3.amazonaws.com/latest)
    if [ -z "${DOWNLOAD_URL}" ]; then
        echo "!!!!! Failed to find latest agentmon. Please report this as a bug. Metrics collection will be disabled this run."
        return 1
    fi

    BASENAME=$(basename "${DOWNLOAD_URL}")

    curl -L --retry 3 -s -o "${BUILD_DIR}/${BASENAME}" "${DOWNLOAD_URL}"

    # Ensure the bin folder exists, if not already.
    mkdir -p "${BUILD_DIR}/bin"

    # Extract agentmon release
    # tar --warning=no-unknown-keyword -C "${BUILD_DIR}/bin" -zxf "${BUILD_DIR}/${BASENAME}"
    tar -C "${BUILD_DIR}/bin" -zxf "${BUILD_DIR}/${BASENAME}"
    chmod +x "${BUILD_DIR}/bin/agentmon"

    ELAPSEDTIME=$(($(date +%s) - STARTTIME))
    echo "agentmon setup took ${ELAPSEDTIME} seconds"

    # AGENTMON_FLAGS=("-statsd-addr=:${PORT}")
    AGENTMON_FLAG="-statsd-addr=:${PORT}"


    # if [[ "${AGENTMON_DEBUG}" = "true" ]]; then
    #     AGENTMON_FLAGS+=("-debug")
    # fi

    if [[ -x "${BUILD_DIR}/bin/agentmon" ]]; then
        (while true; do
            # ${BUILD_DIR}/bin/agentmon "${AGENTMON_FLAGS[@]}" "${HEROKU_METRICS_URL}"
            ${BUILD_DIR}/bin/agentmon "${AGENTMON_FLAG}" "${HEROKU_METRICS_URL}"
            echo "agentmon completed with status=${?}. Restarting"
            sleep 1
        done) &
    else
        echo "No agentmon executable found. Not starting."
    fi
}
setup_metrics