用途
cx-profiling 是 Coralogix cx-cli 的一个持续性能分析(continuous profiling)技能,用于回答此类问题:哪些服务消耗的 CPU 或内存最多,服务内部哪些函数是热点,以及用量何时发生变化。它通过 Coralogix profiling API 查询数据,需要 profiles.cpu-profiles:Read 权限。所有子命令均为只读,不修改任何数据。
主要能力与命令
cx profiling services:列出被分析的服务,按 CPU+wall 秒数(--category cpu)或分配+堆字节数(--category memory)排序。支持--environment、--limit(默认 20,最大 100)、--offset、--include-time-series。--environment匹配 deployment.environment.name,是 services 命令唯一支持的过滤条件。结果分页时,CLI 会打印下一页的--offset;若不传--include-time-series,则省略时间序列。cx profiling metadata <service>:返回服务的语言,以及哪些 profile 类型有数据(has_data)。cx profiling filters <service>:列出某个服务 profile 可用的标签键及其值,支持--profile-type(必填)、--filter、--max-values-per-key(默认 50)。cx profiling metrics <service>:查看某一 profile 类型随时间的用量变化,可按 service、pod 或任意标签拆分,结果按总量从高到低排序。支持--profile-type(必填)、--sum-by(默认 service)、--max-buckets(默认 60)、--limit(默认 10)、--filter。cx profiling flame-graph <service>:获取解码后的火焰图,以及按 self 值排序的热点函数。支持--profile-type(必填)、--filter、--top-functions(默认 20)、--min-node-ratio(默认 0.01)、--max-depth、--show-kernel。
所有命令都支持 -o json 或 -o toon 输出。
使用方式
时间范围
每个命令都接受 --start / --end,默认 now-1h / now。可使用 now、now-1h 形式的相对表达式,或 ISO-8601 时间戳。
profile 类型
--profile-type 可选值:cpu、wall、memory-allocation-bytes、memory-allocation-objects、memory-heap-bytes、memory-heap-objects(大小写不敏感,如 MEMORY_HEAP_BYTES 也可)。建议先查 metadata,只有 has_data: true 的类型才会返回数据。
过滤表达式
--filter 可重复使用,多个条件之间按 AND 组合:
key=v1,v2(任一值匹配)key!=v1,v2(排除这些值)key:exists/key:not-exists
可通过 filters 命令发现可用的键与值。
多 profile 并发
每个子命令都支持重复 -p <profile>;当传入多个 profile 时,结果会按 profile 打标签。
推荐排查流程
- 先找重负载服务(若用户已指定服务可跳过):
cx profiling services --category cpu --limit 10 -o jsoncx profiling services --category memory --environment <env> --limit 10 -o json - 检查该服务有哪些 profile 类型:
cx profiling metadata <service> -o json - 查看可过滤/拆分的维度(pod、版本、环境等):
cx profiling filters <service> --profile-type cpu -o json - 定位用量变化时间与来源,按 pod 或标签拆分:
cx profiling metrics <service> --profile-type cpu --sum-by pod --limit 5 -o jsoncx profiling metrics <service> --profile-type cpu --sum-by service.version --start now-24h -o json - 定位问题代码,先看热点函数再看调用树:
cx profiling flame-graph <service> --profile-type cpu --top-functions 10 -o json | jq '.top_functions'cx profiling flame-graph <service> --profile-type cpu --filter k8s.pod.name=<pod> --min-node-ratio 0.05 --max-depth 20 -o json
输出解读
- Services:cpu 行包含 total_cpu_seconds / total_wall_seconds;memory 行包含 total_memory_allocations_bytes / total_heap_bytes。last_active 为 epoch 秒。
- Metrics:series 按总量(各 bucket 合计之和)排序,并按
--limit截断;total_series 为服务端返回的序列数。unit 根据 profile 类型为秒、字节或对象数。 - Flame graph:top_functions 在整个树中按 function+file 聚合 self 值,因此不受树裁剪影响。树节点包含 function、file、line、self、total、self_percent、total_percent 以及 children(按 total 降序)。omitted_subtrees 表示被
--min-node-ratio/--max-depth丢弃的节点数。服务端本身已裁剪低于总量 0.1% 的节点。无符号的帧显示为地址或 unknown。warnings(如 maxBytes)表示服务端提前停止读取 profile,图只覆盖部分时间范围。
关键原则
- 先排序再下钻:services 和 metrics 开销低,flame-graph 开销高,应先缩小服务、profile 类型、时间范围与过滤条件再请求。
- 大树体积大:Java、Scala 等深调用栈可能产生数百 KB 的 JSON;可提高
--min-node-ratio、设置--max-depth,或只读取.top_functions。 - self 与 total 的区别:self 高说明函数自身是热点;total 高而 self 低说明开销在其调用者中。
- 空结果不是错误:所选 profile 类型或时间范围内无数据时,会返回空序列或零总量图;应重新核对 metadata 并放宽
--start。 - 无 profiling 数据:若 metadata 显示没有任何 has_data 为 true 的类型,说明该服务未启用 profiling。不要另找服务名,可改用 cx-service-catalog 或 cx-telemetry-querying 回答 CPU/内存用量、时间与 pod 信息,并说明函数级细节需要为该服务启用 profiling。
适用场景与限制
适用于 Coralogix 环境中按 CPU、wall、内存各 profile 类型排查服务与函数级性能热点、定位用量变化来源、关联 pod/版本/环境维度等场景。
限制包括:需要 profiles.cpu-profiles:Read 权限;所有命令只读;services 仅支持 --environment 过滤;flame-graph 在大服务上可能耗时约 40 秒,不要设置过短的 --http-timeout;只有当服务已启用 profiling 且对应 profile 类型有数据时才会返回内容。
相关技能
- cx-service-catalog:同一服务的 RED 指标(延迟、错误),可将延迟回归与这里发现的 CPU 峰值关联。
- cx-telemetry-querying:对 profiling 发现的服务或 pod 进一步查询日志、span 或 PromQL 指标(cx logs、cx spans、cx metrics)。
- cx-infra:当热点 pod 指向基础设施问题时,查看主机与容器健康状态。