Skip to content

Commit 6eed200

Browse files
authored
Merge pull request aol#369 from philip-clarke/upgrade-metrics-core
add new InstantGauge metrics in MetricsCatcher
2 parents bf7bfd0 + a018090 commit 6eed200

File tree

8 files changed

+263
-3
lines changed

8 files changed

+263
-3
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=0.91.5
1+
version=0.91.6
22
springVersion=4.3.3.RELEASE
33
springBootVersion=1.4.1.RELEASE
44
jerseyVersion=2.24

micro-event-metrics/src/main/java/com/aol/micro/server/event/metrics/MetricsCatcher.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.aol.micro.server.event.metrics;
22

33
import com.aol.micro.server.events.GenericEvent;
4+
import com.aol.micro.server.spring.metrics.InstantGauge;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.stereotype.Component;
67

@@ -9,6 +10,8 @@
910
import com.aol.micro.server.events.JobStartEvent;
1011
import com.aol.micro.server.events.RequestTypes.AddQuery;
1112
import com.aol.micro.server.events.RequestTypes.RemoveQuery;
13+
import com.aol.micro.server.events.RequestTypes.AddLabelledQuery;
14+
import com.aol.micro.server.events.RequestTypes.RemoveLabelledQuery;
1215
import com.aol.micro.server.events.RequestTypes.RequestData;
1316
import com.aol.micro.server.events.SystemData;
1417
import com.aol.micro.server.health.ErrorEvent;
@@ -39,6 +42,7 @@ public MetricsCatcher(MetricRegistry registry, EventBus bus, Configuration confi
3942
jobs = new TimerManager(
4043
configuration.getNumJobs(), configuration.getHoldJobsForMinutes());
4144
this.configuration = configuration;
45+
4246
}
4347

4448
@Subscribe
@@ -47,6 +51,8 @@ public void requestStart(AddQuery<T> data) {
4751
.mark();
4852
registry.counter(prefix + ".requests-started-count")
4953
.inc();
54+
((InstantGauge) registry.gauge(prefix + ".requests-started-interval-count", () -> new InstantGauge())).increment();
55+
5056
if (this.configuration.isQueriesByType()) {
5157
RequestData<T> rd = data.getData();
5258

@@ -74,6 +80,9 @@ public void requestComplete(RemoveQuery<T> data) {
7480
.mark();
7581
registry.counter(prefix + ".requests-completed-count")
7682
.inc();
83+
((InstantGauge) registry.gauge(prefix + ".requests-completed-interval-count", () -> new InstantGauge()))
84+
.increment();
85+
7786
if (this.configuration.isQueriesByType()) {
7887
RequestData<T> rd = data.getData();
7988
registry.meter(queryEndName(rd))
@@ -84,7 +93,25 @@ public void requestComplete(RemoveQuery<T> data) {
8493
registry.counter(prefix + ".requests-active-" + rd.getType() + "-count")
8594
.dec();
8695
}
96+
}
97+
98+
@Subscribe
99+
public void requestStart(AddLabelledQuery<T> data) {
100+
if (this.configuration.isQueriesByType()) {
101+
RequestData<T> rd = data.getData();
87102

103+
((InstantGauge) registry.gauge(prefix + ".requests-started-" + rd.getType() + "-interval-count", () -> new InstantGauge()))
104+
.increment();
105+
}
106+
}
107+
108+
@Subscribe
109+
public void requestComplete(RemoveLabelledQuery<T> data) {
110+
if (this.configuration.isQueriesByType()) {
111+
RequestData<T> rd = data.getData();
112+
((InstantGauge) registry.gauge(prefix + ".requests-completed-" + rd.getType() + "-interval-count", () -> new InstantGauge()))
113+
.increment();
114+
}
88115
}
89116

90117
@Subscribe

micro-event-metrics/src/test/java/com/aol/micro/server/event/metrics/MetricsCatcherTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ public void queriesCounterDec() {
8686
equalTo(-1l));
8787
}
8888

89+
@Test
90+
public void queriesIntervalCounterInc() {
91+
92+
catcher.requestStart(new AddQuery(
93+
RequestData.builder()
94+
.correlationId(10l)
95+
.type("test")
96+
.build()));
97+
assertThat(registry.getGauges().size(), equalTo(1));
98+
assertThat(registry.getGauges().get(this.config.getPrefix() + ".requests-started-interval-count").getValue(), equalTo(1l));
99+
}
100+
101+
@Test
102+
public void queriesIntervalCounterDec() {
103+
104+
catcher.requestComplete(new RemoveQuery(
105+
RequestData.builder()
106+
.correlationId(10l)
107+
.type("test")
108+
.build()));
109+
assertThat(registry.getGauges().size(), equalTo(1));
110+
assertThat(registry.getGauges().get(this.config.getPrefix() + ".requests-completed-interval-count").getValue(),
111+
equalTo(1l));
112+
}
113+
89114
@Test
90115
public void jobsCounterDec() {
91116

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.aol.micro.server.events;
2+
3+
import com.aol.micro.server.events.RequestTypes.AddLabelledQuery;
4+
import com.aol.micro.server.events.RequestTypes.RemoveLabelledQuery;
5+
import com.aol.micro.server.events.RequestTypes.RequestData;
6+
7+
import com.google.common.eventbus.EventBus;
8+
9+
/**
10+
* Factory class for creating Start and End events which are identified by a custom label
11+
*
12+
*/
13+
public class LabelledEvents {
14+
15+
/**
16+
* Publish start events for each of the specified query types
17+
*
18+
* <pre>
19+
* {@code
20+
LabelledEvents.start("get", 1l, bus, "typeA", "custom");
21+
try {
22+
return "ok";
23+
} finally {
24+
RequestEvents.finish("get", 1l, bus, "typeA", "custom");
25+
}
26+
* }
27+
* </pre>
28+
*
29+
* @param query Completed query
30+
* @param correlationId Identifier
31+
* @param bus EventBus to post events to
32+
* @param labels Query labels to post to event bus
33+
*/
34+
public static <T> void start(T query, long correlationId, EventBus bus, String... labels) {
35+
36+
for (String label : labels) {
37+
AddLabelledQuery<T> next = start(query, correlationId, label, null);
38+
bus.post(next);
39+
}
40+
41+
}
42+
43+
/**
44+
* Marks the start of a query identified by the provided correlationId, with additional query type and data parameters
45+
*
46+
* @param query - Query data
47+
* @param correlationId - Identifier
48+
* @param label - allows queries to be grouped by label
49+
* @return Start event to pass to the Events systems EventBus
50+
*/
51+
public static <T> AddLabelledQuery<T> start(T query, long correlationId, String label) {
52+
return start(query, correlationId, label, null);
53+
}
54+
55+
/**
56+
* Marks the start of a query identified by the provided correlationId, with additional query type and data parameters
57+
*
58+
* @param query - Query data
59+
* @param correlationId - Identifier
60+
* @param label - allows queries to be grouped by label
61+
* @param additionalData - Any additional info about the request to be rendered in the JSON view / rest endpoint
62+
* @return Start event to pass to the Events systems EventBus
63+
*/
64+
public static <T> AddLabelledQuery<T> start(T query, long correlationId, String label, Object additionalData) {
65+
66+
return new AddLabelledQuery(
67+
RequestData.builder()
68+
.query(query)
69+
.correlationId(correlationId)
70+
.type(label)
71+
.additionalData(additionalData)
72+
.build());
73+
}
74+
75+
/**
76+
* Publish finish events for each of the specified query labels
77+
*
78+
* <pre>
79+
* {@code
80+
* LabelledEvents.start("get", 1l, bus, "typeA", "custom");
81+
try {
82+
return "ok";
83+
} finally {
84+
RequestEvents.finish("get", 1l, bus, "typeA", "custom");
85+
}
86+
*
87+
* }
88+
* </pre>
89+
*
90+
*
91+
* @param query Completed query
92+
* @param correlationId Identifier
93+
* @param bus EventBus to post events to
94+
* @param labels Query types to post to event bus
95+
*/
96+
public static <T> void finish(T query, long correlationId, EventBus bus, String... labels) {
97+
for (String type : labels) {
98+
RemoveLabelledQuery<T> next = finish(query, correlationId, type);
99+
bus.post(next);
100+
}
101+
}
102+
/**
103+
* Marks the end of a query identified by the provided correlationId
104+
*
105+
* @param query - Query data
106+
* @param correlationId - Identifier
107+
* @param label - allows queries to be grouped by type
108+
* @return RemoveLabelledQuery event to pass to the Events systems EventBus
109+
*/
110+
public static <T> RemoveLabelledQuery<T> finish(T query, long correlationId, String label) {
111+
112+
return new RemoveLabelledQuery<>(
113+
RequestData.builder()
114+
.query(query)
115+
.correlationId(correlationId)
116+
.type(label)
117+
.build());
118+
}
119+
}

micro-events/src/main/java/com/aol/micro/server/events/RequestTypes.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ public RemoveQuery(RequestData data) {
7979

8080
}
8181

82+
public static class AddLabelledQuery<T> extends AddEvent<RequestData<T>> {
83+
84+
public AddLabelledQuery(RequestData<T> data) {
85+
super(
86+
data);
87+
}
88+
89+
}
90+
91+
public static class RemoveLabelledQuery<T> extends RemoveEvent<RequestData<T>> {
92+
93+
public RemoveLabelledQuery(RequestData data) {
94+
super(
95+
data);
96+
}
97+
98+
}
99+
82100
@AllArgsConstructor
83101
@Builder
84102
@XmlAccessorType(XmlAccessType.FIELD)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.aol.micro.server.events;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import com.aol.micro.server.events.RequestTypes.AddLabelledQuery;
7+
import com.aol.micro.server.events.RequestTypes.RemoveLabelledQuery;
8+
import com.aol.micro.server.events.RequestTypes.RequestData;
9+
10+
import static org.hamcrest.Matchers.is;
11+
import static org.junit.Assert.assertThat;
12+
13+
14+
public class LabelledEventsTest {
15+
16+
String query;
17+
long corrId;
18+
String label;
19+
String addData;
20+
21+
@Before
22+
public void setUp() {
23+
query = "query as string";
24+
corrId = 1234;
25+
label = "label";
26+
addData = "additional data";
27+
}
28+
29+
@Test
30+
public void createAddLabelledQuery() {
31+
32+
AddLabelledQuery<String> userQuery = LabelledEvents.start(query, corrId, label);
33+
RequestData rd = userQuery.getData();
34+
35+
assertThat(rd.getQuery(), is(query));
36+
assertThat(rd.getType(), is(label));
37+
assertThat(rd.getCorrelationId(), is(corrId));
38+
}
39+
40+
@Test
41+
public void createAddLabelledQueryWithAdditionalData() {
42+
43+
AddLabelledQuery<String> userQuery = LabelledEvents.start(query, corrId, label, addData);
44+
RequestData rd = userQuery.getData();
45+
46+
assertThat(rd.getQuery(), is(query));
47+
assertThat(rd.getType(), is(label));
48+
assertThat(rd.getCorrelationId(), is(corrId));
49+
assertThat(rd.getAdditionalData(), is(addData));
50+
}
51+
52+
@Test
53+
public void createRemoveLabelledQuery() {
54+
55+
RemoveLabelledQuery<String> userQuery = LabelledEvents.finish(query, corrId, label);
56+
RequestData<String> rd = userQuery.getData();
57+
58+
assertThat(rd.getQuery(), is(query));
59+
assertThat(rd.getType(), is(label));
60+
assertThat(rd.getCorrelationId(), is(corrId));
61+
}
62+
}

micro-jmx-metrics/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ description = 'micro-jmx-metrics'
33
dependencies {
44

55

6-
compile ('com.ryantenney.metrics:metrics-spring:'+springMetricsVersion){ exclude(module: 'org.springframework') }
6+
compile ('com.ryantenney.metrics:metrics-spring:'+springMetricsVersion){
7+
exclude(module: 'org.springframework')
8+
exclude(group: 'io.dropwizard.metrics', module: 'metrics-core')
9+
}
10+
11+
compile("io.dropwizard.metrics:metrics-core:3.2.2")
712

813
compile ("com.aol.simplereact:cyclops-react:$cyclopsReactVersion")
914
compile project(':micro-core')

micro-metrics/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
description = 'micro-metrics'
22
dependencies {
3-
compile ('com.ryantenney.metrics:metrics-spring:'+springMetricsVersion){
3+
compile('com.ryantenney.metrics:metrics-spring:' + springMetricsVersion) {
44
exclude(module: 'org.springframework')
5+
exclude(group: 'io.dropwizard.metrics', module: 'metrics-core')
56
}
7+
8+
compile("io.dropwizard.metrics:metrics-core:3.2.2")
9+
610
compile project(':micro-core')
711
testCompile project(':micro-jackson-configuration')
812
testCompile project(':micro-grizzly')

0 commit comments

Comments
 (0)