Skip to content

Commit

Permalink
fix Protoc build (#1196)
Browse files Browse the repository at this point in the history
* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

* fix protoc generation

Signed-off-by: Gregor Zeitlinger <[email protected]>

---------

Signed-off-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
zeitlinger authored Nov 8, 2024
1 parent 0ef12e2 commit 09d9eac
Show file tree
Hide file tree
Showing 10 changed files with 949 additions and 930 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
- name: Setup ASDF
uses: equisoft-actions/with-asdf-vm@v2
- name: Cache local Maven repository
uses: actions/cache@v4
with:
java-version: 17
distribution: temurin
cache: 'maven'
- name: Install Protoc
run: |
VERSION=28.2
curl -sL -o protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v$VERSION/protoc-$VERSION-linux-x86_64.zip
sudo unzip protoc.zip -d /usr/local
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Run the Maven verify phase
env:
PROTO_GENERATION: true
REQUIRE_PROTO_UP_TO_DATE: true
run: |
echo "Java version: $(java -version) in $(which java), Maven version: $(mvn -v)"
echo "JAVA_HOME: $JAVA_HOME"
./mvnw clean install
./mvnw javadoc:javadoc -P javadoc # just to check if javadoc is generated
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
java temurin-17.0.7+7
java temurin-17.0.13+11
protoc 28.3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import io.prometheus.client.it.common.LogConsumer;
import io.prometheus.client.it.common.Volume;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.prometheus.metrics.core.exemplars.ExemplarSamplerConfigTestUtil;
import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter;
import io.prometheus.metrics.expositionformats.TextFormatUtil;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.Label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter;
import io.prometheus.metrics.expositionformats.TextFormatUtil;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
import io.prometheus.metrics.model.snapshots.ClassicHistogramBucket;
import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.Exemplars;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter;
import io.prometheus.metrics.expositionformats.TextFormatUtil;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_2.Metrics;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.Unit;
Expand Down
34 changes: 26 additions & 8 deletions prometheus-metrics-exposition-formats/generate-protobuf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,42 @@ set -euo pipefail
TARGET_DIR=$1
PROTO_DIR=src/main/protobuf
PROTOBUF_VERSION_STRING=$2
PROTOBUF_VERSION="${PROTOBUF_VERSION_STRING//_/.}"

echo "Generating protobuf sources for version $PROTOBUF_VERSION_STRING in $TARGET_DIR"
echo "Generating protobuf sources for version $PROTOBUF_VERSION in $TARGET_DIR"

rm -rf TARGET_DIR || true
rm -rf $TARGET_DIR
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/PrometheusProtobufWriter.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" {} +
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 = \"io.prometheus.metrics.expositionformats.generated.com_google_protobuf_${PROTOBUF_VERSION_STRING}\"/" $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

# stop the build if there class is not up-to-date
# show local changes
DIFF=$(git diff)
if [[ ${REQUIRE_PROTO_UP_TO_DATE:-false} == "true" && -n "$DIFF" ]]; then
GENERATED_WITH=$(grep -oP '\/\/ Protobuf Java Version: \K.*' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)

if [[ $GENERATED_WITH != "$PROTOBUF_VERSION" ]]; then
echo "Generated protobuf sources version $GENERATED_WITH does not match provided version $PROTOBUF_VERSION"
echo "Please update the protoc version .tool-versions to the latest version of https://github.com/protocolbuffers/protobuf/releases"
echo "Please use https://github.com/asdf-vm/asdf - this will use the version specified in .tool-versions"
exit 1
fi

STATUS=$(git status --porcelain)
if [[ ${REQUIRE_PROTO_UP_TO_DATE:-false} == "true" && -n "$STATUS" ]]; then
echo "Generated protobuf sources are not up-to-date. Please run 'PROTO_GENERATION=true mvn clean install' and commit the changes."
echo "Local changes:"
echo "$DIFF"
echo "$STATUS"
exit 1
fi

Loading

0 comments on commit 09d9eac

Please sign in to comment.