Bruno Schaatsbergen website Mastodon PGP Key email A drawing of an astronaut in space The Netherlands

Profiling Terraform

in
writing
date
11/13/2024

Typically, there is no reason to profile Terraform, as it will point you to results you can’t really change anyways. But if you want to profile Terraform for whatever reason, you can do so using pprof1.

You can build Terraform with some modifications in main.go:

diff --git a/main.go b/main.go
index c807bb5a40..cc18baa0a2 100644
--- a/main.go
+++ b/main.go
@@ -25,6 +25,9 @@ import (
        "github.com/mitchellh/colorstring"
 
        backendInit "github.com/hashicorp/terraform/internal/backend/init"
+
+       "net/http"
+       _ "net/http/pprof"
 )
 
 const (
@@ -55,6 +58,10 @@ func init() {
 }
 
 func main() {
+       go func() {
+               log.Println(http.ListenAndServe("localhost:6059", nil))
+       }()
+
        os.Exit(realMain())
 }

Compile it to your $GOPATH/bin directory:

$ go install

This generates a terraform.prof file. We can convert it into a text-based file using:

$ go tool pprof -raw -output=terraform.txt terraform.prof

Next, to create a FlameGraph, use the tools from Brendan Gregg’s FlameGraph repository2

$ go tool pprof -raw -output=terraform.txt terraform.prof
$ stackcollapse-go.pl terraform.txt | flamegraph.pl > terraform.svg


👟 Footnotes

  1. https://pkg.go.dev/net/http/pprof ↩︎

  2. https://github.com/brendangregg/FlameGraph ↩︎

/profiling-terraform