Skip to content

Commit

Permalink
Add check prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreYang committed Mar 16, 2020
1 parent 37fc55f commit 979549f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
14 changes: 13 additions & 1 deletion src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.datadog.jmxfetch.tasks.TaskStatusHandler;
import org.datadog.jmxfetch.util.CustomLogger;
import org.datadog.jmxfetch.util.FileHelper;
import org.datadog.jmxfetch.util.ServiceCheckHelper;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

Expand Down Expand Up @@ -811,11 +812,22 @@ private void reportStatus(
private void sendServiceCheck(
Reporter reporter, Instance instance, String message, String status) {
String checkName = instance.getCheckName();
String serviceCheckName = getServiceCheckName(instance);

reporter.sendServiceCheck(checkName, status, message, instance.getServiceCheckTags());
reporter.sendServiceCheck(checkName, serviceCheckName, status, message, instance.getServiceCheckTags());
reporter.resetServiceCheckCount(checkName);
}

private String getServiceCheckName(Instance instance) {
String checkPrefix;
if (instance.getCheckPrefix() != null) {
checkPrefix = instance.getCheckPrefix();
} else {
checkPrefix = ServiceCheckHelper.formatServiceCheckPrefix(instance.getCheckName());
}
return String.format("%s.can_connect", checkPrefix);
}

private Instance instantiate(
Map<String, Object> instanceMap,
Map<String, Object> initConfig,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public Yaml initialValue() {
private String service;
private Map<String, String> tags;
private String checkName;
private String checkPrefix;
private int maxReturnedMetrics;
private boolean limitReached;
private Connection connection;
Expand Down Expand Up @@ -186,6 +187,10 @@ public Instance(
}
}

if (initConfig != null) {
this.checkPrefix = (String) initConfig.get("check_prefix");
}

// Alternative aliasing for CASSANDRA-4009 metrics
// More information: https://issues.apache.org/jira/browse/CASSANDRA-4009
this.cassandraAliasing = (Boolean) instanceMap.get("cassandra_aliasing");
Expand Down Expand Up @@ -706,6 +711,11 @@ public String getCheckName() {
return this.checkName;
}

/** Returns the check prefix. */
public String getCheckPrefix() {
return this.checkPrefix;
}

/** Returns the maximum number of metrics an instance may collect. */
public int getMaxNumberOfMetrics() {
return this.maxReturnedMetrics;
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.timgroup.statsd.ServiceCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import org.datadog.jmxfetch.App;
import org.datadog.jmxfetch.Instance;
Expand Down Expand Up @@ -157,11 +156,8 @@ public void sendMetrics(List<Metric> metrics, String instanceName, boolean canon
}

/** Submits service check. */
public void sendServiceCheck(String checkName, String status, String message, String[] tags) {
public void sendServiceCheck(String checkName, String serviceCheckName, String status, String message, String[] tags) {
this.incrementServiceCheckCount(checkName);
String serviceCheckName = String.format(
"%s.can_connect", Reporter.formatServiceCheckPrefix(checkName));

this.doSendServiceCheck(serviceCheckName, status, message, tags);
}

Expand All @@ -184,13 +180,6 @@ protected Map<String, Integer> getServiceCheckCountMap() {
return this.serviceCheckCount;
}

/** Formats the service check prefix. */
public static String formatServiceCheckPrefix(String fullname) {
String[] chunks = fullname.split("\\.");
chunks[0] = chunks[0].replaceAll("[A-Z0-9:_\\-]", "");
return StringUtils.join(chunks, ".");
}

protected ServiceCheck.Status statusToServiceCheckStatus(String status) {
if (status == Status.STATUS_OK) {
return ServiceCheck.Status.OK;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/datadog/jmxfetch/util/ServiceCheckHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.datadog.jmxfetch.util;

import org.apache.commons.lang.StringUtils;

public class ServiceCheckHelper {
/** Formats the service check prefix. */
public static String formatServiceCheckPrefix(String fullname) {
String[] chunks = fullname.split("\\.");
chunks[0] = chunks[0].replaceAll("[A-Z0-9:_\\-]", "");
return StringUtils.join(chunks, ".");
}
}
42 changes: 35 additions & 7 deletions src/test/java/org/datadog/jmxfetch/TestServiceChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.datadog.jmxfetch.reporter.Reporter;
import org.datadog.jmxfetch.util.ServiceCheckHelper;
import org.junit.Test;

public class TestServiceChecks extends TestCommon {
Expand Down Expand Up @@ -42,7 +42,7 @@ public void testServiceCheckOK() throws Exception {
String scStatus = (String) (sc.get("status"));
String[] scTags = (String[]) (sc.get("tags"));

assertEquals(Reporter.formatServiceCheckPrefix("jmx") + ".can_connect", scName);
assertEquals(ServiceCheckHelper.formatServiceCheckPrefix("jmx") + ".can_connect", scName);
assertEquals(Status.STATUS_OK, scStatus);
assertEquals(scTags.length, 3);
assertTrue(Arrays.asList(scTags).contains("instance:jmx_test_instance"));
Expand Down Expand Up @@ -83,7 +83,7 @@ public void testServiceCheckWarning() throws Exception {
String scStatus = (String) (sc.get("status"));
String[] scTags = (String[]) (sc.get("tags"));

assertEquals(Reporter.formatServiceCheckPrefix("too_many_metrics") + ".can_connect", scName);
assertEquals(ServiceCheckHelper.formatServiceCheckPrefix("too_many_metrics") + ".can_connect", scName);
// We should have an OK service check status when too many metrics are getting sent
assertEquals(Status.STATUS_OK, scStatus);
assertEquals(scTags.length, 3);
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testServiceCheckCRITICAL() throws Exception {
String scMessage = (String) (sc.get("message"));
String[] scTags = (String[]) (sc.get("tags"));

assertEquals(Reporter.formatServiceCheckPrefix("non_running_process") + ".can_connect", scName);
assertEquals(ServiceCheckHelper.formatServiceCheckPrefix("non_running_process") + ".can_connect", scName);
assertEquals(Status.STATUS_ERROR, scStatus);
assertEquals(
"Unable to instantiate or initialize instance process_regex: `.*non_running_process_test.*`. "
Expand All @@ -142,7 +142,7 @@ public void testServiceCheckCRITICAL() throws Exception {
scMessage = (String) (sc.get("message"));
scTags = (String[]) (sc.get("tags"));

assertEquals(Reporter.formatServiceCheckPrefix("non_running_process") + ".can_connect", scName);
assertEquals(ServiceCheckHelper.formatServiceCheckPrefix("non_running_process") + ".can_connect", scName);
assertEquals(Status.STATUS_ERROR, scStatus);
assertEquals(
"Unable to instantiate or initialize instance process_regex: `.*non_running_process_test.*`. "
Expand All @@ -168,7 +168,7 @@ public void testServiceCheckCounter() throws Exception {
// Let's put a service check in the pipeline (we cannot call doIteration()
// here unfortunately because it would call reportStatus which will flush
// the count to the jmx_status.yaml file and reset the counter.
repo.sendServiceCheck("jmx", Status.STATUS_OK, "This is a test", null);
repo.sendServiceCheck("jmx", "jmx.can_connect", Status.STATUS_OK, "This is a test", null);

// Let's check that the counter has been updated
assertEquals(1, repo.getServiceCheckCount("jmx"));
Expand All @@ -190,6 +190,34 @@ public void testPrefixFormatter() throws Exception {

// Let's test them all
for (int i = 0; i < data.length; ++i)
assertEquals(data[i][1], Reporter.formatServiceCheckPrefix(data[i][0]));
assertEquals(data[i][1], ServiceCheckHelper.formatServiceCheckPrefix(data[i][0]));
}


@Test
public void testServiceCheckPrefix() throws Exception {
// We expose a few metrics through JMX
registerMBean(new SimpleTestJavaApp(), "org.datadog.jmxfetch.test:type=ServiceCheckTest");

// We do a first collection
when(appConfig.isTargetDirectInstances()).thenReturn(true);
initApplication("jmx_check_prefix.yaml");

run();
List<Map<String, Object>> metrics = getMetrics();

// Test that the check prefix is used
List<Map<String, Object>> serviceChecks = getServiceChecks();

assertEquals(1, serviceChecks.size());
Map<String, Object> sc = serviceChecks.get(0);
assertNotNull(sc.get("name"));
assertNotNull(sc.get("status"));
assertNull(sc.get("message"));
assertNotNull(sc.get("tags"));

String scName = (String) (sc.get("name"));

assertEquals( "myprefix.can_connect", scName);
}
}
9 changes: 9 additions & 0 deletions src/test/resources/jmx_check_prefix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
init_config:
check_prefix: myprefix

instances:
- jvm_direct: true
name: jmx_test_instance
conf:
- include:
domain: org.datadog.jmxfetch.test

0 comments on commit 979549f

Please sign in to comment.