Selaa lähdekoodia

Allow passing in a custom HTTPServer (#341)

* Avoid opening browser when not in terminal

This also reverts e82ee9a, though only in spirit since the revert
did not apply cleanly.

This change may look cosmetic, but note that `plugin.UI` can be
passed in from the outside. This is important for embedding pprof
into an application for easy access to the web ui: with this change,
such an application can pass a non-Terminal `profile.UI` and avoids
the browser code.

* Allow passing in a custom HTTPServer

This allows embedding the pprof ui into an existing application.
See https://github.com/cockroachdb/cockroach/pull/24145 for an example.
Tobias Schottdorf 7 vuotta sitten
vanhempi
commit
f0154354e2
4 muutettua tiedostoa jossa 35 lisäystä ja 17 poistoa
  1. 24
    12
      driver/driver.go
  2. 1
    1
      internal/driver/driver.go
  3. 2
    2
      internal/driver/webui.go
  4. 8
    2
      internal/driver/webui_test.go

+ 24
- 12
driver/driver.go Näytä tiedosto

@@ -41,24 +41,36 @@ func (o *Options) internalOptions() *plugin.Options {
41 41
 	if o.Sym != nil {
42 42
 		sym = &internalSymbolizer{o.Sym}
43 43
 	}
44
+	var httpServer func(args *plugin.HTTPServerArgs) error
45
+	if o.HTTPServer != nil {
46
+		httpServer = func(args *plugin.HTTPServerArgs) error {
47
+			return o.HTTPServer(((*HTTPServerArgs)(args)))
48
+		}
49
+	}
44 50
 	return &plugin.Options{
45
-		Writer:  o.Writer,
46
-		Flagset: o.Flagset,
47
-		Fetch:   o.Fetch,
48
-		Sym:     sym,
49
-		Obj:     obj,
50
-		UI:      o.UI,
51
+		Writer:     o.Writer,
52
+		Flagset:    o.Flagset,
53
+		Fetch:      o.Fetch,
54
+		Sym:        sym,
55
+		Obj:        obj,
56
+		UI:         o.UI,
57
+		HTTPServer: httpServer,
51 58
 	}
52 59
 }
53 60
 
61
+// HTTPServerArgs contains arguments needed by an HTTP server that
62
+// is exporting a pprof web interface.
63
+type HTTPServerArgs plugin.HTTPServerArgs
64
+
54 65
 // Options groups all the optional plugins into pprof.
55 66
 type Options struct {
56
-	Writer  Writer
57
-	Flagset FlagSet
58
-	Fetch   Fetcher
59
-	Sym     Symbolizer
60
-	Obj     ObjTool
61
-	UI      UI
67
+	Writer     Writer
68
+	Flagset    FlagSet
69
+	Fetch      Fetcher
70
+	Sym        Symbolizer
71
+	Obj        ObjTool
72
+	UI         UI
73
+	HTTPServer func(*HTTPServerArgs) error
62 74
 }
63 75
 
64 76
 // Writer provides a mechanism to write data under a certain name,

+ 1
- 1
internal/driver/driver.go Näytä tiedosto

@@ -54,7 +54,7 @@ func PProf(eo *plugin.Options) error {
54 54
 	}
55 55
 
56 56
 	if src.HTTPHostport != "" {
57
-		return serveWebInterface(src.HTTPHostport, p, o, true)
57
+		return serveWebInterface(src.HTTPHostport, p, o)
58 58
 	}
59 59
 	return interactive(p, o)
60 60
 }

+ 2
- 2
internal/driver/webui.go Näytä tiedosto

@@ -82,7 +82,7 @@ type webArgs struct {
82 82
 	FlameGraph template.JS
83 83
 }
84 84
 
85
-func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, wantBrowser bool) error {
85
+func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) error {
86 86
 	host, port, err := getHostAndPort(hostport)
87 87
 	if err != nil {
88 88
 		return err
@@ -117,7 +117,7 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
117 117
 		},
118 118
 	}
119 119
 
120
-	if wantBrowser {
120
+	if o.UI.IsTerminal() {
121 121
 		go openBrowser("http://"+args.Hostport, o)
122 122
 	}
123 123
 	return server(args)

+ 8
- 2
internal/driver/webui_test.go Näytä tiedosto

@@ -31,6 +31,12 @@ import (
31 31
 	"github.com/google/pprof/profile"
32 32
 )
33 33
 
34
+type nonTerminalUI struct {
35
+	plugin.UI
36
+}
37
+
38
+func (*nonTerminalUI) IsTerminal() bool { return false }
39
+
34 40
 func TestWebInterface(t *testing.T) {
35 41
 	if runtime.GOOS == "nacl" {
36 42
 		t.Skip("test assumes tcp available")
@@ -55,9 +61,9 @@ func TestWebInterface(t *testing.T) {
55 61
 	// Start server and wait for it to be initialized
56 62
 	go serveWebInterface("unused:1234", prof, &plugin.Options{
57 63
 		Obj:        fakeObjTool{},
58
-		UI:         &stdUI{},
64
+		UI:         &nonTerminalUI{&stdUI{}}, // don't open browser
59 65
 		HTTPServer: creator,
60
-	}, false)
66
+	})
61 67
 	<-serverCreated
62 68
 	defer server.Close()
63 69