-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add integration tests for metricCustomizers
Co-authored-by: Karina Calma <[email protected]> Signed-off-by: Martin Bickel <[email protected]>
- Loading branch information
1 parent
3c77dce
commit b3a8404
Showing
10 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
collector/src/test/java/io/prometheus/jmx/CustomValueMBean.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,58 @@ | ||
/* | ||
* Copyright (C) 2023-present The Prometheus jmx_exporter Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prometheus.jmx; | ||
|
||
import javax.management.MBeanServer; | ||
import javax.management.ObjectName; | ||
|
||
/** Class to implement CustomValueMBean */ | ||
public interface CustomValueMBean { | ||
|
||
/** | ||
* Method to get the value | ||
* | ||
* @return value | ||
*/ | ||
Integer getValue(); | ||
|
||
/** | ||
* Method to get the text | ||
* | ||
* @return text | ||
*/ | ||
String getText(); | ||
} | ||
|
||
/** Class to implement CustomValue */ | ||
class CustomValue implements CustomValueMBean { | ||
|
||
@Override | ||
public Integer getValue() { | ||
return 345; | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
return "value"; | ||
} | ||
|
||
public static void registerBean(MBeanServer mbs) throws javax.management.JMException { | ||
ObjectName mbeanName = | ||
new ObjectName("io.prometheus.jmx:type=customValue"); | ||
CustomValueMBean mbean = new CustomValue(); | ||
mbs.registerMBean(mbean, mbeanName); | ||
} | ||
} |
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
162 changes: 162 additions & 0 deletions
162
...te/integration_tests/src/test/java/io/prometheus/jmx/test/core/MetricCustomizersTest.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,162 @@ | ||
/* | ||
* Copyright (C) 2023-present The Prometheus jmx_exporter Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.prometheus.jmx.test.core; | ||
|
||
import static io.prometheus.jmx.test.support.Assertions.assertCommonMetricsResponse; | ||
import static io.prometheus.jmx.test.support.Assertions.assertHealthyResponse; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.prometheus.jmx.test.support.ExporterPath; | ||
import io.prometheus.jmx.test.support.ExporterTestEnvironment; | ||
import io.prometheus.jmx.test.support.TestSupport; | ||
import io.prometheus.jmx.test.support.http.HttpClient; | ||
import io.prometheus.jmx.test.support.http.HttpHeader; | ||
import io.prometheus.jmx.test.support.http.HttpResponse; | ||
import io.prometheus.jmx.test.support.metrics.Metric; | ||
import io.prometheus.jmx.test.support.metrics.MetricsContentType; | ||
import io.prometheus.jmx.test.support.metrics.MetricsParser; | ||
import java.io.IOException; | ||
import java.util.*; | ||
import java.util.stream.Stream; | ||
import org.testcontainers.containers.Network; | ||
import org.verifyica.api.ArgumentContext; | ||
import org.verifyica.api.ClassContext; | ||
import org.verifyica.api.Trap; | ||
import org.verifyica.api.Verifyica; | ||
|
||
public class MetricCustomizersTest { | ||
|
||
@Verifyica.ArgumentSupplier(parallelism = Integer.MAX_VALUE) | ||
public static Stream<ExporterTestEnvironment> arguments() { | ||
return ExporterTestEnvironment.createExporterTestEnvironments(); | ||
} | ||
|
||
@Verifyica.Prepare | ||
public static void prepare(ClassContext classContext) { | ||
TestSupport.getOrCreateNetwork(classContext); | ||
} | ||
|
||
@Verifyica.BeforeAll | ||
public void beforeAll(ArgumentContext argumentContext) { | ||
Class<?> testClass = argumentContext.classContext().testClass(); | ||
Network network = TestSupport.getOrCreateNetwork(argumentContext); | ||
TestSupport.initializeExporterTestEnvironment(argumentContext, network, testClass); | ||
} | ||
|
||
@Verifyica.Test | ||
@Verifyica.Order(1) | ||
public void testHealthy(ExporterTestEnvironment exporterTestEnvironment) throws IOException { | ||
String url = exporterTestEnvironment.getUrl(ExporterPath.HEALTHY); | ||
|
||
HttpResponse httpResponse = HttpClient.sendRequest(url); | ||
|
||
assertHealthyResponse(httpResponse); | ||
} | ||
|
||
@Verifyica.Test | ||
public void testDefaultTextMetrics(ExporterTestEnvironment exporterTestEnvironment) | ||
throws IOException { | ||
String url = exporterTestEnvironment.getUrl(ExporterPath.METRICS); | ||
|
||
HttpResponse httpResponse = HttpClient.sendRequest(url); | ||
|
||
assertMetricsResponse(exporterTestEnvironment, httpResponse, MetricsContentType.DEFAULT); | ||
} | ||
|
||
@Verifyica.Test | ||
public void testOpenMetricsTextMetrics(ExporterTestEnvironment exporterTestEnvironment) | ||
throws IOException { | ||
String url = exporterTestEnvironment.getUrl(ExporterPath.METRICS); | ||
|
||
HttpResponse httpResponse = | ||
HttpClient.sendRequest( | ||
url, | ||
HttpHeader.ACCEPT, | ||
MetricsContentType.OPEN_METRICS_TEXT_METRICS.toString()); | ||
|
||
assertMetricsResponse( | ||
exporterTestEnvironment, | ||
httpResponse, | ||
MetricsContentType.OPEN_METRICS_TEXT_METRICS); | ||
} | ||
|
||
@Verifyica.Test | ||
public void testPrometheusTextMetrics(ExporterTestEnvironment exporterTestEnvironment) | ||
throws IOException { | ||
String url = exporterTestEnvironment.getUrl(ExporterPath.METRICS); | ||
|
||
HttpResponse httpResponse = | ||
HttpClient.sendRequest( | ||
url, | ||
HttpHeader.ACCEPT, | ||
MetricsContentType.PROMETHEUS_TEXT_METRICS.toString()); | ||
|
||
assertMetricsResponse( | ||
exporterTestEnvironment, httpResponse, MetricsContentType.PROMETHEUS_TEXT_METRICS); | ||
} | ||
|
||
@Verifyica.Test | ||
public void testPrometheusProtobufMetrics(ExporterTestEnvironment exporterTestEnvironment) | ||
throws IOException { | ||
String url = exporterTestEnvironment.getUrl(ExporterPath.METRICS); | ||
|
||
HttpResponse httpResponse = | ||
HttpClient.sendRequest( | ||
url, | ||
HttpHeader.ACCEPT, | ||
MetricsContentType.PROMETHEUS_PROTOBUF_METRICS.toString()); | ||
|
||
assertMetricsResponse( | ||
exporterTestEnvironment, | ||
httpResponse, | ||
MetricsContentType.PROMETHEUS_PROTOBUF_METRICS); | ||
} | ||
|
||
@Verifyica.AfterAll | ||
public void afterAll(ArgumentContext argumentContext) throws Throwable { | ||
List<Trap> traps = new ArrayList<>(); | ||
|
||
traps.add(new Trap(() -> TestSupport.destroyExporterTestEnvironment(argumentContext))); | ||
traps.add(new Trap(() -> TestSupport.destroyNetwork(argumentContext))); | ||
|
||
Trap.assertEmpty(traps); | ||
} | ||
|
||
@Verifyica.Conclude | ||
public static void conclude(ClassContext classContext) throws Throwable { | ||
new Trap(() -> TestSupport.destroyNetwork(classContext)).assertEmpty(); | ||
} | ||
|
||
private void assertMetricsResponse( | ||
ExporterTestEnvironment exporterTestEnvironment, | ||
HttpResponse httpResponse, | ||
MetricsContentType metricsContentType) { | ||
assertCommonMetricsResponse(httpResponse, metricsContentType); | ||
|
||
Collection<Metric> metrics = MetricsParser.parseCollection(httpResponse); | ||
|
||
/* | ||
* Assert that a certain metric has specific labels | ||
*/ | ||
metrics.stream() | ||
.filter(metric -> metric.name().contains("io_prometheus_jmx_customValue_Value")) | ||
.forEach( | ||
metric -> | ||
assertThat(metric.labels()) | ||
.containsEntry("Text", "value")); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...test/resources/io/prometheus/jmx/test/core/MetricCustomizersTest/JavaAgent/application.sh
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,6 @@ | ||
#!/bin/bash | ||
|
||
java \ | ||
-Xmx512M \ | ||
-javaagent:jmx_prometheus_javaagent.jar=8888:exporter.yaml \ | ||
-jar jmx_example_application.jar |
8 changes: 8 additions & 0 deletions
8
.../test/resources/io/prometheus/jmx/test/core/MetricCustomizersTest/JavaAgent/exporter.yaml
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,8 @@ | ||
includeObjectNames: ["io.prometheus.jmx:type=customValue"] | ||
metricCustomizers: | ||
- mbeanFilter: | ||
domain: io.prometheus.jmx | ||
properties: | ||
type: customValue | ||
attributesAsLabels: | ||
- Text |
13 changes: 13 additions & 0 deletions
13
...est/resources/io/prometheus/jmx/test/core/MetricCustomizersTest/Standalone/application.sh
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,13 @@ | ||
#!/bin/bash | ||
|
||
java \ | ||
-Xmx512M \ | ||
-Dcom.sun.management.jmxremote=true \ | ||
-Dcom.sun.management.jmxremote.authenticate=false \ | ||
-Dcom.sun.management.jmxremote.local.only=false \ | ||
-Dcom.sun.management.jmxremote.port=9999 \ | ||
-Dcom.sun.management.jmxremote.registry.ssl=false \ | ||
-Dcom.sun.management.jmxremote.rmi.port=9999 \ | ||
-Dcom.sun.management.jmxremote.ssl.need.client.auth=false \ | ||
-Dcom.sun.management.jmxremote.ssl=false \ | ||
-jar jmx_example_application.jar |
6 changes: 6 additions & 0 deletions
6
...c/test/resources/io/prometheus/jmx/test/core/MetricCustomizersTest/Standalone/exporter.sh
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,6 @@ | ||
#!/bin/bash | ||
|
||
java \ | ||
-Xmx512M \ | ||
-javaagent:jmx_prometheus_javaagent.jar=8888:exporter.yaml \ | ||
-jar jmx_example_application.jar |
9 changes: 9 additions & 0 deletions
9
...test/resources/io/prometheus/jmx/test/core/MetricCustomizersTest/Standalone/exporter.yaml
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,9 @@ | ||
hostPort: application:9999 | ||
includeObjectNames: ["io.prometheus.jmx:type=customValue"] | ||
metricCustomizers: | ||
- mbeanFilter: | ||
domain: io.prometheus.jmx | ||
properties: | ||
type: customValue | ||
attributesAsLabels: | ||
- Text |
53 changes: 53 additions & 0 deletions
53
..._test_suite/jmx_example_application/src/main/java/io/prometheus/jmx/CustomValueMBean.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,53 @@ | ||
/* | ||
* Copyright (C) 2023-present The Prometheus jmx_exporter Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.prometheus.jmx; | ||
|
||
/** Class to implement CustomValueMBean */ | ||
public interface CustomValueMBean { | ||
|
||
/** | ||
* Method to get the value | ||
* | ||
* @return value | ||
*/ | ||
Integer getValue(); | ||
|
||
/** | ||
* Method to get the text | ||
* | ||
* @return text | ||
*/ | ||
String getText(); | ||
} | ||
|
||
/** Class to implement CustomValue */ | ||
class CustomValue implements CustomValueMBean { | ||
|
||
/** Constructor */ | ||
public CustomValue() { | ||
// INTENTIONALLY BLANK | ||
} | ||
|
||
@Override | ||
public Integer getValue() { | ||
return 345; | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
return "value"; | ||
} | ||
} |
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