Skip to content

Commit 9b3d074

Browse files
committed
cache delegate
Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 3d5436f commit 9b3d074

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

integration-tests/it-spring-boot-smoke-test/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@
7474
<groupId>org.springframework.boot</groupId>
7575
<artifactId>spring-boot-maven-plugin</artifactId>
7676
</plugin>
77+
<plugin>
78+
<groupId>com.diffplug.spotless</groupId>
79+
<artifactId>spotless-maven-plugin</artifactId>
80+
<version>2.43.0</version>
81+
<configuration>
82+
<java>
83+
<googleJavaFormat/>
84+
</java>
85+
</configuration>
86+
<executions>
87+
<execution>
88+
<phase>verify</phase>
89+
<goals>
90+
<goal>check</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
</plugin>
7795
</plugins>
7896
</build>
7997

integration-tests/it-spring-boot-smoke-test/src/test/java/io/prometheus/metrics/it/springboot/ApplicationTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
package io.prometheus.metrics.it.springboot;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import io.prometheus.client.it.common.ExporterTest;
46
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_28_3.Metrics;
5-
import org.junit.jupiter.api.Test;
6-
import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
7-
import org.springframework.boot.test.context.SpringBootTest;
8-
97
import java.io.IOException;
108
import java.net.URL;
119
import java.util.List;
1210
import java.util.Optional;
13-
14-
import static org.assertj.core.api.Assertions.assertThat;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
13+
import org.springframework.boot.test.context.SpringBootTest;
1514

1615
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
1716
@AutoConfigureObservability
1817
class ApplicationTest {
1918
@Test
2019
public void testPrometheusProtobufFormat() throws IOException {
2120
ExporterTest.Response response =
22-
ExporterTest.scrape(
21+
ExporterTest.scrape(
2322
"GET",
2423
new URL("http://localhost:8080/actuator/prometheus"),
2524
"Accept",
2625
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited");
2726
assertThat(response.status).isEqualTo(200);
2827

2928
List<Metrics.MetricFamily> metrics = response.protoBody();
30-
Optional<Metrics.MetricFamily> metric = metrics.stream()
29+
Optional<Metrics.MetricFamily> metric =
30+
metrics.stream()
3131
.filter(m -> m.getName().equals("application_started_time_seconds"))
3232
.findFirst();
3333
assertThat(metric).isPresent();

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
*/
1414
public class PrometheusProtobufWriter implements ExpositionFormatWriter {
1515

16-
@Nullable private final ExpositionFormatWriter delegate;
16+
@Nullable private static final ExpositionFormatWriter DELEGATE = createProtobufWriter();
1717

1818
public static final String CONTENT_TYPE =
1919
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited";
2020

21-
public PrometheusProtobufWriter() {
22-
this.delegate = createProtobufWriter();
23-
}
24-
2521
@Nullable
2622
private static ExpositionFormatWriter createProtobufWriter() {
2723
try {
@@ -53,23 +49,23 @@ public String getContentType() {
5349

5450
@Override
5551
public boolean isAvailable() {
56-
return delegate != null;
52+
return DELEGATE != null;
5753
}
5854

5955
@Override
6056
public String toDebugString(MetricSnapshots metricSnapshots) {
6157
checkAvailable();
62-
return delegate.toDebugString(metricSnapshots);
58+
return DELEGATE.toDebugString(metricSnapshots);
6359
}
6460

6561
@Override
6662
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
6763
checkAvailable();
68-
delegate.write(out, metricSnapshots);
64+
DELEGATE.write(out, metricSnapshots);
6965
}
7066

7167
private void checkAvailable() {
72-
if (delegate == null) {
68+
if (DELEGATE == null) {
7369
throw new UnsupportedOperationException("Prometheus protobuf writer not available");
7470
}
7571
}

0 commit comments

Comments
 (0)