Hyz.Trace.Storage.ClickHouse
1.2.2
dotnet add package Hyz.Trace.Storage.ClickHouse --version 1.2.2
NuGet\Install-Package Hyz.Trace.Storage.ClickHouse -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.Storage.ClickHouse" 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.Storage.ClickHouse" Version="1.2.2" />
<PackageReference Include="Hyz.Trace.Storage.ClickHouse" />
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.Storage.ClickHouse --version 1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hyz.Trace.Storage.ClickHouse, 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.Storage.ClickHouse@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.Storage.ClickHouse&version=1.2.2
#tool nuget:?package=Hyz.Trace.Storage.ClickHouse&version=1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hyz.Trace.Storage.ClickHouse
基于 ClickHouse 列存数据库的链路追踪存储实现,适合大规模(千万级 Span/天)生产部署。
目标框架
- net8.0 / net9.0 / net10.0
适用场景
- 每日 Span 写入量超过 1000 万
- 需要长期保留历史数据(90 天+)
- 对聚合查询性能有高要求(趋势、统计、拓扑)
- 多应用高并发上报场景
快速开始
安装
dotnet add package Hyz.Trace.Storage.ClickHouse
前置条件
- ClickHouse Server 22.x+(推荐 23.x/24.x LTS)
- 数据库需预先创建(启动时自动建表)
CREATE DATABASE IF NOT EXISTS hyz_trace;
注册存储
using Hyz.Trace.Storage;
builder.Services.UseClickHouseStorage(
"Host=localhost;Port=8123;Database=hyz_trace;Username=default;Password=xxx;Timeout=10"
);
// Dashboard 依赖
builder.Services.AddSingleton<RuntimeConfigStore>();
自动创建的表
启动时自动创建以下表(使用 ReplacingMergeTree 引擎,按时间分区):
applications(应用表,含密钥字段)
CREATE TABLE IF NOT EXISTS applications (
id Int64,
name String,
description Nullable(String),
key_prefix Nullable(String),
key_hash Nullable(String),
enabled UInt8,
created_at DateTime,
updated_at DateTime
) ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
注意:旧版
report_keys表已移除,密钥字段合并到applications表。
trace_spans(Span 数据表,核心表)
CREATE TABLE IF NOT EXISTS trace_spans (
id Int64,
trace_id String,
span_id String,
parent_span_id Nullable(String),
application_id Int64,
application_name String,
method_name String,
start_time_utc DateTime64(3, 'UTC'),
start_time_cst Nullable(String),
duration_ms Float64,
status LowCardinality(String),
error_message Nullable(String),
error_type Nullable(String),
error_stack Nullable(String),
tags_json Nullable(String),
input_parameters Nullable(String),
return_value Nullable(String),
request_headers Nullable(String),
http_method Nullable(String),
http_path Nullable(String),
http_route Nullable(String),
http_status_code Nullable(Int32),
peer_service Nullable(String),
is_root UInt8,
created_at DateTime('UTC')
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(start_time_utc)
ORDER BY (trace_id, start_time_utc);
索引:application_id、status、method_name 通过跳数索引和分区裁剪实现高效过滤。
users(用户表)
CREATE TABLE IF NOT EXISTS users (
id Int64,
username String,
password_hash String,
role LowCardinality(String),
enabled UInt8,
created_at DateTime,
updated_at DateTime
) ENGINE = ReplacingMergeTree(updated_at)
ORDER BY id;
连接字符串参数
| 参数 | 默认值 | 说明 |
|---|---|---|
Host |
localhost | ClickHouse 服务器地址 |
Port |
8123 | HTTP 端口(注意:使用 HTTP 接口,不是 Native 9000) |
Database |
hyz_trace | 数据库名 |
Username |
default | 用户名 |
Password |
(空) | 密码 |
Timeout |
30 | 请求超时(秒) |
Compression |
true | 是否启用压缩(推荐 true 以减少网络传输) |
扩展方法
| 方法 | 说明 |
|---|---|
UseClickHouseStorage(connectionString) |
注册 ClickHouse 存储 |
性能优化建议
- 分区策略:按月份分区(
toYYYYMM(start_time_utc)),过期数据可直接 DROP PARTITION 快速清理 - TTL 设置:可手动添加 TTL 子句自动过期数据:
ALTER TABLE trace_spans MODIFY TTL start_time_utc + INTERVAL 90 DAY; - 副本配置:生产环境建议使用 ReplicatedMergeTree + 分布式表实现高可用
- 批量写入:ClickHouse 对批量写入优化良好,客户端上报时
ReportBatchSize建议设为 200~500 - 内存配置:ClickHouse 建议至少 8GB 内存,
max_memory_usage根据可用内存调整 - 写入并发:建议控制写入并发数(单实例 2~4 个写入连接),避免 too many parts 问题
- 查询优化:时间范围查询始终带上
from/to参数,利用分区裁剪减少扫描数据量
连接池与重试
ClickHouse HTTP 接口天然无状态,实现使用 HttpClient 连接池。批量写入失败时内置指数退避重试(最多 3 次)。
依赖包
Hyz.Trace.Storage.Abstractions— 存储抽象层(依赖)ClickHouse.Client— ClickHouse ADO.NET 驱动Hyz.Trace.IdGenerator— 雪花 ID 生成器
与 FreeSql 存储的对比
| 特性 | FreeSql 存储 | ClickHouse 存储 |
|---|---|---|
| 写入性能 | 千级 TPS | 万~十万级 TPS |
| 聚合查询 | 良好 | 极快(列式存储优势) |
| 事务支持 | 支持 | 不支持(最终一致) |
| 部署复杂度 | 低 | 中 |
| 存储压缩 | 一般 | 极高(列式压缩 5~10 倍) |
| 适用规模 | < 1000 万 Span/天 | > 1000 万 Span/天 |
| 推荐用途 | 中小规模、快速上手 | 大规模生产、长期存储 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- BCrypt.Net-Next (>= 4.2.0)
- Castle.Core (>= 5.2.1)
- ClickHouse.Client (>= 7.9.1)
- Hyz.Trace.IdGenerator (>= 1.2.2)
- Hyz.Trace.Storage.Abstractions (>= 1.2.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Yitter.IdGenerator (>= 1.0.14)
-
net8.0
- BCrypt.Net-Next (>= 4.2.0)
- Castle.Core (>= 5.2.1)
- ClickHouse.Client (>= 7.9.1)
- Hyz.Trace.IdGenerator (>= 1.2.2)
- Hyz.Trace.Storage.Abstractions (>= 1.2.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Yitter.IdGenerator (>= 1.0.14)
-
net9.0
- BCrypt.Net-Next (>= 4.2.0)
- Castle.Core (>= 5.2.1)
- ClickHouse.Client (>= 7.9.1)
- Hyz.Trace.IdGenerator (>= 1.2.2)
- Hyz.Trace.Storage.Abstractions (>= 1.2.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Yitter.IdGenerator (>= 1.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.