Skip to content

Commit be6897b

Browse files
committed
Don't request domain name if we already have it
1 parent b831bd0 commit be6897b

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

src/main/java/io/prometheus/wls/rest/domain/ExporterConfig.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package io.prometheus.wls.rest.domain;
22
/*
3-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
44
*
55
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
66
*/
7+
import java.io.InputStream;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Optional;
15+
import java.util.stream.Stream;
16+
717
import com.google.gson.JsonObject;
818
import org.yaml.snakeyaml.Yaml;
919
import org.yaml.snakeyaml.scanner.ScannerException;
1020

11-
import java.io.InputStream;
12-
import java.util.*;
13-
import java.util.stream.Stream;
14-
1521
/**
1622
* This class represents the configuration for the exporter, created by parsing YAML.
1723
*
@@ -115,7 +121,7 @@ public MBeanSelector[] getQueries() {
115121
}
116122

117123
private Stream<MBeanSelector> withPossibleDomainNameQuery(Stream<MBeanSelector> stream) {
118-
return useDomainQualifier ? Stream.concat(Stream.of(MBeanSelector.DOMAIN_NAME_SELECTOR), stream) : stream;
124+
return useDomainQualifier() ? Stream.concat(Stream.of(MBeanSelector.DOMAIN_NAME_SELECTOR), stream) : stream;
119125
}
120126

121127
public static ExporterConfig loadConfig(Map<String, Object> yamlConfig) {
@@ -219,10 +225,10 @@ private List<Map<String, Object>> getAsListOfMaps(Object queriesYaml) {
219225
}
220226

221227
private boolean isArrayOfMaps(Object object) {
222-
return List.class.isAssignableFrom(object.getClass()) && emptyOrContainsMaps((List) object);
228+
return List.class.isAssignableFrom(object.getClass()) && emptyOrContainsMaps((List<?>) object);
223229
}
224230

225-
private boolean emptyOrContainsMaps(List list) {
231+
private boolean emptyOrContainsMaps(List<?> list) {
226232
return list.isEmpty() || list.get(0) instanceof Map;
227233
}
228234

@@ -255,7 +261,7 @@ boolean getMetricsNameSnakeCase() {
255261
* @return true if the qualifier should be added
256262
*/
257263
boolean useDomainQualifier() {
258-
return useDomainQualifier;
264+
return useDomainQualifier && (domainName == null);
259265
}
260266

261267
String getDomainName() {
@@ -281,6 +287,7 @@ public void replace(ExporterConfig config2) {
281287
this.useDomainQualifier = config2.useDomainQualifier;
282288
MBeanSelector[] newQueries = config2.getQueries();
283289
this.queries = Arrays.copyOf(newQueries, newQueries.length);
290+
this.domainName = null;
284291
}
285292

286293
@Override

src/test/java/io/prometheus/wls/rest/WebClientImplTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package io.prometheus.wls.rest;
22
/*
3-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
44
*
55
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
66
*/
77

8+
import java.io.IOException;
9+
import java.net.SocketException;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
813
import com.google.common.base.Strings;
914
import com.meterware.pseudoserver.HttpUserAgentTest;
1015
import com.meterware.pseudoserver.PseudoServlet;
1116
import com.meterware.pseudoserver.WebResource;
1217
import org.junit.Before;
18+
import org.junit.Ignore;
1319
import org.junit.Test;
1420

15-
import java.io.IOException;
16-
import java.net.SocketException;
17-
import java.util.HashMap;
18-
import java.util.Map;
19-
2021
import static io.prometheus.wls.rest.ServletConstants.AUTHENTICATION_HEADER;
2122
import static io.prometheus.wls.rest.ServletConstants.COOKIE_HEADER;
22-
import static javax.servlet.http.HttpServletResponse.*;
23+
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
24+
import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
25+
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
2326
import static org.hamcrest.Matchers.equalTo;
2427
import static org.hamcrest.Matchers.hasEntry;
2528
import static org.hamcrest.junit.MatcherAssert.assertThat;
@@ -31,7 +34,10 @@ public class WebClientImplTest extends HttpUserAgentTest {
3134
private static final char QUOTE = '"';
3235

3336
/** A URL with a host guaranteed not to exist. */
34-
private static final String UNDEFINED_HOST_URL = "http://mxyptlk/";
37+
private static final String UNDEFINED_HOST_URL = "http://seriously-this-should-not-exist/";
38+
39+
/** A URL on a known host with a port on which no server is listening. */
40+
private static final String UNDEFINED_PORT_URL = "http://localhost:59236";
3541

3642
private WebClientFactory factory = new WebClientFactoryImpl();
3743

@@ -46,14 +52,15 @@ public void setUp() {
4652
sentHeaders.clear();
4753
}
4854

55+
@Ignore("The client seems to find something on some platforms??")
4956
@Test(expected = WebClientException.class)
5057
public void whenUnableToReachHost_throwException() throws Exception {
5158
factory.createClient().withUrl(UNDEFINED_HOST_URL).doGetRequest();
5259
}
5360

5461
@Test(expected = WebClientException.class)
5562
public void whenUnableToReachServer_throwException() throws Exception {
56-
factory.createClient().withUrl(UNDEFINED_HOST_URL).doGetRequest();
63+
factory.createClient().withUrl(UNDEFINED_PORT_URL).doGetRequest();
5764
}
5865

5966
@Test

src/test/java/io/prometheus/wls/rest/domain/ExporterConfigTest.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package io.prometheus.wls.rest.domain;
22
/*
3-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
44
*
55
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
66
*/
77

8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
812
import com.google.gson.JsonObject;
913
import com.google.gson.JsonParser;
1014
import org.hamcrest.Description;
1115
import org.hamcrest.TypeSafeDiagnosingMatcher;
1216
import org.junit.Test;
1317
import org.yaml.snakeyaml.Yaml;
1418

15-
import java.util.HashMap;
16-
import java.util.Map;
17-
1819
import static io.prometheus.wls.rest.domain.ExporterConfigTest.QueryHierarchyMatcher.hasQueryFor;
1920
import static io.prometheus.wls.rest.domain.MetricMatcher.hasMetric;
2021
import static org.hamcrest.MatcherAssert.assertThat;
21-
import static org.hamcrest.Matchers.*;
22+
import static org.hamcrest.Matchers.arrayWithSize;
23+
import static org.hamcrest.Matchers.containsString;
24+
import static org.hamcrest.Matchers.emptyArray;
25+
import static org.hamcrest.Matchers.equalTo;
26+
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
27+
import static org.hamcrest.Matchers.is;
28+
import static org.hamcrest.Matchers.not;
29+
import static org.hamcrest.Matchers.notNullValue;
30+
import static org.hamcrest.Matchers.nullValue;
31+
import static org.hamcrest.Matchers.sameInstance;
2232

2333
/**
2434
* @author Russell Gold
@@ -500,15 +510,32 @@ public void afterScrapingServerConfig_hasDomainName() {
500510
public void afterScrapingMetricsIncludeDomainNameQualifier() {
501511
ExporterConfig exporterConfig = loadFromString(DOMAIN_QUALIFIER_CONFIG);
502512

503-
Map<String, Object> metrics = new HashMap<>();
504-
metrics.putAll(exporterConfig.scrapeMetrics(exporterConfig.getEffectiveQueries()[0], getJsonResponse(CONFIG_RESPONSE)));
505-
metrics.putAll(exporterConfig.scrapeMetrics(exporterConfig.getEffectiveQueries()[1], getJsonResponse(WORK_MANAGER_RESPONSE)));
513+
Map<String, Object> metrics = getMetrics(exporterConfig);
506514

507515
assertThat(metrics, hasMetric("workmanager_pendingRequests{domain=\"mydomain\",applicationName=\"thisOne\"}", 2));
508516
assertThat(metrics, hasMetric("workmanager_completedRequests{domain=\"mydomain\",applicationName=\"thisOne\"}", 15));
509517
assertThat(metrics, hasMetric("workmanager_stuckThreadCount{domain=\"mydomain\",applicationName=\"thisOne\"}", 3));
510518
}
511519

520+
private Map<String, Object> getMetrics(ExporterConfig exporterConfig) {
521+
Map<String, Object> metrics = new HashMap<>();
522+
Arrays.stream(exporterConfig.getEffectiveQueries())
523+
.forEach(q -> metrics.putAll(exporterConfig.scrapeMetrics(q, getJsonResponse(getResponse(q)))));
524+
return metrics;
525+
}
526+
527+
private String getResponse(MBeanSelector selector) {
528+
return selector.acceptsStrings() ? CONFIG_RESPONSE : WORK_MANAGER_RESPONSE;
529+
}
530+
531+
@Test
532+
public void secondScrapeWithDomainQualifierDoesNotAddStringMetric() {
533+
ExporterConfig exporterConfig = loadFromString(DOMAIN_QUALIFIER_CONFIG);
534+
getMetrics(exporterConfig);
535+
536+
assertThat(getMetrics(exporterConfig).values().stream().anyMatch(v -> v instanceof String), is(false));
537+
}
538+
512539
private static final String DOMAIN_QUALIFIER_CONFIG =
513540
ExporterConfig.DOMAIN_QUALIFIER + ": true\n" +
514541
"queries:\n" +

0 commit comments

Comments
 (0)