preface
To realize link tracking, Elastic APM first refers to the open source APMAgent (APM agent), then sends the monitored information to APMServer, and then transfers it to elastic search. Finally, Kibana shows it; The specific process is shown in the figure below:
First, we download ElasticSearch, Kibana and APM Server; It can be simply understood that ElasticSearch is used to store data and Kibana is used to display data.
ElasticSearch download address:
https://www.elastic.co/cn/downloads/elasticsearch
Kibana download address:
https://www.elastic.co/cn/downloads/kibana
APM Server download address:
https://www.elastic.co/cn/downloads/apm
When downloading, it should be noted that ElasticSearch, Kibana and APM Server should download the same version and the same platform; The following figure shows the precautions during downloading.
What I download here are all installation packages under window. The downloaded files are as follows:
Elastic free version support content
Install ElasticSearch
ElasticSearch depends on the Java SDK, so you need to install the Java SDK.
In order to test, I casually found a computer with java environment. The system is windows 10. The java environment is shown in the figure below:
Then unzip the ElasticSearch package and find config \ ElasticSearch YML file, open Notepad, and modify the configuration as follows:
cluster.name: kiba001 node.name: master node.master: true network.host: 0.0.0.0 http.cors.enabled: true http.cors.allow-origin: "*" cluster.initial_master_nodes: ["kiba518"]
Then find ElasticSearch. Under bin Bat, double-click the run script to install ElasticSearch.
elasticsearch.bat will launch a CMD window, which will be the host of ElasticSearch. That is, if you close it, ElasticSearch will close.
Let the CMD window run for a while, then open the browser and enter http://localhost:9200 , the following screen is displayed to show that the ES installation is successful. Because I randomly found a computer with java environment to install, ElasticSearch depends on JDK, but it doesn't seem to be harsh on the version.
Install Kibana
Similar to ElasticSearch, unzip the Kibana compressed package and find Kibana under bin Bat, double-click the run script to install Kibana.
Kibana.bat will also launch a CMD window that will host kibana.
Let the CMD window run for a while, then open the browser and enter http://localhost:5601/ , the following screen is displayed to show that kibana is installed successfully.
Kibana. In Config folder YML is the default configuration file. If you want to make an in-depth study, you can search Baidu for the use of this file. I have no configuration here, so I use the default configuration, and the request address is also localhost.
Modify server Host can modify the request address:
server.host: "0.0.0.0"
Modify I18N If the value of locale is "zh CN", you can start the Chinese version of kibana.
Install APM Server
First, unzip the compressed package of APM Server, as shown in the following figure:
Then find the ApmServer configuration file APM server ym.
Change the initial host of the configuration file: "localhost:8200" to "0.0.0.0:8200", so that it can be accessed through ip: port number. Then add the elasticsearch address configuration:
apm-server: # Defines the host and port the server is listening on. Use "unix:/path/to.sock" to listen on a unix domain socket. host: "0.0.0.0:8200" output.elasticsearch: hosts: ["localhost:9200"] #hosts: ["installed ES addresses, separated by commas"]
The effect is as follows:
Then run APM server Exe, here we need to pay attention to APM server Exe is a window service, so you can't double-click here to run it. However, there is already a file in the installation package that can start the service - install Service APM server ps1.
Specific steps for installing ApmServer.
1. From Download page Download the APM Server Windows zip file.
2. Unzip the contents of the zip file to C:\Program Files.
3. Rename apm-server-7.16.3-windows directory to APM server.
4. Open the PowerShell prompt as administrator (right-click the PowerShell icon and select run as administrator). If you are running Windows XP, you may need to download and install PowerShell.
5. From the PowerShell prompt, run the following command to install APM Server as a Windows service.
Then enter Y in the pop-up page and press enter.
Note: if script execution is disabled in your system, you need to set the execution policy for the current session to allow the script to run. Example:
PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-apm-server.ps1`
Then we open the control panel - management tools - services, and we can see that we have installed an APM server service.
However, his current status is not started. We can right-click to start the service.
Then we browse http://localhost:8200/ , the following interface is obtained, which indicates that ApmServer has been successfully installed.
The default log is in C: \ programdata \ APM server
Joint testing
Let's reopen Kibana—— http://localhost:5601/ , and then click Add integrations.
Then click elastic APM.
Then select Windows.
Then drop down the page and click Check APM Server status - check the status of APM Server.
The following figure indicates that APM Server has been installed correctly.
APMAgent use
First, create a NETCore Web project, and then introduce Elastic related DLL s.
Independent DLL s can be referenced separately.
You can also directly reference an integrated DLL. It is recommended to reference an elastic Apm. NetCoreAll.
Then use app. In the Configure method of Startup UseElasticApm(Configuration).
The code is as follows:
using ... using Elastic; using Elastic.Apm; using Elastic.Apm.AspNetCore; using Elastic.Apm.EntityFrameworkCore; namespace ElasticAPMTest { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseElasticApm(Configuration); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
Then Appsettings JSON is modified as follows:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ElasticApm": { "Environment": "production", // Set the service environment "SecretToken": "", "ServerUrl": "http://192.168.50.28:8200 / ", / / set the previously installed APM Server URL. The default port number is 8200 "LogLevel": "Error", // Log level, "ServiceName": "TEST1", //Application name //"CaptureBody": "all", //"CaptureBodyContentTypes": "application/x-www-form-urlencoded*, text/*, application/json*, application/xml*", "ServiceVersion": "1.0.0.0" //"Environment": "staging", //"CaptureHeaders": "false", //"TransactionSampleRate": 0.456, //"TransactionMaxSpans": 375, //"CaptureBody": "all", //"CaptureBodyContentTypes": "application/x-www-form-urlencoded*, text/*, application/json*, application/xml*" } }
Then run the website, as shown in the following figure:
After the project runs, the first line prompts us that ElasticAPM collects data at an interval of 30000 milliseconds, that is, collect data every 30 seconds.
Github reference website , you can download the source code for more details.
View detected data
Let's go back to Kibana's page just now and continue to pull down, as shown in the following figure (the screenshot is already in Chinese, because I have sinicized it).
Click [detect agent status] to prompt that no data has been received from the agent, but my project is running normally, so directly click to load Kibana object, and then start APM to enter the service management interface.
Click Test1. This is the project for configuring ApmAgent. The interface is as follows.
You can see that he is already checking the access and throughput of my website.
Click [index] and statistics of CPU and memory.
Click [transaction] and the [timeline] here will have the link tracking we requested, that is, the link tracking function required by the microservice.
epilogue
If a company has Java team and C# team, we strongly recommend using ELK when using APM, because it is convenient for everyone to use one tool for communication and avoid many unnecessary troubles.