| 
 | 1 | +/*  | 
 | 2 | + * Copyright The OpenTelemetry Authors  | 
 | 3 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 4 | + */  | 
 | 5 | + | 
 | 6 | +package io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1;  | 
 | 7 | + | 
 | 8 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor;  | 
 | 9 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic;  | 
 | 10 | +import static net.bytebuddy.matcher.ElementMatchers.named;  | 
 | 11 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument;  | 
 | 12 | + | 
 | 13 | +import io.opentelemetry.context.Context;  | 
 | 14 | +import io.opentelemetry.context.Scope;  | 
 | 15 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;  | 
 | 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;  | 
 | 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;  | 
 | 18 | +import net.bytebuddy.asm.Advice;  | 
 | 19 | +import net.bytebuddy.description.type.TypeDescription;  | 
 | 20 | +import net.bytebuddy.matcher.ElementMatcher;  | 
 | 21 | +import org.springframework.util.ErrorHandler;  | 
 | 22 | + | 
 | 23 | +public class DelegatingErrorHandlingRunnableInstrumentation implements TypeInstrumentation {  | 
 | 24 | +  @Override  | 
 | 25 | +  public ElementMatcher<TypeDescription> typeMatcher() {  | 
 | 26 | +    return named("org.springframework.scheduling.support.DelegatingErrorHandlingRunnable");  | 
 | 27 | +  }  | 
 | 28 | + | 
 | 29 | +  @Override  | 
 | 30 | +  public void transform(TypeTransformer transformer) {  | 
 | 31 | +    transformer.applyAdviceToMethod(  | 
 | 32 | +        isConstructor().and(takesArgument(1, named("org.springframework.util.ErrorHandler"))),  | 
 | 33 | +        this.getClass().getName() + "$WrapErrorHandlerAdvice");  | 
 | 34 | + | 
 | 35 | +    transformer.applyAdviceToMethod(  | 
 | 36 | +        isPublic().and(named("run")), this.getClass().getName() + "$RunAdvice");  | 
 | 37 | +  }  | 
 | 38 | + | 
 | 39 | +  @SuppressWarnings("unused")  | 
 | 40 | +  public static class WrapErrorHandlerAdvice {  | 
 | 41 | + | 
 | 42 | +    @Advice.OnMethodEnter(suppress = Throwable.class)  | 
 | 43 | +    public static void onEnter(  | 
 | 44 | +        @Advice.Argument(value = 1, readOnly = false) ErrorHandler errorHandler) {  | 
 | 45 | +      if (errorHandler != null) {  | 
 | 46 | +        errorHandler = new ErrorHandlerWrapper(errorHandler);  | 
 | 47 | +      }  | 
 | 48 | +    }  | 
 | 49 | +  }  | 
 | 50 | + | 
 | 51 | +  @SuppressWarnings("unused")  | 
 | 52 | +  public static class RunAdvice {  | 
 | 53 | + | 
 | 54 | +    @Advice.OnMethodEnter(suppress = Throwable.class)  | 
 | 55 | +    public static Scope onEnter() {  | 
 | 56 | +      Context parentContext = Java8BytecodeBridge.currentContext();  | 
 | 57 | +      return TaskContextHolder.init(parentContext).makeCurrent();  | 
 | 58 | +    }  | 
 | 59 | + | 
 | 60 | +    @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)  | 
 | 61 | +    public static void onExit(@Advice.Enter Scope scope) {  | 
 | 62 | +      if (scope != null) {  | 
 | 63 | +        scope.close();  | 
 | 64 | +      }  | 
 | 65 | +    }  | 
 | 66 | +  }  | 
 | 67 | +}  | 
0 commit comments