Skip to content

Commit

Permalink
Merge pull request #1 from DataDog/add_service_checks
Browse files Browse the repository at this point in the history
Add service check to java dogstatsd client, add test
  • Loading branch information
Arthur committed Mar 12, 2015
2 parents 701faca + b7c4608 commit bedf4ef
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 7 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ java-dogstatsd-client

A statsd client library implemented in Java. Allows for Java applications to easily communicate with statsd.

This version is forked from the upstream [java-statsd-client](https://github.com/youdevise/java-statsd-client) project, adding support for [DataDog](http://datadoghq.com/) extensions for use with [dogstatsd](http://docs.datadoghq.com/guides/dogstatsd/).

This version also adds support for empty or null prefixes, to allow a client to send arbitrary statistic names.
This version is forked from the upstream [java-dogstatsd-client](https://github.com/indeedeng/java-dogstatsd-client) project (which is, in turn, forked from [java-statsd-client](https://github.com/youdevise/java-statsd-client)), adding additional support for [DataDog](http://datadoghq.com/) extensions for use with [dogstatsd](http://docs.datadoghq.com/guides/dogstatsd/).

Downloads
---------
The client jar is distributed via maven central, and can be downloaded [here](http://search.maven.org/#search%7Cga%7C1%7Cg%3Acom.timgroup%20a%3Ajava-statsd-client).

```xml
<dependency>
<groupId>com.indeed</groupId>
<groupId>com.datadog</groupId>
<artifactId>java-dogstatsd-client</artifactId>
<version>2.0.7</version>
<version>2.1.0</version>
</dependency>
```

Expand All @@ -41,6 +39,9 @@ public class Foo {
statsd.recordHistogram("qux", 15) /* DataDog extension: histograms */
statsd.recordHistogram("qux", 15.5) /* ...also floating-point */

ServiceCheck sc = new ServiceCheck("my.check.name", ServiceCheck.OK);
statsd.serviceCheck(sc); /* Datadog extension: send service check status */

/* Compatibility note: Unlike upstream statsd, DataDog expects execution times to be a
* floating-point value in seconds, not a millisecond value. This library
* does the conversion from ms to fractional seconds.
Expand All @@ -50,3 +51,10 @@ public class Foo {
}
```


Change Log
----------

- 2.1.0
- Fork from https://github.com/indeedeng/java-dogstatsd-client (2.0.7)
- Add support for Datadog service checks
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<version>5</version>
</parent>

<groupId>com.indeed</groupId>
<groupId>com.datadog</groupId>
<artifactId>java-dogstatsd-client</artifactId>
<packaging>jar</packaging>
<name>java-dogstatsd-client</name>
<version>2.0.9-SNAPSHOT</version>
<version>2.1.0</version>
<description>A tiny library allowing Java applications to communicate with DataDog statsd instances easily.</description>
<url>https://github.com/indeedeng/java-dogstatsd-client</url>

Expand All @@ -34,6 +34,11 @@
</scm>

<developers>
<developer> <!-- Additions for new Datadog features -->
<id>datadog</id>
<name>Datadog developers</name>
<email>[email protected]</email>
</developer>
<developer> <!-- Initial datadog port -->
<id>duffy</id>
<name>Charles Duffy</name>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/timgroup/statsd/NoOpStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public final class NoOpStatsDClient implements StatsDClient {
@Override public void histogram(String aspect, double value, String... tags) { }
@Override public void recordHistogramValue(String aspect, long value, String... tags) { }
@Override public void histogram(String aspect, long value, String... tags) { }
@Override public void recordServiceCheckRun(ServiceCheck sc) { }
@Override public void serviceCheck(ServiceCheck sc) { }
}
22 changes: 22 additions & 0 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,28 @@ public void histogram(String aspect, long value, String... tags) {
recordHistogramValue(aspect, value, tags);
}

/**
* Records a run status for the specified named service check.
*
* <p>This method is a DataDog extension, and may not work with other servers.</p>
*
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
*
* @param sc
* the service check object
*/
@Override public void recordServiceCheckRun(ServiceCheck sc) {
send(sc.toStatsDString());
}

/**
* Convenience method equivalent to {@link #recordServiceCheckRun(ServiceCheck sc)}.
*/
@Override
public void serviceCheck(ServiceCheck sc) {
recordServiceCheckRun(sc);
}

private void send(String message) {
queue.offer(message);
}
Expand Down
159 changes: 159 additions & 0 deletions src/main/java/com/timgroup/statsd/ServiceCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.timgroup.statsd;

/**
* A service check model, which is used to format a service check message
* sent to the datadog agent
*/
public class ServiceCheck {
public static final int OK = 0;
public static final int WARNING = 1;
public static final int CRITICAL = 2;
public static final int UNKNOWN = 3;

private String name, hostname, message;

private int status, checkRunId, timestamp;

private String[] tags;

/**
*/
public ServiceCheck() {
}

/**
* @param name
* @param status
*/
public ServiceCheck(String name, int status) {
this(name, status, null, null, null);
}

public ServiceCheck(String name, int status, String message, String[] tags) {
this(name, status, message, null, tags);
}

public ServiceCheck(String name, int status, String message, String hostname, String[] tags) {
this.name = name;
this.status = status;
this.message = message;
this.hostname = hostname;
this.tags = tags;
}

/**
* @return
*/
public String getName() {
return name;
}

/**
* @param name
*/
public void setName(String name) {
this.name = name;
}

/**
* @return
*/
public int getStatus() {
return status;
}

/**
* @param status
*/
public void setStatus(int status) {
this.status = status;
}

/**
* @return
*/
public String getMessage() {
return message;
}

/**
* @return
*/
public String getEscapedMessage() {
return message.replace("\n", "\\n").replace("m:", "m\\:");
}

/**
*
* @param message
*/
public void setMessage(String message) {
this.message = message;
}

/**
* @return
*/
public String getHostname() {
return hostname;
}

/**
* @param hostname
*/
public void setHostname(String hostname) {
this.hostname = hostname;
}

/**
* @return
*/
public int getTimestamp() {
return timestamp;
}

/**
* @param timestamp
*/
public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}

/**
* @return
*/
public String[] getTags() {
return tags;
}

/**
* @param tags
*/
public void setTags(String... tags) {
this.tags = tags;
}

/**
* @return
*/
public String toStatsDString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("_sc|%s|%d", name, status));
if (timestamp > 0) {
sb.append(String.format("|d:%d", timestamp));
}
if (hostname != null) {
sb.append(String.format("|h:%s", hostname));
}
if (tags != null && tags.length > 0) {
sb.append(String.format("|#%s", tags[0]));
for(int i=1;i<tags.length;i++) {
sb.append(',').append(tags[i]);
}
}
if (message != null) {
sb.append(String.format("|m:%s", this.getEscapedMessage()));
}
return sb.toString();
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/timgroup/statsd/StatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,21 @@ public interface StatsDClient {
*/
void histogram(String aspect, long value, String... tags);

/**
* Records a run status for the specified named service check.
*
* <p>This method is a DataDog extension, and may not work with other servers.</p>
*
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
*
* @param sc
* the service check object
*/
void recordServiceCheckRun(ServiceCheck sc);

/**
* Convenience method equivalent to {@link #recordServiceCheckRun(ServiceCheck sc)}.
*/
void serviceCheck(ServiceCheck sc);

}
12 changes: 12 additions & 0 deletions src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,16 @@ public void stop() throws Exception {
assertThat(server.messagesReceived(), contains("top.level.value:423|g"));
}

@Test public void
sends_service_check() throws Exception {
String[] tags = {"key1:val1", "key2:val2"};
ServiceCheck sc = new ServiceCheck("my_check.name", ServiceCheck.WARNING, "♬ †øU \n†øU ¥ºu|m: T0µ ♪", "i-abcd1234", tags);
sc.setTimestamp(1420740000);

client.serviceCheck(sc);
server.waitForMessage();

assertThat(server.messagesReceived(), contains(String.format("_sc|my_check.name|1|d:1420740000|h:i-abcd1234|#key1:val1,key2:val2|m:%s",
"♬ †øU \\n†øU ¥ºu|m\\: T0µ ♪")));
}
}

0 comments on commit bedf4ef

Please sign in to comment.