springboot-阿里羚羊 服务端埋点

官方文档

集成Java SDK

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

xml 复制代码
        <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>

基础配置

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

yaml 复制代码
qt:
  serviceId: YMlICJqxxxx
  serviceSecret: E3qttxxxxxxxxxxx
  appKey: WDmVjrSxxxxx
  setQlcEndpoint: https://log-api.aplus.xxxx
  debugKey: "592320xxxxx5095_001"   #生产环境置空
java 复制代码
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;
}

封装方法

java 复制代码
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,修改到配置文件(生产环境记得去除掉),点击下一步来到日志明细,方法执行成功后日志明细会收到相应的消息

测试

java 复制代码
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);
    }
}

日志明细

相关推荐
IT_陈寒2 小时前
Java中equals方法比了个寂寞?原来这才是正确的重写姿势
前端·人工智能·后端
Thneonl2 小时前
Celery 生产踩坑:1000 任务积压与 acks_late 双重执行
后端·python
卷福同学2 小时前
第一次当面试官有感
后端·面试
苏三说技术2 小时前
为什么越来越多人用 OnlyOffice?
后端
知守观2 小时前
@Transactional 事务失效排查,try-catch 吞异常导致回滚失败(附源码分析)
后端·spring
羑悻2 小时前
Codex + Seed-2.1-pro 实测:多模态理解 + Coding Agent 能扛住真实仓库吗?
后端
颜进强2 小时前
14 · NestJS ExecutionContext 执行上下文:守卫、拦截器、过滤器拿到的"同一个 context",为什么能力不一样?
前端·后端·ai编程
小小张说故事2 小时前
Python 多线程为什么跑不快?asyncio 入门指南:异步并发从零上手
后端·python
明月_清风2 小时前
数据进入平台后怎么处理?一文搞懂 ETL
大数据·后端·数据分析
初学AI的小高2 小时前
LangGraph断点恢复与幂等执行实战
后端·架构