@@ -5,11 +5,13 @@ import (
55 "crypto/sha256"
66 "encoding/base64"
77 "fmt"
8+ "github.com/argoproj/gitops-engine/pkg/utils/text"
89 "io/ioutil"
910 "net/http"
1011 "os"
1112 "os/exec"
1213 "path/filepath"
14+ "runtime"
1315 "strings"
1416 "text/tabwriter"
1517 "time"
@@ -24,10 +26,15 @@ import (
2426 "github.com/argoproj/gitops-engine/pkg/engine"
2527 "github.com/argoproj/gitops-engine/pkg/sync"
2628 "github.com/argoproj/gitops-engine/pkg/utils/kube"
29+
30+ _ "net/http/pprof"
2731)
2832
2933const (
3034 annotationGCMark = "gitops-agent.argoproj.io/gc-mark"
35+ envProfile = "GITOPS_ENGINE_PROFILE"
36+ envProfileHost = "GITOPS_ENGINE_PROFILE_HOST"
37+ envProfilePort = "GITOPS_ENGINE_PROFILE_PORT"
3138)
3239
3340func main () {
@@ -96,6 +103,19 @@ func (s *settings) parseManifests() ([]*unstructured.Unstructured, string, error
96103 return res , string (revision ), nil
97104}
98105
106+ func StartProfiler (log logr.Logger ) {
107+ if os .Getenv (envProfile ) == "web" {
108+ go func () {
109+ runtime .SetBlockProfileRate (1 )
110+ runtime .SetMutexProfileFraction (1 )
111+ profilePort := text .WithDefault (os .Getenv (envProfilePort ), "6060" )
112+ profileHost := text .WithDefault (os .Getenv (envProfileHost ), "127.0.0.1" )
113+
114+ log .Info ("pprof" , "err" , http .ListenAndServe (fmt .Sprintf ("%s:%s" , profileHost , profilePort ), nil ))
115+ }()
116+ }
117+ }
118+
99119func newCmd (log logr.Logger ) * cobra.Command {
100120 var (
101121 clientConfig clientcmd.ClientConfig
@@ -125,6 +145,8 @@ func newCmd(log logr.Logger) *cobra.Command {
125145 if namespaced {
126146 namespaces = []string {namespace }
127147 }
148+
149+ StartProfiler (log )
128150 clusterCache := cache .NewClusterCache (config ,
129151 cache .SetNamespaces (namespaces ),
130152 cache .SetLogr (log ),
0 commit comments