Skip to content
Open
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
1 change: 0 additions & 1 deletion mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ run = "./mvnw install -DskipTests -Dspotless.check.skip=true -Dcoverage.skip=tru
[tasks.generate]
description = "bare compile, ignoring formatting and linters"
run = [
"mise use --pin protoc@latest",
Copy link
Collaborator Author

@jaydeluca jaydeluca Feb 6, 2026

Choose a reason for hiding this comment

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

this caused me a lot of confusion because my local mise wasn't up to date, so it kept downgrading the version and then generating code that broke the build. I think the versions are updated by renovate so i dont think this is needed?

"./mvnw clean install -DskipTests -Dspotless.check.skip=true -Dcoverage.skip=true -Dcheckstyle.skip=true -Dwarnings=-nowarn"
]
env.PROTO_GENERATION = "true"
Expand Down
33 changes: 26 additions & 7 deletions prometheus-metrics-exposition-formats/generate-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ set -euo pipefail
# I could not figure out how to use a protoc Maven plugin to use the shaded module,
# so I ran this command to generate the sources manually.

# Use gsed on macOS (requires: brew install gnu-sed) for in-place edits
# BSD sed requires -i '' for in-place with no backup; GNU sed uses -i alone.
if [[ "$OSTYPE" == "darwin"* ]] && command -v gsed >/dev/null 2>&1; then
SED='gsed'
SED_I=(-i)
else
SED='sed'
# BSD sed: -i requires backup extension; '' = no backup
[[ "$OSTYPE" == "darwin"* ]] && SED_I=(-i '') || SED_I=(-i)
fi

# Use mise-provided protoc if available
if command -v mise >/dev/null 2>&1; then
PROTOC="mise exec -- protoc"
else
PROTOC='protoc'
fi

TARGET_DIR=$1
PROTO_DIR=src/main/protobuf
PROTOBUF_VERSION_STRING=$2
Expand All @@ -18,22 +36,23 @@ mkdir -p "$TARGET_DIR"
rm -rf $PROTO_DIR || true
mkdir -p $PROTO_DIR

OLD_PACKAGE=$(sed -nE 's/import (io.prometheus.metrics.expositionformats.generated.*).Metrics;/\1/p' src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java)
OLD_PACKAGE=$($SED -nE 's/import (io.prometheus.metrics.expositionformats.generated.*).Metrics;/\1/p' src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java)
PACKAGE="io.prometheus.metrics.expositionformats.generated.com_google_protobuf_${PROTOBUF_VERSION_STRING}"

if [[ $OLD_PACKAGE != "$PACKAGE" ]]; then
echo "Replacing package $OLD_PACKAGE with $PACKAGE in all java files"
find .. -type f -name "*.java" -exec sed -i "s/$OLD_PACKAGE/$PACKAGE/g" {} +
find .. -type f -name "*.java" -exec "${SED}" "${SED_I[@]}" "s/$OLD_PACKAGE/$PACKAGE/g" {} +
fi

curl -sL https://raw.githubusercontent.com/prometheus/client_model/master/io/prometheus/client/metrics.proto -o $PROTO_DIR/metrics.proto

sed -i "s/java_package = \"io.prometheus.client\"/java_package = \"$PACKAGE\"/" $PROTO_DIR/metrics.proto
protoc --java_out "$TARGET_DIR" $PROTO_DIR/metrics.proto
sed -i '1 i\//CHECKSTYLE:OFF: checkstyle' "$(find src/main/generated/io -type f)"
sed -i -e $'$a\\\n//CHECKSTYLE:ON: checkstyle' "$(find src/main/generated/io -type f)"
"${SED}" "${SED_I[@]}" "s/java_package = \"io.prometheus.client\"/java_package = \"$PACKAGE\"/" $PROTO_DIR/metrics.proto
$PROTOC --java_out "$TARGET_DIR" $PROTO_DIR/metrics.proto
find src/main/generated/io -type f -exec "${SED}" "${SED_I[@]}" '1 i\
//CHECKSTYLE:OFF: checkstyle' {} \;
find src/main/generated/io -type f -exec "${SED}" "${SED_I[@]}" -e $'$a\\\n//CHECKSTYLE:ON: checkstyle' {} \;

GENERATED_WITH=$(grep -oP '\/\/ Protobuf Java Version: \K.*' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)
GENERATED_WITH=$($SED -n 's/.*\/\/ Protobuf Java Version: \(.*\)/\1/p' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)

function help() {
echo "Please use https://mise.jdx.dev/ - this will use the version specified in mise.toml"
Expand Down