Skip to content

Commit

Permalink
Adds tlm for beans registered and unregistered
Browse files Browse the repository at this point in the history
  • Loading branch information
scottopell committed Dec 27, 2023
1 parent ea0bf5b commit 82bbc94
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,11 @@ public synchronized void trackBean(ObjectName beanName) {
log.warn("Cannot get bean attributes or class name: " + e.getMessage(), e);
}
log.debug("Bean registration processed. '{}'. Found {} matching attributes.", beanName, matchedAttributesForBean);

if (instanceTelemetryBean != null) {
int beanRegisterationsHandled = instanceTelemetryBean.getBeanRegistrationsHandled();
instanceTelemetryBean.setBeanRegistrationsHandled(beanRegisterationsHandled + 1);
}
}

/** Removes any matching attributes from the specified bean. */
Expand All @@ -811,6 +816,11 @@ public synchronized void untrackBean(ObjectName beanName) {
removedMetrics,
beanName);
this.metricCountForMatchingAttributes -= removedMetrics;

if (instanceTelemetryBean != null) {
int beanUnRegistrationsHandled = instanceTelemetryBean.getBeanUnregistrationsHandled();
instanceTelemetryBean.setBeanUnregistrationsHandled(beanUnRegistrationsHandled + 1);
}
}

private void subscribeToBeans() {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/datadog/jmxfetch/util/InstanceTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class InstanceTelemetry implements InstanceTelemetryMBean {
private int metricCount;
private int wildcardDomainQueryCount;
private double beanMatchRatio;
private int beanRegistrationsHandled;
private int beanUnregistrationsHandled;

/** Jmxfetch telemetry bean constructor. */
public InstanceTelemetry() {
Expand All @@ -26,6 +28,8 @@ public InstanceTelemetry() {
// - numBeansWithoutMatchingAttributes
// a bit wordy though.
beanMatchRatio = 0.0;
beanRegistrationsHandled = 0;
beanUnregistrationsHandled = 0;
}

public int getBeansFetched() {
Expand All @@ -48,6 +52,14 @@ public double getBeanMatchRatio() {
return beanMatchRatio;
}

public int getBeanRegistrationsHandled() {
return beanRegistrationsHandled;
}

public int getBeanUnregistrationsHandled() {
return beanUnregistrationsHandled;
}

public void setBeansFetched(int count) {
beansFetched = count;
}
Expand All @@ -68,4 +80,12 @@ public void setBeanMatchRatio(double ratio) {
beanMatchRatio = ratio;
}

public void setBeanRegistrationsHandled(int count) {
beanRegistrationsHandled = count;
}

public void setBeanUnregistrationsHandled(int count) {
beanUnregistrationsHandled = count;
}

}
13 changes: 12 additions & 1 deletion src/test/java/org/datadog/jmxfetch/TestInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ public void testBeanSubscription() throws Exception {
metrics = getMetrics();

assertEquals(numRegisteredAttributes, metrics.size());

List<Instance> instances = getInstances();
assertEquals(1, instances.size());
Instance i = instances.get(0);
assertEquals(2, i.getInstanceTelemetryBean().getBeanRegistrationsHandled());
assertEquals(0, i.getInstanceTelemetryBean().getBeanUnregistrationsHandled());
}

@Test
Expand Down Expand Up @@ -319,8 +325,13 @@ public void testBeanUnsubscription() throws Exception {
assertEquals(numRegisteredAttributes, metrics.size());
// Which is why this second check exists, it reflects the running total of metrics
// collected which is used to determine if new attributes can be added
Instance i = app.getInstances().get(0);
List<Instance> instances = getInstances();
assertEquals(1, instances.size());
Instance i = instances.get(0);
assertEquals(numRegisteredAttributes, i.getCurrentNumberOfMetrics());

assertEquals(1, i.getInstanceTelemetryBean().getBeanRegistrationsHandled());
assertEquals(1, i.getInstanceTelemetryBean().getBeanUnregistrationsHandled());
}

@Test
Expand Down

0 comments on commit 82bbc94

Please sign in to comment.