All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.Slf4jScopeAgentRule Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the SLF4J logging framework.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules;

import static com.undefinedlabs.scope.agent.ScopeClassLoaderMatcher.hasClassesNamed;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.undefinedlabs.scope.ScopeGlobalTracer;
import com.undefinedlabs.scope.events.EventFieldsFactory;
import com.undefinedlabs.scope.events.log.LogLevel;
import com.undefinedlabs.scope.rules.events.log.Slf4jLogEventFactory;
import com.undefinedlabs.scope.utils.sourcecode.SourceCodeFrame;
import com.undefinedlabs.scope.utils.sourcecode.SourceCodeFrameFactory;
import com.undefinedlabs.scope.utils.sourcecode.StackTraceElementLocator;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.util.HashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.slf4j.Logger;

public class Slf4jScopeAgentRule extends AbstractScopeAgentRule {

  @Override
  protected ElementMatcher typeMatcher() {
    return hasSuperType(named("org.slf4j.Logger"));
  }

  @Override
  protected ElementMatcher classLoaderMatcher() {
    return hasClassesNamed("org.slf4j.Logger");
  }

  @Override
  protected Map, String> transformers() {
    final Map, String> transformers = new HashMap<>();
    transformers.put(named("info"), Slf4jScopeAgentRule.class.getName() + "$Slf4jLogInfoAdvice");
    transformers.put(named("debug"), Slf4jScopeAgentRule.class.getName() + "$Slf4jLogDebugAdvice");
    transformers.put(named("error"), Slf4jScopeAgentRule.class.getName() + "$Slf4jLogErrorAdvice");
    transformers.put(named("warn"), Slf4jScopeAgentRule.class.getName() + "$Slf4jLogWarnAdvice");
    transformers.put(named("trace"), Slf4jScopeAgentRule.class.getName() + "$Slf4jLogTraceAdvice");
    return transformers;
  }

  public static class Slf4jLogInfoAdvice {

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Logger thiz, @Advice.AllArguments final Object[] args) {
      try {
        if ("org.slf4j.helpers.NOPLogger".equalsIgnoreCase(thiz.getClass().getName())
            || !thiz.isInfoEnabled()) {
          return;
        }
      } catch (final Exception e) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();
      final Span span = tracer.activeSpan();
      if (span == null) {
        return;
      }

      final SourceCodeFrame sourceCodeFrame =
          SourceCodeFrameFactory.INSTANCE.createFrame(
              StackTraceElementLocator.INSTANCE.getFirstWithSourceCode());
      span.log(
          EventFieldsFactory.INSTANCE.createFields(
              Slf4jLogEventFactory.INSTANCE.newEvent(thiz, args, LogLevel.INFO, sourceCodeFrame)));
    }
  }

  public static class Slf4jLogDebugAdvice {

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Logger thiz, @Advice.AllArguments final Object[] args) {
      try {
        if ("org.slf4j.helpers.NOPLogger".equalsIgnoreCase(thiz.getClass().getName())
            || !thiz.isDebugEnabled()) {
          return;
        }
      } catch (final Exception e) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();
      final Span span = tracer.activeSpan();
      if (span == null) {
        return;
      }

      final SourceCodeFrame sourceCodeFrame =
          SourceCodeFrameFactory.INSTANCE.createFrame(
              StackTraceElementLocator.INSTANCE.getFirstWithSourceCode());
      span.log(
          EventFieldsFactory.INSTANCE.createFields(
              Slf4jLogEventFactory.INSTANCE.newEvent(thiz, args, LogLevel.DEBUG, sourceCodeFrame)));
    }
  }

  public static class Slf4jLogErrorAdvice {

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Logger thiz, @Advice.AllArguments final Object[] args) {
      try {
        if ("org.slf4j.helpers.NOPLogger".equalsIgnoreCase(thiz.getClass().getName())
            || !thiz.isErrorEnabled()) {
          return;
        }
      } catch (final Exception e) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();
      final Span span = tracer.activeSpan();
      if (span == null) {
        return;
      }

      final SourceCodeFrame sourceCodeFrame =
          SourceCodeFrameFactory.INSTANCE.createFrame(
              StackTraceElementLocator.INSTANCE.getFirstWithSourceCode());
      span.log(
          EventFieldsFactory.INSTANCE.createFields(
              Slf4jLogEventFactory.INSTANCE.newEvent(thiz, args, LogLevel.ERROR, sourceCodeFrame)));
    }
  }

  public static class Slf4jLogWarnAdvice {

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Logger thiz, @Advice.AllArguments final Object[] args) {
      try {
        if ("org.slf4j.helpers.NOPLogger".equalsIgnoreCase(thiz.getClass().getName())
            || !thiz.isWarnEnabled()) {
          return;
        }
      } catch (final Exception e) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();
      final Span span = tracer.activeSpan();
      if (span == null) {
        return;
      }

      final SourceCodeFrame sourceCodeFrame =
          SourceCodeFrameFactory.INSTANCE.createFrame(
              StackTraceElementLocator.INSTANCE.getFirstWithSourceCode());
      span.log(
          EventFieldsFactory.INSTANCE.createFields(
              Slf4jLogEventFactory.INSTANCE.newEvent(
                  thiz, args, LogLevel.WARNING, sourceCodeFrame)));
    }
  }

  public static class Slf4jLogTraceAdvice {

    @Advice.OnMethodExit
    public static void exit(
        @Advice.This final Logger thiz, @Advice.AllArguments final Object[] args) {
      try {
        if ("org.slf4j.helpers.NOPLogger".equalsIgnoreCase(thiz.getClass().getName())
            || !thiz.isTraceEnabled()) {
          return;
        }
      } catch (final Exception e) {
        return;
      }

      final Tracer tracer = ScopeGlobalTracer.get();
      final Span span = tracer.activeSpan();
      if (span == null) {
        return;
      }

      final SourceCodeFrame sourceCodeFrame =
          SourceCodeFrameFactory.INSTANCE.createFrame(
              StackTraceElementLocator.INSTANCE.getFirstWithSourceCode());
      span.log(
          EventFieldsFactory.INSTANCE.createFields(
              Slf4jLogEventFactory.INSTANCE.newEvent(
                  thiz, args, LogLevel.VERBOSE, sourceCodeFrame)));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy