Search in sources :

Example 1 with MdcExpression

use of co.cask.cdap.logging.filter.MdcExpression 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 : ApplicationLoggingContext(co.cask.cdap.common.logging.ApplicationLoggingContext) ComponentLoggingContext(co.cask.cdap.common.logging.ComponentLoggingContext) NamespaceLoggingContext(co.cask.cdap.common.logging.NamespaceLoggingContext) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) LoggingContext(co.cask.cdap.common.logging.LoggingContext) ImmutableList(com.google.common.collect.ImmutableList) MdcExpression(co.cask.cdap.logging.filter.MdcExpression) OrFilter(co.cask.cdap.logging.filter.OrFilter) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) AndFilter(co.cask.cdap.logging.filter.AndFilter) OrFilter(co.cask.cdap.logging.filter.OrFilter) Filter(co.cask.cdap.logging.filter.Filter) AndFilter(co.cask.cdap.logging.filter.AndFilter)

Aggregations

ApplicationLoggingContext (co.cask.cdap.common.logging.ApplicationLoggingContext)1 ComponentLoggingContext (co.cask.cdap.common.logging.ComponentLoggingContext)1 LoggingContext (co.cask.cdap.common.logging.LoggingContext)1 NamespaceLoggingContext (co.cask.cdap.common.logging.NamespaceLoggingContext)1 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)1 AndFilter (co.cask.cdap.logging.filter.AndFilter)1 Filter (co.cask.cdap.logging.filter.Filter)1 MdcExpression (co.cask.cdap.logging.filter.MdcExpression)1 OrFilter (co.cask.cdap.logging.filter.OrFilter)1 ImmutableList (com.google.common.collect.ImmutableList)1