栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

datadog-define-tags by AOP

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

datadog-define-tags by AOP

Define yourself tags of datadog by AOP
1、POM增加


   com.datadoghq
   dd-java-agent
   0.88.0

2、启动添加注解:proxy by glibc or others method
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=true
4、定义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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号