Skip to content

Simplify bin/report implementation #247

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

Merged
merged 3 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ ENV_DIR=$3
source "${BP_DIR}/lib/util.sh"
source "${BP_DIR}/lib/common.sh"
source "${BP_DIR}/lib/maven.sh"
source "${BP_DIR}/lib/metadata.sh"
source "${BP_DIR}/lib/metrics.sh"

# Initialise the buildpack metadata store.
# This is used to track state across builds (for cache invalidation and messaging when build
# configuration changes) and also so that `bin/report` can generate the build report.
meta_init "${CACHE_DIR}" "java"
meta_setup
metrics::init "${CACHE_DIR}" "java"
metrics::setup

export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"

Expand Down
51 changes: 3 additions & 48 deletions bin/report
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,7 @@ if [[ -f "${EXPORT_FILE}" ]]; then
source "${EXPORT_FILE}"
fi

source "${BUILDPACK_DIR}/lib/metadata.sh"
meta_init "${CACHE_DIR}" "java"
source "${BUILDPACK_DIR}/lib/metrics.sh"

# Emit the key / value pair unquoted to stdout. Skips if the value is empty.
# Based on: https://github.com/heroku/heroku-buildpack-nodejs/blob/main/bin/report
kv_pair() {
local key="${1}"
local value="${2}"
if [[ -n "${value}" ]]; then
echo "${key}: ${value}"
fi
}

# Emit the key / value pair to stdout, safely quoting the string. Skips if the value is empty.
# Based on: https://github.com/heroku/heroku-buildpack-nodejs/blob/main/bin/report
# (Though instead uses single quotes instead of double to avoid escaping issues.)
kv_pair_string() {
local key="${1}"
local value="${2}"
if [[ -n "${value}" ]]; then
# Escape any existing single quotes, which for YAML means replacing `'` with `''`.
value="${value//\'/\'\'}"
echo "${key}: '${value}'"
fi
}

STRING_FIELDS=(
# Fields coming from jvm-common
openjdk_version
openjdk_distribution
openjdk_version_selector
)

# We don't want to quote numeric or boolean fields.
ALL_OTHER_FIELDS=(
# Fields coming from jvm-common
openjdk_install_duration
app_has_jdk_overlay
)

for field in "${STRING_FIELDS[@]}"; do
# shellcheck disable=SC2312 # TODO: Invoke this command separately to avoid masking its return value.
kv_pair_string "${field}" "$(meta_get "${field}")"
done

for field in "${ALL_OTHER_FIELDS[@]}"; do
# shellcheck disable=SC2312 # TODO: Invoke this command separately to avoid masking its return value.
kv_pair "${field}" "$(meta_get "${field}")"
done
metrics::init "${CACHE_DIR}" "java"
metrics::print_bin_report_yaml
85 changes: 0 additions & 85 deletions lib/kvstore.sh

This file was deleted.

78 changes: 0 additions & 78 deletions lib/metadata.sh

This file was deleted.

116 changes: 116 additions & 0 deletions lib/metrics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env bash

# This is technically redundant, since all consumers of this lib will have enabled these,
# however, it helps Shellcheck realise the options under which these functions will run.

# This buildpack cannot currently run with `set -u`! This needs to be fixed.
# set -euo pipefail
set -eo pipefail

BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"

source "${BUILDPACK_DIR}/lib/util.sh"

# Variables shared by this whole module
METRICS_DATA_FILE=""
PREVIOUS_METRICS_DATA_FILE=""

# Initializes the environment required for metrics collection.
# Must be called before you can use any other functions from this file!
#
# Usage:
# ```
# metrics::init "${CACHE_DIR}" "scala"
# ```
metrics::init() {
local cache_dir="${1}"
local buildpack_name="${2}"

METRICS_DATA_FILE="${cache_dir}/metrics-data/${buildpack_name}"
PREVIOUS_METRICS_DATA_FILE="${cache_dir}/metrics-data/${buildpack_name}-prev"
}

# Initializes the metrics collection environment by setting up data files.
#
# WARNING: This function prunes existing metrics should there be any.
#
# Usage:
# ```
# metrics::init "${CACHE_DIR}" "scala"
# metrics::setup
# ```
metrics::setup() {
if [[ -f "${METRICS_DATA_FILE}" ]]; then
cp "${METRICS_DATA_FILE}" "${PREVIOUS_METRICS_DATA_FILE}"
fi

mkdir -p "$(dirname "${METRICS_DATA_FILE}")"
echo "{}" >"${METRICS_DATA_FILE}"
}

# Sets a metric value as raw JSON data.
# The value parameter must be valid JSON value (number, boolean, string, etc.).
#
# NOTE: Strings must be wrapped in double quotes (use `metrics::set_string` for convenience).
#
# Usage:
# ```
# metrics::set_raw "build_duration" "42.5"
# metrics::set_raw "success" "true"
# metrics::set_raw "message" '"Hello World"'
# ```
metrics::set_raw() {
local key="${1}"
local value="${2}"

local new_data_file_contents
new_data_file_contents=$(jq <"${METRICS_DATA_FILE}" --arg key "${key}" --argjson value "${value}" '. + { ($key): ($value) }')

echo "${new_data_file_contents}" >"${METRICS_DATA_FILE}"
}

# Sets a metric value as a string.
# The value will be automatically wrapped in double quotes and escaped for JSON.
#
# Usage:
# ```
# metrics::set_string "buildpack_version" "1.2.3"
# metrics::set_string "jvm_distribution" "Heroku"
# ```
metrics::set_string() {
local key="${1}"
local value="${2}"

# This works because jq's `--arg` always results in a string value
metrics::set_raw "${key}" "$(jq -n --arg value "${value}" '$value')"
}

# Sets a metric for elapsed time between two timestamps.
# If end timestamp is not provided, current time is used.
# Time is calculated in seconds with millisecond precision.
#
# Usage:
# ```
# start_time=$(util::nowms)
# # ... some operation ...
# metrics::set_duration "compile_duration" "${start_time}"
# ```
metrics::set_duration() {
local key="${1}"
local start="${2}"
local end="${3:-$(util::nowms)}"
local time
time="$(echo "${start}" "${end}" | awk '{ printf "%.3f", ($2 - $1)/1000 }')"
metrics::set_raw "${key}" "${time}"
}

# Prints all metrics data in YAML format suitable for `bin/report`.
# Each metric key-value pair is output as a separate YAML line.
#
# Usage:
# ```
# metrics::print_bin_report_yaml
# ```
metrics::print_bin_report_yaml() {
jq -r 'keys[] as $key | (.[$key] | tojson) as $value | "\($key): \($value)"' <"${METRICS_DATA_FILE}"
}