use of co.cask.cdap.common.logging.LoggingContext 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)));
}
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class PreviewHttpHandler method getPreviewLogsPrev.
@GET
@Path("/previews/{preview-id}/logs/prev")
public void getPreviewLogsPrev(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId, @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 {
ProgramRunId runId = getProgramRunId(namespaceId, previewId);
RunRecordMeta runRecord = getRunRecord(namespaceId, previewId);
LoggingContext loggingContext = LoggingContextHelper.getLoggingContextWithRunId(namespaceId, previewId, runId.getProgram(), runId.getType(), runId.getRun(), runRecord.getSystemArgs());
doPrev(responder, loggingContext, maxEvents, fromOffsetStr, escape, filterStr, runRecord, format, suppress);
}
use of co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class DistributedProgramRunner method logProgramStart.
/**
* Set up Logging Context so the Log is tagged correctly for the Program.
* Reset the context once done.
*/
private void logProgramStart(Program program, ProgramOptions options) {
LoggingContext loggingContext = LoggingContextHelper.getLoggingContext(program.getNamespaceId(), program.getApplicationId(), program.getName(), program.getType(), ProgramRunners.getRunId(options).getId(), options.getArguments().asMap());
Cancellable saveContextCancellable = LoggingContextAccessor.setLoggingContext(loggingContext);
String userArguments = Joiner.on(", ").withKeyValueSeparator("=").join(options.getUserArguments());
LOG.info("Starting {} Program '{}' with Arguments [{}]", program.getType(), program.getName(), userArguments);
saveContextCancellable.cancel();
}
use of co.cask.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 co.cask.cdap.common.logging.LoggingContext in project cdap by caskdata.
the class LogHandler method sysList.
@GET
@Path("/system/{component-id}/{service-id}/logs")
public void sysList(HttpRequest request, HttpResponder responder, @PathParam("component-id") String componentId, @PathParam("service-id") String serviceId, @QueryParam("start") @DefaultValue("-1") long fromTimeSecsParam, @QueryParam("stop") @DefaultValue("-1") long toTimeSecsParam, @QueryParam("escape") @DefaultValue("true") boolean escape, @QueryParam("filter") @DefaultValue("") String filterStr, @QueryParam("format") @DefaultValue("text") String format, @QueryParam("suppress") List<String> suppress) {
LoggingContext loggingContext = LoggingContextHelper.getLoggingContext(Id.Namespace.SYSTEM.getId(), componentId, serviceId);
doGetLogs(responder, loggingContext, fromTimeSecsParam, toTimeSecsParam, escape, filterStr, null, format, suppress);
}
Aggregations