GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — flatten submodules · aalapdesai112/java-1@212fe4a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • java-client
    • pom.xml
    • src
      • main/java/com/wavefront/integrations
        • Wavefront.java
        • WavefrontSender.java
      • test/java/com/wavefront/integrations
        • WavefrontTest.java
  • java-lib
    • pom.xml
    • src
      • main
        • antlr4
          • DSLexer.g4
          • DSWrapper.g4
        • avro
          • Events.avdl
          • Reporting.avdl
        • java/com/wavefront
          • api
            • AgentAPI.java
            • agent
              • AgentConfiguration.java
              • Constants.java
              • MetricStage.java
              • ShellOutputDTO.java
              • SshTargetDTO.java
              • WorkUnit.java
            • json
              • InstantMarshaller.java
          • common
            • Pair.java
            • TaggedMetricName.java
          • data
            • Idempotent.java
          • ingester/graphite
            • GraphiteDecoder.java
            • GraphiteHostAnnotator.java
            • GraphiteIngester.java
          • metrics
            • ExpectedAgentMetric.java
            • JsonMetricsGenerator.java
            • JsonMetricsParser.java
            • JsonMetricsReporter.java
        • resources/wavefront/templates
          • enum.vm
          • fixed.vm
          • protocol.vm
          • record.vm
      • test/java/com/wavefront/ingester/graphite
        • GraphiteDecoderTest.java
  • proxy
    • docker
      • Dockerfile
      • README
      • run.sh
    • pom.xml
    • src
      • assembly.xml
      • main
        • java/com/wavefront/agent
          • AbstractAgent.java
          • Clock.java
          • GraphiteStringHandler.java
          • JsonMetricsEndpoint.java
          • JsonNodeWriter.java
          • PointHandler.java
          • PostPushDataTimedTask.java
          • PushAgent.java
          • QueuedAgentService.java
          • QueuedPushTooLargeException.java
          • ResubmissionTask.java
          • ResubmissionTaskDeserializer.java
          • SSLSocketFactoryImpl.java
          • api
            • ForceQueueEnabledAgentAPI.java
          • formatter
            • Formatter.java
            • GraphiteFormatter.java
        • resources
          • build
            • build.properties
          • log4j-detailed.xml
          • log4j.xml
      • test/java/com/wavefront/agent
        • GraphiteStringHandlerTest.java
        • QueuedAgentServiceTest.java

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎java-client/pom.xml‎

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>java-client</artifactId>
7+
<name>Wavefront Java Integrations Core Library</name>
8+
<description>Java client for sending data to Wavefront</description>
9+
10+
<parent>
11+
<groupId>com.wavefront</groupId>
12+
<artifactId>wavefront</artifactId>
13+
<version>3.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<developers>
17+
<developer>
18+
<name>Clement Pang</name>
19+
<email>[email protected]</email>
20+
<organization>Wavefront</organization>
21+
<organizationUrl>http://www.wavefront.com</organizationUrl>
22+
</developer>
23+
<developer>
24+
<name>Conor Beverland</name>
25+
<email>[email protected]</email>
26+
<organization>Wavefront</organization>
27+
<organizationUrl>http://www.wavefront.com</organizationUrl>
28+
</developer>
29+
</developers>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.google.code.findbugs</groupId>
34+
<artifactId>jsr305</artifactId>
35+
<version>3.0.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>commons-lang</groupId>
39+
<artifactId>commons-lang</artifactId>
40+
<version>2.6</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>junit</groupId>
44+
<artifactId>junit</artifactId>
45+
<version>4.11</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
</project>
Collapse file

‎java-client/src/main/java/com/wavefront/integrations/Wavefront.java‎

Copy file name to clipboard
+210Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
* @author Clement Pang ([email protected]).
24+
* @author Conor Beverland ([email protected]).
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+
}
Collapse file

‎java-client/src/main/java/com/wavefront/integrations/WavefrontSender.java‎

Copy file name to clipboard
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.wavefront.integrations;
2+
3+
import javax.annotation.Nullable;
4+
import java.io.Closeable;
5+
import java.io.IOException;
6+
import java.net.UnknownHostException;
7+
import java.util.Map;
8+
9+
/**
10+
* Wavefront Client that sends data directly via TCP to the Wavefront Proxy Agent.
11+
*
12+
* @author Clement Pang ([email protected]).
13+
* @author Conor Beverland ([email protected]).
14+
*/
15+
public interface WavefrontSender extends Closeable {
16+
17+
/**
18+
* Connects to the server.
19+
*
20+
* @throws IllegalStateException if the client is already connected
21+
* @throws IOException if there is an error connecting
22+
*/
23+
void connect() throws IllegalStateException, IOException;
24+
25+
/**
26+
* Send a measurement to Wavefront. The current machine's hostname would be used as the source. The point will be
27+
* timestamped at the agent.
28+
*
29+
* @param name The name of the metric. Spaces are replaced with '-' (dashes) and quotes will be automatically
30+
* escaped.
31+
* @param value The value to be sent.
32+
* @throws IOException Throws if there was an error sending the metric.
33+
* @throws UnknownHostException Throws if there's an error determining the current host.
34+
*/
35+
void send(String name, double value) throws IOException;
36+
37+
/**
38+
* Send a measurement to Wavefront. The current machine's hostname would be used as the source.
39+
*
40+
* @param name The name of the metric. Spaces are replaced with '-' (dashes) and quotes will be automatically
41+
* escaped.
42+
* @param value The value to be sent.
43+
* @param timestamp The timestamp in seconds since the epoch to be sent.
44+
* @throws IOException Throws if there was an error sending the metric.
45+
* @throws UnknownHostException Throws if there's an error determining the current host.
46+
*/
47+
void send(String name, double value, @Nullable Long timestamp) throws IOException;
48+
49+
/**
50+
* Send a measurement to Wavefront.
51+
*
52+
* @param name The name of the metric. Spaces are replaced with '-' (dashes) and quotes will be automatically
53+
* escaped.
54+
* @param value The value to be sent.
55+
* @param timestamp The timestamp in seconds since the epoch to be sent.
56+
* @param source The source (or host) that's sending the metric.
57+
* @throws IOException if there was an error sending the metric.
58+
*/
59+
void send(String name, double value, @Nullable Long timestamp, String source) throws IOException;
60+
61+
/**
62+
* Send the given measurement to the server.
63+
*
64+
* @param name The name of the metric. Spaces are replaced with '-' (dashes) and quotes will be automatically
65+
* escaped.
66+
* @param value The value to be sent.
67+
* @param timestamp The timestamp in seconds since the epoch to be sent. Null to use agent assigned timestamp.
68+
* @param source The source (or host) that's sending the metric. Null to use machine hostname.
69+
* @param pointTags The point tags associated with this measurement.
70+
* @throws IOException if there was an error sending the metric.
71+
*/
72+
void send(String name, double value, @Nullable Long timestamp, String source,
73+
@Nullable Map<String, String> pointTags) throws IOException;
74+
75+
/**
76+
* Flushes buffer, if applicable
77+
*
78+
* @throws IOException
79+
*/
80+
void flush() throws IOException;
81+
82+
/**
83+
* Returns true if ready to send data
84+
*/
85+
boolean isConnected();
86+
87+
/**
88+
* Returns the number of failed writes to the server.
89+
*
90+
* @return the number of failed writes to the server
91+
*/
92+
int getFailureCount();
93+
}
Collapse file

‎java-client/src/test/java/com/wavefront/integrations/WavefrontTest.java‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.wavefront.integrations;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Tests for {@link Wavefront}
9+
*
10+
* @author Clement Pang ([email protected]).
11+
*/
12+
public class WavefrontTest {
13+
14+
@Test
15+
public void testSanitize() {
16+
assertEquals("hello", Wavefront.sanitize("hello"));
17+
assertEquals("hello-world", Wavefront.sanitize("hello world"));
18+
assertEquals("hello.world", Wavefront.sanitize("hello.world"));
19+
assertEquals("\"hello\\\"world\\\"\"", Wavefront.sanitize("hello\"world\""));
20+
assertEquals("\"hello'world\"", Wavefront.sanitize("hello'world"));
21+
}
22+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.