-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AvastJmxMetricsMonitor
- Loading branch information
Showing
10 changed files
with
241 additions
and
5 deletions.
There are no files selected for viewing
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
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
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,20 @@ | ||
# Dropwizard JMX export - Avast custom | ||
|
||
See [AvastJmxMetricsMonitor](src/main/java/com/avast/metrics/dropwizard/AvastJmxMetricsMonitor.java) and | ||
[AvastTreeObjectNameFactoryTest](src/test/java/com/avast/metrics/dropwizard/AvastTreeObjectNameFactoryTest.java). | ||
|
||
```java | ||
import com.avast.metrics.api.*; | ||
import com.avast.metrics.dropwizard.*; | ||
|
||
AvastJmxMetricsMonitor monitor = new AvastJmxMetricsMonitor("com.avast.myapp"); | ||
Handler handler = new Handler(monitor.named("Handler1")); | ||
``` | ||
|
||
```scala | ||
import com.avast.metrics.scalaapi.Monitor | ||
import com.avast.metrics.dropwizard.AvastJmxMetricsMonitor | ||
|
||
val javaMonitor = new AvastJmxMetricsMonitor("com.avast.myapp") | ||
val scalaMonitor = Monitor(javaMonitor) | ||
``` |
37 changes: 37 additions & 0 deletions
37
jmx-avast/src/main/java/com/avast/metrics/dropwizard/AvastJmxMetricsMonitor.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,37 @@ | ||
package com.avast.metrics.dropwizard; | ||
|
||
import com.avast.metrics.api.Naming; | ||
import com.codahale.metrics.MetricRegistry; | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
public class AvastJmxMetricsMonitor extends JmxMetricsMonitor { | ||
|
||
public AvastJmxMetricsMonitor(String domain) { | ||
this(domain, new MetricRegistry(), Naming.defaultNaming()); | ||
} | ||
|
||
public AvastJmxMetricsMonitor(String domain, MetricRegistry metricRegistry, Naming naming) { | ||
super(AvastTreeObjectNameFactory.getInstance(), domain, metricRegistry, naming); | ||
} | ||
|
||
private AvastJmxMetricsMonitor(AvastJmxMetricsMonitor original, String... names) { | ||
super(original, names); | ||
} | ||
|
||
|
||
@Override | ||
public AvastJmxMetricsMonitor named(String name) { | ||
return new AvastJmxMetricsMonitor(this, name); | ||
} | ||
|
||
@Override | ||
public AvastJmxMetricsMonitor named(String name1, String name2, String... restOfNames) { | ||
return new AvastJmxMetricsMonitor(named(name1).named(name2), restOfNames); | ||
} | ||
|
||
@Override | ||
protected String separator() { | ||
return AvastTreeObjectNameFactory.SEPARATOR; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
jmx-avast/src/main/java/com/avast/metrics/dropwizard/AvastTreeObjectNameFactory.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,77 @@ | ||
package com.avast.metrics.dropwizard; | ||
|
||
import com.codahale.metrics.DefaultObjectNameFactory; | ||
import com.codahale.metrics.ObjectNameFactory; | ||
|
||
import javax.management.MalformedObjectNameException; | ||
import javax.management.ObjectName; | ||
import java.util.*; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* This is the Avast alternative for {@link TreeObjectNameFactory}. It uses "type-scope-name" format of resulting {@link ObjectName} (levels | ||
* 3-N are glued together). | ||
* See unit tests. | ||
*/ | ||
public class AvastTreeObjectNameFactory implements ObjectNameFactory { | ||
|
||
public static final String SEPARATOR = "@#*"; | ||
|
||
private static final ObjectNameFactory defaultFactory = new DefaultObjectNameFactory(); | ||
|
||
private static final String[] partNames = {"type", "scope", "name"}; | ||
|
||
private AvastTreeObjectNameFactory() { | ||
} | ||
|
||
public static AvastTreeObjectNameFactory getInstance() { | ||
return Holder.INSTANCE; | ||
} | ||
|
||
@Override | ||
public ObjectName createName(String type, String domain, String name) { | ||
Optional<ObjectName> parsedName = parseName(domain, name); | ||
return parsedName.orElse(defaultFactory.createName(type, domain, name)); | ||
} | ||
|
||
private Optional<ObjectName> parseName(String domain, String name) { | ||
try { | ||
final String[] parts = name.split(Pattern.quote(SEPARATOR), partNames.length); | ||
|
||
/* | ||
Following block of code is a little hack. | ||
The problem is the `ObjectName` requires `HashTable` as parameter but the `HashTable` is unsorted and | ||
thus unusable for us. We hack it by raping the `HashTable` and in-fact using `LinkedHashMap` which is | ||
much more suitable for our needs. | ||
*/ | ||
|
||
final LinkedHashMap<String, String> map = new LinkedHashMap<>(); | ||
final Hashtable<String, String> properties = new Hashtable<String, String>() { | ||
@Override | ||
public Set<Map.Entry<String, String>> entrySet() { | ||
return map.entrySet(); | ||
} | ||
}; | ||
|
||
for (int i = 0; i < parts.length; i++) { | ||
properties.put(partNames[i], quote(parts[i])); | ||
map.put(partNames[i], quote(parts[i])); | ||
} | ||
|
||
return Optional.of(new ObjectName(domain, properties)); | ||
} catch (MalformedObjectNameException ex) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
|
||
private String quote(String objectName) { | ||
return objectName | ||
.replaceAll(Pattern.quote(SEPARATOR), "/") | ||
.replaceAll("[\\Q?*\"\\E]", "_"); | ||
} | ||
|
||
private static class Holder { | ||
static final AvastTreeObjectNameFactory INSTANCE = new AvastTreeObjectNameFactory(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
jmx-avast/src/test/java/com/avast/metrics/dropwizard/AvastMetricsMonitorTest.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,50 @@ | ||
package com.avast.metrics.dropwizard; | ||
|
||
import com.avast.metrics.api.Gauge; | ||
import com.avast.metrics.api.Monitor; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AvastMetricsMonitorTest { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void jmxClose() { | ||
try (Monitor m1 = new AvastJmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { | ||
int value1 = 1; | ||
Gauge<Integer> gauge1 = m1.newGauge("gauge", () -> value1); | ||
assertEquals(value1, (int) gauge1.getValue()); | ||
} | ||
|
||
try (Monitor m2 = new AvastJmxMetricsMonitor("com.avast.metrics.test").named("test-jmx-close")) { | ||
int value2 = 2; | ||
Gauge<Integer> gauge2 = m2.newGauge("gauge", () -> value2); | ||
assertEquals(value2, (int) gauge2.getValue()); | ||
} | ||
|
||
// watch log output for javax.management.InstanceAlreadyExistsException | ||
} | ||
|
||
@Test | ||
public void getName() { | ||
try (AvastJmxMetricsMonitor m1 = new AvastJmxMetricsMonitor("com.avast.metrics.test").named("first", "second", "third", "fourth")) { | ||
final String name = m1.getName(); | ||
|
||
assertEquals("first/second/third/fourth", name); | ||
} | ||
|
||
try (Monitor m1 = new AvastJmxMetricsMonitor("com.avast.metrics.test").named("first/sub").named("second").named("third").named("fourth/fifth")) { | ||
try (Monitor m2 = new JmxMetricsMonitor("com.avast.metrics.test").named("first/sub", "second", "third").named("fourth/fifth")) { | ||
final String name1 = m1.getName(); | ||
final String name2 = m2.getName(); | ||
|
||
assertEquals(name1, name2); | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
jmx-avast/src/test/java/com/avast/metrics/dropwizard/AvastTreeObjectNameFactoryTest.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,22 @@ | ||
package com.avast.metrics.dropwizard; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AvastTreeObjectNameFactoryTest { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void createName() { | ||
final AvastTreeObjectNameFactory nameFactory = AvastTreeObjectNameFactory.getInstance(); | ||
|
||
final String name = nameFactory.createName("theType", "theDomain", String.join(AvastTreeObjectNameFactory.SEPARATOR, "l1", "l2", "l3", "l4", "l5")).toString(); | ||
|
||
assertEquals("theDomain:type=l1,scope=l2,name=l3/l4/l5", name); | ||
} | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
jmx/src/test/java/com/avast/metrics/dropwizard/TreeObjectNameFactoryTest.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,23 @@ | ||
package com.avast.metrics.dropwizard; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TreeObjectNameFactoryTest { | ||
|
||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void createName() { | ||
final TreeObjectNameFactory nameFactory = TreeObjectNameFactory.getInstance(); | ||
|
||
final String name = nameFactory.createName("theType", "theDomain", String.join(TreeObjectNameFactory.SEPARATOR, "l1", "l2", "l3", "l4", "l5")).toString(); | ||
|
||
assertEquals("theDomain:level0=l1,level1=l2,level2=l3,level3=l4,level4=l5",name); | ||
} | ||
} |