Search in sources :

Example 6 with LoggingContext

use of io.cdap.cdap.common.logging.LoggingContext in project cdap by caskdata.

the class MapReduceClassLoader method getTaskContextProvider.

/**
 * Returns the {@link MapReduceTaskContextProvider} associated with this ClassLoader.
 */
public MapReduceTaskContextProvider getTaskContextProvider() {
    // Logging context needs to be set in main thread.
    LoggingContext loggingContext = createMapReduceLoggingContext();
    LoggingContextAccessor.setLoggingContext(loggingContext);
    synchronized (this) {
        taskContextProvider = Optional.fromNullable(taskContextProvider).or(taskContextProviderSupplier);
    }
    taskContextProvider.startAndWait();
    return taskContextProvider;
}
Also used : MapReduceLoggingContext(io.cdap.cdap.logging.context.MapReduceLoggingContext) WorkflowProgramLoggingContext(io.cdap.cdap.logging.context.WorkflowProgramLoggingContext) LoggingContext(io.cdap.cdap.common.logging.LoggingContext)

Example 7 with LoggingContext

use of io.cdap.cdap.common.logging.LoggingContext in project cdap by caskdata.

the class PreviewHttpHandler method sendLogs.

private void sendLogs(HttpResponder responder, String namespaceId, String previewId, Consumer<LogReaderInfo> logsResponder) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, previewId);
    ProgramRunId programRunId = previewManager.getRunId(applicationId);
    if (programRunId == null) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    LoggingContext loggingContext = LoggingContextHelper.getLoggingContextWithRunId(programRunId, null);
    LogReader logReader = previewManager.getLogReader();
    logsResponder.accept(new LogReaderInfo(logReader, loggingContext, null));
}
Also used : LoggingContext(io.cdap.cdap.common.logging.LoggingContext) LogReader(io.cdap.cdap.logging.read.LogReader) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId)

Example 8 with LoggingContext

use of io.cdap.cdap.common.logging.LoggingContext in project cdap by caskdata.

the class SystemWorkerTwillRunnable method doInitialize.

@VisibleForTesting
void doInitialize() throws Exception {
    CConfiguration cConf = CConfiguration.create(new File(getArgument("cConf")).toURI().toURL());
    // Overwrite the app fabric temp directory with the task worker temp directory
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, cConf.get(Constants.SystemWorker.LOCAL_DATA_DIR));
    Configuration hConf = new Configuration();
    hConf.clear();
    hConf.addResource(new File(getArgument("hConf")).toURI().toURL());
    SConfiguration sConf = SConfiguration.create(new File(getArgument("sConf")));
    Injector injector = createInjector(cConf, hConf, sConf);
    // Initialize logging context
    logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class);
    logAppenderInitializer.initialize();
    metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    metricsCollectionService.startAndWait();
    LoggingContext loggingContext = new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, SystemWorkerTwillApplication.NAME);
    LoggingContextAccessor.setLoggingContext(loggingContext);
    systemWorker = injector.getInstance(SystemWorkerService.class);
}
Also used : LogAppenderInitializer(io.cdap.cdap.logging.appender.LogAppenderInitializer) Configuration(org.apache.hadoop.conf.Configuration) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) LoggingContext(io.cdap.cdap.common.logging.LoggingContext) ServiceLoggingContext(io.cdap.cdap.common.logging.ServiceLoggingContext) Injector(com.google.inject.Injector) SConfiguration(io.cdap.cdap.common.conf.SConfiguration) ServiceLoggingContext(io.cdap.cdap.common.logging.ServiceLoggingContext) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with LoggingContext

use of io.cdap.cdap.common.logging.LoggingContext in project cdap by caskdata.

the class LoggingContextHelper method createFilter.

public static Filter createFilter(LoggingContext loggingContext) {
    if (loggingContext instanceof ServiceLoggingContext) {
        LoggingContext.SystemTag systemTag = getByNamespaceOrSystemID(loggingContext.getSystemTagsMap());
        if (systemTag == null) {
            throw new IllegalArgumentException("No namespace or system id present");
        }
        String systemId = systemTag.getValue();
        String componentId = loggingContext.getSystemTagsMap().get(ServiceLoggingContext.TAG_COMPONENT_ID).getValue();
        String tagName = ServiceLoggingContext.TAG_SERVICE_ID;
        String entityId = loggingContext.getSystemTagsMap().get(ServiceLoggingContext.TAG_SERVICE_ID).getValue();
        ImmutableList.Builder<Filter> filterBuilder = ImmutableList.builder();
        // In CDAP 3.5 we removed SystemLoggingContext which had tag .systemId and now we use .namespaceId but to
        // support backward compatibility have an or filter so that we can read old logs too. See CDAP-7482
        OrFilter namespaceFilter = new OrFilter(ImmutableList.of(new MdcExpression(NamespaceLoggingContext.TAG_NAMESPACE_ID, systemId), new MdcExpression(ServiceLoggingContext.TAG_SYSTEM_ID, systemId)));
        filterBuilder.add(namespaceFilter);
        filterBuilder.add(new MdcExpression(ServiceLoggingContext.TAG_COMPONENT_ID, componentId));
        filterBuilder.add(new MdcExpression(tagName, entityId));
        return new AndFilter(filterBuilder.build());
    } else {
        String namespaceId = loggingContext.getSystemTagsMap().get(ApplicationLoggingContext.TAG_NAMESPACE_ID).getValue();
        String applId = loggingContext.getSystemTagsMap().get(ApplicationLoggingContext.TAG_APPLICATION_ID).getValue();
        LoggingContext.SystemTag entityTag = getEntityId(loggingContext);
        ImmutableList.Builder<Filter> filterBuilder = ImmutableList.builder();
        // For backward compatibility: The old logs before namespace have .accountId and developer as value so we don't
        // want them to get filtered out if they belong to this application and entity
        OrFilter namespaceFilter = new OrFilter(ImmutableList.of(new MdcExpression(NamespaceLoggingContext.TAG_NAMESPACE_ID, namespaceId), new MdcExpression(ACCOUNT_ID, Constants.DEVELOPER_ACCOUNT)));
        filterBuilder.add(namespaceFilter);
        filterBuilder.add(new MdcExpression(ApplicationLoggingContext.TAG_APPLICATION_ID, applId));
        filterBuilder.add(new MdcExpression(entityTag.getName(), entityTag.getValue()));
        if (loggingContext instanceof WorkflowProgramLoggingContext) {
            // Program is started by Workflow. Add Program information to filter.
            Map<String, LoggingContext.SystemTag> systemTagsMap = loggingContext.getSystemTagsMap();
            LoggingContext.SystemTag programTag = systemTagsMap.get(WorkflowProgramLoggingContext.TAG_WORKFLOW_MAP_REDUCE_ID);
            if (programTag != null) {
                filterBuilder.add(new MdcExpression(WorkflowProgramLoggingContext.TAG_WORKFLOW_MAP_REDUCE_ID, programTag.getValue()));
            }
            programTag = systemTagsMap.get(WorkflowProgramLoggingContext.TAG_WORKFLOW_SPARK_ID);
            if (programTag != null) {
                filterBuilder.add(new MdcExpression(WorkflowProgramLoggingContext.TAG_WORKFLOW_SPARK_ID, programTag.getValue()));
            }
        }
        // Add runid filter if required
        LoggingContext.SystemTag runId = loggingContext.getSystemTagsMap().get(ApplicationLoggingContext.TAG_RUN_ID);
        if (runId != null && runId.getValue() != null) {
            filterBuilder.add(new MdcExpression(ApplicationLoggingContext.TAG_RUN_ID, runId.getValue()));
        }
        return new AndFilter(filterBuilder.build());
    }
}
Also used : LoggingContext(io.cdap.cdap.common.logging.LoggingContext) ComponentLoggingContext(io.cdap.cdap.common.logging.ComponentLoggingContext) NamespaceLoggingContext(io.cdap.cdap.common.logging.NamespaceLoggingContext) ServiceLoggingContext(io.cdap.cdap.common.logging.ServiceLoggingContext) ImmutableList(com.google.common.collect.ImmutableList) MdcExpression(io.cdap.cdap.logging.filter.MdcExpression) OrFilter(io.cdap.cdap.logging.filter.OrFilter) ServiceLoggingContext(io.cdap.cdap.common.logging.ServiceLoggingContext) AndFilter(io.cdap.cdap.logging.filter.AndFilter) Filter(io.cdap.cdap.logging.filter.Filter) AndFilter(io.cdap.cdap.logging.filter.AndFilter) OrFilter(io.cdap.cdap.logging.filter.OrFilter)

Example 10 with LoggingContext

use of io.cdap.cdap.common.logging.LoggingContext in project cdap by caskdata.

the class LogHttpHandler method prev.

@GET
@Path("/namespaces/{namespace-id}/apps/{app-id}/{program-type}/{program-id}/logs/prev")
public void prev(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("program-type") String programType, @PathParam("program-id") String programId, @QueryParam("max") @DefaultValue("50") int maxEvents, @QueryParam("fromOffset") @DefaultValue("") String fromOffsetStr, @QueryParam("escape") @DefaultValue("true") boolean escape, @QueryParam("filter") @DefaultValue("") String filterStr, @QueryParam("format") @DefaultValue("text") String format, @QueryParam("suppress") List<String> suppress) throws Exception {
    ensureVisibilityOnProgram(namespaceId, appId, programType, programId);
    LoggingContext loggingContext = LoggingContextHelper.getLoggingContext(namespaceId, appId, programId, ProgramType.valueOfCategoryName(programType));
    doPrev(logReader, responder, loggingContext, maxEvents, fromOffsetStr, escape, filterStr, null, format, suppress);
}
Also used : LoggingContext(io.cdap.cdap.common.logging.LoggingContext) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

LoggingContext (io.cdap.cdap.common.logging.LoggingContext)74 Test (org.junit.Test)20 WorkerLoggingContext (io.cdap.cdap.logging.context.WorkerLoggingContext)18 GET (javax.ws.rs.GET)18 Path (javax.ws.rs.Path)18 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)16 ServiceLoggingContext (io.cdap.cdap.common.logging.ServiceLoggingContext)14 Injector (com.google.inject.Injector)12 LogAppenderInitializer (io.cdap.cdap.logging.appender.LogAppenderInitializer)12 LoggingTester (io.cdap.cdap.logging.appender.LoggingTester)12 File (java.io.File)12 Configuration (org.apache.hadoop.conf.Configuration)12 Cancellable (org.apache.twill.common.Cancellable)10 FileLogReader (io.cdap.cdap.logging.read.FileLogReader)8 LogEvent (io.cdap.cdap.logging.read.LogEvent)8 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)8 Logger (org.slf4j.Logger)8 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)6 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)6 RunRecordDetail (io.cdap.cdap.internal.app.store.RunRecordDetail)6