当前位置: 首页 > news >正文

ABP VNext + Elastic APM:微服务性能监控

ABP VNext + Elastic APM:微服务性能监控 🚀


📚目录

  • ABP VNext + Elastic APM:微服务性能监控 🚀
    • 一、引言 ✨
      • 架构全景图 🏗️
    • 二、环境与依赖 📦
    • 三、APM 服务器与 Kibana 快速部署 🐳
      • Docker 网络示意图
    • 四、在 ABP VNext 中集成 Elastic APM .NET Agent ⚙️
      • 4.1 安装与注册
        • 配置来源
      • 4.2 ABP 模块中注册中间件与标签
      • 4.3 自动采集范围
      • 4.4 URL 过滤 🚫
    • 五、自定义打点与标签 🎯
    • 六、日志关联 📝
      • 6.1 Serilog
      • 6.2 Microsoft.Extensions.Logging / NLog
    • 七、Kibana APM 仪表盘构建 📊
      • 7.1 创建仪表盘(Dashboard)
      • 7.2 Transaction 概览
        • 响应时间分布(95th/99th 百分位)
        • 吞吐量(TPS)
      • 7.3 服务依赖图(Service Map)
      • 7.4 错误监控
        • Error rate
        • 异常类型聚合
      • 7.5 原始 Trace 搜索
      • 7.6 告警与通知
    • 八、性能影响与采样策略 ⚖️
      • 8.1 配置示例(`appsettings.json`)
      • 8.2 性能调优要点
      • 8.3 微基准测试
    • 九、端到端演示示例 🎬
    • 参考文档 📖


一、引言 ✨

TL;DR

  • 🕵️ 使用 Elastic.Apm.NetCoreAll 自动采集 HTTP、数据库、外部调用与错误性能指标
  • ⚙️ 支持环境变量与 appsettings.json 双重配置,零侵入式集成到 ABP VNext 微服务
  • 🔗 在 ABP 模块生命周期中注册 APM 中间件,并注入租户与用户标签(SetLabel API)
  • 📊 日志框架(Serilog、NLog、Microsoft.Extensions.Logging)一键关联 TraceId/TransactionId
  • 🧪 完整示例覆盖 URL 过滤、自定义 Span、微基准测试与性能调优

背景与动机
在微服务架构下,跨服务调用链长、性能瓶颈定位困难。Elastic APM 提供从事务到 Span、从代码层到基础设施的一体化可观察方案,能帮助我们在 Kibana 中实时查看响应分布、调用拓扑与错误热点。结合 ABP VNext 的模块化与多租户能力,可快速搭建高性能、可复现的 APM 监控流水线。

架构全景图 🏗️

应用
服务集群
HTTP/gRPC + TLS
HTTP/gRPC + TLS
gRPC/TCP
HTTP
HTTP
Elastic APM Agent
OrderService-1
OrderService-2
APM Server
Elasticsearch
Kibana APM UI

二、环境与依赖 📦

  • 平台版本

    • .NET 7/8
    • ABP VNext 7.x
  • Elastic Stack

    • Elasticsearch ≥ 8.x
    • Kibana ≥ 8.x(内置 APM 应用)
    • APM Server ≥ 8.x
  • NuGet 包

    • Elastic.Apm.NetCoreAll —— 包含 HTTP、EF Core、SqlClient、Redis 等默认自动打点功能

    • 可选:

      • Elastic.Apm.EntityFrameworkCore(EF Core 细粒度支持)
      • Elastic.Apm.SqlClient(针对 MySQL.Data 或其他 ADO.NET 驱动的拓展)
      • Elastic.Apm.SerilogEnricher(Serilog 日志追踪关联)
      • Elastic.Apm.Extensions.Logging(Microsoft.Extensions.Logging 关联)

三、APM 服务器与 Kibana 快速部署 🐳

version: '3'
services:elasticsearch:image: docker.elastic.co/elasticsearch/elasticsearch:8.5.0environment:- discovery.type=single-nodeports: ["9200:9200"]kibana:image: docker.elastic.co/kibana/kibana:8.5.0ports: ["5601:5601"]apm-server:image: docker.elastic.co/apm/apm-server:8.5.0depends_on: ["elasticsearch"]ports: ["8200:8200"]command: >apm-server -e-E output.elasticsearch.hosts=["elasticsearch:9200"]

执行 docker-compose up -d 后,访问 http://localhost:5601/app/apm 确认 APM 界面可用。

Docker 网络示意图

Docker Host
Elasticsearch:9200
APM Server:8200
Kibana:5601

四、在 ABP VNext 中集成 Elastic APM .NET Agent ⚙️

4.1 安装与注册

dotnet add package Elastic.Apm.NetCoreAll
dotnet add package Elastic.Apm.SerilogEnricher
dotnet add package Elastic.Apm.Extensions.Logging
配置来源
  • 环境变量(优先级最高)

    export ELASTIC_APM_SERVER_URLS=http://localhost:8200
    export ELASTIC_APM_SERVICE_NAME=OrderService
    export ELASTIC_APM_ENVIRONMENT=production
    export ELASTIC_APM_TRANSACTION_SAMPLE_RATE=1.0
    
  • appsettings.json

    "ElasticApm": {"ServerUrls": "http://localhost:8200","ServiceName": "OrderService","Environment": "production","TransactionSampleRate": 1.0
    }
    

    Program.cs 中使用:

    builder.Services.AddAllElasticApm(builder.Configuration);
    

4.2 ABP 模块中注册中间件与标签

public class OrderWebModule : AbpModule
{public override void OnApplicationInitialization(ApplicationInitializationContext context){var app = context.GetApplicationBuilder();// 1) 注入 APM 中间件app.UseElasticApm(context.GetConfiguration());// 2) 全局标签注入:租户 & 用户Agent.Tracer.Subscribe(new LabelSubscriber(span =>{var tenant = context.ServiceProvider.GetService<ICurrentTenant>()?.Id?.ToString();var user   = context.ServiceProvider.GetService<ICurrentUser>()?.Id?.ToString();if (span is Transaction txn){txn.SetLabel("tenantId", tenant);txn.SetLabel("userId",   user);}}));// 3) ABP 默认管道app.UseAbpRequestLocalization();app.UseRouting();app.UseAuthentication();app.UseAuthorization();app.UseConfiguredEndpoints();}
}

💡 使用 SetLabelLabelSubscriber,保证线程安全与一致性。

4.3 自动采集范围

  • HTTP/gRPC:自动创建 Transaction;
  • SQL 调用:跟踪 System.Data.SqlClient / Microsoft.Data.SqlClient(含 Dapper) ;
  • 其他 ADO.NET 驱动:可引入 Elastic.Apm.SqlClient 或 Profiler 模式;
  • 外部 HTTPHttpClient 自动跟踪;
  • 未捕获异常:自动上报 Error。

4.4 URL 过滤 🚫

"ElasticApm": {"TransactionIgnoreUrls": "/health*,/metrics*"
}

五、自定义打点与标签 🎯

using Elastic.Apm;
using Elastic.Apm.Api;// 自定义事务
using var transaction = Agent.Tracer.StartTransaction("ProcessOrder", "order");try
{// 业务逻辑
}
catch (Exception ex)
{// 捕获并上报异常Agent.Tracer.CurrentTransaction?.CaptureException(ex);throw;
}// 自定义 Span
transaction.CaptureSpan("CallPaymentGateway", "external", () =>
{// 第三方支付调用
});// 额外标签
transaction.SetLabel("orderType", "Express");

💡 仅在关键路径添加 Span,避免过度细分导致存储与性能压力。


六、日志关联 📝

6.1 Serilog

using Serilog;
using Elastic.Apm.SerilogEnricher;Log.Logger = new LoggerConfiguration().Enrich.WithElasticApmCorrelationInfo().WriteTo.Console(outputTemplate: "[{ElasticApmTraceId} {ElasticApmTransactionId}] {Message:lj}{NewLine}{Exception}").CreateLogger();builder.Host.UseSerilog();

6.2 Microsoft.Extensions.Logging / NLog

builder.Logging.AddConsole().AddConfiguration(builder.Configuration.GetSection("Logging")).AddElasticApm(builder.Configuration);

📑 日志中包含 TraceId/TransactionId,便于跨系统日志与调用链关联


七、Kibana APM 仪表盘构建 📊

7.1 创建仪表盘(Dashboard)

  1. 在 Kibana 左侧菜单中,依次点击 “Dashboard” → “Create dashboard”
  2. 点击 “Create new visualization”,选择 “Lens” 作为可视化工具。

7.2 Transaction 概览

响应时间分布(95th/99th 百分位)
  1. 在 Lens 中选择 APM transaction duration 指标。
  2. transaction.duration.us 拖到纵轴,设置聚合方式为 Percentile,并在右侧设置 Percentiles95,99
  3. 保存为 “响应时间分布(P95/P99)”,添加到 Dashboard。
吞吐量(TPS)
  1. 在 Lens 中选择 APM transactions per minute(或用 count 并按分钟做 X 轴)。
  2. 拖拽 @timestamp 到横轴,聚合方式设为 Date histogram(interval: auto)
  3. 拖拽 transaction.id 计数到纵轴(Count)。
  4. 保存为 “每分钟吞吐量 (TPS)”,添加到 Dashboard。

7.3 服务依赖图(Service Map)

  1. 在 Kibana 左侧菜单点击 “Observability” → “Service map”
  2. 选择目标服务(如 OrderService),即可看到上下游依赖拓扑。
  3. 可切换时间范围和环境过滤标签,查看不同环境下的调用链。

7.4 错误监控

Error rate
  1. 新建 Visualization,选择 Lens
  2. 拖拽 Error count(apm.error.count)到纵轴,横轴同样用 Date histogram。
  3. 再拖一个 Transaction count(apm.transaction.duration.count),使用 Formula 计算 errors / transactions
  4. 保存为 “错误率(Error rate)”
异常类型聚合
  1. 在 Lens 中,选择 apm.error.exception.type 作为分组字段(Break down by)。
  2. 纵轴用 Count of errors
  3. 保存为 “异常类型分布”

7.5 原始 Trace 搜索

  1. 点击左侧 “Observability” → “APM” → “Traces”
  2. 在上方搜索框输入标签或 transaction.id:<ID>,点击 Search
  3. 浏览单次调用链,查看各个 Span 的详细耗时和堆栈。

7.6 告警与通知

  1. 在 Kibana 菜单中选择 “Alerting” → “Create rule”
  2. 规则类型选择 “APM transaction duration threshold”
  3. 配置阈值(如平均响应时间 > 500 ms)、时间窗口和触发条件。
  4. 添加动作(Email、Slack、Webhook 等)并启用规则。
  5. 在 Dashboard 中可以直接用 “Add panel” 引入告警状态监控。


八、性能影响与采样策略 ⚖️

8.1 配置示例(appsettings.json

"ElasticApm": {"TransactionSampleRate": 0.2,"SpanStackTraceMinDuration": "10ms","StackTraceLimit": 200,"CaptureBody": "errors","SanitizeFieldNames": ["password"],"TransactionIgnoreUrls": "/health*,/metrics*"
}

8.2 性能调优要点

  • 🔽 降低采样率可显著提升吞吐与减少资源占用
  • 🔧 调整堆栈阈值可降低内存与 GC 压力

8.3 微基准测试

[MemoryDiagnoser]
public class ApmSamplingBenchmark
{private readonly HttpClient _client = new();[Params(1.0, 0.5, 0.2)]public double SampleRate;[GlobalSetup]public void Setup(){Environment.SetEnvironmentVariable("ELASTIC_APM_TRANSACTION_SAMPLE_RATE",SampleRate.ToString());// 重启应用后执行 Benchmark}[Benchmark]public async Task CallOrderApi() =>await _client.GetAsync("https://localhost:5001/api/orders");
}

CI 跑分,仅供参考

SampleRate吞吐 (ops/s)内存 (MB)覆盖率 (%)
1.05200155100
0.5610012580
0.273009840

九、端到端演示示例 🎬

HTTP/gRPC + TLS
gRPC/TCP
HTTP
HTTP
ABP 微服务
Elastic APM Agent
APM Server
Elasticsearch
Kibana APM UI
  1. 🚀 启动微服务集群与 Elastic Stack
  2. 🔄 调用下单接口,触发数据库与外部 HTTP
  3. 📈 在 Kibana 查看事务、错误与调用链
  4. 🐌 模拟慢查询与异常,验证告警与过滤规则

参考文档 📖

  • ABP VNext 官方文档
  • Elastic APM .NET Agent 快速上手
  • Elastic APM .NET Agent Serilog 集成
  • Elastic APM .NET Agent Logging 集成
  • APM Server 安装与配置
  • Kibana APM 使用概览
  • BenchmarkDotNet 入门指南
  • Elasticsearch 索引生命周期管理(ILM)
  • Docker Compose 快速入门 Elastic Stack

是的,为了让读者快速上手,建议把第七节的各小项都补充成“如何在 Kibana 中操作”——下面是一个示例补全:


http://www.lryc.cn/news/601691.html

相关文章:

  • Kotlin递归
  • C++算法竞赛篇(五)循环嵌套题型讲解
  • 港股历史逐笔成交与分时十档买卖盘口数据深度解析
  • 标签驱动的可信金融大模型训练全流程-Agentar-Fin-R1工程思路浅尝
  • unity开发中Hash、Queue、LinkedList简单介绍
  • PandasAI连接LLM进行智能数据分析
  • LLM 多语言数据集
  • 《Java 程序设计》第 7 章 - 继承与多态
  • 9. 桥接模式
  • 递归查询美国加速-技术演进与行业应用深度解析
  • Cursor下利用Stagewise实现 “所见即改” 的前端开发体验~
  • MyBatis-Plus IService 接口全量方法实现与测试(续)
  • 【程序员私房菜】python尖椒炒口蘑
  • 神经网络模型训练需要的内存大小计算方法
  • 任务提醒工具怎么选?对比16款热门软件
  • 疯狂星期四文案网第21天运营日记
  • 商汤发布具身智能平台,让机器人像人一样和现实世界交互
  • EMCCD相机与电可调变焦透镜的同步控制系统设计与实现
  • Chainlink Functions:为智能合约插上连接现实世界的翅膀
  • PowerDesigner 画ER图并生成sql 教程
  • 青少年编程能力等级测评试卷及答案 Python编程(三级)
  • Rouge:面向摘要自动评估的召回导向型指标——原理、演进与应用全景
  • Java面试全方位解析:从基础到AI的技术交锋
  • 如何思考一个动态规划问题需要几个状态?
  • 负载均衡 LoadBalance
  • 阻止网页重定向
  • 6、企业信息化
  • 齐护Ebook科技与艺术Steam教育套件 可图形化micropython Arduino编程ESP32纸电路手工
  • 装修独栋别墅需要注意的细节有哪些?
  • 像素农场播种机-作物模拟器HTML+CSS+JavaScript