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" />
                    
Directory.Packages.props
<PackageReference Include="Hyz.Trace.Storage.ClickHouse" />
                    
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 Hyz.Trace.Storage.ClickHouse --version 1.2.2
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=Hyz.Trace.Storage.ClickHouse&version=1.2.2
                    
Install as a Cake Tool

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_idstatusmethod_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 存储

性能优化建议

  1. 分区策略:按月份分区(toYYYYMM(start_time_utc)),过期数据可直接 DROP PARTITION 快速清理
  2. TTL 设置:可手动添加 TTL 子句自动过期数据:
    ALTER TABLE trace_spans MODIFY TTL start_time_utc + INTERVAL 90 DAY;
    
  3. 副本配置:生产环境建议使用 ReplicatedMergeTree + 分布式表实现高可用
  4. 批量写入:ClickHouse 对批量写入优化良好,客户端上报时 ReportBatchSize 建议设为 200~500
  5. 内存配置:ClickHouse 建议至少 8GB 内存,max_memory_usage 根据可用内存调整
  6. 写入并发:建议控制写入并发数(单实例 2~4 个写入连接),避免 too many parts 问题
  7. 查询优化:时间范围查询始终带上 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 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.

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
1.2.2 0 8/1/2026
1.2.1 0 8/1/2026
1.2.0 0 8/1/2026
1.1.0 0 8/1/2026
1.0.0 28 7/31/2026
0.9.0 33 7/31/2026
0.8.0 36 7/31/2026
0.7.0 41 7/31/2026
0.6.0 40 7/31/2026
0.5.0 70 7/30/2026
0.4.0 73 7/29/2026
0.3.0 70 7/28/2026
0.2.0 75 7/27/2026