1、POM增加
2、启动添加注解:proxy by glibc or others methodcom.datadoghq dd-java-agent0.88.0
package com.example.inv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication(scanbasePackages = {"com.example.inv", "com.example.auditlog", "com.example.admin", "com.example.security", "com.example.core"},
exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
@EnableJpaAuditing
@EnableAsync
public class InvApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(InvApplication.class, args);
}
}
3、开关AOP,配置文件中增加
spring.aop.auto=true4、定义AOP,注入datadogtags upload to datadog.com
package com.exmaple.inv.configuration;
import com.alibaba.fastjson.JSON;
import io.opentracing.Span;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
//datadog-agent
import io.opentracing.util.GlobalTracer;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Aspect
@Component
public class DatadogTagsAnnoAspectLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(DatadogTagsAnnoAspectLogAspect.class);
@Pointcut("execution(public * com.michaels.inv.inventory.controller.*.*(..))")
public void LogAspect(){}
@Around("LogAspect()")
public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Map map = request.getParameterMap();
Enumeration enumeration = request.getParameterNames();
Map parameterMap = new HashMap();
while (enumeration.hasMoreElements()){
String parameter = enumeration.nextElement();
parameterMap.put(parameter,request.getParameter(parameter));
}
String strRequest = JSON.toJSonString(parameterMap);
final Span span = GlobalTracer.get().activeSpan();
if (span != null) {
// customer_id -> 254889
// customer_tier -> platinum
// cart_value -> 867
span.setTag("michaels-request-info-deAround.REQUEST", strRequest);
span.setTag("michaels-request-info-deAround.METHOD", request.getMethod().toString());
span.setTag("michaels-request-info-deAround.URL", request.getRequestURL().toString());
span.setTag("michaels-request-info-deAround.ClassName", joinPoint.getSignature().getDeclaringTypeName() );
span.setTag("michaels-request-info-deAround.MethodName", joinPoint.getSignature().getName() );
span.setTag("michaels-request-info-deAround.QueryString", request.getQueryString().toString() );
span.setTag("michaels-request-info-deAround.Header:content-type", request.getHeader("content-type"));
span.setTag("michaels-request-info-deAround.Header:host:user-agent", request.getHeader("host"));
span.setTag("michaels-request-info-deAround.Header", request.getHeader("user-agent"));
System.out.println(">>>>>>>>>>>>>>>>>> 【doAround】");
}
LOGGER.info( "【请求 REQUEST 参数】" + strRequest );
///System.out.println("doAround");
return joinPoint.proceed();
}
@Before("LogAspect()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Map map = request.getParameterMap();
Enumeration enumeration = request.getParameterNames();
Map parameterMap = new HashMap();
while (enumeration.hasMoreElements()){
String parameter = enumeration.nextElement();
parameterMap.put(parameter,request.getParameter(parameter));
}
String strRequest = JSON.toJSonString(parameterMap);
final Span span = GlobalTracer.get().activeSpan();
if (span != null) {
// customer_id -> 254889
// customer_tier -> platinum
// cart_value -> 867
span.setTag("michaels-request-info-doBefore.REQUEST", strRequest);
span.setTag("michaels-request-info-doBefore.METHOD", request.getMethod().toString());
span.setTag("michaels-request-info-doBefore.URL", request.getRequestURL().toString());
span.setTag("michaels-request-info-doBefore.ClassName", joinPoint.getSignature().getDeclaringTypeName() );
span.setTag("michaels-request-info-doBefore.MethodName", joinPoint.getSignature().getName() );
span.setTag("michaels-request-info-doBefore.QueryString", request.getQueryString().toString() );
span.setTag("michaels-request-info-doBefore.Header:content-type", request.getHeader("content-type"));
span.setTag("michaels-request-info-doBefore.Header:host", request.getHeader("host"));
span.setTag("michaels-request-info-doBefore.Header:user-agent", request.getHeader("user-agent"));
System.out.println("********************====== 【doBefore】");
}
LOGGER.info( "【请求 REQUEST 参数】" + strRequest );
}
@After("LogAspect()")
public void doAfter(JoinPoint joinPoint){
System.out.println("doAfter");
}
@AfterReturning("LogAspect()")
public void doAfterReturning(JoinPoint joinPoint){
System.out.println("doAfterReturning");
}
@AfterThrowing("LogAspect()")
public void deAfterThrowing(JoinPoint joinPoint){
System.out.println("deAfterThrowing");
}
}
5、自定义注解,选用修改代码,注入datadogtags upload to datadog.com
package com.exmaple.inv.configuration;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DatadogTagsAnno {
String param() default "";
}
package com.exmaple.inv.configuration;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Date;
@Aspect
@Component
public class DatadogTagsAnnoAspect {
@Around("@annotation(DatadogTagsAnno)")
public Object around(ProceedingJoinPoint joinPoint, DatadogTagsAnno DatadogTagsAnno) throws Throwable {
System.out.println("方法开始时间是:"+new Date());
Object oResult = joinPoint.proceed();
System.out.println("方法结束时间是:"+new Date()) ;
return oResult;
}
}



