This monitoring tool is awesome! The performance of the microservice application is in front of it at a glance

Recommended reading:

Summary

As the microservice system becomes larger and larger, the calling relationships between services become more and more complex, and a tool is needed to help sort out the service links requested. Recently, it was discovered that Application Performance Monitoring (APM) can also solve this problem. After comparing SkyWalking and Elastic APM, I found that Elastic APM is even better. Today we come to a wave of Elastic APM use practice!

Introduction to Elastic APM

Elastic APM is an application performance monitoring (APM) system built on Elastic Stack. It mainly has the following uses:

  • Used to monitor application performance information in real time, including HTTP request call duration, database query information, cache call information, and external HTTP request call information. Help us quickly identify and solve performance problems.
  • Automatically collect unhandled errors and exceptions in the application, and display the stack information of the exceptions, which helps to quickly locate the exception and understand the frequency of occurrence.
  • Metrics are another important source of information when debugging production systems. Elastic APM Agent will automatically collect host-level metrics (such as Java JVM and Go Runtime metrics).
  • Supports distributed request link tracking, enabling you to analyze the performance problems of the entire service architecture in one view.

Related components

Elastic APM includes four major components: APM Agent, APM Server, Elasticsearch, Kibana.

  • APM Agent: Provided in the form of an application library, responsible for collecting performance monitoring data and error data during application runtime, and sending it to APM Server after being cached for a short time.
  • APM Server: An independent component responsible for receiving performance monitoring data sent by APM Agent. After the data is verified and processed, it will be transferred to Elasticsearch, and then the performance monitoring data can be viewed in the Kibana APM application.
  • Elasticsearch: Used to store application performance monitoring data and provide aggregation functions.
  • Kibana APM app: Visually view APM performance monitoring data to help find performance bottlenecks.

Data model

The Elastic APM Agent captures different types of information from the applications it detects. These operations are called events and can be Span, Transaction, Error, or Metric.

  • Span: Span contains information about the code execution path during an operation. It measures from the beginning to the end of the operation, and can have a parent/child relationship with other Spans.
  • Transaction: Transaction is a special Span with other attributes associated with it. It describes the highest-level events captured by Elastic APM Agent, such as a request, a batch task, etc.
  • Error: The Error event contains at least the information of the original exception or the created log where the error occurred.
  • Metric: APM Agent automatically obtains basic host-level indicators, including system and process-level CPU and memory indicators. It is also possible to obtain agent-specific metrics, such as JVM metrics in Java Agent and Go runtime metrics in Go Agent.

Use practice

After learning the basic concepts above, it is time to practice in Wave. Next, we will use Elastic APM to monitor the performance information of SpringBoot applications.

Install Elasticsearch and Kibana

Before installing Elastic APM, we need to install Elasticsearch and Kibana. For details, please refer to "You actually go to the server to collect logs. Isn't it good to build a log collection system!" , pay attention to use version 7.6.2.

Install APM Server

  • After the download is complete, unzip it to the specified directory;

  • Modify the configuration file apm-server.ymland modify the connection address of Elasticsearch;
output.elasticsearch:
  hosts: ["localhost:9200"]
  • Use the following command to start APM Server, and APM Server will 8200run on the port if it starts successfully ;
apm-sever -e
  • Check whether the APM Server is started successfully in Kibana, visit the address: http://localhost:5601/app/kibana#/home/tutorial/apm

SpringBoot integrates APM Agent

There are three ways to integrate APM Agent in Java applications. We use the simplest way to integrate directly in the application.

  • In pom.xmladding the relevant dependencies;
<!--Elastic Agent相关依赖-->
<dependency>
    <groupId>co.elastic.apm</groupId>
    <artifactId>apm-agent-attach</artifactId>
    <version>1.17.0</version>
</dependency>
  • mainAdd Elastic APM's Attach API to the method of the application startup class ;
@SpringBootApplication
public class MallTinyApplication {

    public static void main(String[] args) {
        ElasticApmAttacher.attach();
        SpringApplication.run(MallTinyApplication.class, args);
    }

}
  • resourceAdd Elastic APM configuration file in the directory elasticapm.properties;
# 配置服务名称
service_name=mall-tiny-apm
# 配置应用所在基础包
application_packages=com.macro.mall.tiny
# 配置APM Server的访问地址
server_urls=http://localhost:8200
  • Check whether the APM Agent has started successfully in Kibana, visit the address: http://localhost:5601/app/kibana#/home/tutorial/apm

View performance monitoring information

  • After opening the monitoring panel, we can find that our mall-tiny-apmservice already exists;

  • Call the application interface multiple times to view application performance information;

  • Open a certain Transactionview details, we can see that even the SQL execution time-consuming information has been counted for us;

  • Not only that, open the Spanview details of the execution query , and even the SQL statements are collected for us;

  • Add a remote call interface to the project and see if you can collect the request call link;
/**
 * 品牌管理Controller
 * Created by macro on 2019/4/19.
 */
@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @ApiOperation("远程调用获取所有品牌信息")
    @RequestMapping(value = "/remoteListAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> remoteListAll() {
        //模拟耗时操作
        ThreadUtil.sleep(1, TimeUnit.SECONDS);
        //远程调用获取数据
        String response = HttpUtil.get("http://localhost:8088/brand/listAll");
        JSONObject jsonObject = new JSONObject(response);
        JSONArray data = jsonObject.getJSONArray("data");
        List<PmsBrand> brandList = data.toList(PmsBrand.class);
        return CommonResult.success(brandList);
    }
}
  • Found that it is completely possible, Elastic APM can completely replace Sleuth+Zipkin to do the request link tracking of microservices;

  • Using our previous springcloud-learningmicroservice invocation case, it is also possible to track the request link;

  • Next, we artificially create an exception, just add it int i=1/0;to the method, and check the collected exception information;

  • Let's take a look at the measurement information of the application host. It is very comprehensive. The CPU, memory, and JVM information are all available. You can check it out when you perform performance tuning later!

to sum up

Elastic APM can completely replace Sleuth+Zipkin to do distributed request link tracking, and provides statistics on the duration of database and cache calls, which is very good and powerful! More than that, it can also be used to monitor application performance information and metrics in real time, and even error logs are collected. It is a very good application performance monitoring tool!

Guess you like

Origin blog.csdn.net/weixin_45784983/article/details/109285319