springboot-阿里羚羊 服务端埋点

发布于:2024-05-24 ⋅ 阅读:(94) ⋅ 点赞:(0)

官方文档

集成Java SDK

手动引入jar包「quickaplus-log-collector-java-sdk-1.0.1-SNAPSHOT.jar」
image.png

        <dependency>
            <groupId>com.alibaba.lingyang</groupId>
            <artifactId>quickaplus-log-collector-java-sdk</artifactId>
            <version>1.0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/../lib/quickaplus-log-collector-java-sdk-1.0.1-SNAPSHOT.jar</systemPath>
        </dependency>
        <!-- 这里需要okhttp3依赖不然sdk调用syncSendLog方法会报错 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.3</version>
        </dependency>

基础配置

image.png

注意:QlcEndpoint,sdk中会自动拼接/server,自己不要加
image.png

qt:
  serviceId: YMlICJqxxxx
  serviceSecret: E3qttxxxxxxxxxxx
  appKey: WDmVjrSxxxxx
  setQlcEndpoint: https://log-api.aplus.xxxx
  debugKey: "592320xxxxx5095_001"   #生产环境置空
package com.vehicle.manager.core.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author zr 2024/5/24
 */
@Configuration
@ConfigurationProperties(prefix = "qt")
@Data
public class QtProperties {
    private String serviceId;
    private String serviceSecret;
    private String appKey;
    private String setQlcEndpoint;
    private String debugKey;
}

封装方法

package com.vehicle.manager.core.service;

import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.config.QtSdkConfig;
import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.model.QtLog;
import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.sender.QtLogSenderHelper;
import com.vehicle.manager.core.config.QtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zr 2024/5/24
 */
@Service
@Slf4j
public class QtService {
    @Autowired
    private QtProperties qtProperties;

    public void configureQtSdk() {
        QtSdkConfig.setServiceId(qtProperties.getServiceId());
        QtSdkConfig.setServiceSecret(qtProperties.getServiceSecret());
        QtSdkConfig.setAppKey(qtProperties.getAppKey());
        QtSdkConfig.setQlcEndpoint(qtProperties.getSetQlcEndpoint());
        QtSdkConfig.setOpenLog(true);
        QtSdkConfig.setCallback(ctx -> {
            log.info("Response Code: {}", ctx.getResponseCode());
            log.info("Response Message: {}", ctx.getResponseMessage());
            log.info("Send Success: {}", ctx.getSendSuccess());
            log.info("Send Data: {}", ctx.getSendData());
            log.info("Response Data: {}", ctx.getResponseData());
            log.info("Errors: {}", ctx.getErrors());
        });
    }
    // eventId为事件编码,用于区分不同的事件
    // customProperty事件属性,我们需要存的日志
    public void sendLog(String eventId, Map<String, Object> customProperty) {
        configureQtSdk();
        // 构造日志对象
        QtLog log = new QtLog.Builder()
                .eventId(eventId)  //事件编码(必填)
                .userId("hailiang-server")		//设置账号ID(设备ID和账号ID必填一个)
                .customProperty(customProperty)		//设置事件属性(选填)
                .debugKey(qtProperties.getDebugKey())		//设置埋点验证标志位(选填),上线时必须删除
                .eventTimestamp(System.currentTimeMillis())		//设置客户端时间戳(必填)
                .serverTimestamp(System.currentTimeMillis()).build();  //设置服务端时间戳(可选)
        // 发送日志
        QtLogSenderHelper.syncSendLog(log);
    }
}

埋点验证

在这里插入图片描述

拿到DebugKey,修改到配置文件(生产环境记得去除掉),点击下一步来到日志明细,方法执行成功后日志明细会收到相应的消息
image.png
测试

package com.vehicle.manager.core;

import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.config.QtSdkConfig;
import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.model.QtLog;
import com.alibaba.lingyang.quick.tracking.qlc.java.sdk.sender.QtLogSenderHelper;
import com.vehicle.manager.api.StartApplication;
import com.vehicle.manager.core.config.QtProperties;
import com.vehicle.manager.core.service.QtService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zr 2024/5/24
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartApplication.class)
@Slf4j
public class TestSendLog {
    @Autowired
    private QtService qtService;


    @Test
    public void name() {
        HashMap<String, Object> customProperty = new HashMap<>();
        customProperty.put("test2","test2");
        qtService.sendLog("test_success",customProperty);
    }
}

image.png
日志明细
在这里插入图片描述