赞
踩
业务应用集成ES APM SDK -> 数据采集至ES APM SERVER -> 再将数据同步至ES服务器 -> 在ES服务中可以对数据进行可视化监控
本文仅展示APM Server与ES APM SDK集成,ElasticSearch和Kibana未做展示
本次仅介绍windows版本安装部署
重要!!! 版本需要与ElasticSearch保持一致,如ES版本为 v 7.17.10,APM Server也需要 7.17.10(否则索引文件会出现部分字段映射失败)
- 解压缩下载的 APM Server 文件到一个目录中。如: D:\apm-server - 编辑 apm-server.yml 配置文件,配置 Elasticsearch 和 Kibana 的连接信息: ######################### APM Server Configuration ######################### ################################ APM Server ################################ apm-server: host: "localhost:8200" # APM Server 监听的地址和端口 rum: enabled: true # 是否启用 Real User Monitoring (RUM) ilm: enabled: false # 关闭索引生命周期管理 (ILM),可根据需要调整 kibana: enabled: true host: "XXXXXX:5601" username: "elastic" password: "123456" output.elasticsearch: hosts: ["XXXXXX:9200"] # Elasticsearch 服务地址 username: "elastic" # Elasticsearch 认证用户名 password: "123456" # Elasticsearch 认证密码
-打开命令提示符或 PowerShell,进入 APM Server 的目录。
-依次执行以下命令启动 APM Server
安装:APM Server .\install-service.ps1 查看安装情况 Get-Service -Name "apm-server" 启动服务 Start-Service -Name "apm-server" 停止服务 Stop-Service -Name "apm-server" #卸载服务 .\uninstall-service-apm-server.ps1 #强制卸载 sc delete "apm-server"
访问 http://youhost:8200,访问出现如下,则说明安装成功
<dependency>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-agent-attach</artifactId>
<version>1.33.0</version>
</dependency>
<dependency>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-agent-api</artifactId>
<version>1.33.0</version>
</dependency>
# APM Server 相关配置, service_name=dts-wx-api # 配置对应APM Server 服务地址 server_urls=http://localhost:8200 # 应用程序相关配置 environment=test application_packages=com.qiguliuxing # 日志级别配置 log_level=INFO # 事务采样率 transaction_sample_rate=0.5 # 忽略的 URL 配置 ignore_urls=/health,/status # 错误捕获配置 capture_body=errors # 跨度记录最小持续时间配置 span_frames_min_duration=5m
若不想建property文件,想在yml文件中配置,需要手动读yml文件,加入环境变量
@Autowired private ElasticApmProperties elasticApmProperties; @PostConstruct public void apmServerConfig() { // 获取 Elastic APM Java Agent 配置信息 System.setProperty("elastic.apm.service_name", elasticApmProperties.getServiceName()); System.setProperty("elastic.apm.server_urls", elasticApmProperties.getServerUrls()); System.setProperty("elastic.apm.environment", elasticApmProperties.getEnvironment()); System.setProperty("elastic.apm.application_packages", elasticApmProperties.getApplicationPackages()); System.setProperty("elastic.apm.log_level", elasticApmProperties.getLogLevel()); System.setProperty("elastic.apm.transaction_sample_rate", elasticApmProperties.getTransactionSampleRate()); System.setProperty("elastic.apm.ignore_urls", elasticApmProperties.getIgnoreUrls()); System.setProperty("elastic.apm.capture_body", elasticApmProperties.getCaptureBody()); System.setProperty("elastic.apm.span_frames_min_duration", elasticApmProperties.getSpanFramesMinDuration()); // 初始化 Elastic APM Java Agent ElasticApmAttacher.attach(); }
public class Application {
public static void main(String[] args) {
ElasticApmAttacher.attach();
SpringApplication.run(Application.class, args);
}
}
从 Maven Central 下载代理 jar。切勿将该代理添加为您的应用程序的依赖项。
添加 -javaagent 标志并使用系统属性配置代理。
默认动态采集指标,除了可以采集Transaction、span 得运行时间信息外,还可以看到具体执行得SQL语句,出现报错得具体报错信息
在 Elastic APM 中,Transaction 和 Span 是两个重要的概念,用于表示应用程序中的不同层级和时间跨度的工作单元或事件。理解这两个概念对于分析和监控应用程序的性能非常重要。
在代码中,可以通过APM API 创建和管理 Transaction,可以定义在Http请求、后台任务、事件监听等多个场景,
1、API请求添加 Transaction 监控 import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Transaction; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String sayHello() { // 开始一个新的事务 Transaction transaction = ElasticApm.startTransaction(); try { // 设置事务名称 transaction.setName("HelloController.sayHello"); // 执行业务逻辑 String message = "Hello, Elastic APM!"; return message; } catch (Exception e) { // 捕获异常并记录到事务中 transaction.captureException(e); throw e; } finally { // 结束事务 transaction.end(); } } } 2、后台定时任务添加 Transaction 监控 import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Transaction; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTask { @Scheduled(fixedRate = 60000) // 每分钟执行一次 public void performTask() { // 开始一个新的事务 Transaction transaction = ElasticApm.startTransaction(); try { // 设置事务名称 transaction.setName("ScheduledTask.performTask"); // 执行定时任务的业务逻辑 System.out.println("Performing scheduled task..."); // 模拟任务执行时间 Thread.sleep(500); // 模拟任务执行时间为500毫秒 System.out.println("Scheduled task completed."); } catch (Exception e) { // 捕获异常并记录到事务中 transaction.captureException(e); throw e; } finally { // 结束事务 transaction.end(); } } }
Name(名称): 表示 Span 的名称,通常用于描述 Span 的功能或作用。
Type(类型): 表示 Span 的类型,例如 HTTP、DB、External 等。
Timestamp(时间戳): 表示 Span 开始的时间戳。
Duration(持续时间): 表示 Span 的持续时间,即开始到结束的时间间隔。
Tags(标签): 可以添加自定义标签,用于描述或分类 Span。
在代码中,一般作为Transaction得子事件,卸载Transaction标识得方法体内部,用来监控更细颗粒得代码执行情况
import co.elastic.apm.api.ElasticApm; import co.elastic.apm.api.Span; public class MyService { public void performSpan() { Span span = ElasticApm.currentTransaction().startSpan(); span.setName("MyService.performSpan"); try { // 执行 Span 的业务逻辑... span.setTag("status", "success"); } catch (Exception e) { // 发生异常时标记 Span 为失败 span.setTag("status", "error"); throw e; } finally { span.end(); // 结束 Span } } }
在以上 ES 界面的示例中,每个图表/数字称作 Visualize,他们共同组成了一个仪表盘(Dashboard)
仪表盘,可以设置查看的时间和自动更新间隔。在编辑模式,可以配置包含的内容和编排它们的位置。
创建可视化(Visualize)很简单:
在仪表板编辑页面,点击“创建可视化”
然后,左边选择包含 apm-* 的的索引(这是APM信息存储的位置),然后选择图形类型,需要的指标,就可以了。为例:
这样就创建了一个仪表盘,生产环境,所有 API 请求的,按照URL分类的计数可视化图表。
针对具体的业务系统,创建好这些可视化图表,然后安排到一个仪表板,就可以一眼查看系统状态了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。