Skip to content

Commit fd106b4

Browse files
committed
File to trigger JMXFetch exit
Trigger JMXFetch exit when a specified file is created. Usage: `--exit_file_location` or `-e` with the absolute path of the trigger file to watch.
1 parent f83b809 commit fd106b4

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ private static void clearInstances(List<Instance> instances) {
136136
void start() {
137137
// Main Loop that will periodically collect metrics from the JMX Server
138138
while (true) {
139+
// Exit on exit file trigger
140+
if (appConfig.getExitWatcher().shouldExit()){
141+
LOGGER.info("Exit file detected: stopping JMXFetch.");
142+
System.exit(0);
143+
}
144+
139145
long start = System.currentTimeMillis();
140146
if (instances.size() > 0) {
141147
doIteration();
@@ -288,7 +294,7 @@ private void reportStatus(AppConfig appConfig, Reporter reporter, Instance insta
288294
instance.getServiceCheckTags());
289295

290296
appConfig.getStatus().addInstanceStats(checkName, instance.getName(),
291-
metricCount, reporter.getServiceCheckCount(checkName),
297+
metricCount, reporter.getServiceCheckCount(checkName),
292298
message, status);
293299
reporter.resetServiceCheckCount(checkName);
294300
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.util.Arrays;
33
import java.util.List;
44

5+
import org.datadog.jmxfetch.converter.ExitWatcherConverter;
56
import org.datadog.jmxfetch.converter.ReporterConverter;
67
import org.datadog.jmxfetch.converter.StatusConverter;
78
import org.datadog.jmxfetch.reporter.ConsoleReporter;
@@ -71,6 +72,12 @@ class AppConfig {
7172
required = false)
7273
private Status status = new Status();
7374

75+
@Parameter(names = {"--exit_file_location", "-e"},
76+
description = "Absolute path of the trigger file to watch to exit. (default to null = no exit on file)",
77+
converter = ExitWatcherConverter.class,
78+
required = false)
79+
private ExitWatcher exitWatcher = new ExitWatcher();
80+
7481
@Parameter(description = "Action to take, should be in [help, collect, " +
7582
"list_everything, list_collected_attributes, list_matching_attributes, " +
7683
"list_not_matching_attributes, list_limited_attributes]",
@@ -93,6 +100,10 @@ public Status getStatus() {
93100
return status;
94101
}
95102

103+
public ExitWatcher getExitWatcher(){
104+
return exitWatcher;
105+
}
106+
96107
public int getCheckPeriod() {
97108
return checkPeriod;
98109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.datadog.jmxfetch;
2+
3+
import java.io.File;
4+
5+
public class ExitWatcher {
6+
7+
private String exitFileLocation;
8+
private boolean isEnabled;
9+
10+
public ExitWatcher(){
11+
this(null);
12+
}
13+
14+
public ExitWatcher(String exitFileLocation) {
15+
this.exitFileLocation = exitFileLocation;
16+
this.isEnabled = this.exitFileLocation != null;
17+
}
18+
19+
public String getExitFileLocation() {
20+
return exitFileLocation;
21+
}
22+
23+
public boolean isEnabled() {
24+
return isEnabled;
25+
}
26+
27+
public boolean shouldExit(){
28+
if (isEnabled()) {
29+
File f = new File(exitFileLocation);
30+
if(f.exists() && !f.isDirectory()) {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.datadog.jmxfetch.converter;
2+
3+
import com.beust.jcommander.IStringConverter;
4+
import org.datadog.jmxfetch.ExitWatcher;
5+
6+
public class ExitWatcherConverter implements IStringConverter<ExitWatcher> {
7+
8+
public ExitWatcher convert(String value) {
9+
return new ExitWatcher(value);
10+
}
11+
}

src/test/java/org/datadog/jmxfetch/TestApp.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.management.MBeanServer;
1414
import javax.management.ObjectName;
1515

16+
import java.io.File;
1617
import java.lang.management.ManagementFactory;
1718
import java.util.Arrays;
1819
import java.util.HashMap;
@@ -401,12 +402,12 @@ public void testServiceCheckCounter() throws Exception {
401402

402403
// Let's check that the counter is null
403404
assertEquals(0, repo.getServiceCheckCount("jmx"));
404-
405+
405406
// Let's put a service check in the pipeline (we cannot call doIteration()
406-
// here unfortunately because it would call reportStatus which will flush
407-
// the count to the jmx_status.yaml file and reset the counter.
407+
// here unfortunately because it would call reportStatus which will flush
408+
// the count to the jmx_status.yaml file and reset the counter.
408409
repo.sendServiceCheck("jmx", Status.STATUS_OK, "This is a test", "jmx_test_instance", null);
409-
410+
410411
// Let's check that the counter has been updated
411412
assertEquals(1, repo.getServiceCheckCount("jmx"));
412413

@@ -430,6 +431,21 @@ public void testPrefixFormatter() throws Exception {
430431
assertEquals(data[i][1], Reporter.formatServiceCheckPrefix(data[i][0]));
431432
}
432433

434+
@Test
435+
public void testExitWatcher() throws Exception {
436+
// Test the ExitWatcher logic
437+
438+
// Create a temp file
439+
File temp = File.createTempFile("exit-jmxfetch-file-name", ".tmp");
440+
temp.deleteOnExit();
441+
442+
ExitWatcher exitWatcher = new ExitWatcher(temp.getAbsolutePath());
443+
assertTrue(exitWatcher.shouldExit());
444+
445+
temp.delete();
446+
assertFalse(exitWatcher.shouldExit());
447+
}
448+
433449
@Test
434450
public void testApp() throws Exception {
435451
// We expose a few metrics through JMX

src/test/java/org/datadog/jmxfetch/TestParsingJCommander.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class TestParsingJCommander {
2121
private static final String REPORTER_CONSOLE = "console";
2222
private static final String SINGLE_CHECK = "jmx.yaml";
2323
private static final List<String> MULTI_CHECK = Arrays.asList("jmx.yaml", "jmx-2.yaml");
24-
private static final String STATUS_LOCATION = "/status/loaction";
24+
private static final String STATUS_LOCATION = "/status/status_location";
25+
private static final String EXIT_FILE_LOCATION = "/status/exit_locationt";
2526

2627
private static AppConfig testCommand(String[] params) throws ParameterException {
2728
AppConfig appConfig = new AppConfig();
@@ -235,6 +236,21 @@ public void testParsingStatus() {
235236
assertTrue(appConfig.getStatus().isEnabled());
236237
}
237238

239+
@Test
240+
public void testParsingExitWatcher() {
241+
String[] params = new String[]{
242+
"--reporter", REPORTER_CONSOLE,
243+
"--check", SINGLE_CHECK,
244+
"--conf_directory", CONF_DIR,
245+
"--exit_file_location", EXIT_FILE_LOCATION,
246+
AppConfig.ACTION_COLLECT
247+
};
248+
AppConfig appConfig = testCommand(params);
249+
assertNotNull(appConfig.getExitWatcher());
250+
assertEquals(EXIT_FILE_LOCATION, appConfig.getExitWatcher().getExitFileLocation());
251+
assertTrue(appConfig.getExitWatcher().isEnabled());
252+
}
253+
238254
@Test
239255
public void testParsingAction() {
240256
// Positive cases

0 commit comments

Comments
 (0)