Skip to content

Commit b2231da

Browse files
authored
[FLINK-37601] Remove Unirest dependency in PrometheusReporterTest (#26387)
* [FLINK-37601] remove Unirest dependency and use default HTTP library
1 parent ae09a0d commit b2231da

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

flink-metrics/flink-metrics-prometheus/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ under the License.
100100
<artifactId>flink-test-utils-junit</artifactId>
101101
</dependency>
102102

103-
<dependency>
104-
<groupId>com.mashape.unirest</groupId>
105-
<artifactId>unirest-java</artifactId>
106-
<version>1.4.9</version>
107-
<scope>test</scope>
108-
</dependency>
109103
</dependencies>
110104

111105
<build>

flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTaskScopeTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import org.apache.flink.metrics.util.TestMeter;
2828
import org.apache.flink.util.PortRange;
2929

30-
import com.mashape.unirest.http.exceptions.UnirestException;
3130
import org.junit.jupiter.api.AfterEach;
3231
import org.junit.jupiter.api.BeforeEach;
3332
import org.junit.jupiter.api.Test;
3433

34+
import java.io.IOException;
3535
import java.util.Arrays;
3636

3737
import static org.apache.flink.metrics.prometheus.PrometheusReporterTest.pollMetrics;
@@ -126,7 +126,8 @@ void metersCanBeAddedSeveralTimesIfTheyDifferInLabels() {
126126
}
127127

128128
@Test
129-
void histogramsCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
129+
void histogramsCanBeAddedSeveralTimesIfTheyDifferInLabels()
130+
throws IOException, InterruptedException {
130131
TestHistogram histogram1 = new TestHistogram();
131132
histogram1.setCount(1);
132133
TestHistogram histogram2 = new TestHistogram();
@@ -135,7 +136,7 @@ void histogramsCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestExcept
135136
reporter.notifyOfAddedMetric(histogram1, METRIC_NAME, metricGroup1);
136137
reporter.notifyOfAddedMetric(histogram2, METRIC_NAME, metricGroup2);
137138

138-
final String exportedMetrics = pollMetrics(reporter.getPort()).getBody();
139+
final String exportedMetrics = pollMetrics(reporter.getPort()).body();
139140
assertThat(exportedMetrics).contains("label2=\"value1_2\",} 1.0");
140141
assertThat(exportedMetrics).contains("label2=\"value2_2\",} 2.0");
141142

flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
import org.apache.flink.metrics.util.TestMeter;
3030
import org.apache.flink.util.PortRange;
3131

32-
import com.mashape.unirest.http.HttpResponse;
33-
import com.mashape.unirest.http.Unirest;
34-
import com.mashape.unirest.http.exceptions.UnirestException;
3532
import org.junit.jupiter.api.AfterEach;
3633
import org.junit.jupiter.api.BeforeEach;
3734
import org.junit.jupiter.api.Test;
3835

36+
import java.io.IOException;
37+
import java.net.URI;
38+
import java.net.http.HttpClient;
39+
import java.net.http.HttpRequest;
40+
import java.net.http.HttpResponse;
3941
import java.util.Arrays;
4042
import java.util.HashMap;
4143
import java.util.Map;
@@ -58,6 +60,7 @@ class PrometheusReporterTest {
5860
private static final String DEFAULT_LABELS = "{" + DIMENSIONS + ",}";
5961
private static final String SCOPE_PREFIX =
6062
PrometheusReporter.SCOPE_PREFIX + LOGICAL_SCOPE + PrometheusReporter.SCOPE_SEPARATOR;
63+
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
6164

6265
private static final PortRangeProvider portRangeProvider = new PortRangeProvider();
6366

@@ -85,45 +88,46 @@ void teardown() throws Exception {
8588
* {@link io.prometheus.client.Counter} may not decrease, so report {@link Counter} as {@link
8689
* io.prometheus.client.Gauge}.
8790
*
88-
* @throws UnirestException Might be thrown on HTTP problems.
91+
* @throws IOException Might be thrown on I/O problems.
92+
* @throws InterruptedException Might be thrown if the thread is interrupted.
8993
*/
9094
@Test
91-
void counterIsReportedAsPrometheusGauge() throws UnirestException {
95+
void counterIsReportedAsPrometheusGauge() throws IOException, InterruptedException {
9296
Counter testCounter = new SimpleCounter();
9397
testCounter.inc(7);
9498

9599
assertThatGaugeIsExported(testCounter, "testCounter", "7.0");
96100
}
97101

98102
@Test
99-
void gaugeIsReportedAsPrometheusGauge() throws UnirestException {
103+
void gaugeIsReportedAsPrometheusGauge() throws IOException, InterruptedException {
100104
Gauge<Integer> testGauge = () -> 1;
101105

102106
assertThatGaugeIsExported(testGauge, "testGauge", "1.0");
103107
}
104108

105109
@Test
106-
void nullGaugeDoesNotBreakReporter() throws UnirestException {
110+
void nullGaugeDoesNotBreakReporter() throws IOException, InterruptedException {
107111
Gauge<Integer> testGauge = () -> null;
108112

109113
assertThatGaugeIsExported(testGauge, "testGauge", "0.0");
110114
}
111115

112116
@Test
113-
void meterRateIsReportedAsPrometheusGauge() throws UnirestException {
117+
void meterRateIsReportedAsPrometheusGauge() throws IOException, InterruptedException {
114118
Meter testMeter = new TestMeter();
115119

116120
assertThatGaugeIsExported(testMeter, "testMeter", "5.0");
117121
}
118122

119123
private void assertThatGaugeIsExported(Metric metric, String name, String expectedValue)
120-
throws UnirestException {
124+
throws IOException, InterruptedException {
121125
assertThat(addMetricAndPollResponse(metric, name))
122126
.contains(createExpectedPollResponse(name, "", "gauge", expectedValue));
123127
}
124128

125129
@Test
126-
void histogramIsReportedAsPrometheusSummary() throws UnirestException {
130+
void histogramIsReportedAsPrometheusSummary() throws IOException, InterruptedException {
127131
Histogram testHistogram = new TestHistogram();
128132

129133
String histogramName = "testHistogram";
@@ -152,7 +156,8 @@ void histogramIsReportedAsPrometheusSummary() throws UnirestException {
152156
* name still exist.
153157
*/
154158
@Test
155-
void metricIsRemovedWhileOtherMetricsWithSameNameExist() throws UnirestException {
159+
void metricIsRemovedWhileOtherMetricsWithSameNameExist()
160+
throws IOException, InterruptedException {
156161
String metricName = "metric";
157162

158163
Counter metric1 = new SimpleCounter();
@@ -168,7 +173,7 @@ void metricIsRemovedWhileOtherMetricsWithSameNameExist() throws UnirestException
168173
reporter.notifyOfAddedMetric(metric2, metricName, metricGroup2);
169174
reporter.notifyOfRemovedMetric(metric1, metricName, metricGroup);
170175

171-
String response = pollMetrics(reporter.getPort()).getBody();
176+
String response = pollMetrics(reporter.getPort()).body();
172177

173178
assertThat(response).contains("some_value").doesNotContain(labelValueThatShouldBeRemoved);
174179
}
@@ -239,13 +244,18 @@ void canStartTwoReportersWhenUsingPortRange() {
239244
}
240245

241246
private String addMetricAndPollResponse(Metric metric, String metricName)
242-
throws UnirestException {
247+
throws IOException, InterruptedException {
243248
reporter.notifyOfAddedMetric(metric, metricName, metricGroup);
244-
return pollMetrics(reporter.getPort()).getBody();
249+
return pollMetrics(reporter.getPort()).body();
245250
}
246251

247-
static HttpResponse<String> pollMetrics(int port) throws UnirestException {
248-
return Unirest.get("http://localhost:" + port + "/metrics").asString();
252+
static HttpResponse<String> pollMetrics(int port) throws IOException, InterruptedException {
253+
HttpRequest request =
254+
HttpRequest.newBuilder()
255+
.uri(URI.create("http://localhost:" + port + "/metrics"))
256+
.GET()
257+
.build();
258+
return HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
249259
}
250260

251261
private static String createExpectedPollResponse(

0 commit comments

Comments
 (0)