关于后端:Apache-ShardingSphere-Agent-可观察性实用指南

4次阅读

共计 4366 个字符,预计需要花费 11 分钟才能阅读完成。

背景
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 爱好者一起交换。

正文完
 0