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

[status] track dogstatsd client errors #336

Merged
merged 3 commits into from
Oct 30, 2020
Merged
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
14 changes: 8 additions & 6 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
@@ -803,12 +803,14 @@ private void reportStatus(
String status) {
String checkName = instance.getCheckName();

appConfig
.getStatus()
.addInstanceStats(
checkName, instance.getName(),
metricCount, reporter.getServiceCheckCount(checkName),
message, status);
Status stats = appConfig.getStatus();
stats.addInstanceStats(
checkName, instance.getName(),
metricCount, reporter.getServiceCheckCount(checkName),
message, status);
if (reporter.getHandler() != null) {
stats.addErrorStats(reporter.getHandler().getErrors());
}
}

private void sendServiceCheck(
7 changes: 7 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Status.java
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ public class Status {
private static final String API_STATUS_PATH = "agent/jmx/status";
private Map<String, Object> instanceStats;
private Map<String, Object> info;
private int errors;
private String statusFileLocation;
private HttpClient client;
private boolean isEnabled;
@@ -83,6 +84,10 @@ public void addInstanceStats(
INITIALIZED_CHECKS);
}

public void addErrorStats(int errors) {
this.errors = errors;
}

@SuppressWarnings("unchecked")
private void addStats(
String checkName,
@@ -128,6 +133,7 @@ private String generateYaml() {
status.put("info", this.info);
status.put("timestamp", System.currentTimeMillis());
status.put("checks", this.instanceStats);
status.put("errors", this.errors);
return new Yaml().dump(status);
}

@@ -136,6 +142,7 @@ private String generateJson() throws IOException {
status.put("info", this.info);
status.put("timestamp", System.currentTimeMillis());
status.put("checks", this.instanceStats);
status.put("errors", this.errors);
return JSON.std.with(JSON.Feature.WRITE_NULL_PROPERTIES).asString(status);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.datadog.jmxfetch.reporter;

import com.timgroup.statsd.StatsDClientErrorHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;


/** An error handler class to track errors as required. */
@Slf4j
public class LoggingErrorHandler implements StatsDClientErrorHandler {
private AtomicInteger errors = new AtomicInteger();

@Override
public void handle(Exception exception) {
errors.incrementAndGet();
log.error("statsd client error:", exception);
}

public int getErrors() {
return errors.get();
}
}

5 changes: 5 additions & 0 deletions src/main/java/org/datadog/jmxfetch/reporter/Reporter.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public abstract class Reporter {
new HashMap<String, Map<String, Map<String, Object>>>();
private Map<String, Map<String, Long>> countersAggregator =
new HashMap<String, Map<String, Long>>();
protected LoggingErrorHandler handler;

/** Reporter constructor. */
public Reporter() {
@@ -190,6 +191,10 @@ protected Map<String, Integer> getServiceCheckCountMap() {
return this.serviceCheckCount;
}

public LoggingErrorHandler getHandler() {
return this.handler;
}

protected ServiceCheck.Status statusToServiceCheckStatus(String status) {
if (status == Status.STATUS_OK) {
return ServiceCheck.Status.OK;
13 changes: 3 additions & 10 deletions src/main/java/org/datadog/jmxfetch/reporter/StatsdReporter.java
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
import com.timgroup.statsd.ServiceCheck;
import com.timgroup.statsd.StatsDClient;
import com.timgroup.statsd.StatsDClientErrorHandler;
import lombok.extern.slf4j.Slf4j;
import org.datadog.jmxfetch.Instance;
import org.datadog.jmxfetch.JmxAttribute;


/** A reporter class to submit metrics via statsd. */
@Slf4j
public class StatsdReporter extends Reporter {
@@ -17,14 +17,6 @@ public class StatsdReporter extends Reporter {
private int statsdPort;
private long initializationTime;

private class LoggingErrorHandler implements StatsDClientErrorHandler {

@Override
public void handle(Exception exception) {
log.error("statsd client error:", exception);
}
}

/** Constructor, instantiates statsd reported to provided host and port. */
public StatsdReporter(String statsdHost, int statsdPort) {
this.statsdHost = statsdHost;
@@ -38,14 +30,15 @@ private void init() {
// Only set the entityId to "none" if UDS communication is activated
String entityId = this.statsdPort == 0 ? "none" : null;

handler = new LoggingErrorHandler();
/* Create the StatsDClient with "entity-id" set to "none" to avoid
having dogstatsd server adding origin tags, when the connection is
done with UDS. */
NonBlockingStatsDClientBuilder builder = new NonBlockingStatsDClientBuilder()
.enableTelemetry(false)
.hostname(this.statsdHost)
.port(this.statsdPort)
.errorHandler(new LoggingErrorHandler())
.errorHandler(handler)
.entityID(entityId);

// When using UDS set the datagram size to 8k