Skip to content

Commit

Permalink
test jmx retry
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Feb 8, 2024
1 parent c80c23d commit bac2d6b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,24 @@ public void onChange(ConfigurationOption<?> configurationOption, List<JmxMetric>
retryExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
List<JmxMetric> failed = JmxMetricTracker.this.failedMetrics;
synchronized (failed) {
List<JmxMetric> toRetry = new ArrayList<>(failed);
failed.clear();
register(toRetry, platformMBeanServer, failed);
}
retryFailedJmx(platformMBeanServer);
}
}, retryMillis, retryMillis, TimeUnit.MILLISECONDS);
}

register(jmxConfiguration.getCaptureJmxMetrics().get(), platformMBeanServer, failedMetrics);
}

// package-private for testing
void retryFailedJmx(MBeanServer platformMBeanServer) {
List<JmxMetric> failed = JmxMetricTracker.this.failedMetrics;
synchronized (failed) {
List<JmxMetric> toRetry = new ArrayList<>(failed);
failed.clear();
register(toRetry, platformMBeanServer, failed);
}
}

private void registerMBeanNotificationListener(final MBeanServer server) {
MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
filter.enableAllObjectNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,53 @@ void testMBeanUnregister() throws Exception {
assertThat(metricRegistry.getGauge("jvm.jmx.Baz", labels)).isNull();
}

@Test
void testMBeanExceptionWhenRegisteredThenOk() throws Exception {
ObjectName objectName = new ObjectName("foo:type=Foo,name=testMBeanExceptionWhenRegisteredThenOk");
TestMetric testMetric = new TestMetric();
testMetric.setValue(-1);
assertThatThrownBy(testMetric::getBaz).isInstanceOf(RuntimeException.class);

setConfig(JmxMetric.valueOf("object_name[foo:type=Foo,name=*] attribute[Baz]"));

registerMBean(testMetric, objectName);

Labels labels = Labels.Mutable.of("name", "testMBeanExceptionWhenRegisteredThenOk").add("type", "Foo");
assertThat(metricRegistry.getGaugeValue("jvm.jmx.Baz", labels))
.isNaN();
assertThat(metricRegistry.getGauge("jvm.jmx.Baz", labels))
.isNull();

testMetric.setValue(37);
// calling directly the JMX tracker to avoid testing async execution
jmxTracker.retryFailedJmx(mbeanServer);

assertThat(metricRegistry.getGaugeValue("jvm.jmx.Baz", labels))
.isEqualTo(37);
}

public interface TestMetricMBean {
int getBaz();
}

public static class TestMetric implements TestMetricMBean {

private int value;

public TestMetric() {
this.value = 42;
}

void setValue(int value) {
this.value = value;
}

@Override
public int getBaz() {
return 42;
if (value < 0) {
throw new RuntimeException("value less than zero");
}
return value;
}
}

Expand Down

0 comments on commit bac2d6b

Please sign in to comment.