Skip to content

Commit b75a6bf

Browse files
committed
FieldFilter added
1 parent 52b6092 commit b75a6bf

File tree

4 files changed

+101
-60
lines changed

4 files changed

+101
-60
lines changed

pom.xml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46
<groupId>main.java.io.github.delirius325</groupId>
@@ -24,7 +26,7 @@
2426
</licenses>
2527

2628
<scm>
27-
<url>https://github.com/siggitrain/jmeter-elasticsearch-backend-listener.git</url>
29+
<url>https://github.com/delirius325/jmeter-elasticsearch-backend-listener.git</url>
2830
</scm>
2931

3032
<developers>
@@ -100,7 +102,7 @@
100102
</dependency>
101103
<dependency>
102104
<groupId>org.elasticsearch.client</groupId>
103-
<artifactId>elasticsearch-rest-client</artifactId>
105+
<artifactId>elasticsearch-rest-client</artifactId>
104106
<version>${org.elasticsearch.client}</version>
105107
</dependency>
106108
<dependency>
@@ -167,10 +169,10 @@
167169
<profile>
168170
<id>release-sign-artifacts</id>
169171
<activation>
170-
<property>
172+
<property>
171173
<name>performRelease</name>
172174
<value>true</value>
173-
</property>
175+
</property>
174176
</activation>
175177
<build>
176178
<plugins>
@@ -290,15 +292,15 @@
290292
<goal>shade</goal>
291293
</goals>
292294
<configuration>
293-
<artifactSet>
294-
<excludes>
295-
<exclude>org.apache.jmeter:*</exclude>
296-
<exclude>commons-codec:*</exclude>
297-
<exclude>commons-logging:*</exclude>
298-
<exclude>org.apache.httpcomponents:*</exclude>
299-
<exclude>net.java.dev.jna:*</exclude>
300-
</excludes>
301-
</artifactSet>
295+
<artifactSet>
296+
<excludes>
297+
<exclude>org.apache.jmeter:*</exclude>
298+
<exclude>commons-codec:*</exclude>
299+
<exclude>commons-logging:*</exclude>
300+
<exclude>org.apache.httpcomponents:*</exclude>
301+
<exclude>net.java.dev.jna:*</exclude>
302+
</excludes>
303+
</artifactSet>
302304
</configuration>
303305
</execution>
304306
</executions>

src/main/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/ElasticSearchMetric.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Iterator;
1212
import java.util.LinkedList;
1313
import java.util.Map;
14+
import java.util.Set;
1415

1516
import org.apache.jmeter.assertions.AssertionResult;
1617
import org.apache.jmeter.samplers.SampleResult;
@@ -32,19 +33,22 @@ public class ElasticSearchMetric {
3233

3334
private HashMap<String, Object> json;
3435

36+
private Set<String> fields;
37+
3538
private boolean allReqHeaders;
3639

3740
private boolean allResHeaders;
3841

3942
public ElasticSearchMetric(SampleResult sr, String testMode, String timeStamp, int buildNumber,
40-
boolean parseReqHeaders, boolean parseResHeaders) {
43+
boolean parseReqHeaders, boolean parseResHeaders, Set<String> fields) {
4144
this.sampleResult = sr;
4245
this.esTestMode = testMode.trim();
4346
this.esTimestamp = timeStamp.trim();
4447
this.ciBuildNumber = buildNumber;
4548
this.json = new HashMap<>();
4649
this.allReqHeaders = parseReqHeaders;
4750
this.allResHeaders = parseResHeaders;
51+
this.fields = fields;
4852
}
4953

5054
/**
@@ -58,27 +62,27 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
5862
SimpleDateFormat sdf = new SimpleDateFormat(this.esTimestamp);
5963

6064
//add all the default SampleResult parameters
61-
this.json.put("AllThreads", this.sampleResult.getAllThreads());
62-
this.json.put("BodySize", this.sampleResult.getBodySizeAsLong());
63-
this.json.put("Bytes", this.sampleResult.getBytesAsLong());
64-
this.json.put("SentBytes", this.sampleResult.getSentBytes());
65-
this.json.put("ConnectTime", this.sampleResult.getConnectTime());
66-
this.json.put("ContentType", this.sampleResult.getContentType());
67-
this.json.put("DataType", this.sampleResult.getDataType());
68-
this.json.put("ErrorCount", this.sampleResult.getErrorCount());
69-
this.json.put("GrpThreads", this.sampleResult.getGroupThreads());
70-
this.json.put("IdleTime", this.sampleResult.getIdleTime());
71-
this.json.put("Latency", this.sampleResult.getLatency());
72-
this.json.put("ResponseTime", this.sampleResult.getTime());
73-
this.json.put("SampleCount", this.sampleResult.getSampleCount());
74-
this.json.put("SampleLabel", this.sampleResult.getSampleLabel());
75-
this.json.put("ThreadName", this.sampleResult.getThreadName());
76-
this.json.put("URL", this.sampleResult.getURL());
77-
this.json.put("ResponseCode", this.sampleResult.getResponseCode());
78-
this.json.put("StartTime", sdf.format(new Date(this.sampleResult.getStartTime())));
79-
this.json.put("EndTime", sdf.format(new Date(this.sampleResult.getEndTime())));
80-
this.json.put("Timestamp", sdf.format(new Date(this.sampleResult.getTimeStamp())));
81-
this.json.put("InjectorHostname", InetAddress.getLocalHost().getHostName());
65+
addFilteredJSON("AllThreads", this.sampleResult.getAllThreads());
66+
addFilteredJSON("BodySize", this.sampleResult.getBodySizeAsLong());
67+
addFilteredJSON("Bytes", this.sampleResult.getBytesAsLong());
68+
addFilteredJSON("SentBytes", this.sampleResult.getSentBytes());
69+
addFilteredJSON("ConnectTime", this.sampleResult.getConnectTime());
70+
addFilteredJSON("ContentType", this.sampleResult.getContentType());
71+
addFilteredJSON("DataType", this.sampleResult.getDataType());
72+
addFilteredJSON("ErrorCount", this.sampleResult.getErrorCount());
73+
addFilteredJSON("GrpThreads", this.sampleResult.getGroupThreads());
74+
addFilteredJSON("IdleTime", this.sampleResult.getIdleTime());
75+
addFilteredJSON("Latency", this.sampleResult.getLatency());
76+
addFilteredJSON("ResponseTime", this.sampleResult.getTime());
77+
addFilteredJSON("SampleCount", this.sampleResult.getSampleCount());
78+
addFilteredJSON("SampleLabel", this.sampleResult.getSampleLabel());
79+
addFilteredJSON("ThreadName", this.sampleResult.getThreadName());
80+
addFilteredJSON("URL", this.sampleResult.getURL());
81+
addFilteredJSON("ResponseCode", this.sampleResult.getResponseCode());
82+
addFilteredJSON("StartTime", sdf.format(new Date(this.sampleResult.getStartTime())));
83+
addFilteredJSON("EndTime", sdf.format(new Date(this.sampleResult.getEndTime())));
84+
addFilteredJSON("Timestamp", sdf.format(new Date(this.sampleResult.getTimeStamp())));
85+
addFilteredJSON("InjectorHostname", InetAddress.getLocalHost().getHostName());
8286

8387
// Add the details according to the mode that is set
8488
switch (this.esTestMode) {
@@ -113,16 +117,22 @@ private void addAssertions() {
113117
if (assertionResults != null) {
114118
Map<String, Object>[] assertionArray = new HashMap[assertionResults.length];
115119
Integer i = 0;
120+
String failureMessage = "";
121+
boolean isFailure = false;
116122
for (AssertionResult assertionResult : assertionResults) {
117123
HashMap<String, Object> assertionMap = new HashMap<>();
118124
boolean failure = assertionResult.isFailure() || assertionResult.isError();
125+
isFailure = isFailure || assertionResult.isFailure() || assertionResult.isError();
119126
assertionMap.put("failure", failure);
120127
assertionMap.put("failureMessage", assertionResult.getFailureMessage());
128+
failureMessage += assertionResult.getFailureMessage() + "\n";
121129
assertionMap.put("name", assertionResult.getName());
122130
assertionArray[i] = assertionMap;
123131
i++;
124132
}
125-
this.json.put("AssertionResults", assertionArray);
133+
addFilteredJSON("AssertionResults", assertionArray);
134+
addFilteredJSON("FailureMessage", failureMessage);
135+
addFilteredJSON("Success", !isFailure);
126136
}
127137
}
128138

@@ -139,15 +149,15 @@ private void addElapsedTime(SimpleDateFormat sdf) {
139149

140150
if (this.ciBuildNumber != 0) {
141151
elapsedTime = getElapsedTime(true);
142-
this.json.put("BuildNumber", this.ciBuildNumber);
152+
addFilteredJSON("BuildNumber", this.ciBuildNumber);
143153

144154
if (elapsedTime != null)
145-
this.json.put("ElapsedTimeComparison", sdf.format(elapsedTime));
155+
addFilteredJSON("ElapsedTimeComparison", sdf.format(elapsedTime));
146156
}
147157

148158
elapsedTime = getElapsedTime(false);
149159
if (elapsedTime != null)
150-
this.json.put("ElapsedTime", sdf.format(elapsedTime));
160+
addFilteredJSON("ElapsedTime", sdf.format(elapsedTime));
151161
}
152162

153163
/**
@@ -165,11 +175,11 @@ private void addCustomFields(BackendListenerContext context) {
165175
String parameter = context.getParameter(parameterName).trim();
166176

167177
try {
168-
this.json.put(parameterName, Long.parseLong(parameter));
178+
addFilteredJSON(parameterName, Long.parseLong(parameter));
169179
} catch (Exception e) {
170180
if (logger.isDebugEnabled())
171181
logger.debug("Cannot convert custom field to number");
172-
this.json.put(parameterName, context.getParameter(parameterName).trim());
182+
addFilteredJSON(parameterName, context.getParameter(parameterName).trim());
173183
}
174184
}
175185
}
@@ -180,11 +190,11 @@ private void addCustomFields(BackendListenerContext context) {
180190
*
181191
*/
182192
private void addDetails() {
183-
this.json.put("RequestHeaders", this.sampleResult.getRequestHeaders());
184-
this.json.put("RequestBody", this.sampleResult.getSamplerData());
185-
this.json.put("ResponseHeaders", this.sampleResult.getResponseHeaders());
186-
this.json.put("ResponseBody", this.sampleResult.getResponseDataAsString());
187-
this.json.put("ResponseMessage", this.sampleResult.getResponseMessage());
193+
addFilteredJSON("RequestHeaders", this.sampleResult.getRequestHeaders());
194+
addFilteredJSON("RequestBody", this.sampleResult.getSamplerData());
195+
addFilteredJSON("ResponseHeaders", this.sampleResult.getResponseHeaders());
196+
addFilteredJSON("ResponseBody", this.sampleResult.getResponseDataAsString());
197+
addFilteredJSON("ResponseMessage", this.sampleResult.getResponseMessage());
188198
}
189199

190200
/**
@@ -228,6 +238,18 @@ private void parseHeadersAsJsonProps(boolean allReqHeaders, boolean allResHeader
228238
}
229239
}
230240

241+
/**
242+
* Adds a given key-value pair to JSON if the key is contained in the field filter or in case of empty field filter
243+
*
244+
* @param key
245+
* @param value
246+
*/
247+
private void addFilteredJSON(String key, Object value) {
248+
if (this.fields.size() == 0 || this.fields.contains(key.toLowerCase())) {
249+
this.json.put(key, value);
250+
}
251+
}
252+
231253
/**
232254
* This method is meant to return the elapsed time in a human readable format. The purpose of this is mostly for
233255
* build comparison in Kibana. By doing this, the user is able to set the X-axis of his graph to this date and split

src/main/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/ElasticsearchBackendClient.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
3737

3838
private static final String ES_INDEX = "es.index";
3939

40+
private static final String ES_FIELDS = "es.fields";
41+
4042
private static final String ES_TIMESTAMP = "es.timestamp";
4143

4244
private static final String ES_BULK_SIZE = "es.bulk.size";
@@ -83,6 +85,8 @@ public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
8385

8486
private Set<String> filters;
8587

88+
private Set<String> fields;
89+
8690
private int buildNumber;
8791

8892
private int bulkSize;
@@ -96,6 +100,7 @@ public Arguments getDefaultParameters() {
96100
parameters.addArgument(ES_HOST, null);
97101
parameters.addArgument(ES_PORT, "9200");
98102
parameters.addArgument(ES_INDEX, null);
103+
parameters.addArgument(ES_FIELDS, "");
99104
parameters.addArgument(ES_TIMESTAMP, "yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
100105
parameters.addArgument(ES_BULK_SIZE, "100");
101106
parameters.addArgument(ES_TIMEOUT_MS, Long.toString(DEFAULT_TIMEOUT_MS));
@@ -118,6 +123,7 @@ public Arguments getDefaultParameters() {
118123
public void setupTest(BackendListenerContext context) throws Exception {
119124
try {
120125
this.filters = new HashSet<>();
126+
this.fields = new HashSet<>();
121127
this.modes = new HashSet<>(Arrays.asList("info", "debug", "error", "quiet"));
122128
this.bulkSize = Integer.parseInt(context.getParameter(ES_BULK_SIZE));
123129
this.timeoutMs = JMeterUtils.getPropDefault(ES_TIMEOUT_MS, DEFAULT_TIMEOUT_MS);
@@ -149,28 +155,38 @@ public void onFailure(Node node) {
149155
client = RestClient.builder(HttpHost.create(context.getParameter(ES_AWS_ENDPOINT)))
150156
.setHttpClientConfigCallback(hacb -> hacb.addInterceptorLast(interceptor)).build();
151157
}
158+
159+
convertParameterToSet(context, ES_SAMPLE_FILTER, this.filters);
160+
convertParameterToSet(context, ES_FIELDS, this.fields);
161+
152162
this.sender = new ElasticSearchMetricSender(client, context.getParameter(ES_INDEX).toLowerCase(),
153163
context.getParameter(ES_AUTH_USER), context.getParameter(ES_AUTH_PWD),
154164
context.getParameter(ES_AWS_ENDPOINT));
155165
this.sender.createIndex();
156166

157167
checkTestMode(context.getParameter(ES_TEST_MODE));
158168

159-
String[] filterArray = (context.getParameter(ES_SAMPLE_FILTER).contains(";"))
160-
? context.getParameter(ES_SAMPLE_FILTER).split(";")
161-
: new String[] { context.getParameter(ES_SAMPLE_FILTER) };
162-
if (filterArray.length > 0 && !filterArray[0].trim().equals("")) {
163-
for (String filter : filterArray) {
164-
this.filters.add(filter.toLowerCase().trim());
165-
logger.info("Added filter: " + filter.toLowerCase().trim());
166-
}
167-
}
168169
super.setupTest(context);
169170
} catch (Exception e) {
170171
throw new IllegalStateException("Unable to connect to the ElasticSearch engine", e);
171172
}
172173
}
173174

175+
/**
176+
* This method converts a semicolon sepearted list contained in a parameter into s string set
177+
*
178+
*/
179+
private void convertParameterToSet(BackendListenerContext context, String parameter, Set<String> set) {
180+
String[] array = (context.getParameter(parameter).contains(";")) ? context.getParameter(parameter).split(";")
181+
: new String[] { context.getParameter(parameter) };
182+
if (array.length > 0 && !array[0].trim().equals("")) {
183+
for (String entry : array) {
184+
set.add(entry.toLowerCase().trim());
185+
logger.info("Parsed from " + parameter + ": " + entry.toLowerCase().trim());
186+
}
187+
}
188+
}
189+
174190
/**
175191
* This methods sets the system properties for SSL to the provided parameters
176192
*/
@@ -195,7 +211,7 @@ public void handleSampleResults(List<SampleResult> results, BackendListenerConte
195211
ElasticSearchMetric metric = new ElasticSearchMetric(sr, context.getParameter(ES_TEST_MODE),
196212
context.getParameter(ES_TIMESTAMP), this.buildNumber,
197213
context.getBooleanParameter(ES_PARSE_REQ_HEADERS, false),
198-
context.getBooleanParameter(ES_PARSE_RES_HEADERS, false));
214+
context.getBooleanParameter(ES_PARSE_RES_HEADERS, false), fields);
199215

200216
if (validateSample(context, sr)) {
201217
try {

src/test/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/TestElasticSearchBackend.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.text.SimpleDateFormat;
66
import java.util.Date;
7+
import java.util.HashSet;
78

89
import org.apache.jmeter.samplers.SampleResult;
910
import org.junit.Before;
@@ -16,10 +17,10 @@ public class TestElasticSearchBackend {
1617

1718
@Before
1819
public void setUp() throws Exception {
19-
metricCI = new ElasticSearchMetric(new SampleResult(), "info", "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", 1, false,
20-
false);
20+
metricCI = new ElasticSearchMetric(new SampleResult(), "info", "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", 1, false, false,
21+
new HashSet<String>());
2122
metricNoCI = new ElasticSearchMetric(new SampleResult(), "info", "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", 0, false,
22-
false);
23+
false, new HashSet<String>());
2324
}
2425

2526
@Test

0 commit comments

Comments
 (0)