Commit d9668fe4 authored by 杨锐's avatar 杨锐

done.

parent 2a7e8d6f
package cn.quantgroup.tech.brave.aspect;
import brave.Tracer;
import brave.Tracing;
import com.dangdang.ddframe.job.api.ElasticJob;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Aspect
@Configuration
@ConditionalOnClass(ElasticJob.class)
@ConditionalOnProperty(prefix = "tech.brave", name = "enabled", havingValue = "true", matchIfMissing = true)
public class ElasticJobAspect {
@Resource
private Tracing tracing;
@Around("target(com.dangdang.ddframe.job.api.ElasticJob)")
public Object dynamicTrace(ProceedingJoinPoint pjp) throws Throwable {
Tracer tracer = tracing.tracer();
/* 防止应用层非法使用(上下文中存在span),不生成root span。 */
if (tracer.currentSpan() == null) {
brave.Span span = tracer.newTrace();
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
return pjp.proceed();
// note: try-with-resources closes the scope *before* the catch block
} catch (RuntimeException | Error e) {
span.error(e);
throw e;
} finally {
span.finish();
}
}
return pjp.proceed();
}
}
package cn.quantgroup.tech.brave.configuration;
import brave.Tracer;
import brave.Tracing;
import brave.http.HttpTracing;
import brave.httpclient.TracingHttpClientBuilder;
......@@ -14,11 +15,15 @@ import cn.quantgroup.tech.brave.properties.ServiceProperties;
import cn.quantgroup.tech.brave.service.*;
import cn.quantgroup.tech.brave.service.impl.*;
import cn.quantgroup.tech.brave.slf4j.MDCCurrentTraceContext;
import com.dangdang.ddframe.job.api.ElasticJob;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
......@@ -414,6 +419,34 @@ public class BraveAutoConfiguration {
return new TechExecutorServiceBuilderTrace();
}
}
@Aspect
@Configuration
@ConditionalOnClass(ElasticJob.class)
@ConditionalOnProperty(prefix = "tech.brave", name = "enabled", havingValue = "true", matchIfMissing = true)
public class ElasticJobAspect {
@Around("target(com.dangdang.ddframe.job.api.ElasticJob)")
public Object dynamicTrace(ProceedingJoinPoint pjp) throws Throwable {
try {
Tracer tracer = tracing().tracer();
/* 防止应用层非法使用(上下文中存在span),不生成root span。 */
if (tracer.currentSpan() == null) {
brave.Span span = tracer.newTrace();
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
// note: try-with-resources closes the scope *before* the catch block
} catch (RuntimeException | Error e) {
span.error(e);
throw e;
} finally {
span.finish();
}
}
} finally {
return pjp.proceed();
}
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment