Skip to content

Add bash based metrics collection #1635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

schneems
Copy link
Contributor

@schneems schneems commented Aug 18, 2025

The buildpack is split into a bash part that bootstraps ruby and then the Ruby part that does the bulk of the logic. This adds the ability to push metrics to bin/report in the bash section for failures that occur during or before the Ruby logic portion is called.

GUS-W-19310610

If the env var is missing or the file does not exist, print that information.
Simplifies calling metrics functions inside of bash functions, and it's relatively small.
```
Run shellcheck bin/support/bash_functions.sh bin/support/download_ruby -x &&

In bin/support/bash_functions.sh line 234:
	local timestamp=$(date +%s%3N 2>/dev/null)
              ^-------^ SC2155 (warning): Declare and assign separately to avoid masking return values.


In bin/support/bash_functions.sh line 243:
	local seconds=$(date +%s)
              ^-----^ SC2155 (warning): Declare and assign separately to avoid masking return values.


In bin/support/bash_functions.sh line 244:
	local nanoseconds=$(date +%N 2>/dev/null || echo "000000000")
              ^---------^ SC2155 (warning): Declare and assign separately to avoid masking return values.
```
```
       +remote: Updated 2 paths from 60b426a        
       +remote: Compressing source files... done.        
       +remote: Building source:        
       +remote: 
       +remote: -----> Building on the Heroku-24 stack        
       +remote: -----> Using buildpack: https://github.com/heroku/heroku-buildpack-ruby#schneems/bash-metrics-initial        
       +remote: -----> Ruby app detected        
       +remote: /tmp/codon/tmp/buildpacks/bc6628ed5814e6d8925d4527655dd8e7dda421f4/bin/support/ruby_compile:14:in `fetch': key not found: "HEROKU_RUBY_BUILD_REPORT_FILE" (KeyError)        
       +remote: 	from /tmp/codon/tmp/buildpacks/bc6628ed5814e6d8925d4527655dd8e7dda421f4/bin/support/ruby_compile:14:in `<main>'        
       +remote:  !     Push rejected, failed to compile Ruby app.        
       +remote: 
       +remote:  !     Push failed        
       +remote: Verifying deploy...        
       +remote: 
       +remote: !	Push rejected to hatchet-t-90251d64ed.        
       +remote: 
       +To https://git.heroku.com/hatchet-t-90251d64ed.git
       + ! [remote rejected] HEAD -> main (pre-receive hook declined)
       +error: failed to push some refs to 'https://git.heroku.com/hatchet-t-90251d64ed.git'
     # ./vendor/bundle/ruby/3.3.0/gems/rspec-support-3.13.4/lib/rspec/support.rb:110:in `block in <module:Support>'
```
```
In bin/compile line 12:
BUILDPACK_DIR=$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)
^-----------^ SC2034 (warning): BUILDPACK_DIR appears unused. Verify use (or export if used externally).


In bin/test line 9:
BUILDPACK_DIR=$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)
^-----------^ SC2034 (warning): BUILDPACK_DIR appears unused. Verify use (or export if used externally).


In bin/test line 15:
metrics::init "${CACHE_DIR}"
               ^----------^ SC2153 (info): Possible misspelling: CACHE_DIR may not be assigned. Did you mean cache_dir?


In bin/test-compile line 12:
BUILDPACK_DIR=$(cd "$(dirname "$(dirname "${BASH_SOURCE[0]}")")" && pwd)
^-----------^ SC2034 (warning): BUILDPACK_DIR appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- BUILDPACK_DIR appears unused. Ver...
  https://www.shellcheck.net/wiki/SC2153 -- Possible misspelling: CACHE_DIR m...
```
The file now relies on importing bash functions which weren't being copied. This fixes the error.
@schneems schneems marked this pull request as ready for review August 18, 2025 22:24
@schneems schneems requested a review from a team as a code owner August 18, 2025 22:24

metrics::quote_string() {
local value="${1}"
echo "'${value//\'/\'\'}'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this would be handled better in the Ruby integration with this file but you may also want to strip out or replace control chars (see https://github.com/heroku/heroku-buildpack-nodejs/blob/main/bin/report#L41-L54).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to using Ed's json backend, I think (hope) that takes care of issues like this

The python buildpack was modified to use JSON instead of YAML as it's easier to use `jq` to format and handle issues such as escaping.

This preserves the same API, but changes the format and adopts the strategy from heroku/heroku-buildpack-python#1878.
Previously the bash build output helpers were adjusted to emit json. This wires up the Ruby metrics code to be compatible with that change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants