Skip to content

Commit

Permalink
Added very simple Graphite/Carbon sender
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Jan 16, 2017
1 parent 681cc69 commit 3b2bd52
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 17 deletions.
31 changes: 31 additions & 0 deletions src/com/googlecode/utterlyidle/graphite/CarbonSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.googlecode.utterlyidle.graphite;

import com.googlecode.totallylazy.time.Clock;
import com.googlecode.totallylazy.time.Seconds;
import com.googlecode.utterlyidle.statsd.TcpMessenger;

import java.io.Closeable;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

import static java.lang.String.format;

public interface CarbonSender extends Closeable{
void counter(String name, long value) throws IOException;

static CarbonSender carbonSender(SocketAddress socketAddress, Clock clock) throws IOException {
TcpMessenger messenger = new TcpMessenger(SocketChannel.open(socketAddress));
return new CarbonSender() {
@Override
public void counter(final String name, final long value) throws IOException {
messenger.message(format("%s %s %s", name, value, Seconds.sinceEpoch(clock.now())));
}

@Override
public void close() throws IOException {
messenger.close();
}
};
}
}
26 changes: 26 additions & 0 deletions src/com/googlecode/utterlyidle/statsd/ChannelMessenger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.googlecode.utterlyidle.statsd;

import java.io.IOException;
import java.nio.channels.WritableByteChannel;

import static com.googlecode.totallylazy.Bytes.bytes;
import static com.googlecode.totallylazy.Sequences.sequence;
import static java.nio.ByteBuffer.wrap;

public class ChannelMessenger<C extends WritableByteChannel> implements Messenger {
protected final C channel;

public ChannelMessenger(C channel) {
this.channel = channel;
}

@Override
public void message(Iterable<? extends String> values) throws IOException {
channel.write(wrap(bytes(sequence(values).toString("\n"))));
}

@Override
public void close() throws IOException {
channel.close();
}
}
8 changes: 7 additions & 1 deletion src/com/googlecode/utterlyidle/statsd/StatsDClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.googlecode.utterlyidle.statsd;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
Expand All @@ -9,7 +10,7 @@
/**
* Taken from https://github.com/b/statsd_spec
*/
public interface StatsDClient {
public interface StatsDClient extends Closeable {
static StatsDClient statsDClient(String host, int port) throws IOException {
return statsDClient(new InetSocketAddress(host, port));
}
Expand All @@ -22,6 +23,11 @@ static StatsDClient statsDClient(final Messenger messenger) {
return () -> messenger;
}

@Override
default void close() throws IOException{
messager().close();
}

Messenger messager();

/**
Expand Down
22 changes: 22 additions & 0 deletions src/com/googlecode/utterlyidle/statsd/TcpMessenger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.googlecode.utterlyidle.statsd;

import com.googlecode.totallylazy.LazyException;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SocketChannel;

public class TcpMessenger extends ChannelMessenger<SocketChannel> {
public TcpMessenger(final SocketChannel channel) {
super(channel);
}

public static TcpMessenger udpMessager(SocketAddress socketAddress) {
try {
return new TcpMessenger(SocketChannel.open(socketAddress));
} catch (IOException e) {
throw LazyException.lazyException(e);
}
}
}
19 changes: 3 additions & 16 deletions src/com/googlecode/utterlyidle/statsd/UdpMessenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,16 @@
import static com.googlecode.totallylazy.Sequences.sequence;
import static java.nio.ByteBuffer.wrap;

public class UdpMessenger implements Messenger {
private final DatagramChannel channel;

public UdpMessenger(DatagramChannel channel) {
this.channel = channel;
public class UdpMessenger extends ChannelMessenger<DatagramChannel> {
public UdpMessenger(final DatagramChannel channel) {
super(channel);
}


public static UdpMessenger udpMessager(SocketAddress socketAddress) {
try {
return new UdpMessenger(DatagramChannel.open().connect(socketAddress));
} catch (IOException e) {
throw LazyException.lazyException(e);
}
}

@Override
public void message(Iterable<? extends String> values) throws IOException {
channel.write(wrap(bytes(sequence(values).toString("\n"))));
}

@Override
public void close() throws IOException {
channel.close();
}
}

0 comments on commit 3b2bd52

Please sign in to comment.