From f6e28fc387078e83343f4e7ff044cfdc9aa8ea9e Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Fri, 24 Feb 2023 17:30:13 +0100 Subject: [PATCH] Allow to run GCP profiler in other instances (#48204) For MIs, this could also be helpful so allow to enable the profiler outside of dotcom, too with env var GOOGLE_CLOUD_PROFILER_ENABLED. --- internal/profiler/profiler.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/internal/profiler/profiler.go b/internal/profiler/profiler.go index 50c01005531..b251a85bed5 100644 --- a/internal/profiler/profiler.go +++ b/internal/profiler/profiler.go @@ -11,16 +11,14 @@ import ( "github.com/sourcegraph/sourcegraph/internal/version" ) -// Init starts the Google Cloud Profiler when in sourcegraph.com mode in -// production. https://cloud.google.com/profiler/docs/profiling-go -func Init() { - if !envvar.SourcegraphDotComMode() { - return - } +var gcpProfilerEnabled = env.MustGetBool("GOOGLE_CLOUD_PROFILER_ENABLED", false, "If true, enable Google Cloud Profiler. See https://cloud.google.com/profiler/docs/profiling-go") - // SourcegraphDotComMode can be true in dev, so check we are in a k8s - // cluster. - if !deploy.IsDeployTypeKubernetes(deploy.Type()) { +// Init starts the Google Cloud Profiler if configured. +// Will enable when in sourcegraph.com mode in production, or when +// GOOGLE_CLOUD_PROFILER_ENABLED is truthy. +// See https://cloud.google.com/profiler/docs/profiling-go. +func Init() { + if !shouldEnableProfiler() { return } @@ -34,3 +32,18 @@ func Init() { log15.Error("profiler.Init google cloud profiler", "error", err) } } + +func shouldEnableProfiler() bool { + // Force overwrite. + if gcpProfilerEnabled { + return true + } + if envvar.SourcegraphDotComMode() { + // SourcegraphDotComMode can be true in dev, so check we are in a k8s + // cluster. + if deploy.IsDeployTypeKubernetes(deploy.Type()) { + return true + } + } + return false +}