feat: add tracing enrichment and prometheus exporter
This commit is contained in:
@@ -709,35 +709,37 @@ scrape_configs:
|
||||
- targets: ['node-exporter:9100']
|
||||
```
|
||||
|
||||
### 8.2 应用监控指标
|
||||
### 8.2 应用监控指标(OpenTelemetry + Prometheus Exporter)
|
||||
```csharp
|
||||
// Program.cs - 添加Prometheus监控
|
||||
builder.Services.AddPrometheusMetrics();
|
||||
// Program.cs - 指标与探针
|
||||
builder.Services.AddHealthChecks();
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithMetrics(metrics =>
|
||||
{
|
||||
metrics
|
||||
.AddAspNetCoreInstrumentation()
|
||||
.AddHttpClientInstrumentation()
|
||||
.AddRuntimeInstrumentation()
|
||||
.AddPrometheusExporter(); // /metrics
|
||||
});
|
||||
|
||||
app.UseMetricServer(); // /metrics端点
|
||||
app.UseHttpMetrics(); // HTTP请求指标
|
||||
var app = builder.Build();
|
||||
app.MapHealthChecks("/healthz"); // 存活/就绪探针
|
||||
app.MapPrometheusScrapingEndpoint(); // 默认 /metrics
|
||||
```
|
||||
|
||||
// 自定义指标
|
||||
public class MetricsService
|
||||
自定义业务指标(使用 `System.Diagnostics.Metrics`,由 Prometheus Exporter 暴露):
|
||||
```csharp
|
||||
internal static class BusinessMetrics
|
||||
{
|
||||
private static readonly Counter OrderCreatedCounter = Metrics
|
||||
.CreateCounter("orders_created_total", "Total orders created");
|
||||
|
||||
private static readonly Histogram OrderProcessingDuration = Metrics
|
||||
.CreateHistogram("order_processing_duration_seconds", "Order processing duration");
|
||||
|
||||
public void RecordOrderCreated()
|
||||
{
|
||||
OrderCreatedCounter.Inc();
|
||||
}
|
||||
|
||||
public IDisposable MeasureOrderProcessing()
|
||||
{
|
||||
return OrderProcessingDuration.NewTimer();
|
||||
}
|
||||
private static readonly Meter Meter = new("TakeoutSaaS.App", "1.0.0");
|
||||
public static readonly Counter<long> OrdersCreated = Meter.CreateCounter<long>("orders_created_total", "个", "订单创建计数");
|
||||
public static readonly Histogram<double> OrderProcessingSeconds = Meter.CreateHistogram<double>("order_processing_duration_seconds", "s", "订单处理耗时");
|
||||
}
|
||||
```
|
||||
|
||||
Prometheus 抓取示例:见 `deploy/prometheus/prometheus.yml`,默认拉取 `/metrics`,告警规则见 `deploy/prometheus/alert.rules.yml`。
|
||||
|
||||
### 8.3 Grafana仪表板
|
||||
```json
|
||||
{
|
||||
@@ -1007,4 +1009,3 @@ docker-compose up -d --force-recreate --no-deps api
|
||||
docker pull takeout-saas-api:previous-version
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
## 4. 安全与合规
|
||||
- [x] RBAC 权限、租户隔离、用户/权限洞察 API 完整演示并在 Swagger 中提供示例。
|
||||
- [ ] 现状梳理:租户解析/过滤已具备(TenantResolutionMiddleware、TenantAwareDbContext),JWT 已写入 roles/permissions/tenant_id(JwtTokenService),PermissionAuthorize 已在 Admin API 使用,CurrentUserProfile 含角色/权限/租户;但仅有内嵌 string[] 权限存储,无角色/权限表与洞察查询,Swagger 缺少示例与多租户示例。
|
||||
- [x] 现状梳理:租户解析/过滤已具备(TenantResolutionMiddleware、TenantAwareDbContext),JWT 已写入 roles/permissions/tenant_id(JwtTokenService),PermissionAuthorize 已在 Admin API 使用,CurrentUserProfile 含角色/权限/租户;但仅有内嵌 string[] 权限存储,无角色/权限表与洞察查询,Swagger 缺少示例与多租户示例。
|
||||
- [x] 差距与步骤:
|
||||
- [x] 增加权限/租户洞察查询(按用户、按租户分页)并确保带 tenant 过滤(TenantAwareDbContext 或 Dapper 参数化)。
|
||||
- [x] 输出可读的角色/权限列表(基于现有种子/配置的只读查询)。【已落地:RBAC1 模型 + 角色/权限管理 API;Swagger 示例后续补充】
|
||||
@@ -41,8 +41,8 @@
|
||||
- [ ] Secret Store/KeyVault/KMS 管理敏感配置,禁止密钥写入 Git/数据库明文。
|
||||
|
||||
## 5. 观测与运维
|
||||
- [ ] TraceId 贯通,并在 Serilog 中输出 Console/File/ELK 三种目标。
|
||||
- [ ] Prometheus exporter 暴露关键指标,/health 探针与告警规则同步推送。
|
||||
- [x] TraceId 贯通,Serilog 输出 Console/File(ELK 待后续配置)。
|
||||
- [x] Prometheus exporter 暴露关键指标,/health 探针与告警规则同步推送。
|
||||
- [ ] PostgreSQL 全量/增量备份脚本及一次真实恢复演练报告。
|
||||
|
||||
## 6. 业务能力补全
|
||||
|
||||
Reference in New Issue
Block a user