1
1
package com .readytalk .metrics ;
2
2
3
- import org .slf4j .Logger ;
4
- import org .slf4j .LoggerFactory ;
5
-
3
+ import javax .annotation .concurrent .NotThreadSafe ;
6
4
import java .io .Closeable ;
7
5
import java .io .IOException ;
8
6
import java .net .DatagramSocket ;
9
7
import java .net .InetSocketAddress ;
10
8
import java .nio .charset .Charset ;
11
9
import java .util .regex .Pattern ;
12
10
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
13
+
13
14
/**
14
15
* A client to a StatsD server.
15
16
*/
17
+ @ NotThreadSafe
16
18
public class StatsD implements Closeable {
17
19
18
- private static final Logger LOG = LoggerFactory .getLogger (StatsD .class );
19
-
20
- private static final Pattern WHITESPACE = Pattern .compile ("[\\ s]+" );
21
-
22
- private static final Charset UTF_8 = Charset .forName ("UTF-8" );
23
-
24
- private final InetSocketAddress address ;
25
- private final DatagramSocketFactory socketFactory ;
26
-
27
- private DatagramSocket socket ;
28
- private int failures ;
29
-
30
- /**
31
- * Creates a new client which connects to the given address using the default
32
- * {@link DatagramSocketFactory}.
33
- *
34
- * @param address the address of the StatsD server
35
- */
36
- public StatsD (InetSocketAddress address ) {
37
- this (address , new DatagramSocketFactory ());
38
- }
39
-
40
- /**
41
- * Creates a new client which connects to the given address and socket factory.
42
- *
43
- * @param address the address of the Carbon server
44
- * @param socketFactory the socket factory
45
- */
46
- public StatsD (InetSocketAddress address , DatagramSocketFactory socketFactory ) {
47
- this .address = address ;
48
- this .socketFactory = socketFactory ;
49
- }
50
-
51
- /**
52
- * Connects to the server.
53
- *
54
- * @throws IllegalStateException if the client is already connected
55
- * @throws IOException if there is an error connecting
56
- */
57
- public void connect () throws IllegalStateException , IOException {
58
- if (socket != null ) {
59
- throw new IllegalStateException ("Already connected" );
60
- }
61
-
62
- this .socket = socketFactory .createSocket ();
63
- }
64
-
65
- /**
66
- * Sends the given measurement to the server.
67
- *
68
- * @param name the name of the metric
69
- * @param value the value of the metric
70
- * @throws IOException if there was an error sending the metric
71
- */
72
- public void send (String name , String value ) throws IOException {
73
- try {
74
- String formatted = String .format ("%s:%s|g" , sanitize (name ), sanitize (value ));
75
- byte [] bytes = formatted .getBytes (UTF_8 );
76
- socket .send (socketFactory .createPacket (bytes , bytes .length , address ));
77
- failures = 0 ;
78
- } catch (IOException e ) {
79
- failures ++;
80
-
81
- if (failures == 1 ) {
82
- LOG .warn ("unable to send packet to statsd at '{}:{}'" , address .getHostName (), address .getPort ());
83
- } else {
84
- LOG .debug ("unable to send packet to statsd at '{}:{}'" , address .getHostName (), address .getPort ());
85
- }
86
-
87
- throw e ;
88
- }
89
- }
90
-
91
- /**
92
- * Returns the number of failed writes to the server.
93
- *
94
- * @return the number of failed writes to the server
95
- */
96
- public int getFailures () {
97
- return failures ;
98
- }
99
-
100
- @ Override
101
- public void close () throws IOException {
102
- if (socket != null ) {
103
- socket .close ();
104
- }
105
- this .socket = null ;
106
- }
107
-
108
- protected String sanitize (String s ) {
109
- return WHITESPACE .matcher (s ).replaceAll ("-" );
110
- }
111
- }
20
+ private static final Logger LOG = LoggerFactory .getLogger (StatsD .class );
21
+
22
+ private static final Pattern WHITESPACE = Pattern .compile ("[\\ s]+" );
23
+
24
+ private static final Charset UTF_8 = Charset .forName ("UTF-8" );
25
+
26
+ private final InetSocketAddress address ;
27
+ private final DatagramSocketFactory socketFactory ;
28
+
29
+ private DatagramSocket socket ;
30
+ private int failures ;
31
+
32
+ /**
33
+ * Creates a new client which connects to the given address using the default
34
+ * {@link DatagramSocketFactory}.
35
+ *
36
+ * @param address the address of the StatsD server
37
+ */
38
+ public StatsD (final InetSocketAddress address ) {
39
+ this (address , new DatagramSocketFactory ());
40
+ }
41
+
42
+ /**
43
+ * Creates a new client which connects to the given address and socket factory.
44
+ *
45
+ * @param address the address of the Carbon server
46
+ * @param socketFactory the socket factory
47
+ */
48
+ public StatsD (final InetSocketAddress address , final DatagramSocketFactory socketFactory ) {
49
+ this .address = address ;
50
+ this .socketFactory = socketFactory ;
51
+ }
52
+
53
+ /**
54
+ * Connects to the server.
55
+ *
56
+ * @throws IllegalStateException if the client is already connected
57
+ * @throws IOException if there is an error connecting
58
+ */
59
+ public void connect () throws IOException {
60
+ if (socket != null ) {
61
+ throw new IllegalStateException ("Already connected" );
62
+ }
63
+
64
+ this .socket = socketFactory .createSocket ();
65
+ }
66
+
67
+ /**
68
+ * Sends the given measurement to the server. Logs exceptions.
69
+ *
70
+ * @param name the name of the metric
71
+ * @param value the value of the metric
72
+ */
73
+ public void send (final String name , final String value ) {
74
+ try {
75
+ String formatted = String .format ("%s:%s|g" , sanitize (name ), sanitize (value ));
76
+ byte [] bytes = formatted .getBytes (UTF_8 );
77
+ socket .send (socketFactory .createPacket (bytes , bytes .length , address ));
78
+ failures = 0 ;
79
+ } catch (IOException e ) {
80
+ failures ++;
81
+
82
+ if (failures == 1 ) {
83
+ LOG .warn ("unable to send packet to statsd at '{}:{}'" , address .getHostName (), address .getPort ());
84
+ } else {
85
+ LOG .debug ("unable to send packet to statsd at '{}:{}'" , address .getHostName (), address .getPort ());
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Returns the number of failed writes to the server.
92
+ *
93
+ * @return the number of failed writes to the server
94
+ */
95
+ public int getFailures () {
96
+ return failures ;
97
+ }
98
+
99
+ @ Override
100
+ public void close () throws IOException {
101
+ if (socket != null ) {
102
+ socket .close ();
103
+ }
104
+ this .socket = null ;
105
+ }
106
+
107
+ private String sanitize (final String s ) {
108
+ return WHITESPACE .matcher (s ).replaceAll ("-" );
109
+ }
110
+ }
0 commit comments