Skip to content

Commit

Permalink
Removed Java 6 Optional workaround (prometheus#846)
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Hoard <[email protected]>
  • Loading branch information
dhoard authored Jul 28, 2023
1 parent 77be436 commit c1615a3
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 84 deletions.
25 changes: 15 additions & 10 deletions collector/src/main/java/io/prometheus/jmx/JmxScraper.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
Expand Down Expand Up @@ -70,7 +71,6 @@ void recordBean(
private final boolean ssl;
private final List<ObjectName> whitelistObjectNames, blacklistObjectNames;
private final JmxMBeanPropertyCache jmxMBeanPropertyCache;
private final OptionalValueExtractor optionalValueExtractor = new OptionalValueExtractor();

public JmxScraper(String jmxUrl, String username, String password, boolean ssl,
List<ObjectName> whitelistObjectNames, List<ObjectName> blacklistObjectNames,
Expand Down Expand Up @@ -347,16 +347,21 @@ private void processBeanValue(
}
} else if (value.getClass().isArray()) {
logScrape(domain, "arrays are unsupported");
} else if (optionalValueExtractor.isOptional(value)) {
} else if (value instanceof Optional) {
logScrape(domain + beanProperties + attrName, "java.util.Optional");
processBeanValue(
domain,
beanProperties,
attrKeys,
attrName,
attrType,
attrDescription,
optionalValueExtractor.getOptionalValueOrNull(value));
Optional optional = (Optional) value;
if (optional.isPresent()) {
processBeanValue(
domain,
beanProperties,
attrKeys,
attrName,
attrType,
attrDescription,
optional.get());
} else {
logScrape(domain + beanProperties + attrName, "java.util.Optional is empty");
}
} else if (value.getClass().isEnum()) {
logScrape(domain + beanProperties + attrName, value.toString());
processBeanValue(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 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;

import io.prometheus.jmx.test.support.ContentConsumer;
import io.prometheus.jmx.test.support.MetricsRequest;
import io.prometheus.jmx.test.support.MetricsResponse;
import org.antublue.test.engine.api.TestEngine;

import java.util.Collection;

import static io.prometheus.jmx.test.support.RequestResponseAssertions.assertThatResponseForRequest;
import static org.assertj.core.api.Assertions.assertThat;

public class OptionalValueMBeanTest extends BaseTest {

@TestEngine.Test
public void testMetrics() {
assertThatResponseForRequest(new MetricsRequest(testState.httpClient()))
.isSuperset(MetricsResponse.RESULT_200)
.dispatch((ContentConsumer) content -> {
Collection<Metric> metrics = MetricsParser.parse(content);
metrics
.forEach(metric -> {
if (metric.getName().equals("io_prometheus_jmx_optionalValue_Value")) {
assertThat(metric.getValue()).isEqualTo(345.0);
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

java \
-Xmx128M \
-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,2 @@
rules:
- pattern: ".*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

java \
-Xmx128M \
-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,5 @@
#!/bin/bash

java \
-Xmx128M \
-jar jmx_prometheus_httpserver.jar 8888 exporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hostPort: application:9999
rules:
- pattern: ".*"
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static void main(String[] args) throws Exception {

ExistDb.registerBean(server);

ObjectName optionalValueMBean = new ObjectName("io.prometheus.jmx:type=optionalValue");
server.registerMBean(new OptionalValue(), optionalValueMBean);

System.out.println(
String.format("%s | %s | INFO | %s | %s",
SIMPLE_DATE_FORMAT.format(new Date()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2023 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 java.util.Optional;

public interface OptionalValueMBean {

Optional<Integer> getValue();
}

class OptionalValue implements OptionalValueMBean {

private final Integer VALUE = 345;

public OptionalValue() {
// DO NOTHING
}

@Override
public Optional<Integer> getValue() {
return Optional.of(VALUE);
}
}

0 comments on commit c1615a3

Please sign in to comment.