Search in sources :

Example 1 with Stat

use of com.nextdoor.bender.monitoring.Stat 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();
        }
    }
}
Also used : Monitor(com.nextdoor.bender.monitoring.Monitor) Spliterators(java.util.Spliterators) Wrapper(com.nextdoor.bender.wrapper.Wrapper) Context(com.amazonaws.services.lambda.runtime.Context) Stat(com.nextdoor.bender.monitoring.Stat) InternalEvent(com.nextdoor.bender.InternalEvent) OperationProcessor(com.nextdoor.bender.operation.OperationProcessor) ArrayList(java.util.ArrayList) IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) Logger(org.apache.log4j.Logger) Matcher(java.util.regex.Matcher) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) AmazonS3ClientFactory(com.nextdoor.bender.aws.AmazonS3ClientFactory) TransportException(com.nextdoor.bender.ipc.TransportException) BenderConfig(com.nextdoor.bender.config.BenderConfig) StreamSupport(java.util.stream.StreamSupport) ManagementFactory(java.lang.management.ManagementFactory) DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) Iterator(java.util.Iterator) IOException(java.io.IOException) SerializerProcessor(com.nextdoor.bender.serializer.SerializerProcessor) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) Objects(java.util.Objects) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) SerializationException(com.nextdoor.bender.serializer.SerializationException) BenderLayout(com.nextdoor.bender.logging.BenderLayout) Pattern(java.util.regex.Pattern) Source(com.nextdoor.bender.config.Source) Spliterator(java.util.Spliterator) DeserializerProcessor(com.nextdoor.bender.deserializer.DeserializerProcessor) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) HandlerResources(com.nextdoor.bender.config.HandlerResources) Pattern(java.util.regex.Pattern) DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) SerializationException(com.nextdoor.bender.serializer.SerializationException) Matcher(java.util.regex.Matcher) OperationProcessor(com.nextdoor.bender.operation.OperationProcessor) TransportException(com.nextdoor.bender.ipc.TransportException) Source(com.nextdoor.bender.config.Source) InternalEvent(com.nextdoor.bender.InternalEvent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stat(com.nextdoor.bender.monitoring.Stat) Objects(java.util.Objects) DeserializerProcessor(com.nextdoor.bender.deserializer.DeserializerProcessor)

Example 2 with Stat

use of com.nextdoor.bender.monitoring.Stat in project bender by Nextdoor.

the class DeserializerProcessorTest method testOnUnexpectedError.

@Test(expected = RuntimeException.class)
public void testOnUnexpectedError() {
    DummyDeserializer mockDeser = mock(DummyDeserializer.class);
    when(mockDeser.deserialize("foo")).thenThrow(new RuntimeException("unexpected"));
    DeserializerProcessor deser = new DeserializerProcessor(mockDeser);
    /*
     * Mock the Stat object
     */
    Stat runtimeStat = mock(Stat.class);
    Stat successStat = mock(Stat.class);
    Stat errorStat = mock(Stat.class);
    deser.setRuntimeStat(runtimeStat);
    deser.setSuccessCountStat(successStat);
    deser.setErrorCountStat(errorStat);
    try {
        deser.deserialize("foo");
    } catch (DeserializationException e) {
    // expected
    }
}
Also used : DummyDeserializer(com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyDeserializer) Stat(com.nextdoor.bender.monitoring.Stat) Test(org.junit.Test)

Example 3 with Stat

use of com.nextdoor.bender.monitoring.Stat in project bender by Nextdoor.

the class OperationProcessorTest method testStatsLoggingOnError.

@Test
public void testStatsLoggingOnError() {
    DummyOperation operation = mock(DummyOperation.class);
    DummyOperationFactory mutatorFactory = new DummyOperationFactory(operation);
    OperationProcessor processor = new OperationProcessor(mutatorFactory);
    InternalEvent ievent = new InternalEvent("a", null, 1);
    doThrow(new OperationException("Expceted")).when(operation).perform(ievent);
    /*
     * Mock the Stat object
     */
    Stat runtimeStat = mock(Stat.class);
    Stat successStat = mock(Stat.class);
    Stat errorStat = mock(Stat.class);
    processor.setRuntimeStat(runtimeStat);
    processor.setSuccessCountStat(successStat);
    processor.setErrorCountStat(errorStat);
    Stream<InternalEvent> stream = processor.perform(Stream.of(ievent));
    List<InternalEvent> output = stream.collect(Collectors.toList());
    /*
     * Verify start, stop are called, increment error count and never increment success count.
     */
    verify(runtimeStat, times(1)).start();
    verify(runtimeStat, times(1)).stop();
    verify(successStat, never()).increment();
    verify(errorStat, times(1)).increment();
    /*
     * Verify contents of output stream
     */
    assertEquals(0, output.size());
}
Also used : Stat(com.nextdoor.bender.monitoring.Stat) DummyOperationFactory(com.nextdoor.bender.testutils.DummyOperationHelper.DummyOperationFactory) DummyOperation(com.nextdoor.bender.testutils.DummyOperationHelper.DummyOperation) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Example 4 with Stat

use of com.nextdoor.bender.monitoring.Stat in project bender by Nextdoor.

the class IpcSenderServiceTest method testStatsLoggingOnError.

@Test
public void testStatsLoggingOnError() throws InstantiationException, IllegalAccessException, InterruptedException, TransportException {
    DummyTransporter mockDummyTransporter = mock(DummyTransporter.class);
    DummyTransporterFactory tfactory = new DummyTransporterFactory();
    tfactory.transporter = mockDummyTransporter;
    doThrow(new TransportException("expected exception in test")).when(mockDummyTransporter).sendBatch(any(DummyTransportBuffer.class));
    IpcSenderService ipc = new IpcSenderService(tfactory);
    /*
     * Mock the Stat object
     */
    Stat successStat = mock(Stat.class);
    Stat errorStat = mock(Stat.class);
    ipc.setSuccessCountStat(successStat);
    ipc.setErrorCountStat(errorStat);
    /*
     * Every 5 adds a send should happen.
     */
    for (int i = 0; i < 5; i++) {
        ipc.add(mock(InternalEvent.class));
    }
    try {
        ipc.shutdown();
    } catch (TransportException e) {
    // expected
    }
    /*
     * The sendBatch method will be called twice and each will throw an error. Verify that error
     * counting happens as expected.
     */
    verify(successStat, never()).increment();
    verify(errorStat, times(1)).increment();
}
Also used : Stat(com.nextdoor.bender.monitoring.Stat) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Example 5 with Stat

use of com.nextdoor.bender.monitoring.Stat in project bender by Nextdoor.

the class SerializerProcessorTest method testStatsLoggingOnError.

@Test
public void testStatsLoggingOnError() {
    DummySerializer serializer = mock(DummySerializer.class);
    SerializerProcessor processor = new SerializerProcessor(serializer);
    doThrow(new RuntimeException()).when(serializer).serialize("foo");
    /*
     * Mock the Stat object
     */
    Stat runtimeStat = mock(Stat.class);
    Stat successStat = mock(Stat.class);
    Stat errorStat = mock(Stat.class);
    processor.setRuntimeStat(runtimeStat);
    processor.setSuccessCountStat(successStat);
    processor.setErrorCountStat(errorStat);
    try {
        processor.serialize("foo");
    } catch (Exception e) {
    // expected
    }
    /*
     * Verify start, stop are called, increment error count and never increment success count.
     */
    verify(runtimeStat, times(1)).start();
    verify(runtimeStat, times(1)).stop();
    verify(successStat, never()).increment();
    verify(errorStat, times(1)).increment();
}
Also used : DummySerializer(com.nextdoor.bender.testutils.DummySerializerHelper.DummySerializer) Stat(com.nextdoor.bender.monitoring.Stat) Test(org.junit.Test)

Aggregations

Stat (com.nextdoor.bender.monitoring.Stat)14 Test (org.junit.Test)10 InternalEvent (com.nextdoor.bender.InternalEvent)5 DummyDeserializer (com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyDeserializer)3 DummySerializer (com.nextdoor.bender.testutils.DummySerializerHelper.DummySerializer)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DummyOperationFactory (com.nextdoor.bender.testutils.DummyOperationHelper.DummyOperationFactory)2 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)1 MetricDatum (com.amazonaws.services.cloudwatch.model.MetricDatum)1 PutMetricDataRequest (com.amazonaws.services.cloudwatch.model.PutMetricDataRequest)1 Context (com.amazonaws.services.lambda.runtime.Context)1 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 AmazonS3ClientFactory (com.nextdoor.bender.aws.AmazonS3ClientFactory)1 BenderConfig (com.nextdoor.bender.config.BenderConfig)1 ConfigurationException (com.nextdoor.bender.config.ConfigurationException)1 HandlerResources (com.nextdoor.bender.config.HandlerResources)1 Source (com.nextdoor.bender.config.Source)1 DeserializedEvent (com.nextdoor.bender.deserializer.DeserializedEvent)1 DeserializerProcessor (com.nextdoor.bender.deserializer.DeserializerProcessor)1