Skip to content

Commit b75a8c4

Browse files
authored
Emit additional metrics (#262)
1 parent e99babf commit b75a8c4

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

bin/test-compile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
1111
source "${BUILDPACK_DIR}/lib/output.sh"
1212
source "${BUILDPACK_DIR}/lib/util.sh"
1313
source "${BUILDPACK_DIR}/lib/maven.sh"
14+
source "${BUILDPACK_DIR}/lib/metrics.sh"
1415
source "${BUILDPACK_DIR}/lib/openjdk.sh"
1516

17+
metrics::init "${CACHE_DIR}" "java"
18+
metrics::setup
19+
1620
util::export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS"
1721

1822
openjdk::install_openjdk_via_jvm_common_buildpack "${BUILD_DIR}" "${BUILDPACK_DIR}"

lib/maven.sh

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set -euo pipefail
77
BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
88
source "${BUILDPACK_DIR}/lib/java_properties.sh"
99
source "${BUILDPACK_DIR}/lib/util.sh"
10+
source "${BUILDPACK_DIR}/lib/metrics.sh"
1011

1112
export DEFAULT_MAVEN_VERSION="3.9.4"
1213

@@ -34,27 +35,39 @@ function maven::setup_maven_and_build_app() {
3435
export MAVEN_OPTS="-Xmx1024m${maven_java_opts:+ ${maven_java_opts}} -Duser.home=${build_dir} -Dmaven.repo.local=${cache_dir}/.m2/repository"
3536

3637
if maven::should_use_wrapper "${build_dir}"; then
37-
util::cache_copy ".m2/wrapper" "${cache_dir}" "${build_dir}"
38-
chmod +x "${build_dir}/mvnw"
38+
metrics::set_raw "maven_wrapper" "true"
3939
local maven_exe="./mvnw"
40+
41+
util::cache_copy ".m2/wrapper" "${cache_dir}" "${build_dir}"
42+
chmod +x "${build_dir}/${maven_exe}"
4043
else
41-
local maven_version
42-
maven_version="$(java_properties::get "${build_dir}/system.properties" "maven.version")"
43-
maven::install_maven "${maven_version:-${DEFAULT_MAVEN_VERSION}}" "${cache_dir}/.maven"
44+
metrics::set_raw "maven_wrapper" "false"
4445
local maven_exe="mvn"
46+
47+
maven_version_selector=$(java_properties::get "${build_dir}/system.properties" "maven.version")
48+
49+
local maven_install_start_time
50+
maven_install_start_time=$(util::nowms)
51+
52+
maven::install_maven "${maven_version_selector:-${DEFAULT_MAVEN_VERSION}}" "${cache_dir}/.maven"
53+
54+
metrics::set_duration "maven_install_duration" "${maven_install_start_time}"
4555
fi
4656

57+
local maven_version
58+
maven_version="$(cd "${build_dir}" && ${maven_exe} --version 2>/dev/null | awk '/Apache Maven/ {gsub(/\x1b\[[0-9;]*m/, ""); print $3}')"
59+
metrics::set_string "maven_version" "${maven_version}"
60+
4761
maven::install_settings_xml "${build_dir}" "${build_dir}/.m2/settings.xml"
4862

4963
output::step "Executing Maven"
5064

51-
cd "${build_dir}"
5265
echo "$ ${maven_exe} ${maven_opts} ${maven_goals}" | output::indent
5366

5467
# We rely on word splitting for settings_xml_opts, maven_opts, and maven_goals:
5568
# Intentional word splitting needed for Maven command arguments
5669
# shellcheck disable=SC2086
57-
if ! ${maven_exe} -DoutputFile=target/mvn-dependency-list.log -B ${maven_opts} ${maven_goals} | output::indent; then
70+
if ! (cd "${build_dir}" && ${maven_exe} -DoutputFile=target/mvn-dependency-list.log -B ${maven_opts} ${maven_goals}) | output::indent; then
5871
output::error <<-EOF
5972
Error: Maven build failed.
6073
@@ -76,6 +89,7 @@ function maven::setup_maven_and_build_app() {
7689
to reproduce and debug the issue.
7790
EOF
7891

92+
metrics::set_string "failure_reason" "execute_maven::non_zero_exit_code"
7993
return 1
8094
fi
8195

@@ -146,6 +160,7 @@ function maven::install_maven() {
146160
The default supported version is ${DEFAULT_MAVEN_VERSION}.
147161
EOF
148162

163+
metrics::set_string "failure_reason" "install_maven::version_unavailable"
149164
exit 1
150165
elif [[ "${curl_exit_code}" -ne 0 || "${http_status_code}" != "200" ]]; then
151166
output::error <<-EOF
@@ -167,6 +182,7 @@ function maven::install_maven() {
167182
HTTP status code: ${http_status_code}, curl exit code: ${curl_exit_code}
168183
EOF
169184

185+
metrics::set_string "failure_reason" "install_maven::download_error"
170186
exit 1
171187
fi
172188

@@ -193,6 +209,7 @@ function maven::install_maven() {
193209
Error details: $(head --lines=1 "${error_log}" || true)
194210
EOF
195211

212+
metrics::set_string "failure_reason" "install_maven::extraction_error"
196213
exit 1
197214
fi
198215

@@ -239,6 +256,8 @@ function maven::install_settings_xml() {
239256

240257
# Check if settings.xml already exists and warn if any method would be used
241258
if [[ -f "${settings_destination}" ]]; then
259+
metrics::set_string "maven_settings_xml_source_type" "maven_default_location"
260+
242261
if [[ -n "${MAVEN_SETTINGS_PATH:-}" ]]; then
243262
output::warning <<-EOF
244263
Warning: Using existing settings.xml file.
@@ -290,9 +309,11 @@ function maven::install_settings_xml() {
290309
fi
291310

292311
output::step "Using settings.xml from ${MAVEN_SETTINGS_PATH}"
312+
metrics::set_string "maven_settings_xml_source_type" "path"
293313
ln -sf "${settings_source}" "${settings_destination}"
294314
elif [[ -n "${MAVEN_SETTINGS_URL:-}" ]]; then
295315
output::step "Using settings.xml from ${MAVEN_SETTINGS_URL}"
316+
metrics::set_string "maven_settings_xml_source_type" "url"
296317

297318
if ! curl \
298319
--silent \
@@ -324,10 +345,12 @@ function maven::install_settings_xml() {
324345
https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml
325346
EOF
326347

348+
metrics::set_string "failure_reason" "settings_xml::download_error"
327349
exit 1
328350
fi
329351
elif [[ -f "${build_dir}/settings.xml" ]]; then
330352
output::step "Using settings.xml from project directory"
353+
metrics::set_string "maven_settings_xml_source_type" "app_root"
331354
ln -sf "${build_dir}/settings.xml" "${settings_destination}"
332355
fi
333356
}

0 commit comments

Comments
 (0)