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.
This commit is contained in:
Erik Seliger 2023-02-24 17:30:13 +01:00 committed by GitHub
parent 5b1e1066bf
commit f6e28fc387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}