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

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

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.kt]与[HttpInterceptor.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...
相关推荐
LUCIAZZZ2 小时前
简单的SQL语句的快速复习
java·数据库·sql
Elastic 中国社区官方博客4 小时前
使用真实 Elasticsearch 进行高级集成测试
大数据·数据库·elasticsearch·搜索引擎·全文检索·jenkins·集成测试
@_@哆啦A梦4 小时前
Redis 基础命令
java·数据库·redis
fajianchen4 小时前
MySQL 索引存储结构
数据库·mysql
想做富婆5 小时前
oracle: 多表查询之联合查询[交集intersect, 并集union,差集minus]
数据库·oracle·联合查询
xianwu5436 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
Leven1995276 小时前
Flink (十三) :Table API 与 DataStream API 的转换 (一)
数据库·sql·flink
geovindu7 小时前
neo4j-community-5.26.0 create new database
数据库·mysql·neo4j
因特麦克斯8 小时前
索引的底层数据结构、B+树的结构、为什么InnoDB使用B+树而不是B树呢
数据库
java1234_小锋8 小时前
说说Redis的内存淘汰策略?
数据库·redis·缓存