Skip to content

Commit

Permalink
revert incompatible api change (#1192)
Browse files Browse the repository at this point in the history
Signed-off-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
zeitlinger authored Nov 7, 2024
1 parent 7bc442f commit da91960
Show file tree
Hide file tree
Showing 28 changed files with 92 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR
gaugeBuilder.dataPoint(gaugeDataPointBuilder.build());
}
}
Collection<MetricSnapshot<?>> snaps = new ArrayList<>();
Collection<MetricSnapshot> snaps = new ArrayList<>();
snaps.add(counterBuilder.build());
snaps.add(gaugeBuilder.build());
return new MetricSnapshots(snaps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Metric(Builder<?, ?> builder) {
}

@Override
public abstract MetricSnapshot<?> collect();
public abstract MetricSnapshot collect();

protected abstract static class Builder<B extends Builder<B, M>, M extends Metric> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ protected StatefulMetric(Builder<?, ?> builder) {
/**
* labels and metricData have the same size. labels.get(i) are the labels for metricData.get(i).
*/
protected abstract MetricSnapshot<?> collect(List<Labels> labels, List<T> metricData);
protected abstract MetricSnapshot collect(List<Labels> labels, List<T> metricData);

@Override
public MetricSnapshot<?> collect() {
public MetricSnapshot collect() {
if (labelNames.length == 0 && data.isEmpty()) {
// This is a metric without labels that has not been used yet. Initialize the data on the fly.
labelValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Collection<MetricData> collectAllMetrics() {
resourceWithTargetInfo,
scopeFromInfo != null ? scopeFromInfo : instrumentationScopeInfo,
System.currentTimeMillis());
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot instanceof CounterSnapshot) {
addUnlessNull(result, factory.create((CounterSnapshot) snapshot));
} else if (snapshot instanceof GaugeSnapshot) {
Expand All @@ -78,7 +78,7 @@ public Collection<MetricData> collectAllMetrics() {

private Resource resourceFromTargetInfo(MetricSnapshots snapshots) {
ResourceBuilder result = Resource.builder();
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot.getMetadata().getName().equals("target") && snapshot instanceof InfoSnapshot) {
InfoSnapshot targetInfo = (InfoSnapshot) snapshot;
if (!targetInfo.getDataPoints().isEmpty()) {
Expand All @@ -95,7 +95,7 @@ private Resource resourceFromTargetInfo(MetricSnapshots snapshots) {

private InstrumentationScopeInfo instrumentationScopeFromOTelScopeInfo(
MetricSnapshots snapshots) {
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot.getMetadata().getPrometheusName().equals("otel_scope")
&& snapshot instanceof InfoSnapshot) {
InfoSnapshot scopeInfo = (InfoSnapshot) snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getContentType() {
@Override
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
for (MetricSnapshot<?> snapshot : metricSnapshots) {
for (MetricSnapshot snapshot : metricSnapshots) {
if (!snapshot.getDataPoints().isEmpty()) {
if (snapshot instanceof CounterSnapshot) {
writeCounter(writer, (CounterSnapshot) snapshot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getContentType() {

public String toDebugString(MetricSnapshots metricSnapshots) {
StringBuilder stringBuilder = new StringBuilder();
for (MetricSnapshot<?> snapshot : metricSnapshots) {
for (MetricSnapshot snapshot : metricSnapshots) {
if (snapshot.getDataPoints().size() > 0) {
stringBuilder.append(TextFormat.printer().printToString(convert(snapshot)));
}
Expand All @@ -62,14 +62,14 @@ public String toDebugString(MetricSnapshots metricSnapshots) {

@Override
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
for (MetricSnapshot<?> snapshot : metricSnapshots) {
for (MetricSnapshot snapshot : metricSnapshots) {
if (snapshot.getDataPoints().size() > 0) {
convert(snapshot).writeDelimitedTo(out);
}
}
}

public Metrics.MetricFamily convert(MetricSnapshot<?> snapshot) {
public Metrics.MetricFamily convert(MetricSnapshot snapshot) {
Metrics.MetricFamily.Builder builder = Metrics.MetricFamily.newBuilder();
if (snapshot instanceof CounterSnapshot) {
for (CounterDataPointSnapshot data : ((CounterSnapshot) snapshot).getDataPoints()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
// "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and
// "summary".
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
for (MetricSnapshot<?> snapshot : metricSnapshots) {
for (MetricSnapshot snapshot : metricSnapshots) {
if (snapshot.getDataPoints().size() > 0) {
if (snapshot instanceof CounterSnapshot) {
writeCounter(writer, (CounterSnapshot) snapshot);
Expand All @@ -80,7 +80,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
}
}
if (writeCreatedTimestamps) {
for (MetricSnapshot<?> snapshot : metricSnapshots) {
for (MetricSnapshot snapshot : metricSnapshots) {
if (snapshot.getDataPoints().size() > 0) {
if (snapshot instanceof CounterSnapshot) {
writeCreated(writer, snapshot);
Expand All @@ -95,8 +95,7 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
writer.flush();
}

public void writeCreated(OutputStreamWriter writer, MetricSnapshot<?> snapshot)
throws IOException {
public void writeCreated(OutputStreamWriter writer, MetricSnapshot snapshot) throws IOException {
boolean metadataWritten = false;
MetricMetadata metadata = snapshot.getMetadata();
for (DataPointSnapshot data : snapshot.getDataPoints()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2619,47 +2619,45 @@ public void testLabelValueEscape() throws IOException {
assertPrometheusText(prometheus, counter);
}

private void assertOpenMetricsText(String expected, MetricSnapshot<?> snapshot)
throws IOException {
private void assertOpenMetricsText(String expected, MetricSnapshot snapshot) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, false);
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expected);
}

private void assertOpenMetricsTextWithExemplarsOnAllTimeSeries(
String expected, MetricSnapshot<?> snapshot) throws IOException {
String expected, MetricSnapshot snapshot) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true);
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expected);
}

private void assertOpenMetricsTextWithoutCreated(String expected, MetricSnapshot<?> snapshot)
private void assertOpenMetricsTextWithoutCreated(String expected, MetricSnapshot snapshot)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(false, false);
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expected);
}

private void assertPrometheusText(String expected, MetricSnapshot<?> snapshot)
throws IOException {
private void assertPrometheusText(String expected, MetricSnapshot snapshot) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrometheusTextFormatWriter writer = new PrometheusTextFormatWriter(true);
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expected);
}

private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot<?> snapshot)
private void assertPrometheusTextWithoutCreated(String expected, MetricSnapshot snapshot)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrometheusTextFormatWriter writer = new PrometheusTextFormatWriter(false);
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expected);
}

private void assertPrometheusProtobuf(String expected, MetricSnapshot<?> snapshot) {
private void assertPrometheusProtobuf(String expected, MetricSnapshot snapshot) {
PrometheusProtobufWriter writer = new PrometheusProtobufWriter();
Metrics.MetricFamily protobufData = writer.convert(snapshot);
String actual = TextFormatUtil.shortDebugString(protobufData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private MetricMetadata getMetricMetaData(String metricName, Metric metric) {
* Export counter as Prometheus <a
* href="https://prometheus.io/docs/concepts/metric_types/#gauge">Gauge</a>.
*/
MetricSnapshot<?> fromCounter(String dropwizardName, Counter counter) {
MetricSnapshot fromCounter(String dropwizardName, Counter counter) {
MetricMetadata metadata = getMetricMetaData(dropwizardName, counter);
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
CounterSnapshot.CounterDataPointSnapshot.builder()
Expand All @@ -84,7 +84,7 @@ MetricSnapshot<?> fromCounter(String dropwizardName, Counter counter) {
}

/** Export gauge as a prometheus gauge. */
MetricSnapshot<?> fromGauge(String dropwizardName, Gauge<?> gauge) {
MetricSnapshot fromGauge(String dropwizardName, Gauge<?> gauge) {
Object obj = gauge.getValue();
double value;
if (obj instanceof Number) {
Expand Down Expand Up @@ -119,7 +119,7 @@ MetricSnapshot<?> fromGauge(String dropwizardName, Gauge<?> gauge) {
* @param count the total sample count for this snapshot.
* @param factor a factor to apply to histogram values.
*/
MetricSnapshot<?> fromSnapshotAndCount(
MetricSnapshot fromSnapshotAndCount(
String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) {
Quantiles quantiles =
Quantiles.builder()
Expand All @@ -144,7 +144,7 @@ MetricSnapshot<?> fromSnapshotAndCount(
}

/** Convert histogram snapshot. */
MetricSnapshot<?> fromHistogram(String dropwizardName, Histogram histogram) {
MetricSnapshot fromHistogram(String dropwizardName, Histogram histogram) {
return fromSnapshotAndCount(
dropwizardName,
histogram.getSnapshot(),
Expand All @@ -154,7 +154,7 @@ MetricSnapshot<?> fromHistogram(String dropwizardName, Histogram histogram) {
}

/** Export Dropwizard Timer as a histogram. Use TIME_UNIT as time unit. */
MetricSnapshot<?> fromTimer(String dropwizardName, Timer timer) {
MetricSnapshot fromTimer(String dropwizardName, Timer timer) {
return fromSnapshotAndCount(
dropwizardName,
timer.getSnapshot(),
Expand All @@ -164,7 +164,7 @@ MetricSnapshot<?> fromTimer(String dropwizardName, Timer timer) {
}

/** Export a Meter as a prometheus COUNTER. */
MetricSnapshot<?> fromMeter(String dropwizardName, Meter meter) {
MetricSnapshot fromMeter(String dropwizardName, Meter meter) {
MetricMetadata metadata = getMetricMetaData(dropwizardName + "_total", meter);
CounterSnapshot.CounterDataPointSnapshot.Builder dataPointBuilder =
CounterSnapshot.CounterDataPointSnapshot.builder().value(meter.getCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testListenerLogic() {
}

private double getCountByPool(String metricName, String poolName, MetricSnapshots snapshots) {
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot.getMetadata().getPrometheusName().equals(metricName)) {
for (CounterSnapshot.CounterDataPointSnapshot data :
((CounterSnapshot) snapshot).getDataPoints()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testInvalidThreadIds() {

private Map<String, Double> getCountByState(MetricSnapshots snapshots) {
Map<String, Double> result = new HashMap<>();
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot.getMetadata().getName().equals("jvm_threads_state")) {
for (GaugeSnapshot.GaugeDataPointSnapshot data :
((GaugeSnapshot) snapshot).getDataPoints()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
public interface Collector {

/** Called when the Prometheus server scrapes metrics. */
MetricSnapshot<?> collect();
MetricSnapshot collect();

/**
* Provides Collector with the details of the request issued by Prometheus to allow multi-target
* pattern implementation Override to implement request dependent logic to provide MetricSnapshot
*/
default MetricSnapshot<?> collect(PrometheusScrapeRequest scrapeRequest) {
default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) {
return collect();
}

Expand All @@ -28,8 +28,8 @@ default MetricSnapshot<?> collect(PrometheusScrapeRequest scrapeRequest) {
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot<?> collect(Predicate<String> includedNames) {
MetricSnapshot<?> result = collect();
default MetricSnapshot collect(Predicate<String> includedNames) {
MetricSnapshot result = collect();
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
Expand All @@ -43,9 +43,9 @@ default MetricSnapshot<?> collect(Predicate<String> includedNames) {
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot<?> collect(
default MetricSnapshot collect(
Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshot<?> result = collect(scrapeRequest);
MetricSnapshot result = collect(scrapeRequest);
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ default MetricSnapshots collect(
Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshots allSnapshots = scrapeRequest == null ? collect() : collect(scrapeRequest);
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (MetricSnapshot<?> snapshot : allSnapshots) {
for (MetricSnapshot snapshot : allSnapshots) {
if (includedNames.test(snapshot.getMetadata().getPrometheusName())) {
result.metricSnapshot(snapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public MetricSnapshots scrape() {
public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) {
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (Collector collector : collectors) {
MetricSnapshot<?> snapshot =
MetricSnapshot snapshot =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
if (snapshot != null) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
Expand All @@ -82,7 +82,7 @@ public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) {
for (MultiCollector collector : multiCollectors) {
MetricSnapshots snapshots =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
throw new IllegalStateException(
snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
Expand Down Expand Up @@ -111,7 +111,7 @@ public MetricSnapshots scrape(
// prometheusName == null means the name is unknown, and we have to scrape to learn the name.
// prometheusName != null means we can skip the scrape if the name is excluded.
if (prometheusName == null || includedNames.test(prometheusName)) {
MetricSnapshot<?> snapshot =
MetricSnapshot snapshot =
scrapeRequest == null
? collector.collect(includedNames)
: collector.collect(includedNames, scrapeRequest);
Expand All @@ -138,7 +138,7 @@ public MetricSnapshots scrape(
scrapeRequest == null
? collector.collect(includedNames)
: collector.collect(includedNames, scrapeRequest);
for (MetricSnapshot<?> snapshot : snapshots) {
for (MetricSnapshot snapshot : snapshots) {
if (snapshot != null) {
result.metricSnapshot(snapshot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

/** Immutable snapshot of a Counter. */
public class CounterSnapshot extends MetricSnapshot<CounterSnapshot.CounterDataPointSnapshot> {
public class CounterSnapshot extends MetricSnapshot {

/**
* To create a new {@link CounterSnapshot}, you can either call the constructor directly or use
Expand All @@ -19,9 +19,10 @@ public CounterSnapshot(MetricMetadata metadata, Collection<CounterDataPointSnaps
super(metadata, dataPoints);
}

@SuppressWarnings("unchecked")
@Override
public List<CounterDataPointSnapshot> getDataPoints() {
return dataPoints;
return (List<CounterDataPointSnapshot>) dataPoints;
}

public static class CounterDataPointSnapshot extends DataPointSnapshot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

/** Immutable snapshot of a Gauge. */
public final class GaugeSnapshot extends MetricSnapshot<GaugeSnapshot.GaugeDataPointSnapshot> {
public final class GaugeSnapshot extends MetricSnapshot {

/**
* To create a new {@link GaugeSnapshot}, you can either call the constructor directly or use the
Expand All @@ -18,9 +18,10 @@ public GaugeSnapshot(MetricMetadata metadata, Collection<GaugeDataPointSnapshot>
super(metadata, data);
}

@SuppressWarnings("unchecked")
@Override
public List<GaugeDataPointSnapshot> getDataPoints() {
return dataPoints;
return (List<GaugeDataPointSnapshot>) dataPoints;
}

public static final class GaugeDataPointSnapshot extends DataPointSnapshot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import java.util.List;

/** Immutable snapshot of a Histogram. */
public final class HistogramSnapshot
extends MetricSnapshot<HistogramSnapshot.HistogramDataPointSnapshot> {
public final class HistogramSnapshot extends MetricSnapshot {

private final boolean gaugeHistogram;
public static final int CLASSIC_HISTOGRAM = Integer.MIN_VALUE;
Expand Down Expand Up @@ -40,9 +39,10 @@ public boolean isGaugeHistogram() {
return gaugeHistogram;
}

@SuppressWarnings("unchecked")
@Override
public List<HistogramDataPointSnapshot> getDataPoints() {
return dataPoints;
return (List<HistogramDataPointSnapshot>) dataPoints;
}

public static final class HistogramDataPointSnapshot extends DistributionDataPointSnapshot {
Expand Down
Loading

0 comments on commit da91960

Please sign in to comment.