Skip to content

Commit 04634d0

Browse files
committed
Add query URL to recorded message exchanges
1 parent 8125043 commit 04634d0

File tree

7 files changed

+62
-30
lines changed

7 files changed

+62
-30
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2020, Oracle Corporation and/or its affiliates.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
4+
---
5+
metricsNameSnakeCase: true
6+
queries:
7+
- key: name
8+
keyName: server
9+
jtaRuntime:
10+
key: name
11+
keyName: jta
12+
healthState:
13+
prefix: health_

samples/configurations/servlets.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ queries:
1313
type: WebAppComponentRuntime
1414
prefix: webapp_config_
1515
key: name
16-
values: [openSessionsHighCount, openSessionsCurrentCount]
16+
values: [deploymentState, servletReloadCheckSecs, openSessionsHighCount, openSessionsCurrentCount]
1717
servlets:
1818
prefix: weblogic_servlet_
1919
key: servletName

src/main/java/io/prometheus/wls/rest/ExporterServlet.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,17 @@ private TreeMap<String, Object> sort(Map<String, Object> metrics) {
8888
}
8989

9090
private Map<String, Object> getMetrics(WebClient webClient, MBeanSelector selector) throws IOException {
91-
String request = selector.getRequest();
92-
String jsonResponse = webClient.withUrl(LiveConfiguration.getUrl(getProtocol(), selector)).doPostRequest(request);
93-
MessagesServlet.addExchange(request, jsonResponse);
91+
String jsonResponse = requestMetrics(webClient, selector);
9492
if (isNullOrEmptyString(jsonResponse)) return null;
9593

9694
return LiveConfiguration.scrapeMetrics(selector, jsonResponse);
9795
}
9896

97+
private String requestMetrics(WebClient webClient, MBeanSelector selector) throws IOException {
98+
String url = LiveConfiguration.getUrl(getProtocol(), selector);
99+
String jsonResponse = webClient.withUrl(url).doPostRequest(selector.getRequest());
100+
MessagesServlet.addExchange(url, selector.getRequest(), jsonResponse);
101+
return jsonResponse;
102+
}
103+
99104
}

src/main/java/io/prometheus/wls/rest/MessagesServlet.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
package io.prometheus.wls.rest;
66

7-
import javax.servlet.ServletOutputStream;
8-
import javax.servlet.annotation.WebServlet;
9-
import javax.servlet.http.HttpServlet;
10-
import javax.servlet.http.HttpServletRequest;
11-
import javax.servlet.http.HttpServletResponse;
127
import java.io.IOException;
13-
import java.util.ArrayDeque;
148
import java.util.ArrayList;
159
import java.util.List;
1610
import java.util.Queue;
1711
import java.util.concurrent.ConcurrentLinkedDeque;
12+
import javax.servlet.ServletOutputStream;
13+
import javax.servlet.annotation.WebServlet;
14+
import javax.servlet.http.HttpServlet;
15+
import javax.servlet.http.HttpServletRequest;
16+
import javax.servlet.http.HttpServletResponse;
1817

1918
import static io.prometheus.wls.rest.ServletConstants.MESSAGES_PAGE;
2019

@@ -24,7 +23,7 @@
2423
@WebServlet("/" + MESSAGES_PAGE)
2524
public class MessagesServlet extends HttpServlet {
2625
static final int MAX_EXCHANGES = 5;
27-
private static final String TEMPLATE = "REQUEST:%n%s%nREPLY:%n%s%n";
26+
private static final String TEMPLATE = "REQUEST to %s:%n%s%nREPLY:%n%s%n";
2827

2928
private static Queue<String> messages = new ConcurrentLinkedDeque<>();
3029

@@ -40,8 +39,8 @@ static void clear() {
4039
messages.clear();
4140
}
4241

43-
static void addExchange(String request, String response) {
44-
messages.add(String.format(TEMPLATE, request, response));
42+
static void addExchange(String url, String request, String response) {
43+
messages.add(String.format(TEMPLATE, url, request, response));
4544
if (messages.size() > MAX_EXCHANGES) messages.remove();
4645
}
4746

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44

55
package io.prometheus.wls.rest;
66

7-
import org.hamcrest.Matcher;
8-
import org.junit.Before;
9-
import org.junit.Test;
10-
11-
import javax.servlet.ServletException;
12-
import javax.servlet.annotation.WebServlet;
13-
import javax.servlet.http.HttpServlet;
147
import java.io.IOException;
158
import java.lang.reflect.Modifier;
169
import java.util.stream.IntStream;
10+
import javax.servlet.ServletException;
11+
import javax.servlet.annotation.WebServlet;
12+
import javax.servlet.http.HttpServlet;
13+
14+
import org.hamcrest.Matcher;
15+
import org.junit.Before;
16+
import org.junit.Test;
1717

18-
import static io.prometheus.wls.rest.MessagesServlet.MAX_EXCHANGES;
1918
import static io.prometheus.wls.rest.HttpServletRequestStub.createGetRequest;
2019
import static io.prometheus.wls.rest.HttpServletResponseStub.createServletResponse;
21-
import static org.hamcrest.Matchers.*;
20+
import static io.prometheus.wls.rest.MessagesServlet.MAX_EXCHANGES;
21+
import static org.hamcrest.Matchers.arrayContaining;
22+
import static org.hamcrest.Matchers.both;
23+
import static org.hamcrest.Matchers.contains;
24+
import static org.hamcrest.Matchers.containsString;
25+
import static org.hamcrest.Matchers.hasItem;
26+
import static org.hamcrest.Matchers.instanceOf;
27+
import static org.hamcrest.Matchers.is;
28+
import static org.hamcrest.Matchers.notNullValue;
2229
import static org.hamcrest.junit.MatcherAssert.assertThat;
2330

2431
public class MessagesServletTest {
2532
private static final int EXCESS_EXCHANGES = 3;
33+
private static final String URL = "http://localhost:7001";
2634
private MessagesServlet servlet = new MessagesServlet();
2735
private HttpServletRequestStub request = createGetRequest();
2836
private HttpServletResponseStub response = createServletResponse();
@@ -56,7 +64,7 @@ public void servletAnnotationIndicatesMetricsPage() {
5664

5765
@Test
5866
public void afterExchangeAdded_retrieveExchange() {
59-
MessagesServlet.addExchange("request 1", "response1");
67+
MessagesServlet.addExchange(URL, "request 1", "response1");
6068

6169
assertThat(MessagesServlet.getMessages(), hasItem(both(containsString("request 1")).and(containsString("response1"))));
6270
}
@@ -69,7 +77,7 @@ public void afterMaximumExchangesAdded_retrieveAllExchanges() {
6977
}
7078

7179
private void addTestExchange(int i) {
72-
MessagesServlet.addExchange("request " + i, "response " + i);
80+
MessagesServlet.addExchange(URL, "request " + i, "response " + i);
7381
}
7482

7583
@SuppressWarnings("unchecked")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void setUp() {
5757
sentHeaders.clear();
5858
}
5959

60-
@Test(expected = WebClientException.class) @Ignore("Appears not to be reliable")
60+
@Ignore("seems to be unreliable on some machines")
61+
@Test(expected = WebClientException.class)
6162
public void whenUnableToReachHost_throwException() throws Exception {
6263
factory.createClient().withUrl(UNDEFINED_HOST_URL).doGetRequest();
6364
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
*
55
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
66
*/
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
713
import com.google.gson.JsonObject;
814
import com.google.gson.JsonParser;
915
import io.prometheus.wls.rest.domain.ExporterConfig;
1016
import io.prometheus.wls.rest.domain.MBeanSelector;
1117

12-
import java.io.ByteArrayInputStream;
13-
14-
import static io.prometheus.wls.rest.DemoInputs.*;
18+
import static io.prometheus.wls.rest.DemoInputs.RESPONSE;
19+
import static io.prometheus.wls.rest.DemoInputs.compressedJsonForm;
1520

1621
/**
1722
* @author Russell Gold
1823
*/
1924
public class YamlDemo {
2025

21-
public static void main(String... args) {
22-
String yamlString = YAML_STRING3;
26+
public static void main(String... args) throws IOException {
27+
String yamlString = String.join("\n", Files.readAllLines(Paths.get("/Users/rgold/Desktop/mohit.yml")));
28+
// String yamlString = YAML_STRING3;
2329
System.out.println("The following configuration:\n" + yamlString);
2430
ExporterConfig exporterConfig = ExporterConfig.loadConfig(new ByteArrayInputStream(yamlString.getBytes()));
2531

0 commit comments

Comments
 (0)