Skip to content

Commit 3fac677

Browse files
committed
Add stable Metrics class to decouple consumers from protobuf version
The generated protobuf Metrics.java lives in a versioned package that changes every protobuf release, requiring import updates in all consumer files. Introduce a stable extending class in the version-free package so consumers import from a single stable location. On protobuf upgrades only the extends clause in the stable class changes, handled automatically by generate-protobuf.sh. Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 246c956 commit 3fac677

File tree

7 files changed

+33
-7
lines changed

7 files changed

+33
-7
lines changed

CONTRIBUTING.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,23 @@ mise run compile
5858

5959
## Updating the Protobuf Java Classes
6060

61+
The generated protobuf `Metrics.java` lives in a versioned package
62+
(e.g., `...generated.com_google_protobuf_4_33_5`) that changes with each
63+
protobuf release. A stable extending class at
64+
`...generated/Metrics.java` reexports all types so that consumer code
65+
only imports from the version-free package. On protobuf upgrades only
66+
the `extends` clause in the stable class changes.
67+
6168
In the failing PR from renovate, run:
6269

6370
```shell
6471
mise run generate
6572
```
6673

67-
Add the new `Metrics.java` to Git and commit it.
74+
The script will:
75+
76+
1. Re-generate the protobuf sources with the new version.
77+
2. Update the versioned package name in all Java files
78+
(including the stable `Metrics.java` extends clause).
79+
80+
Add the updated files to Git and commit them.

prometheus-metrics-exposition-formats/generate-protobuf.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mkdir -p "$TARGET_DIR"
1818
rm -rf $PROTO_DIR || true
1919
mkdir -p $PROTO_DIR
2020

21-
OLD_PACKAGE=$(sed -nE 's/import (io.prometheus.metrics.expositionformats.generated.*).Metrics;/\1/p' src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java)
21+
OLD_PACKAGE=$(sed -nE 's/.*extends (io\.prometheus\.metrics\.expositionformats\.generated\.[^ ]*?)\.Metrics.*/\1/p' src/main/java/io/prometheus/metrics/expositionformats/generated/Metrics.java)
2222
PACKAGE="io.prometheus.metrics.expositionformats.generated.com_google_protobuf_${PROTOBUF_VERSION_STRING}"
2323

2424
if [[ $OLD_PACKAGE != "$PACKAGE" ]]; then
@@ -33,6 +33,10 @@ protoc --java_out "$TARGET_DIR" $PROTO_DIR/metrics.proto
3333
sed -i '1 i\//CHECKSTYLE:OFF: checkstyle' "$(find src/main/generated/io -type f)"
3434
sed -i -e $'$a\\\n//CHECKSTYLE:ON: checkstyle' "$(find src/main/generated/io -type f)"
3535

36+
GENERATED_FILE="$TARGET_DIR/${PACKAGE//\.//}/Metrics.java"
37+
sed -i 's/public final class Metrics/public class Metrics/' "$GENERATED_FILE"
38+
sed -i 's/private Metrics() {}/protected Metrics() {}/' "$GENERATED_FILE"
39+
3640
GENERATED_WITH=$(grep -oP '\/\/ Protobuf Java Version: \K.*' "$TARGET_DIR/${PACKAGE//\.//}"/Metrics.java)
3741

3842
function help() {

prometheus-metrics-exposition-formats/src/main/generated/io/prometheus/metrics/expositionformats/generated/com_google_protobuf_4_33_5/Metrics.java

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.prometheus.metrics.expositionformats.generated;
2+
3+
public final class Metrics
4+
extends io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_33_5.Metrics {
5+
private Metrics() {}
6+
}

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/internal/PrometheusProtobufWriterImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import io.prometheus.metrics.config.EscapingScheme;
88
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
99
import io.prometheus.metrics.expositionformats.TextFormatUtil;
10-
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_33_5.Metrics;
10+
import io.prometheus.metrics.expositionformats.generated.Metrics;
1111
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
1212
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
1313
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
@@ -30,6 +30,7 @@
3030
import java.io.OutputStream;
3131
import javax.annotation.Nullable;
3232

33+
@SuppressWarnings("NonCanonicalType")
3334
public class PrometheusProtobufWriterImpl implements ExpositionFormatWriter {
3435

3536
@Override

prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/DuplicateNamesProtobufTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import io.prometheus.metrics.config.EscapingScheme;
6-
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_33_5.Metrics;
6+
import io.prometheus.metrics.expositionformats.generated.Metrics;
77
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
88
import io.prometheus.metrics.model.registry.Collector;
99
import io.prometheus.metrics.model.registry.PrometheusRegistry;
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import org.junit.jupiter.api.Test;
2121

22+
@SuppressWarnings("NonCanonicalType")
2223
class DuplicateNamesProtobufTest {
2324

2425
private static PrometheusRegistry getPrometheusRegistry() {

prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/ProtobufExpositionFormatsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import io.prometheus.metrics.config.EscapingScheme;
6-
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_33_5.Metrics;
6+
import io.prometheus.metrics.expositionformats.generated.Metrics;
77
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
88
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
99
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
1010

11+
@SuppressWarnings("NonCanonicalType")
1112
class ProtobufExpositionFormatsTest extends ExpositionFormatsTest {
1213

1314
@Override

0 commit comments

Comments
 (0)