Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service checks in the JSON reporter #278

Merged
merged 4 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ public List<Map<String, Object>> getMetrics() {
}

/** Adds service check to report on. */
public void doSendServiceCheck(String checkName, String status, String message, String[] tags) {
public void doSendServiceCheck(
String serviceCheckName, String status, String message, String[] tags) {
String tagString = "";
if (tags != null && tags.length > 0) {
tagString = "[" + Joiner.on(",").join(tags) + "]";
}
log.info(
checkName + tagString + " - " + System.currentTimeMillis() / 1000 + " = " + status);
log.info(serviceCheckName + tagString + " - " + System.currentTimeMillis() / 1000
+ " = " + status);

Map<String, Object> sc = new HashMap<String, Object>();
sc.put("name", checkName);
sc.put("name", serviceCheckName);
sc.put("status", status);
sc.put("message", message);
sc.put("tags", tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ protected void sendMetricPoint(
}

/** Use the service check callback to display the JSON. */
public void doSendServiceCheck(String checkName, String status, String message, String[] tags) {
public void doSendServiceCheck(
String serviceCheckName, String status, String message, String[] tags) {
log.debug("Displaying JSON output");
Map<String, Object> sc = new HashMap<String, Object>();
sc.put("check", checkName);
AlexandreYang marked this conversation as resolved.
Show resolved Hide resolved
sc.put("check", serviceCheckName);
sc.put("host_name", "default");
sc.put("timestamp", System.currentTimeMillis() / 1000);
sc.put("status", status);
sc.put("status", this.statusToServiceCheckStatus(status));
sc.put("message", message);
sc.put("tags", tags);

Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.datadog.jmxfetch.reporter;

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;
import org.datadog.jmxfetch.JmxAttribute;
import org.datadog.jmxfetch.Metric;
import org.datadog.jmxfetch.Status;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -157,9 +159,10 @@ public void sendMetrics(List<Metric> metrics, String instanceName, boolean canon
/** Submits service check. */
public void sendServiceCheck(String checkName, String status, String message, String[] tags) {
this.incrementServiceCheckCount(checkName);
String dataName = Reporter.formatServiceCheckPrefix(checkName);
String serviceCheckName = String.format(
"%s.can_connect", Reporter.formatServiceCheckPrefix(checkName));

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

/** Increments the service check count - for book-keeping purposes. */
Expand Down Expand Up @@ -188,6 +191,17 @@ public static String formatServiceCheckPrefix(String fullname) {
return StringUtils.join(chunks, ".");
}

protected ServiceCheck.Status statusToServiceCheckStatus(String status) {
if (status == Status.STATUS_OK) {
return ServiceCheck.Status.OK;
} else if (status == Status.STATUS_WARNING) {
return ServiceCheck.Status.WARNING;
} else if (status == Status.STATUS_ERROR) {
return ServiceCheck.Status.CRITICAL;
}
return ServiceCheck.Status.UNKNOWN;
}

protected abstract void sendMetricPoint(
String metricType, String metricName, double value, String[] tags);

Expand Down
17 changes: 3 additions & 14 deletions src/main/java/org/datadog/jmxfetch/reporter/StatsdReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.slf4j.Slf4j;
import org.datadog.jmxfetch.Instance;
import org.datadog.jmxfetch.JmxAttribute;
import org.datadog.jmxfetch.Status;

/** A reporter class to submit metrics via statsd. */
@Slf4j
Expand Down Expand Up @@ -68,26 +67,16 @@ protected void sendMetricPoint(
}
}

private ServiceCheck.Status statusToServiceCheckStatus(String status) {
if (status == Status.STATUS_OK) {
return ServiceCheck.Status.OK;
} else if (status == Status.STATUS_WARNING) {
return ServiceCheck.Status.WARNING;
} else if (status == Status.STATUS_ERROR) {
return ServiceCheck.Status.CRITICAL;
}
return ServiceCheck.Status.UNKNOWN;
}

/** Submits service check. */
public void doSendServiceCheck(String checkName, String status, String message, String[] tags) {
public void doSendServiceCheck(
String serviceCheckName, String status, String message, String[] tags) {
if (System.currentTimeMillis() - this.initializationTime > 300 * 1000) {
this.statsDClient.stop();
init();
}

ServiceCheck sc = ServiceCheck.builder()
.withName(String.format("%s.can_connect", checkName))
.withName(serviceCheckName)
.withStatus(this.statusToServiceCheckStatus(status))
.withMessage(message)
.withTags(tags)
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/datadog/jmxfetch/TestServiceChecks.java
Original file line number Diff line number Diff line change
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"), scName);
assertEquals(Reporter.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"), scName);
assertEquals(Reporter.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"), scName);
assertEquals(Reporter.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"), scName);
assertEquals(Reporter.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 Down