feature: add skipFile flag for profile API
This commit is contained in:
parent
ea5a6654c3
commit
ede278cb64
@ -56,6 +56,7 @@ goc profile --force
|
|||||||
Service: svrList,
|
Service: svrList,
|
||||||
Address: addrList,
|
Address: addrList,
|
||||||
CoverFilePatterns: coverFilePatterns,
|
CoverFilePatterns: coverFilePatterns,
|
||||||
|
SkipFilePatterns: skipFilePatterns,
|
||||||
}
|
}
|
||||||
res, err := cover.NewWorker(center).Profile(p)
|
res, err := cover.NewWorker(center).Profile(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -84,6 +85,7 @@ var (
|
|||||||
force bool // --force flag
|
force bool // --force flag
|
||||||
output string // --output flag
|
output string // --output flag
|
||||||
coverFilePatterns []string // --coverfile flag
|
coverFilePatterns []string // --coverfile flag
|
||||||
|
skipFilePatterns []string // --skipfile flag
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -92,6 +94,7 @@ func init() {
|
|||||||
profileCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to fetch profile, see 'goc list' for all addresses.")
|
profileCmd.Flags().StringSliceVarP(&addrList, "address", "", nil, "address to fetch profile, see 'goc list' for all addresses.")
|
||||||
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force fetching all available profiles")
|
profileCmd.Flags().BoolVarP(&force, "force", "f", false, "force fetching all available profiles")
|
||||||
profileCmd.Flags().StringSliceVarP(&coverFilePatterns, "coverfile", "", nil, "only output coverage data of the files matching the patterns")
|
profileCmd.Flags().StringSliceVarP(&coverFilePatterns, "coverfile", "", nil, "only output coverage data of the files matching the patterns")
|
||||||
|
profileCmd.Flags().StringSliceVarP(&skipFilePatterns, "skipfile", "", nil, "skip the files matching the patterns when outputing coverage data")
|
||||||
addBasicFlags(profileCmd.Flags())
|
addBasicFlags(profileCmd.Flags())
|
||||||
rootCmd.AddCommand(profileCmd)
|
rootCmd.AddCommand(profileCmd)
|
||||||
}
|
}
|
||||||
|
@ -108,6 +108,7 @@ type ProfileParam struct {
|
|||||||
Service []string `form:"service" json:"service"`
|
Service []string `form:"service" json:"service"`
|
||||||
Address []string `form:"address" json:"address"`
|
Address []string `form:"address" json:"address"`
|
||||||
CoverFilePatterns []string `form:"coverfile" json:"coverfile"`
|
CoverFilePatterns []string `form:"coverfile" json:"coverfile"`
|
||||||
|
SkipFilePatterns []string `form:"skipfile" json:"skipfile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//listServices list all the registered services
|
//listServices list all the registered services
|
||||||
@ -209,6 +210,14 @@ func (s *server) profile(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(body.SkipFilePatterns) > 0 {
|
||||||
|
merged, err = skipProfile(body.SkipFilePatterns, merged)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to skip profile based on the patterns: %v, error: %v", body.SkipFilePatterns, err)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := cov.DumpProfile(merged, c.Writer); err != nil {
|
if err := cov.DumpProfile(merged, c.Writer); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
@ -234,6 +243,31 @@ func filterProfile(coverFile []string, profiles []*cover.Profile) ([]*cover.Prof
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// skipProfile skips profiles of the packages matching the coverFile pattern
|
||||||
|
func skipProfile(skipFile []string, profiles []*cover.Profile) ([]*cover.Profile, error) {
|
||||||
|
var out = make([]*cover.Profile, 0)
|
||||||
|
for _, profile := range profiles {
|
||||||
|
var shouldSkip bool
|
||||||
|
for _, pattern := range skipFile {
|
||||||
|
matched, err := regexp.MatchString(pattern, profile.FileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s, err: %v", pattern, profile.FileName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
shouldSkip = true
|
||||||
|
break // no need to check again for the file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !shouldSkip {
|
||||||
|
out = append(out, profile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) clear(c *gin.Context) {
|
func (s *server) clear(c *gin.Context) {
|
||||||
var body ProfileParam
|
var body ProfileParam
|
||||||
if err := c.ShouldBind(&body); err != nil {
|
if err := c.ShouldBind(&body); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user