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;
}
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));
}
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);
}
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());
}
}
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);
}
Aggregations