-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added very simple Graphite/Carbon sender
- Loading branch information
1 parent
681cc69
commit 3b2bd52
Showing
5 changed files
with
89 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/com/googlecode/utterlyidle/statsd/ChannelMessenger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters