Permalink
Browse files

Fix http event client support for V1 events coming from the http server

  • Loading branch information...
1 parent 83d7897 commit 2da9f1f6274761df63407ae7669e66162a518e84 @mattstep mattstep committed Apr 11, 2012
@@ -4,6 +4,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.codehaus.jackson.JsonGenerator;
+import org.joda.time.DateTime;
import java.io.IOException;
import java.lang.reflect.Method;
@@ -183,4 +184,14 @@ public void writeFieldV1(JsonGenerator jsonGenerator, Object event)
jsonGenerator.writeEndObject();
}
}
+
+ public void writeTimestampV1(JsonGenerator jsonGenerator, Object event)
+ throws IOException
+ {
+ Object value = getValue(event);
+ if (value != null) {
+ EventDataType.validateFieldValueType(value, DateTime.class);
+ jsonGenerator.writeNumberField("timestamp", ((DateTime) value).getMillis());
+ }
+ }
}
@@ -23,8 +23,7 @@
private EventJsonSerializerV1(EventTypeMetadata<T> eventTypeMetadata)
{
Preconditions.checkNotNull(eventTypeMetadata, "eventTypeMetadata is null");
- Preconditions.checkState(eventTypeMetadata.getHostField() == null, "custom host field not supported for JSON V1");
- Preconditions.checkState(eventTypeMetadata.getTimestampField() == null, "custom timestamp field not supported for JSON V1");
+
this.eventTypeMetadata = eventTypeMetadata;
try {
hostName = InetAddress.getLocalHost().getHostName();
@@ -48,8 +47,20 @@ public void serialize(T event, JsonGenerator jsonGenerator, SerializerProvider p
jsonGenerator.writeStringField("name", eventTypeMetadata.getTypeName());
jsonGenerator.writeStringField("type", "metrics"); // todo
- jsonGenerator.writeStringField("host", hostName);
- jsonGenerator.writeNumberField("timestamp", System.currentTimeMillis());
+
+ if (eventTypeMetadata.getHostField() != null) {
+ eventTypeMetadata.getHostField().writeField(jsonGenerator, event);
+ }
+ else {
+ jsonGenerator.writeStringField("host", hostName);
+ }
+
+ if (eventTypeMetadata.getTimestampField() != null) {
+ eventTypeMetadata.getTimestampField().writeTimestampV1(jsonGenerator, event);
+ }
+ else {
+ jsonGenerator.writeNumberField("timestamp", System.currentTimeMillis());
+ }
jsonGenerator.writeArrayFieldStart("data");
for (EventFieldMetadata field : eventTypeMetadata.getFields()) {
@@ -0,0 +1,71 @@
+package com.proofpoint.event.client;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableList;
+import org.joda.time.DateTime;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Set;
+import java.util.UUID;
+
+import static com.proofpoint.event.client.EventTypeMetadata.getValidEventTypeMetaDataSet;
+import static org.testng.Assert.assertEquals;
+
+public class TestJsonEventWriterV1
+{
+ private JsonEventWriter eventWriter;
+
+ @BeforeMethod
+ public void setup()
+ throws Exception
+ {
+ Set<EventTypeMetadata<?>> eventTypes = getValidEventTypeMetaDataSet(FixedDummyEventClass.class);
+ eventWriter = new JsonEventWriter(eventTypes, new HttpEventClientConfig().setJsonVersion(1));
+ }
+
+ @Test
+ public void testEventWriter()
+ throws Exception
+ {
+ assertEventJson(createEventGenerator(TestingUtils.getEvents()), "V1events.json");
+ }
+
+ @Test
+ public void testNullValue()
+ throws Exception
+ {
+ FixedDummyEventClass event = new FixedDummyEventClass(
+ "localhost", new DateTime("2011-09-09T01:59:59.999Z"), UUID.fromString("1ea8ca34-db36-11e0-b76f-8b7d505ab1ad"), 123, null);
+
+ assertEventJson(createEventGenerator(ImmutableList.of(event)), "V1event.json");
+ }
+
+ private void assertEventJson(EventClient.EventGenerator<?> events, String resource)
+ throws Exception
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ eventWriter.writeEvents(events, out);
+
+ String json = out.toString(Charsets.UTF_8.name());
+ assertEquals(json, TestingUtils.getNormalizedJson(resource));
+ }
+
+ private static <T> EventClient.EventGenerator<T> createEventGenerator(final Iterable<T> events)
+ {
+ return new EventClient.EventGenerator<T>()
+ {
+ @Override
+ public void generate(EventClient.EventPoster<T> eventPoster)
+ throws IOException
+ {
+ for (T event : events) {
+ eventPoster.post(event);
+ }
+ }
+ };
+ }
+}
@@ -0,0 +1,14 @@
+[
+ {
+ "name":"FixedDummy",
+ "type":"metrics",
+ "host":"localhost",
+ "timestamp":1315533599999,
+ "data":[
+ {
+ "name":"IntValue",
+ "value":123
+ }
+ ]
+ }
+]
@@ -0,0 +1,50 @@
+[
+ {
+ "name":"FixedDummy",
+ "type":"metrics",
+ "host":"localhost",
+ "timestamp":1315532128333,
+ "data":[
+ {
+ "name":"IntValue",
+ "value":5678
+ },
+ {
+ "name":"StringValue",
+ "value":"foo"
+ }
+ ]
+ },
+ {
+ "name":"FixedDummy",
+ "type":"metrics",
+ "host":"localhost",
+ "timestamp":1315532598123,
+ "data":[
+ {
+ "name":"IntValue",
+ "value":1
+ },
+ {
+ "name":"StringValue",
+ "value":"bar"
+ }
+ ]
+ },
+ {
+ "name":"FixedDummy",
+ "type":"metrics",
+ "host":"localhost",
+ "timestamp":1315532755555,
+ "data":[
+ {
+ "name":"IntValue",
+ "value":1234
+ },
+ {
+ "name":"StringValue",
+ "value":"hello"
+ }
+ ]
+ }
+]
@@ -32,12 +32,16 @@
import com.ning.http.client.Response;
import com.proofpoint.configuration.ConfigurationFactory;
import com.proofpoint.configuration.ConfigurationModule;
+import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
import com.proofpoint.event.client.EventClient;
+import com.proofpoint.event.client.HttpEventModule;
import com.proofpoint.event.client.InMemoryEventClient;
import com.proofpoint.event.client.InMemoryEventModule;
import com.proofpoint.event.client.NullEventModule;
+import com.proofpoint.json.JsonModule;
import com.proofpoint.node.NodeInfo;
import com.proofpoint.node.NodeModule;
+import com.proofpoint.node.testing.TestingNodeModule;
import com.proofpoint.testing.FileUtils;
import com.proofpoint.tracetoken.TraceTokenModule;
import org.testng.Assert;
@@ -110,6 +114,36 @@ public void configure(Binder binder)
}
@Test
+ public void testHttpEventModuleConstructionWithV1Events()
+ throws Exception
+ {
+ Injector injector = Guice.createInjector(
+ new HttpServerModule(),
+ new HttpEventModule(),
+ new JsonModule(),
+ new TestingNodeModule(),
+ new TestingDiscoveryModule(),
+ new Module()
+ {
+ @Override
+ public void configure(Binder binder)
+ {
+ binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(DummyServlet.class);
+ }
+ },
+ new ConfigurationModule(new ConfigurationFactory(
+ ImmutableMap.<String, String>builder()
+ .put("node.environment", "test")
+ .put("http-server.http.port", "0")
+ .put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath())
+ .put("collector.json-version", "1")
+ .build())));
+
+ HttpServer server = injector.getInstance(HttpServer.class);
+ Assert.assertNotNull(server);
+ }
+
+ @Test
public void testHttpServerUri()
throws Exception
{

0 comments on commit 2da9f1f

Please sign in to comment.