// Path: instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/ExtensionTags.java
// public class ExtensionTags {
// 
//   private ExtensionTags() {
//   }
// 
//   /**
//    * Tag for {@link Class#getSimpleName()}
//    */
//   public static final StringTag CLASS_TAG = new StringTag("class");
// 
//   /**
//    * Tag for method name
//    */
//   public static final StringTag METHOD_TAG = new StringTag("method");
// }
// 
// Path: instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/SpanUtils.java
// public class SpanUtils {
// 
//   private SpanUtils() {
//   }
// 
//   /**
//    * Add appropriate error tags and logs to a span when an exception occurs
//    *
//    * @param span the span "monitoring" the code which threw the exception
//    * @param ex captured exception
//    */
//   public static void captureException(Span span, Exception ex) {
//     Map<String, Object> exceptionLogs = new LinkedHashMap<>(2);
//     exceptionLogs.put("event", Tags.ERROR.getKey());
//     exceptionLogs.put("error.object", ex);
//     span.log(exceptionLogs);
//     Tags.ERROR.set(span, true);
//   }
// }


import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.spring.cloud.ExtensionTags;
import io.opentracing.contrib.spring.cloud.SpanUtils;
import io.opentracing.tag.Tags;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ReflectionUtils;
/**
 * Copyright 2017-2019 The OpenTracing Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package io.opentracing.contrib.spring.cloud.async;


/**
 * @author kameshsampath
 */
@Aspect
public class TraceAsyncAspect {

  static final String TAG_COMPONENT = "async";

  @Autowired
  private Tracer tracer;

  public TraceAsyncAspect(Tracer tracer) {
    this.tracer = tracer;
  }

  @Around("execution (@org.springframework.scheduling.annotation.Async * *.*(..))")
  public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
    /**
     * We create a span because parent span might be missing. E.g. method is invoked
     */
    Span span = this.tracer.buildSpan(operationName(pjp))
        .withTag(Tags.COMPONENT.getKey(), TAG_COMPONENT)
        .withTag(ExtensionTags.CLASS_TAG.getKey(), pjp.getTarget().getClass().getSimpleName())
        .withTag(ExtensionTags.METHOD_TAG.getKey(), pjp.getSignature().getName())
        .start();
    try (Scope scope = this.tracer.scopeManager().activate(span)) {
      return pjp.proceed();
    } catch (Exception ex) {
// Path: instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/ExtensionTags.java
// public class ExtensionTags {
// 
//   private ExtensionTags() {
//   }
// 
//   /**
//    * Tag for {@link Class#getSimpleName()}
//    */
//   public static final StringTag CLASS_TAG = new StringTag("class");
// 
//   /**
//    * Tag for method name
//    */
//   public static final StringTag METHOD_TAG = new StringTag("method");
// }
// 
// Path: instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/SpanUtils.java
// public class SpanUtils {
// 
//   private SpanUtils() {
//   }
// 
//   /**
//    * Add appropriate error tags and logs to a span when an exception occurs
//    *
//    * @param span the span "monitoring" the code which threw the exception
//    * @param ex captured exception
//    */
//   public static void captureException(Span span, Exception ex) {
//     Map<String, Object> exceptionLogs = new LinkedHashMap<>(2);
//     exceptionLogs.put("event", Tags.ERROR.getKey());
//     exceptionLogs.put("error.object", ex);
//     span.log(exceptionLogs);
//     Tags.ERROR.set(span, true);
//   }
// }


// Path: instrument-starters/opentracing-spring-cloud-core/src/main/java/io/opentracing/contrib/spring/cloud/async/TraceAsyncAspect.java
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.spring.cloud.ExtensionTags;
import io.opentracing.contrib.spring.cloud.SpanUtils;
import io.opentracing.tag.Tags;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ReflectionUtils;

/**
 * Copyright 2017-2019 The OpenTracing Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package io.opentracing.contrib.spring.cloud.async;


/**
 * @author kameshsampath
 */
@Aspect
public class TraceAsyncAspect {

  static final String TAG_COMPONENT = "async";

  @Autowired
  private Tracer tracer;

  public TraceAsyncAspect(Tracer tracer) {
    this.tracer = tracer;
  }

  @Around("execution (@org.springframework.scheduling.annotation.Async * *.*(..))")
  public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
    /**
     * We create a span because parent span might be missing. E.g. method is invoked
     */
    Span span = this.tracer.buildSpan(operationName(pjp))
        .withTag(Tags.COMPONENT.getKey(), TAG_COMPONENT)
        .withTag(ExtensionTags.CLASS_TAG.getKey(), pjp.getTarget().getClass().getSimpleName())
        .withTag(ExtensionTags.METHOD_TAG.getKey(), pjp.getSignature().getName())
        .start();
    try (Scope scope = this.tracer.scopeManager().activate(span)) {
      return pjp.proceed();
    } catch (Exception ex) {
      SpanUtils.captureException(span, ex);