Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CloudWatchOutputWriter using the v2 AWS SDK and async cloudwatch client #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ Out of the box output writers:
* `retentionPolicy`: retention policy to use - optional
* `connectTimeoutMillis`: connect timeout for the HTTP connection to influx - optional, defaults to 3000
* `readTimeoutMillis`: read timeout for the HTTP connection to influx - optional, defaults to 5000
* [CloudWatchOutputWriter](https://github.com/jmxtrans/jmxtrans-agent/blob/master/src/main/java/org/jmxtrans/agent/CloudWatchOutputWriter.java): Output to AWS CloudWatch Metrics. Credentials and region are loaded using the default region and credential providers (https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatch/CloudWatchClient.html#create--). Configuration parameters:
* `namespace`: Cloudwatch namespace. Default is "JMX". e.g. `JMX/Production`. - optional
* `dimensions`: additional dimensions to use for all metrics. Use `n1:v1,n2:v2` format, e.g. `<dimensions>host:#hostname#</dimensions>` - optional


Output writers configuration support an [expression language](https://github.com/jmxtrans/jmxtrans-agent/wiki/Expression-Language) based on property placeholders with the `{prop-name[:default-value]}` syntax (e.g. "`${graphite.port:2003}`").
Expand Down Expand Up @@ -365,6 +368,13 @@ tomcat.bytesReceived 0
application.activeSessions 0
```

# Building

```
mvn compile
mvn package
```


# Release Notes

Expand Down
37 changes: 32 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
<url>https://github.com/jmxtrans/jmxtrans-agent</url>
<tag>HEAD</tag>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.13.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
Expand Down Expand Up @@ -90,14 +101,16 @@
<version>2.5.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatch</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
Expand All @@ -111,14 +124,28 @@
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/org/jmxtrans/agent/CloudWatchOutputWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.jmxtrans.agent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.jmxtrans.agent.util.ConfigurationUtils;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
import software.amazon.awssdk.services.cloudwatch.model.Dimension;
import software.amazon.awssdk.services.cloudwatch.model.MetricDatum;
import software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest;

public class CloudWatchOutputWriter extends AbstractOutputWriter {

private CloudWatchAsyncClient client;
private String configNamespace;
private String configDimensions;
private List<Tag> listOfDimensions;
private Collection<Dimension> dimensions;

@Override
public void postConstruct(Map<String, String> settings) {

client = CloudWatchAsyncClient.create();
configNamespace = ConfigurationUtils.getString(settings, "namespace", "JMX");
configDimensions = ConfigurationUtils.getString(settings, "dimensions", "");
listOfDimensions = Tag.tagsFromCommaSeparatedString(configDimensions);

dimensions = new ArrayList<Dimension>();

for (Tag thisDimension : listOfDimensions) {
dimensions.add(
Dimension.builder()
.name(thisDimension.getName())
.value(thisDimension.getValue())
.build());
}
}

@Override
public void writeQueryResult(String name, String type, Object value) {

Double doubleValue;

if (value instanceof Number) {
Number numberValue = (Number) value;
doubleValue = numberValue.doubleValue();
} else {
logger.log(Level.WARNING, "Cannot write result " + name + ". " + value + " is not a Number.");
return;
}

try {

MetricDatum datum =
MetricDatum.builder().metricName(name).value(doubleValue).dimensions(dimensions).build();

PutMetricDataRequest request =
PutMetricDataRequest.builder().namespace(configNamespace).metricData(datum).build();

client.putMetricData(request);

} catch (CloudWatchException e) {
logger.log(Level.SEVERE, e.awsErrorDetails().errorMessage());
}
}

@Override
public void writeInvocationResult(String invocationName, Object value) throws IOException {
writeQueryResult(invocationName, null, value);
}

@Override
public void preDestroy() {
super.preDestroy();
client.close();
}
}