Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

Commit

Permalink
skip empty json array in result
Browse files Browse the repository at this point in the history
  • Loading branch information
Guenter Grossberger committed Dec 11, 2018
1 parent 4ed8fd5 commit b162039
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ release.properties
.DS_Store
dependency-reduced-pom.xml
buildNumber.properties
.checkstyle
2 changes: 1 addition & 1 deletion asm-monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ca.apm.extensions</groupId>
<artifactId>asm-monitor</artifactId>
<version>1.5.6</version>
<version>1.5.7</version>
<name>ASMMonitor</name>
<description>Extension to integrate App Synthetic Monitor (fka Cloud Monitor, WatchMouse) into CA APM.</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,48 +208,54 @@ public Map<String, String> generateMetrics(

// if result of first object was "No checkpoint available"
// and there is a next element skip it
JSONObject arrayElement = innerJsonArray.getJSONObject(i);
if (skipNoCheckpointAvailable
&& arrayElement.has(RESULT_TAG)
&& (RESULT_NO_CHECKPOINT_AVAILABLE
== arrayElement.optInt(RESULT_TAG))
&& ((i + 1) < innerJsonArray.length())) {

EpaUtils.getFeedback().debug(module,
"skipping node '" + thisKey
+ "' with result value "
+ arrayElement.optInt(RESULT_TAG));
} else {
JSONObject resultObj = innerJsonArray.getJSONObject(i);
try {
// find the monitor
Monitor mon =
MonitorFactory.findMonitor(resultObj.getString("name"));
if (mon != null) {
// hand over parsing to a monitor-specific handler chain
mon.generateMetrics(metricMap,
resultObj.toString(),
metricTree);
} else {
// recursively generate metrics for these tags
generateMetrics(metricMap, resultObj.toString(), metricTree);
}
} catch (JSONException e) {
JSONObject arrayElement = innerJsonArray.optJSONObject(i);

// skip empty? json array
if (null != arrayElement) {
if (skipNoCheckpointAvailable
&& arrayElement.has(RESULT_TAG)
&& (RESULT_NO_CHECKPOINT_AVAILABLE
== arrayElement.optInt(RESULT_TAG))
&& ((i + 1) < innerJsonArray.length())) {

EpaUtils.getFeedback().debug(module,
"Missing name key for log record.");
} catch (AsmException e) {
handleException(e, metricTree, metricMap, module);
"skipping node '" + thisKey
+ "' with result value "
+ arrayElement.optInt(RESULT_TAG));
} else {
JSONObject resultObj = innerJsonArray.getJSONObject(i);
try {
// find the monitor
Monitor mon =
MonitorFactory.findMonitor(resultObj.getString("name"));
if (mon != null) {
// hand over parsing to a monitor-specific handler chain
mon.generateMetrics(metricMap,
resultObj.toString(),
metricTree);
} else {
// recursively generate metrics for these tags
generateMetrics(metricMap,
resultObj.toString(),
metricTree);
}
} catch (JSONException e) {
EpaUtils.getFeedback().debug(module,
"Missing name key for log record.");
} catch (AsmException e) {
handleException(e, metricTree, metricMap, module);
}
}
}
} else if (thisKey.equals(MONITORS_TAG)
|| thisKey.equals(STATS_TAG)) {
// recursively generate metrics for these tags
generateMetrics(metricMap,
innerJsonArray.getJSONObject(i).toString(), metricTree);
innerJsonArray.getJSONObject(i).toString(), metricTree);
} else {
generateMetrics(metricMap,
innerJsonArray.getJSONObject(i).toString(),
metricTree + METRIC_PATH_SEPARATOR + thisKey);
innerJsonArray.getJSONObject(i).toString(),
metricTree + METRIC_PATH_SEPARATOR + thisKey);
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.ca.apm.swat.epaplugins.asm;

import com.wily.introscope.epagent.EpaUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

/**
* Test class for testing the rule_log API.
* Run with -DDEBUG=true for debug output.
*
* @author Guenter Grossberger - CA APM SWAT Team
*
*/
public class LogArrayTest extends FileTest {

@Override
public void setup() {
super.setup();

// we need to load the the monitoring station map
try {
requestHelper.getMonitoringStations();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("error getting monitoring stations: " + e.getMessage());
}
}

/**
* Test getLog() for a json string that includes empty arrays.
*/
@Test
public void getLogArray() {

try {
// set properties
EpaUtils.getProperties().setProperty(METRICS_LOGS, TRUE);
EpaUtils.getProperties().setProperty(DISPLAY_STATIONS, FALSE);

String folder = "ArrayTest";
int numMonitors = 4;
String metricPrefix = MONITOR_METRIC_PREFIX + folder;

// load file
accessor.loadFile(LOGS_CMD, "target/test-classes/rule_log_array.json");

// call API
Map<String, String> metricMap =
requestHelper.getLogs(folder, numMonitors, metricPrefix, null).getMap();

// metricMap should contain those entries

String[] expectedMetrics = {
"Monitors|ArrayTest:Agent Time Zone",
"Monitors|ArrayTest|Monitor1:Alerts Per Interval",
"Monitors|ArrayTest|Monitor1:Check End Time",
"Monitors|ArrayTest|Monitor1:Check Start Time",
"Monitors|ArrayTest|Monitor1:Connect Time (ms)",
"Monitors|ArrayTest|Monitor1:Download Size (kB)",
"Monitors|ArrayTest|Monitor1:Download Time (ms)",
"Monitors|ArrayTest|Monitor1:IP Address",
"Monitors|ArrayTest|Monitor1:Location Code",
"Monitors|ArrayTest|Monitor1:Processing Time (ms)",
"Monitors|ArrayTest|Monitor1:Repeat",
"Monitors|ArrayTest|Monitor1:Resolve Time (ms)",
"Monitors|ArrayTest|Monitor1:Result Code",
"Monitors|ArrayTest|Monitor1:Monitor ID",
"Monitors|ArrayTest|Monitor1:Monitor Name",
"Monitors|ArrayTest|Monitor1:Total Time (ms)",
"Monitors|ArrayTest|Monitor1:Type",
"Monitors|ArrayTest|Monitor1:Monitor ID",
"Monitors|ArrayTest|Monitor2:Alerts Per Interval",
"Monitors|ArrayTest|Monitor2:Check End Time",
"Monitors|ArrayTest|Monitor2:Check Start Time",
"Monitors|ArrayTest|Monitor2:Download Size (kB)",
"Monitors|ArrayTest|Monitor2:Download Time (ms)",
"Monitors|ArrayTest|Monitor3:IP Address",
"Monitors|ArrayTest|Monitor3:Location Code",
"Monitors|ArrayTest|Monitor3:Processing Time (ms)",
"Monitors|ArrayTest|Monitor3:Repeat",
"Monitors|ArrayTest|Monitor3:Result Code",
"Monitors|ArrayTest|Monitor3:Monitor ID",
"Monitors|ArrayTest|Monitor3:Monitor Name",
"Monitors|ArrayTest|Monitor4:Total Time (ms)",
"Monitors|ArrayTest|Monitor4:Type",
"Monitors|ArrayTest|Monitor4:Monitor ID",
"Monitors|ArrayTest|Monitor4:Connect Time (ms)",
};

if (DEBUG) {
TreeSet<String> sortedSet = new TreeSet<String>(metricMap.keySet());
for (Iterator<String> it = sortedSet.iterator(); it.hasNext(); ) {
String key = it.next();
System.out.println(key + " = " + metricMap.get(key));
}
}

// check
checkMetrics(expectedMetrics, metricMap);

} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
1 change: 1 addition & 0 deletions asm-monitor/src/test/resources/rule_log_array.json

Large diffs are not rendered by default.

0 comments on commit b162039

Please sign in to comment.