use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.
the class BaseHandler method processInternal.
/**
* Method called by Handler implementations to process records.
*
* @param context Lambda invocation context.
* @throws HandlerException
*/
private void processInternal(Context context) throws HandlerException {
Stat runtime = new Stat("runtime.ns");
runtime.start();
Source source = this.getSource();
DeserializerProcessor deser = source.getDeserProcessor();
List<OperationProcessor> operations = source.getOperationProcessors();
List<String> containsStrings = source.getContainsStrings();
List<Pattern> regexPatterns = source.getRegexPatterns();
this.getIpcService().setContext(context);
Iterator<InternalEvent> events = this.getInternalEventIterator();
/*
* For logging purposes log when the function started running
*/
this.monitor.invokeTimeNow();
AtomicLong eventCount = new AtomicLong(0);
AtomicLong oldestArrivalTime = new AtomicLong(System.currentTimeMillis());
AtomicLong oldestOccurrenceTime = new AtomicLong(System.currentTimeMillis());
/*
* Process each record
*/
int characteristics = Spliterator.IMMUTABLE;
Spliterator<InternalEvent> spliterator = Spliterators.spliteratorUnknownSize(events, characteristics);
Stream<InternalEvent> input = StreamSupport.stream(spliterator, false);
/*
* Filter out raw events
*/
Stream<InternalEvent> filtered = input.filter(/*
* Perform regex filter
*/
ievent -> {
eventCount.incrementAndGet();
String eventStr = ievent.getEventString();
/*
* Apply String contains filters before deserialization
*/
for (String containsString : containsStrings) {
if (eventStr.contains(containsString)) {
return false;
}
}
/*
* Apply regex patterns before deserialization
*/
for (Pattern regexPattern : regexPatterns) {
Matcher m = regexPattern.matcher(eventStr);
if (m.find()) {
return false;
}
}
return true;
});
/*
* Deserialize
*/
Stream<InternalEvent> deserialized = filtered.map(ievent -> {
DeserializedEvent data = deser.deserialize(ievent.getEventString());
if (data == null || data.getPayload() == null) {
logger.warn("Failed to deserialize: " + ievent.getEventString());
return null;
}
ievent.setEventObj(data);
return ievent;
}).filter(Objects::nonNull);
/*
* Perform Operations
*/
Stream<InternalEvent> operated = deserialized;
for (OperationProcessor operation : operations) {
operated = operation.perform(operated);
}
/*
* Serialize
*/
Stream<InternalEvent> serialized = operated.map(ievent -> {
try {
String raw = null;
raw = this.ser.serialize(this.wrapper.getWrapped(ievent));
ievent.setSerialized(raw);
return ievent;
} catch (SerializationException e) {
return null;
}
}).filter(Objects::nonNull);
/*
* Transport
*/
serialized.forEach(ievent -> {
/*
* Update times
*/
updateOldest(oldestArrivalTime, ievent.getArrivalTime());
updateOldest(oldestOccurrenceTime, ievent.getEventTime());
try {
this.getIpcService().add(ievent);
} catch (TransportException e) {
logger.warn("error adding event", e);
}
});
/*
* Wait for transporters to finish
*/
try {
this.getIpcService().shutdown();
} catch (TransportException e) {
throw new HandlerException("encounted TransportException while shutting down ipcService", e);
} catch (InterruptedException e) {
throw new HandlerException("thread was interruptedwhile shutting down ipcService", e);
} finally {
String evtSource = this.getSourceName();
runtime.stop();
if (!this.skipWriteStats) {
writeStats(eventCount.get(), oldestArrivalTime.get(), oldestOccurrenceTime.get(), evtSource, runtime);
}
if (logger.isTraceEnabled()) {
getGCStats();
}
}
}
use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.
the class GelfOperation method prefix.
protected InternalEvent prefix(InternalEvent ievent) {
DeserializedEvent devent;
if ((devent = ievent.getEventObj()) == null) {
return null;
}
Object payload = devent.getPayload();
if (payload == null) {
return null;
}
if (!(payload instanceof JsonObject)) {
throw new OperationException("Payload data is not a JsonObject");
}
JsonObject obj = (JsonObject) payload;
Set<Entry<String, JsonElement>> entries = obj.entrySet();
Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);
/*
* Prefix additional fields with "_". Everything that is not a GELF field is additional.
*/
for (Entry<String, JsonElement> entry : orgEntries) {
String key = entry.getKey();
if (GELF_FIELDS.contains(key)) {
continue;
}
JsonElement val = entry.getValue();
obj.remove(key);
obj.add("_" + key, val);
}
return ievent;
}
use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.
the class PayloadOperation method perform.
/**
* The {@link DeserializedEvent} payload must be a {@link JsonObject}.
*
* @param ievent Event that contains a JSON object deserialized payload.
* @return
*/
public InternalEvent perform(InternalEvent ievent) {
DeserializedEvent devent;
if ((devent = ievent.getEventObj()) == null) {
return null;
}
Object payload = devent.getPayload();
if (payload == null) {
return null;
}
if (!(payload instanceof JsonObject)) {
throw new OperationException("Payload data is not a JsonObject");
}
perform((JsonObject) payload);
return ievent;
}
use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.
the class GenericJsonDeserializerTest method testGetMissingField.
@Test
public void testGetMissingField() throws UnsupportedEncodingException, IOException {
String input = TestUtils.getResourceString(this.getClass(), "basic.json");
GenericJsonDeserializer deser = new GenericJsonDeserializer(Collections.emptyList());
deser.init();
DeserializedEvent event = deser.deserialize(input);
assertEquals(null, event.getField("$.not_a_member"));
}
use of com.nextdoor.bender.deserializer.DeserializedEvent in project bender by Nextdoor.
the class GenericJsonDeserializerTest method testNestedPrefix.
@Test
public void testNestedPrefix() throws UnsupportedEncodingException, IOException {
String input = TestUtils.getResourceString(this.getClass(), "nested_prefix.json");
GenericJsonDeserializerConfig.FieldConfig fconfig = new GenericJsonDeserializerConfig.FieldConfig();
fconfig.setField("MESSAGE");
fconfig.setPrefixField("MESSAGE_PREFIX");
GenericJsonDeserializer deser = new GenericJsonDeserializer(Arrays.asList(fconfig));
deser.init();
DeserializedEvent devent = deser.deserialize(input);
JsonObject obj = (JsonObject) devent.getPayload();
/*
* Nested message is there along with the prefix of the string
*/
assertTrue(obj.has("MESSAGE"));
assertTrue(obj.get("MESSAGE").isJsonObject());
assertTrue(obj.has("MESSAGE_PREFIX"));
assertTrue(obj.get("MESSAGE_PREFIX").isJsonPrimitive());
assertTrue(obj.get("MESSAGE_PREFIX").getAsJsonPrimitive().isString());
assertEquals("this is a prefix ", obj.get("MESSAGE_PREFIX").getAsString());
}
Aggregations