Skip to content

Commit 9664cf8

Browse files
authored
feat: add profile feature for agent, and fix logr's panic (#444)
Signed-off-by: jingchao.djc <[email protected]> Signed-off-by: jingchao.djc <[email protected]>
1 parent 98ccd3d commit 9664cf8

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

agent/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,21 @@ Update the container env [variables](https://github.com/kubernetes/git-sync#para
5656

5757
### Demo Recording
5858

59-
[![asciicast](https://asciinema.org/a/FWbvVAiSsiI87wQx2TJbRMlxN.svg)](https://asciinema.org/a/FWbvVAiSsiI87wQx2TJbRMlxN)
59+
[![asciicast](https://asciinema.org/a/FWbvVAiSsiI87wQx2TJbRMlxN.svg)](https://asciinema.org/a/FWbvVAiSsiI87wQx2TJbRMlxN)
60+
61+
62+
### Profiling
63+
64+
Using env variables to enable profiling mode, the agent can be started with the following envs:
65+
66+
```bash
67+
export GITOPS_ENGINE_PROFILE=web
68+
# optional, default pprofile address is 127.0.0.1:6060
69+
export GITOPS_ENGINE_PROFILE_HOST=127.0.0.1
70+
export GITOPS_ENGINE_PROFILE_PORT=6060
71+
```
72+
73+
And then you can open profile in the browser(or using [pprof](https://github.com/google/pprof) cmd to generate diagrams):
74+
75+
- http://127.0.0.1:6060/debug/pprof/goroutine?debug=2
76+
- http://127.0.0.1:6060/debug/pprof/mutex?debug=2

agent/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2933
const (
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

3340
func 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+
99119
func 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),

pkg/cache/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ func (c *clusterCache) sync() error {
730730
if !ok {
731731
return err
732732
}
733-
c.log.Info("warning loading openapi schema: %s", e)
733+
c.log.Info("warning loading openapi schema: %s", e.Error())
734734
}
735735

736736
if gvkParser != nil {

pkg/utils/text/text.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ func FirstNonEmpty(args ...string) string {
88
}
99
return ""
1010
}
11+
12+
// WithDefault return defaultValue when val is blank
13+
func WithDefault(val string, defaultValue string) string {
14+
if len(val) == 0 {
15+
return defaultValue
16+
}
17+
return val
18+
}

0 commit comments

Comments
 (0)