前言
Elastic APM實作鏈路追蹤,首先要參考開源的APMAgent(APM代理),然后將監控的資訊發送到APMServer,然后在轉存入ElasticSearch,最后有Kibana展示;具體流程如下圖所示:

首先我們下載ElasticSearch、Kibana、APM Server;可以先簡單的理解為 ElasticSearch是存盤資料用的,Kibana是展示資料用的,
ElasticSearch下載地址:
https://www.elastic.co/cn/downloads/elasticsearch
Kibana下載地址:
https://www.elastic.co/cn/downloads/kibana
APM Server下載地址:
https://www.elastic.co/cn/downloads/apm
在下載時需要注意,ElasticSearch、Kibana、APM Server要下載相同的版本,相同平臺的;下圖為下載時的注意事項,

這里我下載的都是window下的安裝包,下載檔案如下圖:

Elastic免費版本支持內容
安裝ElasticSearch
ElasticSearch依賴于java的SDK,所以需要安裝Java的SDK,
java安裝參考,
為了測驗,我隨便找了一臺有java環境的電腦,系統是window10,java環境如下圖:

然后將ElasticSearch壓縮包解壓,找到config\elasticsearch.yml檔案,記事本打開,修改配置如下:
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"]
然后找到bin下面的elasticsearch.bat,雙擊運行腳本安裝ElasticSearch,

elasticsearch.bat會啟動一個CMD視窗,該視窗將是ElasticSearch的宿主,即關閉了他,ElasticSearch就會關閉,
讓CMD視窗運行一會,然后打開瀏覽器,輸入 http://localhost:9200 ,顯式以下畫面,說明ES安裝成功,因為我是隨機找的有java環境的電腦安裝的,所以ElasticSearch雖然依賴于JDK,但似乎對版本并不苛刻,

安裝Kibana
與ElasticSearch類似,將Kibana壓縮包解壓,找到bin下面的Kibana.bat,雙擊運行腳本安裝Kibana,
Kibana.bat也會啟動一個CMD視窗,該視窗將是Kibana的宿主,
讓CMD視窗運行一會,然后打開瀏覽器,輸入http://localhost:5601/,顯式以下畫面,說明kibana安裝成功,

Config檔案夾下的kibana.yml是默認的組態檔,想深入研究的可以百度搜索一下這個檔案的使用,這里我沒有配置,所以使用的都是默認配置,用的請求地址也是localhost,

修改server.host可以修改請求地址:
server.host: "0.0.0.0"
修改i18n.locale的值為"zh-CN"為,可以啟動漢化版本kibana,

安裝APM Server
首先解壓APM Server的壓縮包,如下圖:

然后在里面找到ApmServer的組態檔apm-server.ym,
將組態檔最開始的host: “localhost:8200”修改成“0.0.0.0:8200”,以便讓他能允許通過ip:埠號的方式訪問,然后增加elasticsearch地址配置:
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: ["已安裝好的ES地址,多個之間用逗號隔開"]
效果如下:

然后運行apm-server.exe,這里需要注意的是apm-server.exe是個window服務,所以這里不能雙擊運行,不過安裝包中已經有可以啟動服務的檔案了——install-service-apm-server.ps1,
具體安裝ApmServer步驟,
1.從下載頁面下載 APM Server Windows zip 檔案,
2.將 zip 檔案的內容解壓縮到 C:\Program Files,
3.將 apm-server-7.16.3-windows 目錄重命名為 APM-Server,
4.以管理員身份打開 PowerShell 提示符(右鍵單擊 PowerShell 圖示,然后選擇以管理員身份運行),如果運行的是 Windows XP,則可能需要下載并安裝 PowerShell,
5.從 PowerShell 提示符處,運行以下命令以將 APM Server 安裝為 Windows 服務,

然后在彈出的頁面中輸入Y,然后回車,

注意:如果您的系統禁用了腳本執行,則需要為當前會話設定執行策略,以允許腳本運行,示例:
PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-apm-server.ps1`
然后我們打開控制面板—管理工具—服務,就可以看到,我們已經安裝了一個apm-server的服務了,
不過他的當前狀態是未啟動,我們右鍵將服務啟動就可以了,

然后我們瀏覽http://localhost:8200/,得到如下界面,代表ApmServer已經安裝成功了,

默認的日志在:C:\ProgramData\apm-server
聯合測驗
我們重新打開Kibana——http://localhost:5601/,然后點擊AddIntegrations,
然后點擊ElasticAPM,

然后選擇Windows,

然后頁面向下拉,點擊Check APM Server status——檢測APM Server的狀態,

如下圖,提示了已經正確安裝了APM Server,

APMAgent使用
首先創建一個NETCore的Web專案,然后引入Elastic相關DLL,
可以單獨參考獨立的DLL,
-
Elastic.Apm
-
Elastic.Apm.AspNetCore
-
Elastic.Apm.EntityFrameworkCore
也可以直接就參考一個整合的DLL,推薦就參考一個Elastic.Apm.NetCoreAll,
-
Elastic.Apm.NetCoreAll
然后在Startup的Configure方法中使用 app.UseElasticApm(Configuration),
代碼如下:
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();
});
}
}
}
然后appsettings.json修改如下:
{
"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/", //設定前面安裝好的APM Server URL,默認埠號是8200
"LogLevel": "Error", // Log級別,
"ServiceName": "TEST1", //應用的名字
//"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*"
}
}
然后運行網站,如下圖:

專案運行后,第一行提示我們ElasticAPM收集資料是30000毫秒的間隔,即每30秒采集一次資料,
Github參考網站,可以下載源代碼了解更多詳情,
查看檢測的資料
我們回到剛才Kibana的頁面,繼續向下拉,如下圖(截圖已經是中文 了,因為我已經漢化了),

點擊【檢測代理狀態】,提示尚未從代理收到任何資料,但我的專案運行正常,所以直接點擊加載Kibana物件,然后啟動APM,進入服務管理界面,

點擊Test1,這是配置ApmAgent的專案,界面如下,

可以看到,他已經在檢測我的網站的訪問和吞吐量等等資訊了,
點擊【指標】,還有CPU和記憶體的統計,

點擊【事務】,這里的【時間線】就有我們請求的鏈路跟蹤,即微服務所需的鏈路跟蹤功能,

結語
如果一個公司存在Java團隊和C#團隊,我們在使用APM的時候,強烈建議使用ELK,因為大家都統一使用一個工具交流很方便,可以避免非常非常非常多不必要的麻煩,
https://www.cnblogs.com/kiba/轉載請註明出處,本文鏈接:https://www.uj5u.com/net/421265.html
標籤:.NET技术
