From 828148934bfe8b84003deebe9993c5fcf74759ba Mon Sep 17 00:00:00 2001 From: dillanb-hashi Date: Mon, 16 Jun 2025 08:19:20 -0700 Subject: [PATCH] chore(e2e): Added build tag for enabling pprof tracing (#5839) * added pprof tag and make command * updated tracing readme --- Makefile | 6 ++++++ internal/cmd/base/pprof_off.go | 14 +++++++++++++ internal/cmd/base/pprof_on.go | 28 ++++++++++++++++++++++++++ internal/cmd/commands/dev/dev.go | 2 ++ internal/cmd/commands/server/server.go | 1 + testing/TRACING.md | 19 +++-------------- 6 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 internal/cmd/base/pprof_off.go create mode 100644 internal/cmd/base/pprof_on.go diff --git a/Makefile b/Makefile index cfb876d7f0..866bee0b84 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,12 @@ install: build install-no-plugins: export SKIP_PLUGIN_BUILD=1 install-no-plugins: install +.PHONY: build-pprof +build-pprof: BUILD_TAGS+=pprof +build-pprof: + @echo "==> Building Boundary with memory pprof enabled" + @CGO_ENABLED=$(CGO_ENABLED) BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" + .PHONY: build-memprof build-memprof: BUILD_TAGS+=memprofiler build-memprof: diff --git a/internal/cmd/base/pprof_off.go b/internal/cmd/base/pprof_off.go new file mode 100644 index 0000000000..e8e9cbb5bb --- /dev/null +++ b/internal/cmd/base/pprof_off.go @@ -0,0 +1,14 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +//go:build !pprof +// +build !pprof + +package base + +import ( + "context" +) + +func StartPprof(_ context.Context) { +} diff --git a/internal/cmd/base/pprof_on.go b/internal/cmd/base/pprof_on.go new file mode 100644 index 0000000000..7c12a47e65 --- /dev/null +++ b/internal/cmd/base/pprof_on.go @@ -0,0 +1,28 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +//go:build pprof +// +build pprof + +package base + +import ( + "context" + "errors" + "net/http" + + "github.com/hashicorp/boundary/internal/event" + + _ "net/http/pprof" +) + +func StartPprof(ctx context.Context) { + const op = "base.StartPprof" + go func() { + const addr = "localhost:6060" + event.WriteSysEvent(ctx, op, "starting pprof HTTP server", "addr", addr) + if err := http.ListenAndServe(addr, nil); err != nil && !errors.Is(err, http.ErrServerClosed) { + event.WriteSysEvent(ctx, op, "failed to serve pprof HTTP server", "error", err.Error()) + } + }() +} diff --git a/internal/cmd/commands/dev/dev.go b/internal/cmd/commands/dev/dev.go index 3596c2c888..803fb5a03a 100644 --- a/internal/cmd/commands/dev/dev.go +++ b/internal/cmd/commands/dev/dev.go @@ -722,6 +722,8 @@ func (c *Command) Run(args []string) int { return base.CommandCliError } + base.StartPprof(c.Context) + if c.flagRecoveryKey != "" { c.Config.DevRecoveryKey = c.flagRecoveryKey } diff --git a/internal/cmd/commands/server/server.go b/internal/cmd/commands/server/server.go index 6134de6d66..b9d8bee85f 100644 --- a/internal/cmd/commands/server/server.go +++ b/internal/cmd/commands/server/server.go @@ -217,6 +217,7 @@ func (c *Command) Run(args []string) int { c.WorkerAuthDebuggingEnabled.Store(c.Config.EnableWorkerAuthDebugging) base.StartMemProfiler(c.Context) + base.StartPprof(c.Context) // Note: the checks directly after this must remain where they are because // they rely on the state of configured KMSes. diff --git a/testing/TRACING.md b/testing/TRACING.md index 62b60b7804..f9fa37a32f 100644 --- a/testing/TRACING.md +++ b/testing/TRACING.md @@ -1,23 +1,10 @@ # Tracing in Boundary Boundary includes a small number of runtime tracing user regions, which can be used to see where Boundary spends its time during execution. -To create a trace, we first need to expose the pprof endpoint. It is disabled by default. Exposing the pprof endpoint is as simple as importing the correct package and starting a HTTP server anywhere in Boundary: +To create a trace, we first need to expose the pprof endpoint. It is disabled by default. Exposing the pprof endpoint is as simple as building with the `pprof` build tag or running -```go -package anything - -import ( - "log" - "net/http" - - _ "net/http/pprof" -) - -... - go func() { - log.Println(http.ListenAndServe("localhost:6060", nil)) - }() -... +``` +make build-pprof ``` This will create a new HTTP endpoint on `localhost:6060` of the running binary. As such, it's only accessible to the users on the same machine.