Procházet zdrojové kódy

Add flag to append a comment to a profile. (#221)

Add a flag to append a comment to a profile.
Alexey Alexandrov před 7 roky
rodič
revize
41ac4e1c27

+ 7
- 2
internal/driver/cli.go Zobrazit soubor

@@ -34,6 +34,7 @@ type source struct {
34 34
 	Timeout      int
35 35
 	Symbolize    string
36 36
 	HTTPHostport string
37
+	Comment      string
37 38
 }
38 39
 
39 40
 // Parse parses the command lines through the specified flags package
@@ -43,9 +44,11 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
43 44
 	flag := o.Flagset
44 45
 	// Comparisons.
45 46
 	flagBase := flag.StringList("base", "", "Source for base profile for comparison")
46
-	// Internal options.
47
+	// Source options.
47 48
 	flagSymbolize := flag.String("symbolize", "", "Options for profile symbolization")
48 49
 	flagBuildID := flag.String("buildid", "", "Override build id for first mapping")
50
+	flagTimeout := flag.Int("timeout", -1, "Timeout in seconds for fetching a profile")
51
+	flagAddComment := flag.String("add_comment", "", "Annotation string to record in the profile")
49 52
 	// CPU profile options
50 53
 	flagSeconds := flag.Int("seconds", -1, "Length of time for dynamic profiles")
51 54
 	// Heap profile options
@@ -59,7 +62,6 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
59 62
 	flagMeanDelay := flag.Bool("mean_delay", false, "Display mean delay at each region")
60 63
 	flagTools := flag.String("tools", os.Getenv("PPROF_TOOLS"), "Path for object tool pathnames")
61 64
 
62
-	flagTimeout := flag.Int("timeout", -1, "Timeout in seconds for fetching a profile")
63 65
 	flagHTTP := flag.String("http", "", "Present interactive web based UI at the specified http host:port")
64 66
 
65 67
 	// Flags used during command processing
@@ -135,6 +137,7 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
135 137
 		Timeout:      *flagTimeout,
136 138
 		Symbolize:    *flagSymbolize,
137 139
 		HTTPHostport: *flagHTTP,
140
+		Comment:      *flagAddComment,
138 141
 	}
139 142
 
140 143
 	for _, s := range *flagBase {
@@ -278,6 +281,8 @@ var usageMsgSrc = "\n\n" +
278 281
 	"    -seconds              Duration for time-based profile collection\n" +
279 282
 	"    -timeout              Timeout in seconds for profile collection\n" +
280 283
 	"    -buildid              Override build id for main binary\n" +
284
+	"    -add_comment          Free-form annotation to add to the profile\n" +
285
+	"                          Displayed on some reports or with pprof -comments\n" +
281 286
 	"    -base source          Source of profile to use as baseline\n" +
282 287
 	"    profile.pb.gz         Profile in compressed protobuf format\n" +
283 288
 	"    legacy_profile        Profile in legacy pprof format\n" +

+ 3
- 3
internal/driver/driver_test.go Zobrazit soubor

@@ -73,7 +73,7 @@ func TestParse(t *testing.T) {
73 73
 		{"dot,unit=minimum", "heap_sizetags"},
74 74
 		{"dot,addresses,flat,ignore=[X3]002,focus=[X1]000", "contention"},
75 75
 		{"dot,files,cum", "contention"},
76
-		{"comments", "cpu"},
76
+		{"comments,add_comment=some-comment", "cpu"},
77 77
 		{"comments", "heap"},
78 78
 		{"tags", "cpu"},
79 79
 		{"tags,tagignore=tag[13],tagfocus=key[12]", "cpu"},
@@ -153,6 +153,8 @@ func TestParse(t *testing.T) {
153 153
 			addFlags(&f, flags[:1])
154 154
 			solution = solutionFilename(tc.source, &f)
155 155
 		}
156
+		// The add_comment flag is not idempotent so only apply it on the first run.
157
+		delete(f.strings, "add_comment")
156 158
 
157 159
 		// Second pprof invocation to read the profile from profile.proto
158 160
 		// and generate a report.
@@ -401,7 +403,6 @@ func (testFetcher) Fetch(s string, d, t time.Duration) (*profile.Profile, string
401 403
 		}
402 404
 	case "heap_tags":
403 405
 		p = heapProfile()
404
-
405 406
 		for i := 0; i < len(p.Sample); i += 2 {
406 407
 			s := p.Sample[i]
407 408
 			if s.Label == nil {
@@ -410,7 +411,6 @@ func (testFetcher) Fetch(s string, d, t time.Duration) (*profile.Profile, string
410 411
 			s.NumLabel["request"] = s.NumLabel["bytes"]
411 412
 			s.Label["key1"] = []string{"tag"}
412 413
 		}
413
-
414 414
 	case "contention":
415 415
 		p = contentionProfile()
416 416
 	case "symbolz":

+ 4
- 0
internal/driver/fetch.go Zobrazit soubor

@@ -83,6 +83,10 @@ func fetchProfiles(s *source, o *plugin.Options) (*profile.Profile, error) {
83 83
 	p.RemoveUninteresting()
84 84
 	unsourceMappings(p)
85 85
 
86
+	if s.Comment != "" {
87
+		p.Comments = append(p.Comments, s.Comment)
88
+	}
89
+
86 90
 	// Save a copy of the merged profile if there is at least one remote source.
87 91
 	if save {
88 92
 		dir, err := setTmpDir(o.UI)

+ 1
- 0
internal/driver/testdata/pprof.cpu.comments Zobrazit soubor

@@ -0,0 +1 @@
1
+some-comment