Skip to content

Commit ba2dbc4

Browse files
committed
debug memory leak
1 parent 9e89798 commit ba2dbc4

File tree

7 files changed

+452
-445
lines changed

7 files changed

+452
-445
lines changed

src/main/java/org/datadog/jmxfetch/App.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class App
2525
{
2626
private static ArrayList<Instance> _instances = new ArrayList<Instance>();
2727
private static LinkedList<Instance> _brokenInstances = new LinkedList<Instance>();
28-
private final static Logger LOGGER = Logger.getLogger(App.class.getName());
28+
private final static Logger LOGGER = Logger.getLogger(App.class.getName());
2929
private static int _loopCounter;
3030

3131

@@ -54,11 +54,11 @@ public static void main( String[] args ) {
5454
}
5555

5656
// Set up the logger to add file handler
57-
try {
58-
CustomLogger.setup(Level.toLevel(config.logLevel), config.logLocation);
59-
} catch (IOException e) {
60-
LOGGER.error("Unable to setup file handler to file: " + config.logLocation, e);
61-
}
57+
// try {
58+
// CustomLogger.setup(Level.toLevel(config.logLevel), config.logLocation);
59+
// } catch (IOException e) {
60+
// LOGGER.error("Unable to setup file handler to file: " + config.logLocation, e);
61+
// }
6262

6363

6464
// The specified action is unknown
@@ -119,16 +119,16 @@ public void run() {
119119
}
120120

121121

122-
private static void _doLoop(AppConfig config) {
122+
public static void _doLoop(AppConfig config) {
123123
// Main Loop that will periodically collect metrics from the JMX Server
124124
while(true) {
125125
long start = System.currentTimeMillis();
126-
if (_instances.size() > 0) {
126+
if (false && _instances.size() > 0) {
127127
doIteration(config);
128128
} else {
129129
LOGGER.warn("No instance could be initiated. Retrying initialization.");
130130
config.status.flush();
131-
init(config, true);
131+
init(config, false);
132132
}
133133
long length = System.currentTimeMillis() - start;
134134
LOGGER.debug("Iteration ran in " + length + " ms");
@@ -178,7 +178,7 @@ public static void doIteration(AppConfig config) {
178178

179179
instanceStatus = Status.STATUS_WARNING;
180180
// We don't want to log the warning at every iteration so we use this custom logger.
181-
CustomLogger.laconic(LOGGER, Level.WARN, instanceMessage, 0);
181+
// CustomLogger.laconic(LOGGER, Level.WARN, instanceMessage, 0);
182182
}
183183
reporter.sendMetrics(metrics, instance.getName());
184184
config.status.addInstanceStats(instance.getCheckName(), instance.getName(), metrics.size(), instanceMessage, instanceStatus);
@@ -268,7 +268,7 @@ public static void init(AppConfig config, boolean forceNewConnection) {
268268
Map.Entry<String, YamlParser> entry = (Map.Entry<String, YamlParser>)it.next();
269269
String name = entry.getKey();
270270
YamlParser yamlConfig = entry.getValue();
271-
it.remove();
271+
it.remove();
272272

273273

274274
ArrayList<LinkedHashMap<String, Object>> configInstances = ((ArrayList<LinkedHashMap<String, Object>>) yamlConfig.getYamlInstances());
@@ -279,12 +279,14 @@ public static void init(AppConfig config, boolean forceNewConnection) {
279279
continue;
280280
}
281281

282-
for(Iterator<LinkedHashMap<String,Object>> i = configInstances.iterator(); i.hasNext(); ) {
282+
for(Iterator<LinkedHashMap<String,Object>> i = configInstances.iterator(); i.hasNext(); ) {
283283
Instance instance = null;
284284
//Create a new Instance object
285285
try {
286-
instance = new Instance(i.next(), ((LinkedHashMap<String, Object>) yamlConfig.getInitConfig()), name, config);
286+
LinkedHashMap<String, Object> initConfig = (LinkedHashMap<String, Object>) yamlConfig.getInitConfig();
287+
instance = new Instance(i.next(), new LinkedHashMap<String, Object>(), name, config);
287288
} catch(Exception e) {
289+
e.printStackTrace();
288290
String warning = "Unable to create instance. Please check your yaml file";
289291
config.status.addInitFailedCheck(name, warning, Status.STATUS_ERROR);
290292
LOGGER.error(warning);
@@ -301,7 +303,7 @@ public static void init(AppConfig config, boolean forceNewConnection) {
301303
LOGGER.error(warning);
302304
} catch (Exception e) {
303305
_brokenInstances.add(instance);
304-
String warning = "Unexpected exception while initiating instance "+ instance + " : " + e.getMessage();
306+
String warning = "Unexpected exception while initiating instance "+ instance + " : " + e.getMessage();
305307
config.status.addInstanceStats(name, instance.getName(), 0, warning, Status.STATUS_ERROR);
306308
LOGGER.error(warning, e);
307309
}

src/main/java/org/datadog/jmxfetch/AppConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class AppConfig {
4848
public List<String> yamlFileList;
4949

5050
@Parameter(names= {"--check_period", "-p"}, description = "Sleeping time during two iterations in ms", validateWith = PositiveInteger.class, required = false)
51-
public int loopPeriod = 15000;
51+
public int loopPeriod = 1;
5252

5353
@Parameter(names= {"--status_location", "-s"}, description = "Absolute path of the status file. (default to null = no status file written)", converter = StatusWritableLocation.class, required = false)
5454
public Status status = Status.getInstance();

src/main/java/org/datadog/jmxfetch/CustomLogger.java

+30-28
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,35 @@
1111
public class CustomLogger {
1212
static private HashMap<String, Integer> message_stats = new HashMap<String, Integer>();
1313
private final static Logger LOGGER = Logger.getLogger(CustomLogger.class.getName());
14-
static public void setup(Level level, String logLocation) throws IOException {
15-
16-
if( logLocation != null) {
17-
FileAppender fa = new FileAppender();
18-
fa.setName("FileLogger");
19-
fa.setFile(logLocation);
20-
fa.setLayout(new PatternLayout("%d | %-5p| %c{1} | %m%n"));
21-
fa.setThreshold(level);
22-
fa.setAppend(true);
23-
fa.activateOptions();
24-
Logger.getRootLogger().addAppender(fa);
25-
LOGGER.info("File Handler set");
26-
} else {
27-
System.out.println("Log location is not set, not logging to file");
28-
}
29-
30-
}
31-
32-
static public void laconic(Logger logger, Level level, String message, int max) {
33-
if (!message_stats.containsKey(message)) {
34-
logger.log(level, message);
35-
message_stats.put(message, 1);
36-
} else if( message_stats.get(message) < max) {
37-
logger.log(level, message);
38-
message_stats.put(message, message_stats.get(message) + 1);
39-
}
40-
41-
}
14+
// static public void setup(Level level, String logLocation) throws IOException {
15+
16+
// if( logLocation != null) {
17+
// FileAppender fa = new FileAppender();
18+
// fa.setName("FileLogger");
19+
// fa.setFile(logLocation);
20+
// fa.setLayout(new PatternLayout("%d | %-5p| %c{1} | %m%n"));
21+
// fa.setThreshold(level);
22+
// fa.setAppend(true);
23+
// fa.activateOptions();
24+
// Logger.getRootLogger().addAppender(fa);
25+
// LOGGER.info("File Handler set");
26+
// } else {
27+
// System.out.println("Log location is not set, not logging to file");
28+
// }
29+
30+
// }
31+
32+
// static public void laconic(Logger logger, Level level, String message, int max) {
33+
// message = new String(message);
34+
// if (!message_stats.containsKey(message)) {
35+
// logger.log(level, message);
36+
// message_stats.put(message, 1);
37+
// } else if( message_stats.get(message) < max) {
38+
// logger.log(level, message);
39+
// message_stats.put(message, message_stats.get(message) + 1);
40+
// }
41+
42+
// }
43+
4244

4345
}

src/main/java/org/datadog/jmxfetch/Instance.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,24 @@ private void _getMatchingAttributes() {
205205
LOGGER.debug("Attribute: " + beanName + " : " + a + " has an unsupported type: " + attributeType);
206206
continue;
207207
}
208-
208+
//
209209
// For each attribute we try it with each configuration to see if there is one that matches
210210
// If so, we store the attribute so metrics will be collected from it. Otherwise we discard it.
211211
for ( Configuration conf : this._configurationList) {
212212
try {
213213
if ( jmxAttribute.match(conf) ) {
214214
jmxAttribute.matching_conf = conf;
215-
metricsCount += jmxAttribute.getMetricsCount();
215+
metricsCount += jmxAttribute.getMetricsCount();
216216
this._matchingAttributes.add(jmxAttribute);
217217

218-
if (action.equals(AppConfig.ACTION_LIST_EVERYTHING) ||
219-
action.equals(AppConfig.ACTION_LIST_MATCHING) ||
218+
if (action.equals(AppConfig.ACTION_LIST_EVERYTHING) ||
219+
action.equals(AppConfig.ACTION_LIST_MATCHING) ||
220220
action.equals(AppConfig.ACTION_LIST_COLLECTED) && !this._limitReached ||
221221
action.equals(AppConfig.ACTION_LIST_LIMITED) && this._limitReached) {
222222
reporter.displayMatchingAttributeName(jmxAttribute, metricsCount, this._maxReturnedMetrics);
223223
}
224224
break;
225-
}
225+
}
226226
} catch (Exception e) {
227227
LOGGER.error("Error while trying to match a configuration with the Attribute: " + beanName + " : " + a, e);
228228
}

src/main/java/org/datadog/jmxfetch/Reporter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public abstract class Reporter {
1010

11-
private final static Logger LOGGER = Logger.getLogger(App.class.getName());
11+
private final static Logger LOGGER = Logger.getLogger(App.class.getName());
1212

1313
protected HashMap<String, HashMap<String, HashMap<String, Object>>> ratesAggregator = new HashMap<String, HashMap<String, HashMap<String, Object>>>();
1414

src/main/java/org/datadog/jmxfetch/Status.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void flush() {
121121

122122
} catch (Exception e) {
123123
LOGGER.warn("Cannot write status to temp file: " + e.getMessage());
124-
}
124+
}
125125
this._clearStats();
126126
}
127127

0 commit comments

Comments
 (0)