文档文档

InfluxDB 运行时

InfluxDB 提供了 Go 运行时 profile、trace 和其他信息,这些信息对于分析和调试服务器运行时执行非常有用。

Go 运行时 profile 概述

Go 运行时 profile 是堆栈跟踪的集合,显示了导致特定事件实例的调用序列。 InfluxDB 为以下事件提供 profile 数据

  • 阻塞
  • CPU 使用率
  • 内存分配
  • 互斥 (mutex)
  • OS 线程创建

当您向 InfluxDB 发送 profile 请求时,Golang 运行时 pprof 包 对运行时上的事件进行采样,以收集堆栈跟踪和统计信息(例如,堆分配事件的内存字节数)。对于某些 profile,您可以设置 InfluxDB 将收集 profile 数据的秒数。

数据收集完成后,InfluxDB 返回 profile 数据。默认响应格式是 profile.proto 格式的压缩协议缓冲区。 profile.proto 文件与 pprofgo tool pprof 分析工具兼容。对于某些 profile,InfluxDB 提供了一种替代的人类可读纯文本格式,其中包含转换为函数调用和行号的注释,但 pprof 工具和 profile.proto 格式具有以下优点

  • 从磁盘或 HTTP 读取 profile。
  • 聚合和比较相同类型的多个 profile。
  • 分析和过滤 profile 数据。
  • 生成可视化和报告。

分析 Go 运行时 profile

使用 /debug/pprof InfluxDB 端点一次下载所有 profile 或单独请求它们。

获取所有运行时 profile

要一次下载所有运行时 profile,请使用 HTTP 客户端向 /debug/pprof/all 端点发送 GET 请求。 go tool pprof 无法直接从 /debug/pprof/all 获取 profile。

GET http://localhost:8086/debug/pprof/all

InfluxDB 返回一个 gzipped tar 文件,其中包含以下 profile.proto 格式的 profile

选项包含方式
Profile CPU在您的请求 URL 中使用 cpu 查询参数传递 秒持续时间

使用 HTTP 客户端(如 curlwget)从 /debug/pprof/all 下载 profile。

示例

# Use `curl` to download a `.tar.gz` of all profiles after 10 seconds of CPU sampling.
# Use `tar` to extract the profiles folder.

curl "http://localhost:8086/debug/pprof/all?cpu=10s" | tar -xz

# Analyze an extracted profile.

go tool pprof profiles/heap.pb.gz

Profile 所有内存分配

Profile 内存分配并将默认 profile 显示设置为 alloc_space,即程序开始以来分配的总字节数(包括垃圾回收的字节数)。

GET http://localhost:8086/debug/pprof/allocs
选项包含方式
采样秒数在您的请求 URL 中使用 seconds 查询参数传递 无符号整数
输出纯文本(与 seconds 互斥)在您的请求 URL 中使用 debug 查询参数传递 1
# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/allocs

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

# At the prompt, get the top N memory allocations.

(pprof) top10

Profile 阻塞操作

Profile 导致阻塞同步原语并导致 Go 暂停 Goroutine 执行的操作。

GET http://localhost:8086/debug/pprof/block
选项包含方式
输出纯文本在您的请求 URL 中使用 debug 查询参数传递 1
# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/block

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

#  At the prompt, get the top N entries.

(pprof) top10

Profile CPU

Profile 从执行堆栈采样的程序计数器。要下载 profile,请使用 HTTP 客户端向 /debug/pprof/profile 端点发送 GET 请求。 go tool pprof 无法直接获取 CPU profile。

GET http://localhost:8086/debug/pprof/profile
选项包含方式
采样秒数(默认为 30在您的请求 URL 中使用 seconds 查询参数传递 无符号整数

使用 HTTP 客户端(如 curlwget)下载 profile。

示例

# Get the profile.

curl http://localhost:8086/debug/pprof/profile -o cpu

# Analyze the profile in interactive mode.

go tool pprof ./cpu

# At the prompt, get the top N functions most often running
# or waiting during the sample period.

(pprof) top10

使用 seconds 查询参数控制采样持续时间。

/debug/pprof/profile?seconds=SECONDS 返回与 /debug/pprof/all?cpu=DURATION 相同的 CPU profile。

示例

# Get the CPU profile after 10 seconds of sampling.

curl "http://localhost:8086/debug/pprof/profile?seconds=10" -o cpu

# Get all profiles after 10 seconds of CPU sampling.

curl "http://localhost:8086/debug/pprof/all?cpu=10s" -o all.tar.gz

Profile Goroutine

Profile 所有当前 Goroutine。

GET http://localhost:8086/debug/pprof/goroutine
选项包含方式
采样秒数在您的请求 URL 中使用 seconds 查询参数传递 无符号整数
输出纯文本(与 seconds 互斥)在您的请求 URL 中使用 debug 查询参数传递 1

示例

# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/goroutine

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

#  At the prompt, get the top N entries.

(pprof) top10

Profile 堆内存分配

Profile 堆或活动对象的内存分配。

GET http://localhost:8086/debug/pprof/heap
选项包含方式
在采样前运行垃圾回收在您的请求 URL 中使用 gc 查询参数传递 1
采样秒数在您的请求 URL 中使用 seconds 查询参数传递 无符号整数
输出纯文本(与 seconds 互斥)在您的请求 URL 中使用 debug 查询参数传递 1

示例

# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/heap

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

# At the prompt, get the top N memory-intensive nodes.

(pprof) top10

# pprof displays the list:
#   Showing nodes accounting for 142.46MB, 85.43% of 166.75MB total
#   Dropped 895 nodes (cum <= 0.83MB)
#   Showing top 10 nodes out of 143

Profile 互斥锁

Profile 争用互斥锁的持有者。

GET http://localhost:8086/debug/pprof/mutex
选项包含方式
采样秒数在您的请求 URL 中使用 seconds 查询参数传递 无符号整数
输出纯文本(与 seconds 互斥)在您的请求 URL 中使用 debug 查询参数传递 1

示例

# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/mutex

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

#  At the prompt, get the top N entries.

(pprof) top10

Profile 线程创建

Profile 导致创建 OS 线程的操作。

GET http://localhost:8086/debug/pprof/threadcreate
选项包含方式
采样秒数在您的请求 URL 中使用 seconds 查询参数传递 无符号整数
输出纯文本(与 seconds 互斥)在您的请求 URL 中使用 debug 查询参数传递 1

示例

# Analyze the profile in interactive mode.

go tool pprof http://localhost:8086/debug/pprof/threadcreate

# `pprof` returns the following prompt:
#   Entering interactive mode (type "help" for commands, "o" for options)
#   (pprof)

#  At the prompt, get the top N entries.

(pprof) top10

分析 Go 运行时 trace

要跟踪 InfluxDB 的执行事件,请将 /debug/pprof/trace 端点与 go tool trace 一起使用。

GET http://localhost:8086/debug/pprof/trace

示例

# Download the trace file.

curl http://localhost:8086/debug/pprof/trace -o trace.out

# Analyze the trace.

go tool trace ./trace.out

从 trace 生成类似 pprof 的 profile

您可以使用 go tool trace 从 trace 文件生成类似 pprof 的 profile,然后使用 go tool pprof 分析它们。

示例

# Generate a profile from the downloaded trace file.

go tool trace -pprof=PROFILE_TYPE ./trace.out > PROFILE_TYPE.pprof

PROFILE_TYPE 替换为以下 Golang profile 类型 之一

  • net:网络阻塞 profile
  • sync:同步阻塞 profile
  • syscall:系统调用阻塞 profile
  • sched:调度器延迟 profile

查看调用 InfluxDB 的命令行

要查看调用 InfluxDB 的命令、参数和命令行变量,请使用 /debug/pprof/cmdline 端点。

GET http://localhost:8086/debug/pprof/cmdline

/debug/pprof/cmdline 以纯文本形式返回命令行调用。

查看运行时配置

在 InfluxDB v2.3+ 中,您可以查看您的活动运行时配置,包括标志和环境变量。请参阅如何查看您的运行时服务器配置


此页内容对您有帮助吗?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像现在一样使用它,而无需对您的代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速、最新的数据引擎,可实时收集和处理数据,并将其持久保存到本地磁盘或对象存储。 InfluxDB 3 Enterprise 以 Core 的基础为构建,增加了高可用性、只读副本、增强的安全性以及数据压缩,从而实现更快的查询和优化的存储。 InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看