@@ -32,7 +32,7 @@ public class ElasticSearchMetric {
3232 private boolean allResHeaders ;
3333
3434 public ElasticSearchMetric (SampleResult sr , String testMode , String timeStamp , int buildNumber ,
35- boolean parseReqHeaders , boolean parseResHeaders , Set <String > fields ) {
35+ boolean parseReqHeaders , boolean parseResHeaders , Set <String > fields ) {
3636 this .sampleResult = sr ;
3737 this .esTestMode = testMode .trim ();
3838 this .esTimestamp = timeStamp .trim ();
@@ -45,9 +45,8 @@ public ElasticSearchMetric(SampleResult sr, String testMode, String timeStamp, i
4545
4646 /**
4747 * This method returns the current metric as a Map(String, Object) for the provided sampleResult
48- *
49- * @param context
50- * BackendListenerContext
48+ *
49+ * @param context BackendListenerContext
5150 * @return a JSON Object as Map(String, Object)
5251 */
5352 public Map <String , Object > getMetric (BackendListenerContext context ) throws Exception {
@@ -79,18 +78,18 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
7978
8079 // Add the details according to the mode that is set
8180 switch (this .esTestMode ) {
82- case "debug" :
83- addDetails ();
84- break ;
85- case "error" :
86- addDetails ();
87- break ;
88- case "info" :
89- if (!this .sampleResult .isSuccessful ())
81+ case "debug" :
82+ addDetails ();
83+ break ;
84+ case "error" :
9085 addDetails ();
91- break ;
92- default :
93- break ;
86+ break ;
87+ case "info" :
88+ if (!this .sampleResult .isSuccessful ())
89+ addDetails ();
90+ break ;
91+ default :
92+ break ;
9493 }
9594
9695 addAssertions ();
@@ -103,7 +102,6 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
103102
104103 /**
105104 * This method adds all the assertions for the current sampleResult
106- *
107105 */
108106 private void addAssertions () {
109107 AssertionResult [] assertionResults = this .sampleResult .getAssertionResults ();
@@ -133,7 +131,6 @@ private void addAssertions() {
133131 * This method adds the ElapsedTime as a key:value pair in the JSON object. Also, depending on whether or not the
134132 * tests were launched from a CI tool (i.e Jenkins), it will add a hard-coded version of the ElapsedTime for results
135133 * comparison purposes
136- *
137134 */
138135 private void addElapsedTime () {
139136 Date elapsedTime ;
@@ -154,8 +151,7 @@ private void addElapsedTime() {
154151 /**
155152 * Methods that add all custom fields added by the user in the Backend Listener's GUI panel
156153 *
157- * @param context
158- * BackendListenerContext
154+ * @param context BackendListenerContext
159155 */
160156 private void addCustomFields (BackendListenerContext context ) {
161157 Iterator <String > pluginParameters = context .getParameterNamesIterator ();
@@ -178,7 +174,6 @@ private void addCustomFields(BackendListenerContext context) {
178174
179175 /**
180176 * Method that adds the request and response's body/headers
181- *
182177 */
183178 private void addDetails () {
184179 addFilteredJSON ("RequestHeaders" , this .sampleResult .getRequestHeaders ());
@@ -193,14 +188,12 @@ private void addDetails() {
193188 * all headers into different ElasticSearch document properties by passing "true" This is a work-around the native
194189 * behaviour of JMeter where variables are not accessible within the backend listener.
195190 *
196- * @param allReqHeaders
197- * boolean to determine if the user wants to separate ALL request headers into different ES JSON
198- * properties.
199- * @param allResHeaders
200- * boolean to determine if the user wants to separate ALL response headers into different ES JSON
201- * properties.
202- *
203- * NOTE: This will be fixed as soon as a patch comes in for JMeter to change the behaviour.
191+ * @param allReqHeaders boolean to determine if the user wants to separate ALL request headers into different ES JSON
192+ * properties.
193+ * @param allResHeaders boolean to determine if the user wants to separate ALL response headers into different ES JSON
194+ * properties.
195+ * <p>
196+ * NOTE: This will be fixed as soon as a patch comes in for JMeter to change the behaviour.
204197 */
205198 private void parseHeadersAsJsonProps (boolean allReqHeaders , boolean allResHeaders ) {
206199 LinkedList <String []> headersArrayList = new LinkedList <String []>();
@@ -213,23 +206,32 @@ private void parseHeadersAsJsonProps(boolean allReqHeaders, boolean allResHeader
213206 headersArrayList .add (this .sampleResult .getResponseHeaders ().split ("\n " ));
214207 }
215208
216- for (String [] lines : headersArrayList ) {
217- for (int i =0 ; i < lines .length ; i ++) {
218- String [] header = lines [i ].split (":" ,2 );
209+ if (!allReqHeaders && !allResHeaders ) {
210+ headersArrayList .add (this .sampleResult .getRequestHeaders ().split ("\n " ));
211+ headersArrayList .add (this .sampleResult .getResponseHeaders ().split ("\n " ));
212+ }
213+
214+ for (String [] lines : headersArrayList ) {
215+ for (int i = 0 ; i < lines .length ; i ++) {
216+ String [] header = lines [i ].split (":" , 2 );
219217
220- // if not all req headers and header contains special X-tag
221- if (header .length > 1 ) {
222- if (! this . allReqHeaders && header [0 ].startsWith ("X-es-backend" ) || this . allReqHeaders ) {
218+ // if not all res/ req headers and header contains special X-tag
219+ if (! allReqHeaders && ! allResHeaders && header .length > 1 ) {
220+ if (header [0 ].startsWith ("X-es-backend" )) {
223221 this .json .put (header [0 ].replaceAll ("X-es-backend-" , "" ).trim (), header [1 ].trim ());
224222 }
225223 }
224+
225+ if ((allReqHeaders || allResHeaders ) && header .length > 1 ) {
226+ this .json .put (header [0 ].trim (), header [1 ].trim ());
227+ }
226228 }
227229 }
228230 }
229231
230232 /**
231233 * Adds a given key-value pair to JSON if the key is contained in the field filter or in case of empty field filter
232- *
234+ *
233235 * @param key
234236 * @param value
235237 */
@@ -244,8 +246,7 @@ private void addFilteredJSON(String key, Object value) {
244246 * build comparison in Kibana. By doing this, the user is able to set the X-axis of his graph to this date and split
245247 * the series by build numbers. It allows him to overlap test results and see if there is regression or not.
246248 *
247- * @param forBuildComparison
248- * boolean to determine if there is CI (continuous integration) or not
249+ * @param forBuildComparison boolean to determine if there is CI (continuous integration) or not
249250 * @return The elapsed time in YYYY-MM-dd HH:mm:ss format
250251 */
251252 public Date getElapsedTime (boolean forBuildComparison ) {
0 commit comments