|
| 1 | +package com.wavefront.integrations; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | + |
| 5 | +import javax.annotation.Nullable; |
| 6 | +import javax.net.SocketFactory; |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStreamWriter; |
| 10 | +import java.io.Writer; |
| 11 | +import java.net.InetAddress; |
| 12 | +import java.net.InetSocketAddress; |
| 13 | +import java.net.Socket; |
| 14 | +import java.net.UnknownHostException; |
| 15 | +import java.nio.charset.Charset; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | +import java.util.regex.Pattern; |
| 19 | + |
| 20 | +/** |
| 21 | + * Wavefront Client that sends data directly via TCP to the Wavefront Proxy Agent. |
| 22 | + * |
| 23 | + |
| 24 | + |
| 25 | + */ |
| 26 | +public class Wavefront implements WavefrontSender { |
| 27 | + |
| 28 | + private static final Pattern WHITESPACE = Pattern.compile("[\\s]+"); |
| 29 | + // this may be optimistic about Carbon/Wavefront |
| 30 | + private static final Charset UTF_8 = Charset.forName("UTF-8"); |
| 31 | + |
| 32 | + private final InetSocketAddress address; |
| 33 | + private final SocketFactory socketFactory; |
| 34 | + |
| 35 | + private Socket socket; |
| 36 | + private Writer writer; |
| 37 | + private AtomicInteger failures = new AtomicInteger(); |
| 38 | + /** |
| 39 | + * Source to use if there's none. |
| 40 | + */ |
| 41 | + private String source; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new client which connects to the given address using the default |
| 45 | + * {@link SocketFactory}. |
| 46 | + * |
| 47 | + * @param agentHostName The hostname of the Wavefront Proxy Agent |
| 48 | + * @param port The port of the Wavefront Proxy Agent |
| 49 | + */ |
| 50 | + public Wavefront(String agentHostName, int port) { |
| 51 | + this(agentHostName, port, SocketFactory.getDefault()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a new client which connects to the given address and socket factory. |
| 56 | + * |
| 57 | + * @param agentHostName The hostname of the Wavefront Proxy Agent |
| 58 | + * @param port The port of the Wavefront Proxy Agent |
| 59 | + * @param socketFactory the socket factory |
| 60 | + */ |
| 61 | + public Wavefront(String agentHostName, int port, SocketFactory socketFactory) { |
| 62 | + this(new InetSocketAddress(agentHostName, port), socketFactory); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new client which connects to the given address using the default |
| 67 | + * {@link SocketFactory}. |
| 68 | + * |
| 69 | + * @param agentAddress the address of the Wavefront Proxy Agent |
| 70 | + */ |
| 71 | + public Wavefront(InetSocketAddress agentAddress) { |
| 72 | + this(agentAddress, SocketFactory.getDefault()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a new client which connects to the given address and socket factory using the given |
| 77 | + * character set. |
| 78 | + * |
| 79 | + * @param agentAddress the address of the Wavefront Proxy Agent |
| 80 | + * @param socketFactory the socket factory |
| 81 | + */ |
| 82 | + public Wavefront(InetSocketAddress agentAddress, SocketFactory socketFactory) { |
| 83 | + this.address = agentAddress; |
| 84 | + this.socketFactory = socketFactory; |
| 85 | + } |
| 86 | + |
| 87 | + private void initializeSource() throws UnknownHostException { |
| 88 | + if (source == null) { |
| 89 | + source = InetAddress.getLocalHost().getHostName(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public synchronized void connect() throws IllegalStateException, IOException { |
| 95 | + if (socket != null) { |
| 96 | + throw new IllegalStateException("Already connected"); |
| 97 | + } |
| 98 | + InetSocketAddress address = this.address; |
| 99 | + if (address.getAddress() == null) { |
| 100 | + throw new UnknownHostException(address.getHostName()); |
| 101 | + } |
| 102 | + |
| 103 | + this.socket = socketFactory.createSocket(address.getAddress(), address.getPort()); |
| 104 | + this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8)); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void send(String name, double value) throws IOException { |
| 109 | + initializeSource(); |
| 110 | + internalSend(name, value, null, source, null); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void send(String name, double value, @Nullable Long timestamp) throws IOException { |
| 115 | + initializeSource(); |
| 116 | + internalSend(name, value, null, source, null); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void send(String name, double value, @Nullable Long timestamp, String source) throws IOException { |
| 121 | + internalSend(name, value, timestamp, source, null); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void send(String name, double value, @Nullable Long timestamp, String source, |
| 126 | + @Nullable Map<String, String> pointTags) throws IOException { |
| 127 | + internalSend(name, value, timestamp, source, pointTags); |
| 128 | + } |
| 129 | + |
| 130 | + public boolean isConnected() { |
| 131 | + return socket != null && socket.isConnected() && !socket.isClosed(); |
| 132 | + } |
| 133 | + |
| 134 | + private void internalSend(String name, double value, @Nullable Long timestamp, String source, |
| 135 | + @Nullable Map<String, String> pointTags) throws IOException { |
| 136 | + if (!isConnected()) { |
| 137 | + try { |
| 138 | + connect(); |
| 139 | + } catch (IllegalStateException ex) { |
| 140 | + // already connected. |
| 141 | + } |
| 142 | + } |
| 143 | + if (StringUtils.isBlank(name)) { |
| 144 | + throw new IllegalArgumentException("metric name cannot be blank"); |
| 145 | + } |
| 146 | + if (StringUtils.isBlank(source)) { |
| 147 | + throw new IllegalArgumentException("source cannot be blank"); |
| 148 | + } |
| 149 | + try { |
| 150 | + writer.write(sanitize(name)); |
| 151 | + writer.write(' '); |
| 152 | + writer.write(Double.toString(value)); |
| 153 | + if (timestamp != null) { |
| 154 | + writer.write(' '); |
| 155 | + writer.write(Long.toString(timestamp)); |
| 156 | + } |
| 157 | + writer.write(" host="); |
| 158 | + writer.write(sanitize(source)); |
| 159 | + if (pointTags != null) { |
| 160 | + for (Map.Entry<String, String> tag : pointTags.entrySet()) { |
| 161 | + if (StringUtils.isBlank(tag.getKey())) { |
| 162 | + throw new IllegalArgumentException("point tag key cannot be blank"); |
| 163 | + } |
| 164 | + if (StringUtils.isBlank(tag.getValue())) { |
| 165 | + throw new IllegalArgumentException("point tag value cannot be blank"); |
| 166 | + } |
| 167 | + writer.write(sanitize(tag.getKey())); |
| 168 | + writer.write("="); |
| 169 | + writer.write(sanitize(tag.getValue())); |
| 170 | + } |
| 171 | + } |
| 172 | + writer.write('\n'); |
| 173 | + } catch (IOException e) { |
| 174 | + failures.incrementAndGet(); |
| 175 | + throw e; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int getFailureCount() { |
| 181 | + return failures.get(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void flush() throws IOException { |
| 186 | + if (writer != null) { |
| 187 | + writer.flush(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public synchronized void close() throws IOException { |
| 193 | + flush(); |
| 194 | + if (socket != null) { |
| 195 | + socket.close(); |
| 196 | + } |
| 197 | + this.socket = null; |
| 198 | + this.writer = null; |
| 199 | + } |
| 200 | + |
| 201 | + static String sanitize(String s) { |
| 202 | + final String whitespaceSanitized = WHITESPACE.matcher(s).replaceAll("-"); |
| 203 | + if (s.contains("\"") || s.contains("'")) { |
| 204 | + // for single quotes, once we are double-quoted, single quotes can exist happily inside it. |
| 205 | + return "\"" + whitespaceSanitized.replaceAll("\"", "\\\\\"") + "\""; |
| 206 | + } else { |
| 207 | + return whitespaceSanitized; |
| 208 | + } |
| 209 | + } |
| 210 | +} |