Search in sources :

Example 1 with LogOffset

use of co.cask.cdap.logging.read.LogOffset in project cdap by caskdata.

the class MockLogReader method generateWorkflowRunLogs.

/**
   * Generate logs for Workflow run.
   */
private void generateWorkflowRunLogs(LoggingContext loggingContext) {
    Logger logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    String programName;
    if (loggingContext instanceof WorkflowProgramLoggingContext) {
        // Logging is being done for programs running inside Workflow
        LoggingContext.SystemTag systemTag;
        systemTag = loggingContext.getSystemTagsMap().get(WorkflowProgramLoggingContext.TAG_WORKFLOW_MAP_REDUCE_ID);
        if (systemTag == null) {
            systemTag = loggingContext.getSystemTagsMap().get(WorkflowProgramLoggingContext.TAG_WORKFLOW_SPARK_ID);
        }
        programName = systemTag.getValue();
    } else {
        // Logging is done for Workflow
        programName = loggingContext.getSystemTagsMap().get(WorkflowLoggingContext.TAG_WORKFLOW_ID).getValue();
    }
    for (int i = 0; i < MAX; i++) {
        LoggingEvent event = new LoggingEvent("co.cask.Test", (ch.qos.logback.classic.Logger) logger, Level.INFO, programName + "<img>-" + i, null, null);
        Map<String, String> tagMap = Maps.newHashMap(Maps.transformValues(loggingContext.getSystemTagsMap(), TAG_TO_STRING_FUNCTION));
        event.setMDCPropertyMap(tagMap);
        logEvents.add(new LogEvent(event, new LogOffset(i, i)));
    }
}
Also used : LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) WorkflowProgramLoggingContext(co.cask.cdap.logging.context.WorkflowProgramLoggingContext) ApplicationLoggingContext(co.cask.cdap.common.logging.ApplicationLoggingContext) LoggingContext(co.cask.cdap.common.logging.LoggingContext) WorkflowLoggingContext(co.cask.cdap.logging.context.WorkflowLoggingContext) MapReduceLoggingContext(co.cask.cdap.logging.context.MapReduceLoggingContext) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) WorkflowProgramLoggingContext(co.cask.cdap.logging.context.WorkflowProgramLoggingContext) UserServiceLoggingContext(co.cask.cdap.logging.context.UserServiceLoggingContext) LogEvent(co.cask.cdap.logging.read.LogEvent) LogOffset(co.cask.cdap.logging.read.LogOffset) Logger(org.slf4j.Logger)

Example 2 with LogOffset

use of co.cask.cdap.logging.read.LogOffset in project cdap by caskdata.

the class MockLogReader method generateLogs.

/**
   * This method is used to generate the logs for program which are used for testing.
   * Single call to this method would add {@link #MAX} number of events.
   * First 20 events are generated without {@link ApplicationLoggingContext#TAG_RUN_ID} tag.
   * For next 40 events, alternate event is tagged with {@code ApplicationLoggingContext#TAG_RUN_ID}.
   * Last 20 events are not tagged with {@code ApplicationLoggingContext#TAG_RUN_ID}.
   * All events are alternately marked as {@link Level#ERROR} and {@link Level#WARN}.
   * All events are alternately tagged with "plugin", "program" and "system" as value of MDC property ".origin"
   * All events are alternately tagged with "lifecycle" as value of MDC property "MDC:eventType
   */
private void generateLogs(LoggingContext loggingContext, ProgramId programId, ProgramRunStatus runStatus) throws InterruptedException {
    // All possible values of " MDC property ".origin
    String[] origins = { "plugin", "program", "system" };
    String entityId = LoggingContextHelper.getEntityId(loggingContext).getValue();
    StackTraceElement stackTraceElementNative = new StackTraceElement("co.cask.Test", "testMethod", null, -2);
    RunId runId = null;
    Long stopTs = null;
    for (int i = 0; i < MAX; ++i) {
        // Setup run id for event with ids >= 20
        if (i == 20) {
            runId = RunIds.generate(TimeUnit.SECONDS.toMillis(getMockTimeSecs(i)));
        } else if (i == 60 && runStatus != ProgramRunStatus.RUNNING && runStatus != ProgramRunStatus.SUSPENDED) {
            // Record stop time for run for 60th event, but still continue to record run in the other logging events.
            stopTs = getMockTimeSecs(i);
        }
        LoggingEvent event = new LoggingEvent("co.cask.Test", (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), i % 2 == 0 ? Level.ERROR : Level.WARN, entityId + "<img>-" + i, null, null);
        event.setTimeStamp(TimeUnit.SECONDS.toMillis(getMockTimeSecs(i)));
        // Add runid to logging context
        Map<String, String> tagMap = Maps.newHashMap(Maps.transformValues(loggingContext.getSystemTagsMap(), TAG_TO_STRING_FUNCTION));
        if (runId != null && stopTs == null && i % 2 == 0) {
            tagMap.put(ApplicationLoggingContext.TAG_RUN_ID, runId.getId());
        }
        // Determine the value of ".origin" property by (i % 3)
        tagMap.put(".origin", origins[i % 3]);
        if (i % 2 == 0) {
            tagMap.put("MDC:eventType", "lifecycle");
        }
        if (i == 30) {
            event.setCallerData(new StackTraceElement[] { stackTraceElementNative });
        }
        event.setMDCPropertyMap(tagMap);
        logEvents.add(new LogEvent(event, new LogOffset(i, i)));
    }
    long startTs = RunIds.getTime(runId, TimeUnit.SECONDS);
    if (programId != null) {
        //noinspection ConstantConditions
        runRecordMap.put(programId, new RunRecord(runId.getId(), startTs, stopTs, runStatus, null));
        store.setStart(programId, runId.getId(), startTs);
        if (stopTs != null) {
            store.setStop(programId, runId.getId(), stopTs, runStatus);
        }
    }
}
Also used : LogEvent(co.cask.cdap.logging.read.LogEvent) LogOffset(co.cask.cdap.logging.read.LogOffset) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) RunRecord(co.cask.cdap.proto.RunRecord) RunId(org.apache.twill.api.RunId)

Example 3 with LogOffset

use of co.cask.cdap.logging.read.LogOffset in project cdap by caskdata.

the class AbstractLogHandler method doPrev.

protected void doPrev(HttpResponder responder, LoggingContext loggingContext, int maxEvents, String fromOffsetStr, boolean escape, String filterStr, @Nullable RunRecordMeta runRecord, String format, List<String> fieldsToSuppress) {
    try {
        Filter filter = FilterParser.parse(filterStr);
        Callback logCallback = getNextOrPrevLogsCallback(format, responder, fieldsToSuppress, escape);
        LogOffset logOffset = FormattedTextLogEvent.parseLogOffset(fromOffsetStr);
        ReadRange readRange = ReadRange.createToRange(logOffset);
        readRange = adjustReadRange(readRange, runRecord, true);
        try {
            logReader.getLogPrev(loggingContext, readRange, maxEvents, filter, logCallback);
        } catch (Exception ex) {
            LOG.debug("Exception while reading logs for logging context {}", loggingContext, ex);
        } finally {
            logCallback.close();
        }
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    }
}
Also used : Callback(co.cask.cdap.logging.read.Callback) ReadRange(co.cask.cdap.logging.read.ReadRange) Filter(co.cask.cdap.logging.filter.Filter) LogOffset(co.cask.cdap.logging.read.LogOffset)

Example 4 with LogOffset

use of co.cask.cdap.logging.read.LogOffset in project cdap by caskdata.

the class LogLocation method readToEndSyncPosition.

/**
   *  Read current block in Avro file from current block sync marker to next block sync marker
   */
private List<LogEvent> readToEndSyncPosition(DataFileReader<GenericRecord> dataFileReader, Filter logFilter, long fromTimeMs, long endSyncPosition) throws IOException {
    List<LogEvent> logSegment = new ArrayList<>();
    long currentSyncPosition = dataFileReader.previousSync();
    // or read until endSyncPosition has been reached
    while (dataFileReader.hasNext() && (endSyncPosition == -1 || (currentSyncPosition < endSyncPosition))) {
        ILoggingEvent loggingEvent = new LoggingEvent(dataFileReader.next());
        loggingEvent.prepareForDeferredProcessing();
        // Stop when reached fromTimeMs
        if (loggingEvent.getTimeStamp() > fromTimeMs) {
            break;
        }
        if (logFilter.match(loggingEvent)) {
            logSegment.add(new LogEvent(loggingEvent, new LogOffset(LogOffset.INVALID_KAFKA_OFFSET, loggingEvent.getTimeStamp())));
        }
        currentSyncPosition = dataFileReader.previousSync();
    }
    return logSegment;
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(co.cask.cdap.logging.serialize.LoggingEvent) LogEvent(co.cask.cdap.logging.read.LogEvent) LogOffset(co.cask.cdap.logging.read.LogOffset) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 5 with LogOffset

use of co.cask.cdap.logging.read.LogOffset in project cdap by caskdata.

the class AbstractLogHandler method doNext.

protected void doNext(HttpResponder responder, LoggingContext loggingContext, int maxEvents, String fromOffsetStr, boolean escape, String filterStr, @Nullable RunRecordMeta runRecord, String format, List<String> fieldsToSuppress) {
    try {
        Filter filter = FilterParser.parse(filterStr);
        Callback logCallback = getNextOrPrevLogsCallback(format, responder, fieldsToSuppress, escape);
        LogOffset logOffset = FormattedTextLogEvent.parseLogOffset(fromOffsetStr);
        ReadRange readRange = ReadRange.createFromRange(logOffset);
        readRange = adjustReadRange(readRange, runRecord, true);
        try {
            logReader.getLogNext(loggingContext, readRange, maxEvents, filter, logCallback);
        } catch (Exception ex) {
            LOG.debug("Exception while reading logs for logging context {}", loggingContext, ex);
        } finally {
            logCallback.close();
        }
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    }
}
Also used : Callback(co.cask.cdap.logging.read.Callback) ReadRange(co.cask.cdap.logging.read.ReadRange) Filter(co.cask.cdap.logging.filter.Filter) LogOffset(co.cask.cdap.logging.read.LogOffset)

Aggregations

LogOffset (co.cask.cdap.logging.read.LogOffset)7 LogEvent (co.cask.cdap.logging.read.LogEvent)5 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)2 Filter (co.cask.cdap.logging.filter.Filter)2 Callback (co.cask.cdap.logging.read.Callback)2 ReadRange (co.cask.cdap.logging.read.ReadRange)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ApplicationLoggingContext (co.cask.cdap.common.logging.ApplicationLoggingContext)1 LoggingContext (co.cask.cdap.common.logging.LoggingContext)1 FlowletLoggingContext (co.cask.cdap.logging.context.FlowletLoggingContext)1 MapReduceLoggingContext (co.cask.cdap.logging.context.MapReduceLoggingContext)1 UserServiceLoggingContext (co.cask.cdap.logging.context.UserServiceLoggingContext)1 WorkflowLoggingContext (co.cask.cdap.logging.context.WorkflowLoggingContext)1 WorkflowProgramLoggingContext (co.cask.cdap.logging.context.WorkflowProgramLoggingContext)1 LoggingEvent (co.cask.cdap.logging.serialize.LoggingEvent)1 RunRecord (co.cask.cdap.proto.RunRecord)1 ArrayList (java.util.ArrayList)1 RunId (org.apache.twill.api.RunId)1 Logger (org.slf4j.Logger)1