Search in sources :

Example 6 with LogEvent

use of io.cdap.cdap.logging.read.LogEvent in project cdap by caskdata.

the class AbstractLogHttpHandler method doGetLogs.

protected void doGetLogs(LogReader logReader, HttpResponder responder, LoggingContext loggingContext, long fromTimeSecsParam, long toTimeSecsParam, boolean escape, String filterStr, @Nullable RunRecordDetail runRecord, String format, List<String> fieldsToSuppress) {
    try {
        TimeRange timeRange = parseTime(fromTimeSecsParam, toTimeSecsParam, responder);
        if (timeRange == null) {
            return;
        }
        Filter filter = FilterParser.parse(filterStr);
        ReadRange readRange = new ReadRange(timeRange.getFromMillis(), timeRange.getToMillis(), LogOffset.INVALID_KAFKA_OFFSET);
        readRange = adjustReadRange(readRange, runRecord, fromTimeSecsParam != -1);
        try {
            // the iterator is closed by the BodyProducer passed to the HttpResponder
            CloseableIterator<LogEvent> logIter = logReader.getLog(loggingContext, readRange.getFromMillis(), readRange.getToMillis(), filter);
            AbstractChunkedLogProducer logsProducer = getFullLogsProducer(format, logIter, fieldsToSuppress, escape);
            responder.sendContent(HttpResponseStatus.OK, logsProducer, logsProducer.getResponseHeaders());
        } catch (Exception ex) {
            LOG.debug("Exception while reading logs for logging context {}", loggingContext, ex);
            responder.sendStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
        }
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    }
}
Also used : ReadRange(io.cdap.cdap.logging.read.ReadRange) Filter(io.cdap.cdap.logging.filter.Filter) LogEvent(io.cdap.cdap.logging.read.LogEvent)

Example 7 with LogEvent

use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.

the class TextChunkedLogProducer method writeLogEvents.

@Override
protected ByteBuf writeLogEvents(CloseableIterator<LogEvent> logEventIter) throws IOException {
    ByteBuf buffer = Unpooled.buffer(BUFFER_BYTES);
    while (logEventIter.hasNext() && buffer.readableBytes() < BUFFER_BYTES) {
        LogEvent logEvent = logEventIter.next();
        String logLine = patternLayout.doLayout(logEvent.getLoggingEvent());
        logLine = escape ? StringEscapeUtils.escapeHtml(logLine) : logLine;
        buffer.writeCharSequence(logLine, StandardCharsets.UTF_8);
    }
    return buffer;
}
Also used : LogEvent(io.cdap.cdap.logging.read.LogEvent) ByteBuf(io.netty.buffer.ByteBuf)

Example 8 with LogEvent

use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.

the class CDAPLogAppenderTest method assertLogEventDetails.

private void assertLogEventDetails(LoggingEvent expectedLoggingEvent, LogLocation logLocation) throws IOException {
    Assert.assertEquals(LogLocation.VERSION_1, logLocation.getFrameworkVersion());
    Assert.assertTrue(logLocation.getLocation().exists());
    CloseableIterator<LogEvent> logEventCloseableIterator = logLocation.readLog(Filter.EMPTY_FILTER, 0, Long.MAX_VALUE, Integer.MAX_VALUE);
    int logCount = 0;
    while (logEventCloseableIterator.hasNext()) {
        logCount++;
        LogEvent logEvent = logEventCloseableIterator.next();
        Assert.assertEquals(expectedLoggingEvent.getMessage(), logEvent.getLoggingEvent().getMessage());
    }
    logEventCloseableIterator.close();
    Assert.assertEquals(1, logCount);
}
Also used : LogEvent(io.cdap.cdap.logging.read.LogEvent)

Example 9 with LogEvent

use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.

the class TestDistributedLogReader method generateCheckpointTime.

private static void generateCheckpointTime(LoggingContext loggingContext, int numExpectedEvents, String kafkaTopic) throws IOException {
    FileLogReader logReader = injector.getInstance(FileLogReader.class);
    List<LogEvent> events = Lists.newArrayList(logReader.getLog(loggingContext, 0, Long.MAX_VALUE, Filter.EMPTY_FILTER));
    Assert.assertEquals(numExpectedEvents, events.size());
    // Save checkpoint (time of last event)
    TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class);
    CheckpointManager<KafkaOffset> checkpointManager = new KafkaCheckpointManager(transactionRunner, Constants.Logging.SYSTEM_PIPELINE_CHECKPOINT_PREFIX + kafkaTopic);
    long checkpointTime = events.get(numExpectedEvents - 1).getLoggingEvent().getTimeStamp();
    checkpointManager.saveCheckpoints(ImmutableMap.of(stringPartitioner.partition(loggingContext.getLogPartition(), -1), new Checkpoint<>(new KafkaOffset(numExpectedEvents, checkpointTime), checkpointTime)));
}
Also used : KafkaCheckpointManager(io.cdap.cdap.logging.meta.KafkaCheckpointManager) Checkpoint(io.cdap.cdap.logging.meta.Checkpoint) LogEvent(io.cdap.cdap.logging.read.LogEvent) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) KafkaOffset(io.cdap.cdap.logging.meta.KafkaOffset) FileLogReader(io.cdap.cdap.logging.read.FileLogReader)

Example 10 with LogEvent

use of io.cdap.cdap.logging.read.LogEvent in project cdap by cdapio.

the class TestDistributedLogReader method testDistributedLogPrev.

private void testDistributedLogPrev(ReadRange readRange, LoggingContext loggingContext, int numCalls, int step, String assertMessage, int assertCount) throws Exception {
    DistributedLogReader distributedLogReader = injector.getInstance(DistributedLogReader.class);
    for (int i = 0; i < numCalls; ++i) {
        LoggingTester.LogCallback callback = new LoggingTester.LogCallback();
        distributedLogReader.getLogPrev(loggingContext, readRange, step, Filter.EMPTY_FILTER, callback);
        List<LogEvent> events = callback.getEvents();
        Assert.assertFalse(events.isEmpty());
        readRange = ReadRange.createToRange(events.get(0).getOffset());
        Collections.reverse(events);
        for (LogEvent event : events) {
            Assert.assertEquals(assertMessage + --assertCount, event.getLoggingEvent().getFormattedMessage());
            // All logs generated by this test have value "system" in MDC property ".origin"
            Assert.assertEquals("system", event.getLoggingEvent().getMDCPropertyMap().get(".origin"));
        }
    }
    Assert.assertEquals(0, assertCount);
}
Also used : DistributedLogReader(io.cdap.cdap.logging.read.DistributedLogReader) LogEvent(io.cdap.cdap.logging.read.LogEvent) Checkpoint(io.cdap.cdap.logging.meta.Checkpoint)

Aggregations

LogEvent (io.cdap.cdap.logging.read.LogEvent)36 LogOffset (io.cdap.cdap.logging.read.LogOffset)10 LoggingContext (io.cdap.cdap.common.logging.LoggingContext)8 Checkpoint (io.cdap.cdap.logging.meta.Checkpoint)8 Test (org.junit.Test)8 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)6 WorkerLoggingContext (io.cdap.cdap.logging.context.WorkerLoggingContext)6 Filter (io.cdap.cdap.logging.filter.Filter)6 FileLogReader (io.cdap.cdap.logging.read.FileLogReader)6 ReadRange (io.cdap.cdap.logging.read.ReadRange)6 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 DistributedLogReader (io.cdap.cdap.logging.read.DistributedLogReader)4 TransactionRunner (io.cdap.cdap.spi.data.transaction.TransactionRunner)4 FileMetaDataReader (io.cdap.cdap.logging.meta.FileMetaDataReader)3 KafkaOffset (io.cdap.cdap.logging.meta.KafkaOffset)3 LogLocation (io.cdap.cdap.logging.write.LogLocation)3 IOException (java.io.IOException)3 Logger (org.slf4j.Logger)3 LoggerContext (ch.qos.logback.classic.LoggerContext)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2