Apache ShardingSphere Agent 可观察性实用指南

背景
Apache ShardingSphere 作为践行 Database Plus 理念的数据服务平台,其包含数据分片、读写分离、数据加密、影子库、分布式事务、弹性伸缩等多种数据管理功能。当 Apache ShardingSphere 应用于实际生产中,用户往往需要对其性能进行监控,并结合业务分析定位出现的异常问题。我们知道 APM(应用性能监控)是通过对系统可观察性数据进行采集、存储和分析,进行系统的性能监控与诊断,主要功能包括性能指标监控、调用链分析,应用拓扑图等,一般通过 Tracing(链路跟踪)、Metrics(指标监控)和 Logging(日志)的手段来获取系统运行状况的可观察性数据,因此 Apache ShardingSphere 也为用户提供了可观察性的功能。

蒋平川,SphereEx 高级中间件工程师,Apache Tomcat & Apache ShardingSphere Contributor。目前专注于开源技术领域,在 SphereEx 主要负责 SphereEx-Console、SphereEx-Boot 的研发工作。

姜茂林,SphereEx 高级中间件工程师,Apache ShardingSphere Contributor。目前专注于 SphereEx 商业相关研发。

设计目标
Apache ShardingSphere 通过配置已经提供了日志的输出,所以当前 Apache ShardingSphere 可观察性功能目标主要是为用户提供必要的指标数据、链路数据。同时,我们希望在提供可观察性的时候,不会因为这部分功能引入对其他代码模块的改动,避免代码复杂度的上升。

设计内容
我们选择了监控常用的代理探针设计模式来实现 Apache ShardingSphere 的可观察模块,并采用优秀的 ByteBuddy 作为运行时代理生成组件,结合 Java Agent 技术实现了可观察性模块的代码零侵入。此外,我们通过插件化的技术来支持的不同框架或者系统的 Metrics 与 Tracing。通过插件化的设计,也能支持用户进行自定义,开发特定业务的自有功能组件。

当前 Apache ShardingSphere 的 Agent 模块自带功能提供了对 Prometheus、Zipkin、Jaeger、SkyWalking 及 OpenTelemetry 的支持。

实战指南
我们通过在 Apache ShardingSphere-Proxy 上部署 Prometheus 及 Zipkin 的可观察性数据插件的示例来具体实战演示一下 Apache ShardingSphere Agent 模块的使用。

软件准备
prometheus-2.32.1.linux-amd64.tar.gz (https://prometheus.io/download)
zipkin-server-2.23.9-exec.jar (https://zipkin.io/pages/quick...)
apache-shardingsphere-5.1.0-SNAPSHOT-shardingsphere-proxy-bin.tar.gz
apache-shardingsphere-5.1.0-SNAPSHOT-shardingsphere-agent-bin.tar.gz
MySQL 5.7.34
系统部署
端口说明

Prometheus Server:9090

Zipkin Server:9411

Apache ShardingSphere-Proxy:3307

Apache ShardingSphere Agent(Prometheus Plugin):9000

Prometheus

我们需要添加监控对象到 Prometheus,这里需要将 Apache ShardingSphere Agent 配置的 9000 端口地址添加到 Prometheus 的配置文件 prometheus.yml。

vi prometheus.yml
在文件中的 static_configs 下面添加:

  • targets: ["localhost:9000"]

然后启动:

./prometheus &
Zipkin

使用 Zipkin Server 比较简单,在 Zipkin Server 的目录通过如下命令启动:

java -jar zipkin-server-2.23.9-exec.jar &
ShardingSphere

部署 Apache ShardingSphere-Proxy 及 Agent,可以参考官网(https://shardingsphere.apache...)关于 Proxy 和可观察性的使用说明文档。假设我们将 Proxy、Agent 都在 /tmp 目录,以下是部署 Agent 的具体步骤:

修改配置
修改 agent.yaml 配置文件,开启 Prometheus、 Zipkin 插件,Prometheus 插件数据端口修改为 9000,与前面的端口说明对应。具体内容如下:

applicationName: shardingsphere-agent
ignoredPluginNames:

  • Jaeger
  • OpenTracing
  • OpenTelemetry
  • Logging

plugins:
Prometheus:

host:  "localhost"
port: 9000
props:
  JVM_INFORMATION_COLLECTOR_ENABLED : "true"

Jaeger:

host: "localhost"
port: 5775
props:
  SERVICE_NAME: "shardingsphere-agent"
  JAEGER_SAMPLER_TYPE: "const"
  JAEGER_SAMPLER_PARAM: "1"

Zipkin:

host: "localhost"
port: 9411
props:
  SERVICE_NAME: "shardingsphere-agent"
  URL_VERSION: "/api/v2/spans"
  SAMPLER_TYPE: "const"
  SAMPLER_PARAM: "1"

OpenTracing:

props:
  OPENTRACING_TRACER_CLASS_NAME: "org.apache.skywalking.apm.toolkit.opentracing.SkywalkingTracer"

OpenTelemetry:

props:
  otel.resource.attributes: "service.name=shardingsphere-agent"
  otel.traces.exporter: "zipkin"

Logging:

props:
  LEVEL: "INFO"

添加到启动命令
修改 /tmp/apache-shardingsphere-5.1.0-shardingsphere-proxy-bin/bin/start.sh 文件,将 Agent 下的 shardingsphere-agent.jar 文件的绝对路径添加到启动脚本中。

nohup java ${JAVA_OPTS} ${JAVA_MEM_OPTS} \
-classpath ${CLASS_PATH} ${MAIN_CLASS} >> ${STDOUT_FILE} 2>&1 &
改为:

nohup java ${JAVA_OPTS} ${JAVA_MEM_OPTS} \
-javaagent:/tmp/apache-shardingsphere-5.1.0-shardingsphere-agent-bin/shardingsphere-agent.jar \
-classpath ${CLASS_PATH} ${MAIN_CLASS} >> ${STDOUT_FILE} 2>&1 &
启动
现在可以在 Proxy 的目录下启动了:

bin/start.sh
访问测试
指标及链路数据

我们采用 Proxy 默认提供的分库分片配置场景 config-sharding.yaml 来进行后续访问及数据的展示。

使用 MySQL 命令行连接到启动的 ShardingSphere-Proxy

检查 Prometheus Server 及 Zipkin Server 的数据结果
通过 Prometheus Web 查询 proxyinfo 信息,获取 proxyinfo 数据结果。

通过 MySQL client 连接后,查看 Zipkin Web 链路信息,如下:

通过 MySQL 命令行查询数据

检查 Prometheus Server 及 Zipkin Server 的数据结果
通过 Prometheus Web 查询 parsesqldmlselecttotal 数据结果。

通过 Zipkin Web 查询链路信息,如下:

通过对 Span 细心查找,我们可以选中具体查看 select * from t_order 这条 SQL 语句的链路情况:

拓扑图

我们通过 Zipkin Web 查看依赖关系的时候,发现没有生成拓扑图。现在,我们需要通过配置来添加上依赖拓扑。

下载文件
首先下载下表中 Zipkin 相关的依赖文件,并拷贝到 Proxy 的 lib 目录。

修改配置
配置 Proxy 的 conf 目录下对应数据源配置文件 config-sharding.yaml,在 dataSources 节点的下数据源连接对应 url 上添加如下配置:

MySQL 5.x:statementInterceptors=brave.mysql.TracingStatementInterceptor

或 MySQL 8.x:queryInterceptors=brave.mysql8.TracingQueryInterceptor

重新启动 Proxy
然后在进行跟前面的一样访问测试后,在通过 Zipkin Web 查看依赖,我们可以看到拓扑图:

采样率

Apache ShardingSphere Agent 的可观察性插件还支持采样率的设置,针对不同的场景,可以配置不同的采样率来适应。Zipkin 插件支持的采样率类型配置有 const、counting、ratelimiting、boundary。对于请求量比较大的场景,我们建议使用类型 boundary,并配置合适的采样率可以很好降低链路数据的采集量。

Zipkin:

host: "localhost"
port: 9411
props:
  SERVICE_NAME: "shardingsphere-agent"
  URL_VERSION: "/api/v2/spans"
  SAMPLER_TYPE: "boundary"
  SAMPLER_PARAM: "0.001"

结语
Apache ShardingSphere 通过提供的可观察性功能,并且默认支持与多种常用的监控框架和系统对接,方便用户对其进行监控管理。未来我们还将继续增强并完善 Apache ShardingSphere 的监控方面的能力,希望大家持续关注。

欢迎添加社区经理微信(ss_assistant_1)加入交流群,与众多 ShardingSphere 爱好者一同交流。

你可能感兴趣的:(后端数据库程序员)