Search in sources :

Example 1 with MessageFilter

use of org.graylog2.plugin.filters.MessageFilter in project graylog2-server by Graylog2.

the class MessageFilterChainProcessor method process.

@Override
public Messages process(Messages messages) {
    for (final MessageFilter filter : filterRegistry) {
        for (Message msg : messages) {
            final String timerName = name(filter.getClass(), "executionTime");
            final Timer timer = metricRegistry.timer(timerName);
            final Timer.Context timerContext = timer.time();
            try {
                LOG.debug("Applying filter [{}] on message <{}>.", filter.getName(), msg.getId());
                if (filter.filter(msg)) {
                    LOG.debug("Filter [{}] marked message <{}> to be discarded. Dropping message.", filter.getName(), msg.getId());
                    msg.setFilterOut(true);
                    filteredOutMessages.mark();
                    journal.markJournalOffsetCommitted(msg.getJournalOffset());
                }
            } catch (Exception e) {
                LOG.error("Could not apply filter [" + filter.getName() + "] on message <" + msg.getId() + ">: ", e);
            } finally {
                final long elapsedNanos = timerContext.stop();
                msg.recordTiming(serverStatus, timerName, elapsedNanos);
            }
        }
    }
    return messages;
}
Also used : Message(org.graylog2.plugin.Message) Timer(com.codahale.metrics.Timer) MessageFilter(org.graylog2.plugin.filters.MessageFilter)

Example 2 with MessageFilter

use of org.graylog2.plugin.filters.MessageFilter in project graylog2-server by Graylog2.

the class MessageFilterChainProcessorTest method testFiltersAreOrdered.

@Test
public void testFiltersAreOrdered() {
    final DummyFilter third = new DummyFilter(30);
    final DummyFilter first = new DummyFilter(10);
    final DummyFilter second = new DummyFilter(20);
    final Set<MessageFilter> filters = ImmutableSet.of(third, first, second);
    final MessageFilterChainProcessor processor = new MessageFilterChainProcessor(new MetricRegistry(), filters, journal, serverStatus);
    final List<MessageFilter> filterRegistry = processor.getFilterRegistry();
    Assert.assertEquals(filterRegistry.get(0), first);
    Assert.assertEquals(filterRegistry.get(1), second);
    Assert.assertEquals(filterRegistry.get(2), third);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) Test(org.junit.Test)

Example 3 with MessageFilter

use of org.graylog2.plugin.filters.MessageFilter in project graylog2-server by Graylog2.

the class MessageFilterChainProcessorTest method testHandleMessage.

@Test
public void testHandleMessage() {
    MessageFilter filterOnlyFirst = new MessageFilter() {

        private boolean filterOut = true;

        @Override
        public boolean filter(Message msg) {
            if (filterOut) {
                msg.setFilterOut(true);
                filterOut = false;
                return true;
            }
            return false;
        }

        @Override
        public String getName() {
            return "first filtered out, subsequent pass";
        }

        @Override
        public int getPriority() {
            return 0;
        }
    };
    final MessageFilterChainProcessor filterTest = new MessageFilterChainProcessor(new MetricRegistry(), Collections.singleton(filterOnlyFirst), journal, serverStatus);
    Message filteredoutMessage = new Message("filtered out", "source", Tools.nowUTC());
    filteredoutMessage.setJournalOffset(1);
    Message unfilteredMessage = new Message("filtered out", "source", Tools.nowUTC());
    final Messages messages1 = filterTest.process(filteredoutMessage);
    final Messages messages2 = filterTest.process(unfilteredMessage);
    Assert.assertTrue(filteredoutMessage.getFilterOut());
    Assert.assertFalse(unfilteredMessage.getFilterOut());
    Assert.assertEquals(0, Iterables.size(messages1));
    Assert.assertEquals(1, Iterables.size(messages2));
}
Also used : Messages(org.graylog2.plugin.Messages) Message(org.graylog2.plugin.Message) MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) Test(org.junit.Test)

Example 4 with MessageFilter

use of org.graylog2.plugin.filters.MessageFilter in project graylog2-server by Graylog2.

the class MessageFilterChainProcessorTest method testAllFiltersAreBeingRun.

@Test
public void testAllFiltersAreBeingRun() {
    final DummyFilter first = new DummyFilter(10);
    final DummyFilter second = new DummyFilter(20);
    final DummyFilter third = new DummyFilter(30);
    final Set<MessageFilter> filters = ImmutableSet.of(first, second, third);
    final MessageFilterChainProcessor processor = new MessageFilterChainProcessor(new MetricRegistry(), filters, journal, serverStatus);
    final Message message = new Message("message", "source", new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC));
    final Message result = Iterables.getFirst(processor.process(message), null);
    assertThat(result).isNotNull();
    assertThat(result.getFields()).containsKeys("prio-10", "prio-20", "prio-30");
}
Also used : Message(org.graylog2.plugin.Message) MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with MessageFilter

use of org.graylog2.plugin.filters.MessageFilter in project graylog2-server by Graylog2.

the class MessageFilterChainProcessorTest method testMessagesCanBeDropped.

@Test
public void testMessagesCanBeDropped() {
    final MessageFilter first = new DummyFilter(10);
    final MessageFilter second = new RemovingMessageFilter();
    final Set<MessageFilter> filters = ImmutableSet.of(first, second);
    final MessageFilterChainProcessor processor = new MessageFilterChainProcessor(new MetricRegistry(), filters, journal, serverStatus);
    final Message message = new Message("message", "source", new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC));
    final Messages result = processor.process(message);
    assertThat(result).isEmpty();
}
Also used : Messages(org.graylog2.plugin.Messages) Message(org.graylog2.plugin.Message) MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

MessageFilter (org.graylog2.plugin.filters.MessageFilter)6 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Message (org.graylog2.plugin.Message)4 Test (org.junit.Test)4 Messages (org.graylog2.plugin.Messages)2 DateTime (org.joda.time.DateTime)2 Timer (com.codahale.metrics.Timer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1