Mud.Feishu.OpenTelemetry 3.0.0

dotnet add package Mud.Feishu.OpenTelemetry --version 3.0.0
                    
NuGet\Install-Package Mud.Feishu.OpenTelemetry -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mud.Feishu.OpenTelemetry" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mud.Feishu.OpenTelemetry" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Mud.Feishu.OpenTelemetry" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mud.Feishu.OpenTelemetry --version 3.0.0
                    
#r "nuget: Mud.Feishu.OpenTelemetry, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mud.Feishu.OpenTelemetry@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mud.Feishu.OpenTelemetry&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Mud.Feishu.OpenTelemetry&version=3.0.0
                    
Install as a Cake Tool

Mud.Feishu.OpenTelemetry

NuGet License

Mud.Feishu OpenTelemetry 适配包,一键开启飞书 SDK 的分布式追踪与指标采集。

项目简介

Mud.Feishu.OpenTelemetry 是 MudFeishu SDK 的可观测性扩展,通过一行代码自动注册飞书 SDK 的 ActivitySource 和 Meter,实现分布式追踪(Tracing)、指标采集(Metrics)和日志导出(Logging)。基于 OpenTelemetry .NET SDK 构建,支持 OTLP gRPC 导出,可与 Jaeger、Tempo、Prometheus、Grafana 等主流可观测性平台无缝集成。

特性

  • ✅ 一键启用 - 一行代码完成飞书 SDK 全链路可观测性配置
  • ✅ 自动注册 - 自动注册 Mud.Feishu 和 Mud.HttpUtils 的 ActivitySource 和 Meter
  • ✅ 追踪(Tracing) - 飞书事件处理、Webhook、WebSocket、HTTP 出站请求全链路追踪
  • ✅ 指标(Metrics) - Token 缓存、事件处理、HTTP 请求、WebSocket 连接等指标采集
  • ✅ 日志导出 - 可选的 OTLP 日志导出
  • ✅ 采样策略 - 支持 ParentBased + TraceIdRatioBased 采样
  • ✅ 资源标签 - 自动添加 service.name、service.version、deployment.environment 标签
  • ✅ 配置验证 - 内置 IValidateOptions 验证,支持 ValidateOnStart
  • ✅ 灵活扩展 - 提供自定义配置委托,可追加/覆盖默认配置
  • ✅ 多框架支持 - 支持 .NET Standard 2.0、.NET 6.0、.NET 8.0、.NET 10.0
  • ✅ 原生 AOT 支持 - net8.0+ 一等公民支持 Native AOT 发布,指标与追踪源自动注册零反射

安装

dotnet add package Mud.Feishu.OpenTelemetry

快速开始

方式一:代码配置(推荐)

using Mud.Feishu.OpenTelemetry;

var builder = WebApplication.CreateBuilder(args);

// 一键开启飞书 SDK 的 OpenTelemetry 追踪与指标采集
builder.Services.AddFeishuOpenTelemetry(options =>
{
    options.OtlpEndpoint = new Uri("http://otel-collector:4317");
    options.ServiceName = "my-feishu-app";
    options.SamplingRatio = 0.1; // 生产环境建议 0.1~0.3
});

var app = builder.Build();
app.Run();

方式二:从配置文件绑定

{
  "FeishuOpenTelemetry": {
    "ServiceName": "my-feishu-app",
    "ServiceVersion": "1.0.0",
    "DeploymentEnvironment": "production",
    "SamplingRatio": 0.1,
    "OtlpEndpoint": "http://otel-collector:4317",
    "EnableTracing": true,
    "EnableMetrics": true,
    "EnableLogging": false,
    "IncludeMudHttpUtils": true,
    "EnableHttpClientInstrumentation": true,
    "EnableAspNetCoreInstrumentation": true
  }
}
builder.Services.AddFeishuOpenTelemetry(builder.Configuration);

自动注册的可观测性源

ActivitySource(追踪)

ActivitySource 说明
Mud.Feishu 飞书事件处理、Webhook、WebSocket 追踪
Mud.HttpUtils.HttpClient HTTP 出站请求、Token 刷新、重试追踪(可选,默认开启)

Meter(指标)

Meter 说明
Mud.Feishu 事件处理、WebSocket 连接、Webhook 指标
Mud.HttpUtils.HttpClient HTTP 请求、Token 刷新、重试、熔断器、下载指标(可选,默认开启)

采集的指标列表

Mud.Feishu Meter(FeishuMetrics)采集的飞书指标:

指标名称 类型 说明
feishu.event.handling Counter 事件处理总次数
feishu.event.handling.duration Histogram 事件处理耗时分布(毫秒)
feishu.event.deduplication Counter 事件去重命中/未命中计数
feishu.websocket.connections ObservableGauge WebSocket 活跃连接数
feishu.websocket.message.duration Histogram WebSocket 消息处理耗时分布(毫秒)
feishu.websocket.reconnect Counter WebSocket 重连次数
feishu.websocket.backlog ObservableGauge WebSocket 待处理消息积压数
feishu.webhook.request Counter Webhook 入站请求计数
feishu.webhook.request.duration Histogram Webhook 请求处理耗时分布(毫秒)

HTTP 请求与 Token 刷新指标由 Mud.HttpUtils 自动采集(Mud.HttpUtils.HttpClient Meter), 包括 mud.http.requests、mud.http.request.duration、mud.token.refresh、mud.token.refresh.duration。

配置选项

配置项 类型 默认值 说明
EnableTracing bool true 是否启用追踪
EnableMetrics bool true 是否启用指标
EnableLogging bool false 是否启用 OTLP 日志导出
IncludeMudHttpUtils bool true 是否同时注册 Mud.HttpUtils 的 ActivitySource 和 Meter
EnableHttpClientInstrumentation bool true 是否启用 .NET HttpClient Instrumentation
EnableAspNetCoreInstrumentation bool true 是否启用 ASP.NET Core 入站请求 Instrumentation
OtlpEndpoint Uri? http://localhost:4317 OTLP 导出端点,设为 null 则不配置 OTLP 导出器
ServiceName string Mud.Feishu.Application 服务名称(OTel Resource)
ServiceVersion string SDK 版本 服务版本(OTel Resource)
DeploymentEnvironment string production 部署环境(OTel Resource)
SamplingRatio double 1.0 采样比率(0.0~1.0),生产环境建议 0.1~0.3
ConfigureTracing Action? null 自定义追踪配置委托
ConfigureMetrics Action? null 自定义指标配置委托
ConfigureLogging Action? null 自定义日志配置委托

高级用法

自定义导出器

builder.Services.AddFeishuOpenTelemetry(options =>
{
    // 不使用默认 OTLP 导出,自定义配置
    options.OtlpEndpoint = null;
    options.SamplingRatio = 1.0;

    // 自定义追踪配置:追加 Console 导出器
    options.ConfigureTracing = tp => tp.AddConsoleExporter();

    // 自定义指标配置:追加 Prometheus 导出器
    options.ConfigureMetrics = mp => mp.AddPrometheusExporter();
});

仅启用飞书指标(不含 HTTP 追踪)

builder.Services.AddFeishuOpenTelemetry(options =>
{
    options.EnableTracing = false;
    options.IncludeMudHttpUtils = false;
    options.EnableHttpClientInstrumentation = false;
    options.EnableAspNetCoreInstrumentation = false;
});

配合 ASP.NET Core 启动验证

AddFeishuOpenTelemetry 已自动注册 IValidateOptions<FeishuOpenTelemetryOptions>(校验 SamplingRatio 范围、ServiceName / ServiceVersion / DeploymentEnvironment 非空、OtlpEndpoint 为绝对 URI),并在注册时即时校验 SamplingRatio(越界直接抛出 ArgumentOutOfRangeException)。

如需在应用启动阶段 fail-fast,可追加 ValidateOnStart:

builder.Services.AddFeishuOpenTelemetry(builder.Configuration);

// 扩展已自动注册 IValidateOptions<FeishuOpenTelemetryOptions>,
// 启用 ValidateOnStart 后,配置无效将在应用启动时失败并给出错误信息
builder.Services.AddOptions<FeishuOpenTelemetryOptions>()
    .ValidateOnStart();

var app = builder.Build();

app.Run();

依赖项

包 版本 说明
Mud.Feishu.Abstractions * 飞书 SDK 抽象层(提供 ActivitySource 和 Meter 定义)
Mud.HttpUtils 2.0.6 HTTP 出站请求与 Token 刷新的可观测性源(MudHttpActivitySource / MudHttpMeter)
OpenTelemetry 1.16.0 OpenTelemetry .NET SDK
OpenTelemetry.Extensions.Hosting 1.16.0 主机集成
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.16.0 OTLP 导出器
OpenTelemetry.Instrumentation.Http 1.16.0 HttpClient Instrumentation
OpenTelemetry.Instrumentation.AspNetCore 1.16.0 ASP.NET Core Instrumentation

框架支持

  • .NET Standard 2.0
  • .NET 6.0
  • .NET 8.0
  • .NET 10.0

相关项目

许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件


Mud.Feishu.OpenTelemetry - 一键开启飞书 SDK 全链路可观测性!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 0 9/28/2026
3.0.0-rc3 49 9/24/2026
3.0.0-rc2 57 9/18/2026