-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/net/thisptr/jmx/exporter/agent/handler/jq/JsonQueryScriptEngineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |