golang Terraform运行性能分析与可视化工具插件tf-profile的使用

Golang Terraform运行性能分析与可视化工具插件tf-profile的使用

简介

tf-profile是一个用Go编写的CLI工具,用于分析Terraform运行性能。主要功能包括:

  • 基于cobra的现代CLI界面,支持自动补全
  • 直接从Terraform进程(使用管道)或日志文件读取日志
  • 可以生成全局统计、资源级统计或可视化图表
  • 提供多级粒度和聚合以及可定制输出

Coverage

警告:tf-profile不再积极开发。作者不打算添加新功能或支持更新版本的Terraform。

安装

Brew安装

❱ brew tap datarootsio/tf-profile 
❱ brew install tf-profile
❱ tf-profile --help
tf-profile is a CLI tool to profile Terraform runs

Usage:
  tf-profile [command]

二进制下载

  1. 前往发布页面
  2. 下载适合您操作系统的二进制文件
  3. 将其复制到您的$PATH路径中。在Linux系统上,/usr/local/bin是最常见的位置

使用Docker

如果您想在不安装任何东西的情况下尝试tf-profile,可以使用Docker运行:

❱ cat my_log_file.log | docker run -i qbruynseraede/tf-profile:0.5.0 stats

Key                                Value                                     
                                                                             
Number of resources created        1510                                      
                                                                             
Cumulative duration                36m19s                                    
Longest apply time                 7m18s                                     
Longest apply resource             time_sleep.foo[*]                         
...

可选地,定义一个别名:

❱ alias tf-profile=docker run -i qbruynseraede/tf-profile:0.5.0
❱ cat my_log_file.log | tf-profile

从源码构建

这需要至少1.23版本的go CLI。

❱ git clone git@github.com:QuintenBruynseraede/tf-profile.git
❱ cd tf-profile && go build .
❱ sudo ln -s $(pwd)/tf-profile /usr/local/bin  # 可选:仅当您想从其他目录运行tf-profile时
❱ tf-profile --help
tf-profile is a CLI tool to profile Terraform runs

Usage:
  tf-profile [command]

基本用法

tf-profile处理来自stdin和文件的输入。因此这两个命令是等效的:

❱ terraform apply -auto-approve | tf-profile table
❱ terraform apply -auto-approve > log.txt && tf-profile table log.txt

支持四个主要命令:

  • tf-profile stats: 提供关于Terraform运行的总体统计信息
  • tf-profile table: 提供详细的、资源级别的Terraform运行统计信息
  • tf-profile filter: 过滤日志以仅包含某些资源
  • tf-profile graph: 生成Terraform运行的可视化概览

tf-profile stats示例

tf-profile stats是最基本的命令。给定一个Terraform日志,它只提供高级统计信息。

❱ terraform apply -auto-approve > log.txt
❱ tf-profile stats log.txt

Key                                Value    
-----------------------------------------------------------------                       
Number of resources in configuration        1510                            
                                                                   
Cumulative duration                         36m19s                          
Longest apply time                          7m18s                           
Longest apply resource                      time_sleep.foo[*]               

Resources marked for operation Create       892
Resources marked for operation None         18
Resources marked for operation Replace      412      
    
Resources in state AllCreated               800                             
Resources in state Created                  695                             
Resources in state Started                  15        
    
Resources in desired state                  1492 out of 1510 (98.8%)
Resources not in desired state              18 out of 1510 (0.01%)
                                                                
Number of top-level modules                 13                              
Largest top-level module                    module.core[2]                  
Size of largest top-level module            170                             
Deepest module                              module.core[2].module.role[47]  
Deepest module depth                        2                               
Largest leaf module                         module.dbt[4]                   
Size of largest leaf module                 40

stats.png

tf-profile table示例

tf-profile table将解析日志并提供每个资源的指标。

❱ terraform apply -auto-approve > log.txt
❱ tf-profile table log.txt

resource              n  tot_time  modify_started  modify_ended  desired_state  operation  final_state  
aws_ssm_parameter.p6  1  0s        6               7             Created        Replace    Created      
aws_ssm_parameter.p1  1  0s        7               5             Created        Replace    Created      
aws_ssm_parameter.p3  1  0s        5               6             Created        Replace    Created      
aws_ssm_parameter.p4  1  0s        /               1             NotCreated     Destroy    NotCreated   
aws_ssm_parameter.p5  1  0s        4               4             Created        Modify     Created      
aws_ssm_parameter.p2  1  0s        /               /             Created        None       Created

table.png

tf-profile filter示例

tf-profile filter过滤日志以仅包含某些资源。支持通配符以过滤多个资源。

❱ tf-profile filter "module.*.null_resource.*" log.txt

  # module.mod1.null_resource.foo will be created
  + resource "null_resource" "foo" {
    ...
    }

  # module.mod2.null_resource.bar will be created
  + resource "null_resource" "bar" {
    ...
    }

module.mod1.null_resource.foo: Creating...
module.mod2.null_resource.bar: Creating...
module.mod1.null_resource.foo: Creation complete after 1s [id=foo]
module.mod2.null_resource.bar: Creation complete after 1s [id=bar]

tf-profile graph示例

tf-profile graph用于可视化terraform日志。它生成一个类似甘特图的图表,显示资源创建的先后顺序。

❱ tf-profile graph my_log.log --out graph.png --size 2000,1000 | gnuplot

graph.png

graph2.png

免责声明:Terraform的日志不包含任何绝对时间戳。我们只能推导出资源开始和完成修改的顺序。因此,tf-profile graph的输出只能大致指示某件事实际花费了多长时间。换句话说:X轴除了单调递增外没有意义。


更多关于golang Terraform运行性能分析与可视化工具插件tf-profile的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang Terraform运行性能分析与可视化工具插件tf-profile的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Terraform 性能分析与可视化工具 tf-profile 使用指南

什么是 tf-profile

tf-profile 是一个用于分析 Terraform 运行性能的工具插件,它能够记录 Terraform 执行过程中的各个阶段耗时,并生成可视化的性能分析报告,帮助开发者识别 Terraform 运行中的性能瓶颈。

安装 tf-profile

1. 下载二进制文件

# 下载最新版本
wget https://github.com/datarootsio/tf-profile/releases/latest/download/tf-profile_linux_amd64 -O tf-profile
chmod +x tf-profile
sudo mv tf-profile /usr/local/bin/

2. 通过 Go 安装

go install github.com/datarootsio/tf-profile@latest

基本使用方法

1. 生成性能数据

# 运行 Terraform 并生成性能数据
TF_LOG_PATH=./terraform.log TF_LOG=TRACE terraform apply -auto-approve

2. 分析性能数据

# 生成分析报告
tf-profile analyze --logfile terraform.log --outfile profile.html

3. 查看报告

生成的 profile.html 文件包含了可视化图表,可以用浏览器打开查看。

Golang 集成示例

以下是一个使用 Go 代码集成 tf-profile 的示例:

package main

import (
	"log"
	"os"
	"os/exec"
)

func main() {
	// 设置环境变量
	os.Setenv("TF_LOG_PATH", "terraform.log")
	os.Setenv("TF_LOG", "TRACE")

	// 执行 Terraform 命令
	cmd := exec.Command("terraform", "apply", "-auto-approve")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	
	if err := cmd.Run(); err != nil {
		log.Fatalf("Terraform 执行失败: %v", err)
	}

	// 执行 tf-profile 分析
	profileCmd := exec.Command("tf-profile", "analyze", 
		"--logfile", "terraform.log", 
		"--outfile", "profile.html")
	
	if err := profileCmd.Run(); err != nil {
		log.Fatalf("性能分析失败: %v", err)
	}

	log.Println("性能分析报告已生成: profile.html")
}

高级功能

1. 自定义分析选项

// 使用更多分析选项
profileCmd := exec.Command("tf-profile", "analyze",
	"--logfile", "terraform.log",
	"--outfile", "custom-profile.html",
	"--min-duration", "100ms",  // 只显示耗时超过100ms的操作
	"--sort-by", "duration")    // 按耗时排序

2. 生成 JSON 格式报告

tf-profile analyze --logfile terraform.log --format json --outfile profile.json

3. 过滤特定资源

tf-profile analyze --logfile terraform.log --resource-filter "aws_instance.*"

报告解读

生成的 HTML 报告包含以下主要部分:

  1. 执行概览:显示总执行时间和各阶段占比
  2. 资源耗时排名:列出耗时最长的资源操作
  3. 时间线视图:显示操作的并行执行情况
  4. 依赖关系图:展示资源间的依赖关系

性能优化建议

根据 tf-profile 报告,常见的优化方向包括:

  1. 减少依赖:优化资源间的依赖关系
  2. 并行化:确保无依赖关系的资源可以并行创建
  3. 模块优化:拆分大型模块为小型模块
  4. 提供者优化:更新或更换性能较差的提供者

注意事项

  1. 性能分析会产生额外的日志文件,注意磁盘空间
  2. TRACE 级别的日志可能包含敏感信息,妥善保管
  3. 对于大型基础设施,分析过程可能需要较多内存

通过 tf-profile 工具,开发者可以更直观地了解 Terraform 执行的性能特征,从而有针对性地进行优化,提高基础设施管理的效率。

回到顶部