Skip to content

Commit 0fb9869

Browse files
authored
Attempt to reduce code 500 errors (#156)
* Avoid sending null message to HttpServletResponse * Failback to localhost if unable to reach request local server
1 parent 173e185 commit 0fb9869

32 files changed

+384
-193
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Note that there are two parts to the configuration. The optional top portion def
5353
| `domainQualifier` | If true, the domain name will be included as a qualifier for all metrics. Defaults to false. |
5454
| `restPort` | Optional, used in the web application only. Overrides the port on which the exporter should contact the REST API. Needed if the exporter cannot find the REST API. The most common case is running on a system with the administration port enabled. In that case, you must specify the administration port in this field and access the exporter by using the SSL port. |
5555

56+
Note that if unable to contact the REST API using the inferred host and port, the exporter will try the local host name and, if the REST port is specified, the local port.
57+
5658
The `query` field is more complex. Each query consists of a hierarchy of the [MBeans](https://docs.oracle.com/middleware/12213/wls/WLMBR/core/index.html), starting relative to `ServerRuntimes`.
5759
Within each section, there are a number of options:
5860

@@ -178,7 +180,7 @@ Setting | Default | Property
178180
------------ | ------------- | -------------
179181
Domain name | (use WLS definition) | `DOMAIN`
180182
Exporter port | `8080` | `EXPORTER_PORT`
181-
WebLogic host | `localhost` | `WLS_HOST`
183+
WebLogic host name | `localhost` | `WLS_HOST`
182184
WebLogic port | `7001` | `WLS_PORT`
183185
Use https | `false` | `WLS_SECURE`
184186

@@ -234,7 +236,7 @@ vulnerability disclosure process.
234236

235237
## License
236238

237-
Copyright (c) 2019, 2021 Oracle and/or its affiliates.
239+
Copyright (c) 2019, 2022 Oracle and/or its affiliates.
238240

239241
Released under the Universal Permissive License v1.0 as shown at
240242
<https://oss.oracle.com/licenses/upl/>.

wls-exporter-core/src/main/java/com/oracle/wls/exporter/AuthenticatedCall.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (c) 2017, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2017, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.exporter;
55

66
import java.io.IOException;
77
import java.io.PrintStream;
8+
import java.util.List;
89

910
import com.oracle.wls.exporter.domain.MBeanSelector;
1011
import com.oracle.wls.exporter.domain.QueryType;
@@ -77,12 +78,21 @@ public void doWithAuthentication() throws IOException {
7778
context.sendError(status, e.getMessage());
7879
} catch (RestPortConnectionException e) {
7980
context.setStatus(HTTP_INTERNAL_ERROR);
80-
reportUnableToContactRestApi(e.getUri());
81+
reportUnableToContactRestApiPort(getFailedHosts());
8182
} finally {
8283
context.close();
8384
}
8485
}
8586

87+
private String getFailedHosts() {
88+
final List<String> hosts = urlBuilder.getFailedHosts();
89+
if (hosts.size() < 3) {
90+
return String.join(" or ", hosts);
91+
} else {
92+
return String.join(", ", hosts.subList(0, hosts.size()-1)) + " or " + hosts.get(hosts.size() - 1);
93+
}
94+
}
95+
8696
/**
8797
* Returns a name for the instance for which the exporter is reporting metrics.
8898
*/
@@ -104,15 +114,17 @@ private void performRequest(WebClient webClient) throws IOException {
104114
} while (webClient.isRetryNeeded());
105115
}
106116

107-
private void reportUnableToContactRestApi(String uri) throws IOException {
117+
private void reportUnableToContactRestApiPort(String uri) throws IOException {
108118
try (PrintStream out = context.getResponseStream()) {
109-
out.println("# Unable to contact the REST API at " + uri + ". May be using the wrong port.");
119+
out.println("# Unable to contact the REST API at " + uri + ". May be using the wrong host name or port.");
110120
out.println("#");
111-
out.println("# This most commonly occurs when the exporter is accessed via a load balancer");
112-
out.println("# configured on a different port than the managed server.");
121+
out.println("# This can happen when the exporter is accessed via a load balancer");
122+
out.println("# configured on a different port than the server's REST API,");
123+
out.println("# or when the remote server DNS name is not resolvable from the server itself.");
113124
out.println("#");
114125
out.println("# You can correct this by giving the exporter WAR an initial configuration with the");
115-
out.println("# restPort field set to the managed server's plain text port.");
126+
out.println("# restPort field set to the managed server's plain text port and/or");
127+
out.println("# restHostName field set to the managed server's locally resolvable name.");
116128
}
117129
}
118130

wls-exporter-core/src/main/java/com/oracle/wls/exporter/DemoInputs.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, 2020, Oracle and/or its affiliates.
1+
// Copyright (c) 2017, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.exporter;
@@ -10,8 +10,6 @@ class DemoInputs {
1010
@SuppressWarnings("unused")
1111
static final String YAML_STRING = "---\n" +
1212
"startDelaySeconds: 5\n" +
13-
"host: localhost\n" +
14-
"port: 7001\n" +
1513
"username: system\n" +
1614
"password: gumby1234\n" +
1715
"\n" +
@@ -35,8 +33,6 @@ class DemoInputs {
3533
@SuppressWarnings("unused")
3634
static final String YAML_STRING2 = "---\n" +
3735
"startDelaySeconds: 5\n" +
38-
"host: localhost\n" +
39-
"port: 7001\n" +
4036
"username: system\n" +
4137
"password: gumby1234\n" +
4238
"queries:\n" +

wls-exporter-core/src/main/java/com/oracle/wls/exporter/LiveConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2017, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.exporter;
@@ -124,7 +124,7 @@ private static void installUpdater(QuerySyncConfiguration syncConfiguration) {
124124
* @return a human readable representation of the configuration
125125
*/
126126
public static String asString() {
127-
return "host: " + serverName + '\n' +
127+
return "hostName: " + serverName + '\n' +
128128
"port: " + serverPort + '\n' + getConfig();
129129
}
130130

wls-exporter-core/src/main/java/com/oracle/wls/exporter/ServerErrorException.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Copyright 2020, 2021, Oracle and/or its affiliates.
1+
// Copyright 2020, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.exporter;
55

6+
import java.util.Optional;
7+
68
/**
79
* An exception thrown when a 5xx status code is received from the server.
810
*/
@@ -22,4 +24,11 @@ public ServerErrorException(int status, String message) {
2224
public int getStatus() {
2325
return status;
2426
}
27+
28+
@Override
29+
public String getMessage() {
30+
StringBuilder sb = new StringBuilder("Received status ").append(status);
31+
Optional.ofNullable(super.getMessage()).ifPresent(m -> sb.append(" - ").append(m));
32+
return sb.toString();
33+
}
2534
}

wls-exporter-core/src/main/java/com/oracle/wls/exporter/ServletInvocationContext.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// Copyright (c) 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2021, 2022, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.wls.exporter;
55

66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.PrintStream;
9+
import java.net.InetAddress;
10+
import java.net.UnknownHostException;
911
import java.util.Optional;
1012
import javax.servlet.http.HttpServletRequest;
1113
import javax.servlet.http.HttpServletResponse;
@@ -18,6 +20,7 @@ public class ServletInvocationContext implements InvocationContext {
1820

1921
private final HttpServletRequest request;
2022
private final HttpServletResponse response;
23+
private final String localHostName = getLocalHostName();
2124

2225
public ServletInvocationContext(HttpServletRequest request, HttpServletResponse response) {
2326
this.request = request;
@@ -31,11 +34,21 @@ public void close() {
3134

3235
@Override
3336
public UrlBuilder createUrlBuilder() {
34-
return UrlBuilder.create(request.getLocalName(), request.isSecure())
37+
return UrlBuilder.create(request.isSecure())
38+
.withHostName(request.getLocalName())
39+
.withHostName(localHostName)
3540
.withPort(LiveConfiguration.getConfiguredRestPort())
3641
.withPort(request.getLocalPort());
3742
}
3843

44+
static String getLocalHostName() {
45+
try {
46+
return InetAddress.getLocalHost().getHostName();
47+
} catch (UnknownHostException e) {
48+
return "localhost";
49+
}
50+
}
51+
3952
@Override
4053
public String getApplicationContext() {
4154
return request.getContextPath();
@@ -68,7 +81,11 @@ public PrintStream getResponseStream() throws IOException {
6881

6982
@Override
7083
public void sendError(int status, String msg) throws IOException {
84+
if (msg == null) {
85+
response.sendError(status);
86+
} else {
7187
response.sendError(status, msg);
88+
}
7289
}
7390

7491
@Override

0 commit comments

Comments
 (0)