Skip to content

Commit

Permalink
add integration tests for metricCustomizers
Browse files Browse the repository at this point in the history
Co-authored-by: Karina Calma <[email protected]>
Signed-off-by: Martin Bickel <[email protected]>
  • Loading branch information
2 people authored and Martin Bickel committed Jan 17, 2025
1 parent 3c77dce commit b3a8404
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 0 deletions.
58 changes: 58 additions & 0 deletions collector/src/test/java/io/prometheus/jmx/CustomValueMBean.java
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);
}
}
16 changes: 16 additions & 0 deletions collector/src/test/java/io/prometheus/jmx/JmxCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static void classSetUp() throws Exception {
TomcatServlet.registerBean(mbs);
Bool.registerBean(mbs);
Camel.registerBean(mbs);
CustomValue.registerBean(mbs);
}

@Before
Expand Down Expand Up @@ -163,6 +164,21 @@ public void testNameAndLabelSanitized() throws Exception {
.001);
}

@Test
public void testMetricCustomizers() throws Exception {
new JmxCollector(
"\n---\nincludeObjectNames: [`io.prometheus.jmx:type=customValue`]\nmetricCustomizers:\n - mbeanFilter:\n domain: io.prometheus.jmx\n properties:\n type: customValue\n attributesAsLabels:\n - Text"
.replace('`', '"'))
.register(prometheusRegistry);
assertEquals(
345,
getSampleValue(
"io_prometheus_jmx_customValue_Value",
new String[] {"Text"},
new String[] {"value"}),
.001);
}

/*
@Test
public void testHelpFromPattern() throws Exception {
Expand Down
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"));
}
}
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
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
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
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
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
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static void main(String[] args) throws Exception {
new PerformanceMetrics(),
new ObjectName("io.prometheus.jmx.test:name=PerformanceMetricsMBean"));

mBeanServer.registerMBean(
new CustomValue(), new ObjectName("io.prometheus.jmx:type=customValue"));

System.out.printf(
"%s | %s | INFO | %s | %s%n",
LocalDateTime.now().format(DATE_TIME_FORMATTER),
Expand Down

0 comments on commit b3a8404

Please sign in to comment.