Skip to content

Commit b4f5707

Browse files
committed
make protobuf optional
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 98bb0e7 commit b4f5707

File tree

7 files changed

+76
-29
lines changed

7 files changed

+76
-29
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@
9191
</dependencyManagement>
9292

9393
<dependencies>
94+
<dependency>
95+
<groupId>com.google.code.findbugs</groupId>
96+
<artifactId>jsr305</artifactId>
97+
<version>3.0.2</version>
98+
<scope>compile</scope>
99+
</dependency>
100+
101+
<!-- test dependencies -->
94102
<dependency>
95103
<groupId>org.junit.jupiter</groupId>
96104
<artifactId>junit-jupiter-engine</artifactId>

prometheus-metrics-exporter-common/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
<artifactId>prometheus-metrics-model</artifactId>
2727
<version>${project.version}</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>io.prometheus</groupId>
31+
<artifactId>prometheus-writer-textformat</artifactId>
32+
<version>${project.version}</version>
33+
</dependency>
2934
<dependency>
3035
<groupId>io.prometheus</groupId>
3136
<artifactId>prometheus-metrics-exposition-formats</artifactId>
3237
<version>${project.version}</version>
38+
<scope>runtime</scope>
3339
</dependency>
3440
</dependencies>
3541

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import java.io.IOException;
1212
import java.io.OutputStream;
1313
import java.nio.charset.StandardCharsets;
14+
import java.util.ArrayList;
1415
import java.util.Arrays;
1516
import java.util.Enumeration;
17+
import java.util.List;
1618
import java.util.concurrent.atomic.AtomicInteger;
1719
import java.util.function.Predicate;
1820
import java.util.zip.GZIPOutputStream;
@@ -23,7 +25,8 @@ public class PrometheusScrapeHandler {
2325
private final PrometheusRegistry registry;
2426
private final ExpositionFormats expositionFormats;
2527
private final Predicate<String> nameFilter;
26-
private AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
28+
private final AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
29+
private final List<String> supportedFormats;
2730

2831
public PrometheusScrapeHandler() {
2932
this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
@@ -41,6 +44,10 @@ public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry r
4144
this.expositionFormats = ExpositionFormats.init(config.getExporterProperties());
4245
this.registry = registry;
4346
this.nameFilter = makeNameFilter(config.getExporterFilterProperties());
47+
supportedFormats = new ArrayList<>(Arrays.asList("openmetrics", "text"));
48+
if (expositionFormats.getPrometheusProtobufWriter() != null) {
49+
supportedFormats.add("prometheus-protobuf");
50+
}
4451
}
4552

4653
public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
@@ -137,9 +144,7 @@ private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExch
137144
return false;
138145
} else {
139146
response.setHeader("Content-Type", "text/plain; charset=utf-8");
140-
boolean supportedFormat =
141-
Arrays.asList("openmetrics", "text", "prometheus-protobuf").contains(debugParam);
142-
int responseStatus = supportedFormat ? 200 : 500;
147+
int responseStatus = supportedFormats.contains(debugParam) ? 200 : 500;
143148
OutputStream body = response.sendHeadersAndGetBody(responseStatus, 0);
144149
switch (debugParam) {
145150
case "openmetrics":

prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import io.prometheus.metrics.config.ExporterPushgatewayProperties;
66
import io.prometheus.metrics.config.PrometheusProperties;
77
import io.prometheus.metrics.config.PrometheusPropertiesException;
8-
import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter;
8+
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
9+
import io.prometheus.metrics.expositionformats.ExpositionFormats;
910
import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;
1011
import io.prometheus.metrics.model.registry.Collector;
1112
import io.prometheus.metrics.model.registry.MultiCollector;
@@ -64,7 +65,7 @@ public class PushGateway {
6465
private static final int MILLISECONDS_PER_SECOND = 1000;
6566

6667
private final URL url;
67-
private final Format format;
68+
private final ExpositionFormatWriter writer;
6869
private final Map<String, String> requestHeaders;
6970
private final PrometheusRegistry registry;
7071
private final HttpConnectionFactory connectionFactory;
@@ -76,10 +77,19 @@ private PushGateway(
7677
HttpConnectionFactory connectionFactory,
7778
Map<String, String> requestHeaders) {
7879
this.registry = registry;
79-
this.format = format;
8080
this.url = url;
8181
this.requestHeaders = Collections.unmodifiableMap(new HashMap<>(requestHeaders));
8282
this.connectionFactory = connectionFactory;
83+
writer = getWriter(format);
84+
}
85+
86+
private ExpositionFormatWriter getWriter(Format format) {
87+
if (format == Format.PROMETHEUS_TEXT) {
88+
return new PrometheusTextFormatWriter(false);
89+
} else {
90+
// use reflection to avoid a compile-time dependency on the expositionformats module
91+
return ExpositionFormats.createProtobufWriter();
92+
}
8393
}
8494

8595
/**
@@ -160,11 +170,7 @@ private void doRequest(PrometheusRegistry registry, String method) throws IOExce
160170
try {
161171
HttpURLConnection connection = connectionFactory.create(url);
162172
requestHeaders.forEach(connection::setRequestProperty);
163-
if (format == Format.PROMETHEUS_TEXT) {
164-
connection.setRequestProperty("Content-Type", PrometheusTextFormatWriter.CONTENT_TYPE);
165-
} else {
166-
connection.setRequestProperty("Content-Type", PrometheusProtobufWriter.CONTENT_TYPE);
167-
}
173+
connection.setRequestProperty("Content-Type", writer.getContentType());
168174
if (!method.equals("DELETE")) {
169175
connection.setDoOutput(true);
170176
}
@@ -177,11 +183,7 @@ private void doRequest(PrometheusRegistry registry, String method) throws IOExce
177183
try {
178184
if (!method.equals("DELETE")) {
179185
OutputStream outputStream = connection.getOutputStream();
180-
if (format == Format.PROMETHEUS_TEXT) {
181-
new PrometheusTextFormatWriter(false).write(outputStream, registry.scrape());
182-
} else {
183-
new PrometheusProtobufWriter().write(outputStream, registry.scrape());
184-
}
186+
writer.write(outputStream, registry.scrape());
185187
outputStream.flush();
186188
outputStream.close();
187189
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public String getContentType() {
5050
return CONTENT_TYPE;
5151
}
5252

53+
@Override
5354
public String toDebugString(MetricSnapshots metricSnapshots) {
5455
StringBuilder stringBuilder = new StringBuilder();
5556
for (MetricSnapshot snapshot : metricSnapshots) {
56-
if (snapshot.getDataPoints().size() > 0) {
57+
if (!snapshot.getDataPoints().isEmpty()) {
5758
stringBuilder.append(TextFormat.printer().printToString(convert(snapshot)));
5859
}
5960
}
@@ -63,7 +64,7 @@ public String toDebugString(MetricSnapshots metricSnapshots) {
6364
@Override
6465
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
6566
for (MetricSnapshot snapshot : metricSnapshots) {
66-
if (snapshot.getDataPoints().size() > 0) {
67+
if (!snapshot.getDataPoints().isEmpty()) {
6768
convert(snapshot).writeDelimitedTo(out);
6869
}
6970
}

prometheus-writer-textformat/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormatWriter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.prometheus.metrics.expositionformats;
22

33
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
4+
import java.io.ByteArrayOutputStream;
45
import java.io.IOException;
56
import java.io.OutputStream;
67

@@ -10,5 +11,15 @@ public interface ExpositionFormatWriter {
1011
/** Text formats use UTF-8 encoding. */
1112
void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException;
1213

14+
default String toDebugString(MetricSnapshots metricSnapshots) {
15+
ByteArrayOutputStream out = new ByteArrayOutputStream();
16+
try {
17+
write(out, metricSnapshots);
18+
return out.toString("UTF-8");
19+
} catch (IOException e) {
20+
throw new RuntimeException(e);
21+
}
22+
}
23+
1324
String getContentType();
1425
}

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java renamed to prometheus-writer-textformat/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
import io.prometheus.metrics.config.ExporterProperties;
44
import io.prometheus.metrics.config.PrometheusProperties;
5+
import javax.annotation.Nullable;
56

67
public class ExpositionFormats {
78

8-
private final PrometheusProtobufWriter prometheusProtobufWriter;
9-
private final PrometheusTextFormatWriter prometheusTextFormatWriter;
10-
private final OpenMetricsTextFormatWriter openMetricsTextFormatWriter;
9+
@Nullable private final ExpositionFormatWriter prometheusProtobufWriter;
10+
private final ExpositionFormatWriter prometheusTextFormatWriter;
11+
private final ExpositionFormatWriter openMetricsTextFormatWriter;
1112

1213
private ExpositionFormats(
13-
PrometheusProtobufWriter prometheusProtobufWriter,
14-
PrometheusTextFormatWriter prometheusTextFormatWriter,
15-
OpenMetricsTextFormatWriter openMetricsTextFormatWriter) {
14+
ExpositionFormatWriter prometheusProtobufWriter,
15+
ExpositionFormatWriter prometheusTextFormatWriter,
16+
ExpositionFormatWriter openMetricsTextFormatWriter) {
1617
this.prometheusProtobufWriter = prometheusProtobufWriter;
1718
this.prometheusTextFormatWriter = prometheusTextFormatWriter;
1819
this.openMetricsTextFormatWriter = openMetricsTextFormatWriter;
@@ -24,12 +25,25 @@ public static ExpositionFormats init() {
2425

2526
public static ExpositionFormats init(ExporterProperties properties) {
2627
return new ExpositionFormats(
27-
new PrometheusProtobufWriter(),
28+
createProtobufWriter(),
2829
new PrometheusTextFormatWriter(properties.getIncludeCreatedTimestamps()),
2930
new OpenMetricsTextFormatWriter(
3031
properties.getIncludeCreatedTimestamps(), properties.getExemplarsOnAllMetricTypes()));
3132
}
3233

34+
@Nullable
35+
public static ExpositionFormatWriter createProtobufWriter() {
36+
try {
37+
return Class.forName("io.prometheus.metrics.expositionformats.PrometheusProtobufWriter")
38+
.asSubclass(ExpositionFormatWriter.class)
39+
.getDeclaredConstructor()
40+
.newInstance();
41+
} catch (Exception e) {
42+
// not in classpath
43+
return null;
44+
}
45+
}
46+
3347
public ExpositionFormatWriter findWriter(String acceptHeader) {
3448
if (prometheusProtobufWriter.accepts(acceptHeader)) {
3549
return prometheusProtobufWriter;
@@ -40,15 +54,15 @@ public ExpositionFormatWriter findWriter(String acceptHeader) {
4054
return prometheusTextFormatWriter;
4155
}
4256

43-
public PrometheusProtobufWriter getPrometheusProtobufWriter() {
57+
public ExpositionFormatWriter getPrometheusProtobufWriter() {
4458
return prometheusProtobufWriter;
4559
}
4660

47-
public PrometheusTextFormatWriter getPrometheusTextFormatWriter() {
61+
public ExpositionFormatWriter getPrometheusTextFormatWriter() {
4862
return prometheusTextFormatWriter;
4963
}
5064

51-
public OpenMetricsTextFormatWriter getOpenMetricsTextFormatWriter() {
65+
public ExpositionFormatWriter getOpenMetricsTextFormatWriter() {
5266
return openMetricsTextFormatWriter;
5367
}
5468
}

0 commit comments

Comments
 (0)