Search in sources :

Example 1 with WorkflowProgramLoggingContext

use of co.cask.cdap.logging.context.WorkflowProgramLoggingContext 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 WorkflowProgramLoggingContext

use of co.cask.cdap.logging.context.WorkflowProgramLoggingContext in project cdap by caskdata.

the class MockLogReader method generateWorkflowLogs.

/**
 * Generate Workflow logs.
 */
private void generateWorkflowLogs() {
    ProgramId workflowId = SOME_WORKFLOW_APP.workflow(SOME_WORKFLOW);
    long currentTime = TimeUnit.SECONDS.toMillis(10);
    RunId workflowRunId = RunIds.generate();
    setStartAndRunning(workflowId.run(workflowRunId.getId()), currentTime);
    runRecordMap.put(workflowId, store.getRun(workflowId.run(workflowRunId.getId())));
    WorkflowLoggingContext wfLoggingContext = new WorkflowLoggingContext(workflowId.getNamespace(), workflowId.getApplication(), workflowId.getProgram(), workflowRunId.getId());
    generateWorkflowRunLogs(wfLoggingContext);
    // Generate logs for MapReduce program started by above Workflow run
    ProgramId mapReduceId = SOME_WORKFLOW_APP.mr(SOME_MAPREDUCE);
    currentTime = TimeUnit.SECONDS.toMillis(20);
    RunId mapReduceRunId = RunIds.generate();
    Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.WORKFLOW_NODE_ID, SOME_MAPREDUCE, ProgramOptionConstants.WORKFLOW_NAME, SOME_WORKFLOW, ProgramOptionConstants.WORKFLOW_RUN_ID, workflowRunId.getId());
    setStartAndRunning(mapReduceId.run(mapReduceRunId.getId()), currentTime, new HashMap<>(), systemArgs);
    runRecordMap.put(mapReduceId, store.getRun(mapReduceId.run(mapReduceRunId.getId())));
    WorkflowProgramLoggingContext context = new WorkflowProgramLoggingContext(workflowId.getNamespace(), workflowId.getApplication(), workflowId.getProgram(), workflowRunId.getId(), ProgramType.MAPREDUCE, SOME_MAPREDUCE, mapReduceRunId.getId());
    generateWorkflowRunLogs(context);
    // Generate logs for Spark program started by Workflow run above
    ProgramId sparkId = SOME_WORKFLOW_APP.spark(SOME_SPARK);
    currentTime = TimeUnit.SECONDS.toMillis(40);
    RunId sparkRunId = RunIds.generate();
    systemArgs = ImmutableMap.of(ProgramOptionConstants.WORKFLOW_NODE_ID, SOME_SPARK, ProgramOptionConstants.WORKFLOW_NAME, SOME_WORKFLOW, ProgramOptionConstants.WORKFLOW_RUN_ID, workflowRunId.getId());
    setStartAndRunning(sparkId.run(sparkRunId.getId()), currentTime, new HashMap<>(), systemArgs);
    runRecordMap.put(sparkId, store.getRun(sparkId.run(sparkRunId.getId())));
    context = new WorkflowProgramLoggingContext(workflowId.getNamespace(), workflowId.getApplication(), workflowId.getProgram(), workflowRunId.getId(), ProgramType.SPARK, SOME_SPARK, sparkRunId.getId());
    generateWorkflowRunLogs(context);
    // Generate some more logs for Workflow
    generateWorkflowRunLogs(wfLoggingContext);
}
Also used : WorkflowProgramLoggingContext(co.cask.cdap.logging.context.WorkflowProgramLoggingContext) WorkflowLoggingContext(co.cask.cdap.logging.context.WorkflowLoggingContext) ProgramId(co.cask.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId)

Example 3 with WorkflowProgramLoggingContext

use of co.cask.cdap.logging.context.WorkflowProgramLoggingContext in project cdap by caskdata.

the class KafkaLogProcessorPipelineTest method testMetricsAppender.

@Test
public void testMetricsAppender() throws Exception {
    Injector injector = KAFKA_TESTER.getInjector();
    MetricsCollectionService collectionService = injector.getInstance(MetricsCollectionService.class);
    collectionService.startAndWait();
    LoggerContext loggerContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), injector.getInstance(MetricsCollectionService.class));
    final File logDir = TEMP_FOLDER.newFolder();
    loggerContext.putProperty("logDirectory", logDir.getAbsolutePath());
    LogPipelineConfigurator configurator = new LogPipelineConfigurator(CConfiguration.create());
    configurator.setContext(loggerContext);
    URL configURL = getClass().getClassLoader().getResource("pipeline-metric-appender.xml");
    Assert.assertNotNull(configURL);
    configurator.doConfigure(configURL);
    String topic = "metricsPipeline";
    TestCheckpointManager checkpointManager = new TestCheckpointManager();
    KafkaPipelineConfig config = new KafkaPipelineConfig(topic, Collections.singleton(0), 1024L, 100L, 1048576, 200L);
    KAFKA_TESTER.createTopic(topic, 1);
    loggerContext.start();
    KafkaLogProcessorPipeline pipeline = new KafkaLogProcessorPipeline(new LogProcessorPipelineContext(CConfiguration.create(), "testMetricAppender", loggerContext, NO_OP_METRICS_CONTEXT, 0), checkpointManager, KAFKA_TESTER.getBrokerService(), config);
    pipeline.startAndWait();
    // Publish some log messages to Kafka
    long now = System.currentTimeMillis();
    FlowletLoggingContext flowletLoggingContext = new FlowletLoggingContext("default", "app1", "flow1", "flowlet1", "run1", "instance1");
    publishLog(topic, ImmutableList.of(createLoggingEvent("test.logger", Level.INFO, "0", now - 1000), createLoggingEvent("test.logger", Level.INFO, "2", now - 700), createLoggingEvent("test.logger", Level.INFO, "3", now - 500), createLoggingEvent("test.logger", Level.INFO, "1", now - 900), createLoggingEvent("test.logger", Level.DEBUG, "hidden", now - 600), createLoggingEvent("test.logger", Level.INFO, "4", now - 100)), flowletLoggingContext);
    WorkflowProgramLoggingContext workflowProgramLoggingContext = new WorkflowProgramLoggingContext("default", "app1", "wflow1", "run1", ProgramType.MAPREDUCE, "mr1", "mrun1");
    publishLog(topic, ImmutableList.of(createLoggingEvent("test.logger", Level.WARN, "0", now - 1000), createLoggingEvent("test.logger", Level.WARN, "2", now - 700), createLoggingEvent("test.logger", Level.TRACE, "3", now - 500)), workflowProgramLoggingContext);
    ServiceLoggingContext serviceLoggingContext = new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.TRANSACTION);
    publishLog(topic, ImmutableList.of(createLoggingEvent("test.logger", Level.ERROR, "0", now - 1000), createLoggingEvent("test.logger", Level.ERROR, "2", now - 700), createLoggingEvent("test.logger", Level.ERROR, "3", now - 500), createLoggingEvent("test.logger", Level.INFO, "1", now - 900)), serviceLoggingContext);
    final MetricStore metricStore = injector.getInstance(MetricStore.class);
    try {
        verifyMetricsWithRetry(metricStore, new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, "system.app.log.info", AggregationFunction.SUM, LoggingContextHelper.getMetricsTags(flowletLoggingContext), new ArrayList<String>()), 5L);
        verifyMetricsWithRetry(metricStore, new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, "system.app.log.debug", AggregationFunction.SUM, LoggingContextHelper.getMetricsTags(flowletLoggingContext), new ArrayList<String>()), 1L);
        verifyMetricsWithRetry(metricStore, new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, "system.app.log.warn", AggregationFunction.SUM, // mapreduce metrics context
        ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, "default", Constants.Metrics.Tag.APP, "app1", Constants.Metrics.Tag.MAPREDUCE, "mr1", Constants.Metrics.Tag.RUN_ID, "mrun1"), new ArrayList<String>()), 2L);
        verifyMetricsWithRetry(metricStore, new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, "system.app.log.trace", AggregationFunction.SUM, // workflow metrics context
        ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, "default", Constants.Metrics.Tag.APP, "app1", Constants.Metrics.Tag.WORKFLOW, "wflow1", Constants.Metrics.Tag.RUN_ID, "run1"), new ArrayList<String>()), 1L);
        verifyMetricsWithRetry(metricStore, new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, "system.services.log.error", AggregationFunction.SUM, LoggingContextHelper.getMetricsTags(serviceLoggingContext), new ArrayList<String>()), 3L);
    } finally {
        pipeline.stopAndWait();
        loggerContext.stop();
        collectionService.stopAndWait();
    }
}
Also used : DefaultMetricStore(co.cask.cdap.metrics.store.DefaultMetricStore) MetricStore(co.cask.cdap.api.metrics.MetricStore) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) LocalMetricsCollectionService(co.cask.cdap.metrics.collect.LocalMetricsCollectionService) ArrayList(java.util.ArrayList) LogProcessorPipelineContext(co.cask.cdap.logging.pipeline.LogProcessorPipelineContext) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) LoggerContext(ch.qos.logback.classic.LoggerContext) URL(java.net.URL) LocationFactory(org.apache.twill.filesystem.LocationFactory) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) WorkflowProgramLoggingContext(co.cask.cdap.logging.context.WorkflowProgramLoggingContext) Injector(com.google.inject.Injector) FlowletLoggingContext(co.cask.cdap.logging.context.FlowletLoggingContext) MetricDataQuery(co.cask.cdap.api.metrics.MetricDataQuery) File(java.io.File) LogPipelineConfigurator(co.cask.cdap.logging.pipeline.LogPipelineConfigurator) Test(org.junit.Test)

Example 4 with WorkflowProgramLoggingContext

use of co.cask.cdap.logging.context.WorkflowProgramLoggingContext in project cdap by caskdata.

the class MapReduceClassLoader method createMapReduceLoggingContext.

/**
 * Creates logging context for MapReduce program. If the program is started
 * by Workflow an instance of {@link WorkflowProgramLoggingContext} is returned,
 * otherwise an instance of {@link MapReduceLoggingContext} is returned.
 */
private LoggingContext createMapReduceLoggingContext() {
    MapReduceContextConfig contextConfig = new MapReduceContextConfig(parameters.getHConf());
    ProgramId programId = contextConfig.getProgramId();
    RunId runId = ProgramRunners.getRunId(contextConfig.getProgramOptions());
    WorkflowProgramInfo workflowProgramInfo = contextConfig.getWorkflowProgramInfo();
    if (workflowProgramInfo == null) {
        return new MapReduceLoggingContext(programId.getNamespace(), programId.getApplication(), programId.getProgram(), runId.getId());
    }
    String workflowId = workflowProgramInfo.getName();
    String workflowRunId = workflowProgramInfo.getRunId().getId();
    return new WorkflowProgramLoggingContext(programId.getNamespace(), programId.getApplication(), workflowId, workflowRunId, ProgramType.MAPREDUCE, programId.getProgram(), runId.getId());
}
Also used : WorkflowProgramLoggingContext(co.cask.cdap.logging.context.WorkflowProgramLoggingContext) WorkflowProgramInfo(co.cask.cdap.internal.app.runtime.workflow.WorkflowProgramInfo) ProgramId(co.cask.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) MapReduceLoggingContext(co.cask.cdap.logging.context.MapReduceLoggingContext)

Aggregations

WorkflowProgramLoggingContext (co.cask.cdap.logging.context.WorkflowProgramLoggingContext)4 FlowletLoggingContext (co.cask.cdap.logging.context.FlowletLoggingContext)2 MapReduceLoggingContext (co.cask.cdap.logging.context.MapReduceLoggingContext)2 WorkflowLoggingContext (co.cask.cdap.logging.context.WorkflowLoggingContext)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 RunId (org.apache.twill.api.RunId)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)1 MetricDataQuery (co.cask.cdap.api.metrics.MetricDataQuery)1 MetricStore (co.cask.cdap.api.metrics.MetricStore)1 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)1 ApplicationLoggingContext (co.cask.cdap.common.logging.ApplicationLoggingContext)1 LoggingContext (co.cask.cdap.common.logging.LoggingContext)1 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)1 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)1 WorkflowProgramInfo (co.cask.cdap.internal.app.runtime.workflow.WorkflowProgramInfo)1 UserServiceLoggingContext (co.cask.cdap.logging.context.UserServiceLoggingContext)1 LocalAppenderContext (co.cask.cdap.logging.framework.LocalAppenderContext)1 LogPipelineConfigurator (co.cask.cdap.logging.pipeline.LogPipelineConfigurator)1 LogProcessorPipelineContext (co.cask.cdap.logging.pipeline.LogProcessorPipelineContext)1