Skip to content

Commit

Permalink
Merge branch 'release-0.0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
eiiches committed Jul 24, 2020
2 parents 422ece9 + d26fc81 commit 888fd69
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Installation

#### Download from the Maven Central (Recommended)

[https://repo1.maven.org/maven2/net/thisptr/scriptable-jmx-exporter/0.0.6/scriptable-jmx-exporter-0.0.6.jar](https://repo1.maven.org/maven2/net/thisptr/scriptable-jmx-exporter/0.0.6/scriptable-jmx-exporter-0.0.6.jar)
[https://repo1.maven.org/maven2/net/thisptr/scriptable-jmx-exporter/0.0.7/scriptable-jmx-exporter-0.0.7.jar](https://repo1.maven.org/maven2/net/thisptr/scriptable-jmx-exporter/0.0.7/scriptable-jmx-exporter-0.0.7.jar)

#### Building from source

Expand Down Expand Up @@ -393,7 +393,7 @@ Please also keep in mind that performance is highly dependent on the configurati
See [examples/benchmark-kafka](examples/benchmark-kafka) for the setup details. Here's the results:
| Exporter | # of metrics | Throughput [req/s] |
|-|-|
|-|-|-|
| scriptable-jmx-exporter (*1) | 5254 | 552.03 |
| jmx_exporter 0.13.0 | 3157 (*2) | 12.14 |
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

<groupId>net.thisptr</groupId>
<artifactId>scriptable-jmx-exporter</artifactId>
<name>Prometheus Metrics Agent</name>
<name>Scriptable JMX Exporter</name>
<packaging>jar</packaging>
<version>0.0.6</version>
<description>Java agent for collecting and reporting metrics to Prometheus</description>
<version>0.0.7</version>
<description>Java agent for collecting and exposing metrics to Prometheus</description>
<url>https://github.com/eiiches/scriptable-jmx-exporter</url>

<licenses>
Expand All @@ -32,7 +32,7 @@
<connection>scm:git:git@github.com:eiiches/scriptable-jmx-exporter.git</connection>
<developerConnection>scm:git:git@github.com:eiiches/scriptable-jmx-exporter.git</developerConnection>
<url>git@github.com:eiiches/scriptable-jmx-exporter.git</url>
<tag>v0.0.6</tag>
<tag>v0.0.7</tag>
</scm>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PrometheusMetric convert(final JsonNode tree) {
m.value = value != null ? value.asDouble() : 0L;

final JsonNode timestamp = tree.get("timestamp");
m.timestamp = timestamp != null ? timestamp.asLong() : null;
m.timestamp = timestamp != null ? timestamp.asLong() : 0L;

final JsonNode labels = tree.get("labels");
if (labels != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public Script<JsonQuery> compile(final String script) throws ScriptCompileExcept
@Override
public void handle(final Sample<PrometheusScrapeRule> sample, final JsonQuery script, final PrometheusMetricOutput output) {
final JsonNode mbeanAttributeNode = SampleToJsonInputConverter.getInstance().convert(sample);
final JsonQuery transform = sample.rule != null && sample.rule.transform != null ? script : DEFAULT_TRANSFORM;

final List<JsonNode> metricNodes = new ArrayList<>();
try {
transform.apply(scope, mbeanAttributeNode, metricNodes::add);
script.apply(scope, mbeanAttributeNode, metricNodes::add);
} catch (final Throwable th) {
LOG.log(Level.INFO, "Failed to transform a MBean attribute (" + mbeanAttributeNode + ") to Prometheus metrics.", th);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.thisptr.jmx.exporter.agent.handler.jq;

import static org.assertj.core.api.Assertions.assertThat;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.junit.jupiter.api.Test;

import net.thisptr.jmx.exporter.agent.PrometheusMetric;
import net.thisptr.jmx.exporter.agent.Sample;
import net.thisptr.jmx.exporter.agent.config.Config.PrometheusScrapeRule;
import net.thisptr.jmx.exporter.agent.misc.FastObjectName;

public class JsonQueryScriptEngineTest {
private final JsonQueryScriptEngine sut = new JsonQueryScriptEngine();

private static Sample<PrometheusScrapeRule> sample(final ObjectName objectName, final String attributeName) throws MalformedObjectNameException, InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IntrospectionException {
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
final Object value = server.getAttribute(objectName, attributeName);
final long timestamp = System.currentTimeMillis();
final MBeanInfo mbeanInfo = server.getMBeanInfo(objectName);
final MBeanAttributeInfo attributeInfo = Arrays.stream(mbeanInfo.getAttributes()).filter(a -> attributeName.equals(a.getName())).findFirst().get();
return new Sample<PrometheusScrapeRule>(null, timestamp, new FastObjectName(objectName), mbeanInfo, attributeInfo, value);
}

@Test
void testSimple() throws Exception {
final Sample<PrometheusScrapeRule> sample = sample(new ObjectName("java.lang:type=OperatingSystem"), "ProcessCpuLoad");

final List<PrometheusMetric> metrics = new ArrayList<>();
sut.compile("default_transform_v1([\"type\"]; true)").execute(sample, metrics::add);

assertThat(metrics.size()).isEqualTo(1);
assertThat(metrics.get(0).value).isEqualTo((Double) sample.value);
assertThat(metrics.get(0).name).isEqualTo("java.lang:OperatingSystem:ProcessCpuLoad");
assertThat(metrics.get(0).labels).isEmpty();
}
}

0 comments on commit 888fd69

Please sign in to comment.