Repository: google/pprof
Pull request #509 (2020-02-10 03:08:32)

Make tag filter of the form `-tagfilter=foo=1` work.
Tag filters like `-tagfilter=foo=1` currently cannot be used to filter
by a unitless numeric tag as the tag filter regular expression expects
the unit part to be always present.  They can be made working by using
`-tagfilter=foo=1unit` instead, but that's weird syntax. So fix this by
merely making the unit part optional.

I was wondering if this simple fix could have any unintended effects but
as far as I can tell from testing it does not.

codecov-io: # [Codecov](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=h1) Report
> Merging [#509](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=desc) into [master](https://codecov.io/gh/google/pprof/commit/d4f498aebedc02388a1c34ebe8dcb2e7c9e99f29?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/google/pprof/pull/509/graphs/tree.svg?width=650&token=nHgs4Smibv&height=150&src=pr)](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #509      +/-   ##
==========================================
+ Coverage   67.04%   67.06%   +0.01%     
==========================================
  Files          37       37              
  Lines        7611     7611              
==========================================
+ Hits         5103     5104       +1     
+ Misses       2104     2103       -1     
  Partials      404      404
```


| [Impacted Files](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/driver/driver\_focus.go](https://codecov.io/gh/google/pprof/pull/509/diff?src=pr&el=tree#diff-aW50ZXJuYWwvZHJpdmVyL2RyaXZlcl9mb2N1cy5nbw==) | `86.06% <ø> (+0.81%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=footer). Last update [d4f498a...4bc1480](https://codecov.io/gh/google/pprof/pull/509?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/driver/driver_focus.go b/internal/driver/driver_focus.go
index 551965e..af7b8d4 100644
--- a/internal/driver/driver_focus.go
+++ b/internal/driver/driver_focus.go
@@ -25,7 +25,7 @@ import (
 	"github.com/google/pprof/profile"
 )
 
-var tagFilterRangeRx = regexp.MustCompile("([+-]?[[:digit:]]+)([[:alpha:]]+)")
+var tagFilterRangeRx = regexp.MustCompile("([+-]?[[:digit:]]+)([[:alpha:]]+)?")
 
 // applyFocus filters samples based on the focus/ignore options
 func applyFocus(prof *profile.Profile, numLabelUnits map[string]string, v variables, ui plugin.UI) error {


Test patch:
diff --git a/internal/driver/driver_test.go b/internal/driver/driver_test.go
index 8eeeb48..8503d31 100644
--- a/internal/driver/driver_test.go
+++ b/internal/driver/driver_test.go
@@ -1455,6 +1455,55 @@ func TestNumericTagFilter(t *testing.T) {
 			map[string]string{"bytes": "bytes"},
 			false,
 		},
+		{
+			"Match exact value, unitless tag",
+			"pid=123",
+			map[string][]int64{"pid": {123}},
+			nil,
+			true,
+		},
+		{
+			"Match range, unitless tag",
+			"pid=123:123",
+			map[string][]int64{"pid": {123}},
+			nil,
+			true,
+		},
+		{
+			"Don't match range, unitless tag",
+			"pid=124:124",
+			map[string][]int64{"pid": {123}},
+			nil,
+			false,
+		},
+		{
+			"Match range without upper bound, unitless tag",
+			"pid=100:",
+			map[string][]int64{"pid": {123}},
+			nil,
+			true,
+		},
+		{
+			"Don't match range without upper bound, unitless tag",
+			"pid=200:",
+			map[string][]int64{"pid": {123}},
+			nil,
+			false,
+		},
+		{
+			"Match range without lower bound, unitless tag",
+			"pid=:200",
+			map[string][]int64{"pid": {123}},
+			nil,
+			true,
+		},
+		{
+			"Don't match range without lower bound, unitless tag",
+			"pid=:100",
+			map[string][]int64{"pid": {123}},
+			nil,
+			false,
+		},
 	}
 	for _, test := range tagFilterTests {
 		t.Run(test.desc, func(t *testing.T) {