pg_tracing
PostgreSQL分布式Tracing
仓库
DataDog/pg_tracing
https://github.com/DataDog/pg_tracing
源码
pg_tracing-0.1.3.tar.gz
pg_tracing-0.1.3.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_tracing | 0.1.3 | STAT | MIT | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 6010 | pg_tracing | 否 | 是 | 是 | 是 | 否 | 是 | - |
| 相关扩展 | pg_profile pg_show_plans pg_stat_kcache pg_stat_monitor pg_qualstats pg_store_plans pg_track_settings pg_wait_sampling |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.1.3 | 1817161514 | pg_tracing | - |
| RPM | PIGSTY | 0.1.3 | 1817161514 | pg_tracing_$v | - |
| DEB | PIGSTY | 0.1.3 | 1817161514 | postgresql-$v-pg-tracing | - |
构建
您可以使用 pig build 命令构建 pg_tracing 扩展的 RPM / DEB 包:
pig build pkg pg_tracing # 构建 RPM / DEB 包
安装
您可以直接安装 pg_tracing 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_tracing; # 当前活跃 PG 版本安装
pig ext install -y pg_tracing -v 18 # PG 18
pig ext install -y pg_tracing -v 17 # PG 17
pig ext install -y pg_tracing -v 16 # PG 16
pig ext install -y pg_tracing -v 15 # PG 15
pig ext install -y pg_tracing -v 14 # PG 14
dnf install -y pg_tracing_18 # PG 18
dnf install -y pg_tracing_17 # PG 17
dnf install -y pg_tracing_16 # PG 16
dnf install -y pg_tracing_15 # PG 15
dnf install -y pg_tracing_14 # PG 14
apt install -y postgresql-18-pg-tracing # PG 18
apt install -y postgresql-17-pg-tracing # PG 17
apt install -y postgresql-16-pg-tracing # PG 16
apt install -y postgresql-15-pg-tracing # PG 15
apt install -y postgresql-14-pg-tracing # PG 14
预加载配置:
shared_preload_libraries = 'pg_tracing';
创建扩展:
CREATE EXTENSION pg_tracing;
用法
pg_tracing 生成服务端 Span 用于分布式追踪。它为规划器、执行器、计划节点、嵌套查询、触发器、并行工作进程和事务提交创建 Span。
通过 SQLCommenter 传播追踪上下文
将追踪上下文作为 SQL 注释传递。带有采样标志的查询将生成 Span:
/*traceparent='00-00000000000000000000000000000123-0000000000000123-01'*/ SELECT 1;
通过 GUC 传播追踪上下文
BEGIN;
SET LOCAL pg_tracing.trace_context='traceparent=''00-00000000000000000000000000000005-0000000000000005-01''';
UPDATE pgbench_accounts SET abalance=1 WHERE aid=1;
COMMIT;
独立采样
无需外部追踪上下文,随机采样查询:
SET pg_tracing.sample_rate = 1.0; -- 追踪所有查询
SELECT 1;
查看 Span
-- 消费 Span(从缓冲区移除)
SELECT trace_id, parent_id, span_id, span_start, span_end, span_type, span_operation
FROM pg_tracing_consume_spans ORDER BY span_start;
-- 查看 Span(非破坏性)
SELECT * FROM pg_tracing_peek_spans;
-- 导出为 OTLP JSON
SELECT pg_tracing_json_spans();
统计信息
SELECT * FROM pg_tracing_info;
SELECT pg_tracing_reset();
发送 Span 到 OpenTelemetry Collector
在 postgresql.conf 中配置:
pg_tracing.otel_endpoint = http://127.0.0.1:4318/v1/traces
pg_tracing.otel_naptime = 2000
关键 GUC 参数
| 参数 | 默认值 | 描述 |
|---|---|---|
pg_tracing.max_span | 10000 | 共享内存中的最大 Span 数 |
pg_tracing.track | all | 追踪哪些语句 |
pg_tracing.sample_rate | 0 | 随机采样查询的比例 |
pg_tracing.otel_endpoint | (空) | 用于 Span 导出的 OTLP HTTP 端点 |