Skip to content

Commit

Permalink
Merge pull request #364 from jijisv/master
Browse files Browse the repository at this point in the history
GenericEvent to trigger/log arbitrary events
  • Loading branch information
jijisv authored Jul 18, 2017
2 parents eefe296 + 31a20e4 commit ed1093f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aol.micro.server.event.metrics;

import com.aol.micro.server.events.GenericEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -15,6 +16,8 @@
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import java.util.Objects;

@Component
public class MetricsCatcher<T> {

Expand Down Expand Up @@ -169,6 +172,21 @@ public void error(ErrorEvent event) {

}

@Subscribe
public void genericEvent(GenericEvent event) {
GenericEvent.GenericEventData eventData = event.getData();
String name = prefix + ".event-" + eventData.getName();
registry.counter(name + "-count").inc();
registry.meter(name + "-meter").mark();
if (Objects.nonNull(eventData.getSubTypes())) {
for (String subType : eventData.getSubTypes()) {
name = name + "." + subType;
registry.counter(name + "-count").inc();
registry.meter(name + "-meter").mark();
}
}
}

private String name(ErrorCode c) {
return prefix + ".error-" + c.getSeverity() + "-" + c.getErrorId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;

import com.aol.micro.server.events.GenericEvent;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -170,4 +171,15 @@ public void testErrorSeverityMeter() {

}

@Test
public void testGenericEvent() {
GenericEvent.trigger("generic", bus, "data", new String[] {"p1", "p2"});
assertThat(registry.meter(this.config.getPrefix() + ".event-generic-meter").getCount(), greaterThan(0L));
assertThat(registry.counter(this.config.getPrefix() + ".event-generic-count").getCount(), greaterThan(0L));
assertThat(registry.meter(this.config.getPrefix() + ".event-generic.p1-meter").getCount(), greaterThan(0L));
assertThat(registry.counter(this.config.getPrefix() + ".event-generic.p1-count").getCount(), greaterThan(0L));
assertThat(registry.meter(this.config.getPrefix() + ".event-generic.p1.p2-meter").getCount(), greaterThan(0L));
assertThat(registry.counter(this.config.getPrefix() + ".event-generic.p1.p2-count").getCount(), greaterThan(0L));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.aol.micro.server.events;

import com.google.common.eventbus.EventBus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class GenericEvent<T> {

private GenericEventData<T> data;

public static <T> GenericEvent<T> trigger(String name, EventBus bus) {
return trigger(name, bus, null, new String[]{});
}

public static <T> GenericEvent<T> trigger(String name, EventBus bus, String[] subTypes) {
return trigger(name, bus, null, subTypes);
}

public static <T> GenericEvent<T> trigger(String name, EventBus bus, T data, String[] subTypes) {
GenericEvent<T> event = new GenericEvent<>(GenericEventData.<T>builder()
.name(name)
.data(data)
.subTypes(subTypes)
.build());
bus.post(event);
return event;
}

@AllArgsConstructor
@Builder
@Getter
public static class GenericEventData<T> extends BaseEventInfo {
private final String name;
private final String[] subTypes;
private final T data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.aol.micro.server.events;

import com.google.common.eventbus.EventBus;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class GenericEventTest {

@Mock
EventBus bus;

@Test
public void trigger() throws Exception {
GenericEvent<Object> event = GenericEvent.trigger("some-event", bus);
Mockito.verify(bus).post(event);
}

@Test
public void trigger1() throws Exception {
GenericEvent<Object> event = GenericEvent.trigger("some-event", bus, new String[] {"start", "phase1"});
Mockito.verify(bus).post(event);
}

@Test
public void trigger2() throws Exception {
GenericEvent<String> event = GenericEvent.trigger("some-event", bus, "data", new String[]{"finish", "terminate"});
Mockito.verify(bus).post(event);
}

@Test
public void getData() throws Exception {
GenericEvent<Integer> event = GenericEvent.trigger("some-event", bus, 10, new String[]{"finish", "terminate"});
GenericEvent.GenericEventData<Integer> eventData = event.getData();
assertEquals("some-event", eventData.getName());
assertEquals(Integer.valueOf(10), eventData.getData());
assertNotNull(eventData.getSubTypes());
assertEquals(eventData.getSubTypes()[0], "finish");
assertEquals(eventData.getSubTypes()[1], "terminate");
}

}

0 comments on commit ed1093f

Please sign in to comment.