Skip to content

Commit 5baf01e

Browse files
simon622DC2-DanielKrueger
authored andcommitted
tests
1 parent aed292b commit 5baf01e

File tree

4 files changed

+96
-18
lines changed

4 files changed

+96
-18
lines changed

hivemq-edge/src/main/java/com/hivemq/bridge/metrics/PerBridgeMetrics.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class PerBridgeMetrics {
4040
private final @NotNull Counter loopPreventionForwardDropCounter;
4141
private final @NotNull Counter loopPreventionRemoteDropCounter;
4242
private final @NotNull Set<String> metricNames = new HashSet<>();
43+
private final @NotNull Object mutex = new Object();
4344

4445
public PerBridgeMetrics(final @NotNull String bridgeName, final @NotNull MetricRegistry metricRegistry) {
4546

@@ -96,7 +97,9 @@ public PerBridgeMetrics(final @NotNull String bridgeName, final @NotNull MetricR
9697

9798
private Counter createBridgeCounter(final @NotNull MetricRegistry metricRegistry, @NotNull final String... names){
9899
final String metricName = MetricRegistry.name(BRIDGE_PREFIX, names);
99-
metricNames.add(metricName);
100+
synchronized (mutex){
101+
metricNames.add(metricName);
102+
}
100103
return metricRegistry.counter(metricName);
101104
}
102105

@@ -142,7 +145,9 @@ private Counter createBridgeCounter(final @NotNull MetricRegistry metricRegistry
142145

143146
public void clearAll(final @NotNull MetricRegistry metricRegistry){
144147
Preconditions.checkNotNull(metricRegistry);
145-
metricNames.forEach(metricRegistry::remove);
146-
metricNames.clear();
148+
synchronized (mutex){
149+
metricNames.forEach(metricRegistry::remove);
150+
metricNames.clear();
151+
}
147152
}
148153
}

hivemq-edge/src/main/java/com/hivemq/edge/modules/adapters/metrics/ProtocolAdapterMetricsHelper.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ProtocolAdapterMetricsHelper {
3939
private @NotNull String protocolAdapterId;
4040
private @NotNull MetricRegistry metricRegistry;
4141
private final @NotNull Set<String> metricNames = new HashSet<>();
42+
private final Object mutex = new Object();
4243
static final String SUCCESS_COUNT = "success.count";
4344
static final String FAILED_COUNT = "failed.count";
4445
static final String PERIOD = ".";
@@ -61,10 +62,10 @@ public ProtocolAdapterMetricsHelper(final @NotNull String protocolAdapterType,
6162
}
6263

6364
protected void initRegistry(){
64-
publishSuccessCounter = metricRegistry.counter(createAdapterMetricsNamespace("read.publish", true) + SUCCESS_COUNT);
65-
publishFailedCounter = metricRegistry.counter(createAdapterMetricsNamespace("read.publish", true) + FAILED_COUNT);
66-
connectionSuccessCounter = metricRegistry.counter(createAdapterMetricsNamespace("connection", true) + SUCCESS_COUNT);
67-
connectionFailedCounter = metricRegistry.counter(createAdapterMetricsNamespace("connection", true) + FAILED_COUNT);
65+
publishSuccessCounter = metricRegistry.counter(createAdapterMetricsNamespace("read.publish" + SUCCESS_COUNT));
66+
publishFailedCounter = metricRegistry.counter(createAdapterMetricsNamespace("read.publish" + FAILED_COUNT) );
67+
connectionSuccessCounter = metricRegistry.counter(createAdapterMetricsNamespace("connection" + SUCCESS_COUNT));
68+
connectionFailedCounter = metricRegistry.counter(createAdapterMetricsNamespace("connection" + FAILED_COUNT));
6869
}
6970

7071
/**
@@ -102,14 +103,19 @@ public void incrementConnectionSuccess(){
102103
*/
103104
public void increment(final @NotNull String metricName){
104105
Preconditions.checkNotNull(metricName);
105-
metricRegistry.counter(createAdapterMetricsNamespace(metricName, false)).inc();
106+
metricRegistry.counter(createAdapterMetricsNamespace(metricName)).inc();
106107
}
107108

109+
/**
110+
* Will clear down all metrics in the registry created by this metrics helper.
111+
* NB: metrics created outside the context of this helper will not be touched.
112+
*/
108113
public void clearAll(){
109114
Preconditions.checkNotNull(metricRegistry);
110-
LoggerFactory.getLogger(ProtocolAdapterMetricsHelper.class).info("Clearing all protocol adapter metrics");
111-
metricNames.forEach(metricRegistry::remove);
112-
metricNames.clear();
115+
synchronized (mutex){
116+
metricNames.forEach(metricRegistry::remove);
117+
metricNames.clear();
118+
}
113119
}
114120

115121
/**
@@ -118,22 +124,20 @@ public void clearAll(){
118124
* Example format of the namespace:
119125
* com.hivemq.edge.protocol-adapters.[test-type].[test-id].[suffix](.) with optional trailing period
120126
* @param suffix - the suffix to append to the namespace
121-
* @param trailingPeriod - should the namespace by suffixed with a trailing period
122127
* @return a namespace string for use in the metrics registry
123128
*/
124-
protected String createAdapterMetricsNamespace(@NotNull final String suffix, final boolean trailingPeriod){
129+
protected String createAdapterMetricsNamespace(@NotNull final String suffix){
125130
StringBuilder stringBuilder = new StringBuilder();
126131
stringBuilder.append(HiveMQMetrics.PROTOCOL_ADAPTER_PREFIX);
127132
stringBuilder.append(protocolAdapterType);
128133
stringBuilder.append(PERIOD);
129134
stringBuilder.append(protocolAdapterId);
130135
stringBuilder.append(PERIOD);
131136
stringBuilder.append(suffix);
132-
if(trailingPeriod){
133-
stringBuilder.append(PERIOD);
134-
}
135137
String metricName = stringBuilder.toString();
136-
metricNames.add(metricName);
138+
synchronized (mutex){
139+
metricNames.add(metricName);
140+
}
137141
return metricName;
138142
}
139143
}

hivemq-edge/src/test/java/com/hivemq/adapter/ProtocolMetricsHelperTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
public class ProtocolMetricsHelperTest {
2828

29+
static final String ARBITRARY_METRIC = "arbitrary-metric";
30+
2931
@Test
3032
void testMetricsAdapterWrapperUpdatesRegistry() {
3133

@@ -38,8 +40,29 @@ void testMetricsAdapterWrapperUpdatesRegistry() {
3840
assertEquals(1, registry.getCounters().get("com.hivemq.edge.protocol-adapters.test-adapter-name.test-adapter-id.read.publish.failed.count").getCount(), "Matching failed data point should be incremented");
3941
assertEquals(1, registry.getCounters().get("com.hivemq.edge.protocol-adapters.test-adapter-name.test-adapter-id.read.publish.success.count").getCount(), "Matching success data point should be incremented");
4042

41-
helper.increment("arbitrary-metric");
43+
helper.increment(ARBITRARY_METRIC);
4244
assertEquals(1, registry.getCounters().get("com.hivemq.edge.protocol-adapters.test-adapter-name.test-adapter-id.arbitrary-metric").getCount(), "Matching arbitrary data point should be incremented");
4345

4446
}
47+
48+
@Test
49+
void testTearDownMetrics() {
50+
51+
MetricRegistry registry = new MetricRegistry();
52+
53+
//adapter helper creates 4 metrics
54+
ProtocolAdapterMetricsHelper helper1 =
55+
new ProtocolAdapterMetricsHelper("tear-down-name1","test-adapter-id", registry);
56+
57+
//add an arbitrary fifth
58+
registry.counter(ARBITRARY_METRIC).inc();
59+
60+
assertEquals(5, registry.getMetrics().size(), "Number of metrics should match");
61+
62+
helper1.clearAll();
63+
64+
assertEquals(1, registry.getMetrics().size(), "Number of metrics should match");
65+
assertEquals(1, registry.getCounters().get(ARBITRARY_METRIC).getCount(), "Matching success data point should be incremented");
66+
67+
}
4568
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.hivemq.bridge;
2+
3+
import com.codahale.metrics.MetricRegistry;
4+
import com.hivemq.bridge.metrics.PerBridgeMetrics;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
/**
10+
* @author Simon L Johnson
11+
*/
12+
public class BridgeMetricsTest {
13+
14+
static final String ARBITRARY_METRIC = "arbitrary-metric";
15+
@Test
16+
void testTearDownMetrics() {
17+
18+
MetricRegistry registry = new MetricRegistry();
19+
20+
//adapter helper creates 10 metrics
21+
PerBridgeMetrics perBridgeMetrics = new PerBridgeMetrics("bridge-name", registry);
22+
23+
assertEquals(10, registry.getMetrics().size(), "Number of metrics should match");
24+
25+
PerBridgeMetrics perBridgeMetrics2 = new PerBridgeMetrics("bridge-name2", registry);
26+
27+
assertEquals(20, registry.getMetrics().size(), "Number of metrics should match");
28+
29+
//add an arbitrary fifth
30+
registry.counter(ARBITRARY_METRIC).inc();
31+
32+
assertEquals(21, registry.getMetrics().size(), "Number of metrics should match");
33+
34+
perBridgeMetrics.clearAll(registry);
35+
36+
assertEquals(11, registry.getMetrics().size(), "Number of metrics should match");
37+
38+
perBridgeMetrics2.clearAll(registry);
39+
40+
assertEquals(1, registry.getMetrics().size(), "Number of metrics should match");
41+
42+
assertEquals(1, registry.getCounters().get(ARBITRARY_METRIC).getCount(), "Matching success data point should be incremented");
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)