Hyz.Trace.Client
1.2.2
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package Hyz.Trace.Client --version 1.2.2
NuGet\Install-Package Hyz.Trace.Client -Version 1.2.2
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="Hyz.Trace.Client" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyz.Trace.Client" Version="1.2.2" />
<PackageReference Include="Hyz.Trace.Client" />
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 Hyz.Trace.Client --version 1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hyz.Trace.Client, 1.2.2"
#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 Hyz.Trace.Client@1.2.2
#: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=Hyz.Trace.Client&version=1.2.2
#tool nuget:?package=Hyz.Trace.Client&version=1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hyz.Trace.Client
分布式链路追踪客户端元包(一站式安装),自动引入核心引擎 + HTTP 上报器 + ASP.NET Core 集成 + Prometheus 指标,开箱即用。
目标框架
- netstandard2.0 / net8.0 / net9.0 / net10.0
包含的包
安装本元包时自动引入以下依赖:
| 包名 | 功能 | 适用框架 |
|---|---|---|
Hyz.Trace |
核心引擎(TraceContext、TraceScope、采样、脱敏、AOP) | 全部 |
Hyz.Trace.Reporting.Http |
HTTP 协议上报器(JSON 批量、重试、缓冲) | 全部 |
Hyz.Trace.Metrics.Prometheus |
Prometheus 指标收集器 | 全部 |
Hyz.Trace.AspNet |
ASP.NET Core 中间件 + HttpClient 追踪头注入 | net8.0+ |
Hyz.Trace.Reporting.gRPC |
gRPC 协议上报器(高性能、Protobuf 序列化) | net8.0+ |
说明:gRPC 上报器和 ASP.NET Core 集成为内嵌 DLL,仅在 net8.0+ 目标框架下可用;netstandard2.0 目标框架仅包含核心引擎、HTTP 上报和 Prometheus 指标。
快速开始
1. 安装
dotnet add package Hyz.Trace.Client
2. 启动 Dashboard 服务端并获取 API Key
部署 Hyz.Trace.Server(详见其 README),在应用管理页面创建应用获取 API Key(格式:trk_xxxxxxxxxxxxxxxx)。
3. 配置服务
using Hyz.Trace;
using Hyz.Trace.AspNet;
var builder = WebApplication.CreateBuilder(args);
// 链式配置:一行完成追踪 + 上报器注册
builder.AddTrace(options =>
{
options.Enabled = true;
options.SampleRate = 1.0; // 全量采样
options.DashboardServerAddress = "http://localhost:5000"; // Dashboard 地址
options.DashboardApiKey = "trk_xxxxxxxxxxxxxxxx"; // 应用 API Key(从 Dashboard 获取)
options.DashboardProtocol = ReportProtocol.Http; // 上报协议
options.MaxActiveSpans = 10000;
})
.AddTraceReporting(); // 根据配置的 DashboardProtocol 自动注册对应的上报器
// 注册带追踪的服务(DynamicProxy AOP)
builder.Services.AddTraced<IOrderService, OrderService>(ServiceLifetime.Scoped);
// 注册 HttpClient 并自动注入 traceparent 头
builder.Services.AddHttpClient("ExternalApi")
.AddTracedHttpClient();
4. 启用中间件
var app = builder.Build();
// 追踪中间件必须放在管道最前面(在路由、认证等之前)
app.UseTrace();
app.MapControllers();
app.Run();
完成!每个 HTTP 请求自动创建 Root Span,方法调用自动嵌套 Span,批量上报到 Dashboard。
场景示例
场景一:远程上报到独立 Dashboard(推荐生产)
builder.AddTrace(options =>
{
options.DashboardServerAddress = "http://trace-server:5000";
options.DashboardApiKey = "trk_xxxxxxxxxxxxxxxx";
options.DashboardProtocol = ReportProtocol.Http;
options.SampleRate = 0.1; // 生产环境建议 10% 采样
options.ReportBatchSize = 200;
options.ReportBatchIntervalMs = 3000;
})
.AddTraceReporting();
场景二:gRPC 上报(高性能,仅 net8.0+)
builder.AddTrace(options =>
{
options.DashboardServerAddress = "http://trace-server:5001"; // gRPC 端口
options.DashboardApiKey = "trk_xxxxxxxxxxxxxxxx";
options.DashboardProtocol = ReportProtocol.Grpc;
})
.AddTraceReporting();
场景三:进程内模式(开发调试,Dashboard 嵌入应用)
// 需要额外安装存储和 Dashboard 包
// dotnet add package Hyz.Trace.Storage.FreeSql
// dotnet add package Hyz.Trace.Dashboard
builder.Services.UseSqliteStorage("Data Source=traces.db");
builder.Services.AddSingleton<RuntimeConfigStore>();
builder.AddTrace(options =>
{
options.DashboardProtocol = ReportProtocol.InProcess;
})
.AddTraceReporting();
// ...
app.UseTrace();
app.UseTraceDashboard(); // 启用嵌入式面板
场景四:结合日志
安装对应的日志包(单独安装):
dotnet add package Hyz.Trace.Logging.Serilog # 或 Hyz.Trace.Logging.NLog
环境变量
支持通过环境变量配置(无需修改代码):
| 环境变量 | 说明 | 示例 |
|---|---|---|
HYZ_TRACE_ENABLED |
是否启用 | true |
HYZ_TRACE_SAMPLE_RATE |
采样率 | 0.5 |
HYZ_TRACE_DASHBOARD_SERVER |
Dashboard 地址 | http://localhost:5000 |
HYZ_TRACE_DASHBOARD_API_KEY |
API Key | trk_xxxx... |
HYZ_TRACE_DASHBOARD_PROTOCOL |
上报协议 | Http / Grpc / InProcess |
重要说明
- 无需配置 ApplicationName:客户端不再需要设置应用名称,应用归属完全由
DashboardApiKey确定,服务端通过 API Key 的 SHA256 哈希自动绑定应用 - 链式 API:
builder.AddTrace(...).AddTraceReporting()是推荐的配置方式,替代旧版的三段式配置 - 自动路径排除:
UseTrace()中间件自动排除 Dashboard 路径(/trace-dashboard)和健康检查路径,防止递归追踪 - 异步传播:TraceContext 基于 AsyncLocal,
async/await、Task.Run、线程池切换时自动传播
扩展包安装
如需额外功能,请单独安装:
# Serilog 日志集成
dotnet add package Hyz.Trace.Logging.Serilog
# NLog 日志集成
dotnet add package Hyz.Trace.Logging.NLog
# 进程内 Dashboard(嵌入式 UI)
dotnet add package Hyz.Trace.Dashboard
dotnet add package Hyz.Trace.Storage.FreeSql # 或 Hyz.Trace.Storage.ClickHouse
# Fody IL 编织(无接口 AOP)
dotnet add package Hyz.Trace.Weaving.Fody
完整选项参考
详见 Hyz.Trace README 的 TraceOptions 完整列表。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 is compatible. 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.
-
.NETStandard 2.0
- Castle.Core (>= 5.2.1)
- Hyz.HttpClient (>= 0.1.9)
- Hyz.Trace (>= 1.2.2)
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- prometheus-net (>= 8.2.1)
- System.Diagnostics.DiagnosticSource (>= 8.0.1)
- System.Text.Json (>= 10.0.6)
-
net10.0
- Castle.Core (>= 5.2.1)
- Google.Protobuf (>= 3.28.2)
- Grpc.Net.Client (>= 2.66.0)
- Hyz.HttpClient (>= 0.1.9)
- Hyz.Trace (>= 1.2.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- prometheus-net (>= 8.2.1)
-
net8.0
- Castle.Core (>= 5.2.1)
- Google.Protobuf (>= 3.28.2)
- Grpc.Net.Client (>= 2.66.0)
- Hyz.HttpClient (>= 0.1.9)
- Hyz.Trace (>= 1.2.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- prometheus-net (>= 8.2.1)
- System.Text.Json (>= 10.0.6)
-
net9.0
- Castle.Core (>= 5.2.1)
- Google.Protobuf (>= 3.28.2)
- Grpc.Net.Client (>= 2.66.0)
- Hyz.HttpClient (>= 0.1.9)
- Hyz.Trace (>= 1.2.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- prometheus-net (>= 8.2.1)
- System.Text.Json (>= 10.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.