Android网络请求监控与数据上报

Monitor-Network

基于 ServiceLoader,监控 Okhttp 网络请求,实现拦截、解析、转发、存储、上报等功能。采用组件化开发方式,各业务组件均可单独拆分使用,同时提供export对外调用组件,外部调用无需关心内部实现,组件之间集成完全解耦

Github 地址 > github.com/MannaYang/M...

集成组件介绍

Components-stone

  • 提供基础依赖管理build.gradle.kts(可修改依赖版本)
  • 提供DataStore/Gson/RetrofitProvider对外调用管理类(可替换)
  • 提供NetworkInterceptor全局拦截器(Okhttp Interceptor)(如需替换,需全局处理接口注解)
  • 提供Application生命周期分发管理,基于@AutoService注解生成并解耦( 如需替换,需全局处理接口注解)

Components-http-export

该组件为对外集成组件,可直接依赖

  • HttpDataResult.kt 提供对外获取Interceptor拦截器解析实体model
  • HttpInterceptorListener.kt 提供对外转发实体model数据接口,可供上层业务获取网络请求数据并自定义处理,参考app组件模块[HttpInterceptor.kt](https://link.juejin.cn?target=app%252Fsrc%252Fmain%252Fjava%252Fcom%252Fmanna%252Fmonitor%252Fnetwork%252Finterceptor%252FHttpInterceptor.kt "app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fmanna%2Fmonitor%2Fnetwork%2Finterceptor%2FHttpInterceptor.kt")

Components-http

该组件是http-export内部业务实现组件,处理网络请求白名单、解析、转发等业务操作,不可直接依赖,建议上层business业务集成

Components-room-export

该组件为对外集成组件,可直接依赖

Components-room

该组件是room-export内部业务实现组件,提供room database、table、dao实现

Components-report-export

该组件为对外集成组件,可直接依赖

  • WorkReport.kt 对外开放业务查询过滤实体、业务上报平台区分,关联WorkManager-InputData参数

Components-report

该组件是report-export内部业务实现组件,提供WorkManager后台上报功能

  • dingtalk 钉钉工作空间及鉴权服务与数据上报(仅作参考),具体实现可换为实际上报业务平台
  • markdown 格式化上报报文(仅作参考),具体按实际业务要求处理
  • work 处理数据上报及冗余数据处理(仅作参考),具体按实际业务要求处理

Components-app

上层业务集成组件壳,包含所有export及内部实现组件,组装额外公共参数(用户信息、设备信息、应用信息)

scss 复制代码
kapt("com.google.auto.service:auto-service:1.1.1")
api("com.google.auto.service:auto-service-annotations:1.1.1")
implementation(project(":stone"))
implementation(project(":http"))
implementation(project(":http-export"))
implementation(project(":room"))
implementation(project(":room-export"))
implementation(project(":report"))
implementation(project(":report-export"))

具体调用方法在MainActivity.ktHttpInterceptor.kt,应用内BaseApplication应替换为项目实际使用类

使用说明

项目中为了演示效果采用钉钉上报,因此需要申请企业应用AppKey/SecretKey/AgentId/进行鉴权以,通过以上key信息获取UserIds完成消息推送通知, 实际应用中推荐上报到日志中台或相关日志平台,作为全链路监控的一环

钉钉开放平台 > open.dingtalk.com/
gradle.properties 需配置以下内容,关联影响鉴权方法位置 : DingRepository.kt

ini 复制代码
# Ding key config
dingAppKey=
dingSecretKey=
dingAgentId=
dingUsersId=

实际调用代码示例

DingProvider.kt

kotlin 复制代码
suspend fun reportWorkSpace(title: String, content: String, success: (Boolean) -> Unit) {
  ...
  ...
  ...
}

可自定义DataServerProvider.kt ,处理实际数据上报平台

kotlin 复制代码
suspend fun reportDataServer(title: String, content: String, success: (Boolean) -> Unit) {
   //do something
}

suspend fun reportDataOther(title: String, content: String, success: (Boolean) -> Unit) {
   //do something
}

演示效果截图 screenshot

ServiceLoader

基于@AutoService注解生成的metadata可在各自组件build目录查看,例如:

  • report/build/intermediates/java_res/debug/out/META_INF/services/*
  • report/build/intermediates/runtime_library_classes_dir/debug/META_INF/services/*
  • report/build/tmp/kapt3/classes/debug/META_INF.services

使用方式可参考各业务组件中含有@AutoService class,例如:

auto-service官方文档地址 > github.com/google/auto...
Declaration Interface

kotlin 复制代码
/**
 * Define lifecycle methods what you want to use
 */
interface ApplicationLifecycle {

    /** Lifecycle onAttachBaseContext */
    fun onAttachBaseContext(context: Context)

    /** Lifecycle onCreate */
    fun onCreate(application: Application)

    /** marking priority,0 is highest priority,next is 1,2,3...100 ,you can custom it */
    fun priority(): Int
}

Annotation Class

kotlin 复制代码
/**
 * Add google [@AutoService] annotation,to implementation it
 */
@AutoService(ApplicationLifecycle::class)
class ApplicationLifecycleProxy : ApplicationLifecycle {

    override fun onAttachBaseContext(context: Context) {
        //do somethings
    }

    override fun onCreate(application: Application) {
        //do somethings
        DataStoreProvider.initDataStore(application)
    }

    override fun priority(): Int {
        //in base application context,do somethings highest priority,so you should return 0
        return 0
    }
}

ServiceLoader.load

kotlin 复制代码
/**
 * With google auto-service,it will collect all @AutoService annotation class
 */
class ServiceLoaderProxy {

    private val loader: ServiceLoader<ApplicationLifecycle> by lazy {
        ServiceLoader.load(ApplicationLifecycle::class.java)
    }

    val lifecycleQueue by lazy { loader.sortedWith(compareBy { it.priority() }) }
}

引用三方库

  1. Google JetPacks > developer.android.com/jetpack
  2. Kotlin Coroutines > github.com/Kotlin/kotl...
  3. Retrofit & Okhttp > github.com/square/retr... & github.com/square/okht...
  4. Gson > github.com/google/gson
  5. auto-service > github.com/google/auto...
相关推荐
AC赳赳老秦1 小时前
OpenClaw + 云数据库运维:自动备份、扩容、迁移 RDS/MySQL 云数据库
运维·开发语言·数据库·人工智能·python·mysql·openclaw
TDengine (老段)2 小时前
TDengine 物理计划生成 — 算子下沉、Exchange 与 Subplan 切分
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
swordbob2 小时前
MYSQL RR 解决“脏读+不可重复读“和“幻读“的本质区别
数据库·mysql
IvorySQL2 小时前
PostgreSQL 全球对话:开源链接世界,共建共治共享
数据库·postgresql·开源
Nontee2 小时前
新手数据库进阶:大白话图解四大隔离级别与底层机制
数据库·oracle
dishugj2 小时前
【YashanDB 认证】我的崖山数据库初体验:从陌生到上手的成长之路
数据库
前端 贾公子2 小时前
Claude Code 的 skills 源码解析 (上)
数据库·人工智能
吠品2 小时前
.NET 8 单文件发布:把 exe 和一堆 dll 打进一个文件里
服务器·数据库·windows
cmes_love2 小时前
期货五档tick数据下载教程期权五档高频历史数据以及分钟量化回测下载
数据库
蚂蚁数据AntData2 小时前
从ChatBI到业务记忆:重新定义数据智能的生产力边界
大数据·网络·数据库·人工智能·算法