Browse Source

cmd/stcrashreceiver: Pick up extra build tags and send to Sentry (#6710)

This extracts the extra tags from any `[foo]` stuff at the end of the
version and sends them to Sentry for indexing.

If I need to modify that regexp again I'll probably write a from scratch
tokenizer and parser for our version string instead...
Jakob Borg 5 years ago
parent
commit
1b77ab2b52
2 changed files with 40 additions and 13 deletions
  1. 23 10
      cmd/stcrashreceiver/sentry.go
  2. 17 3
      cmd/stcrashreceiver/sentry_test.go

+ 23 - 10
cmd/stcrashreceiver/sentry.go

@@ -148,22 +148,26 @@ func parseReport(path string, report []byte) (*raven.Packet, error) {
 	if version.commit != "" {
 		pkt.Tags = append(pkt.Tags, raven.Tag{Key: "commit", Value: version.commit})
 	}
+	for _, tag := range version.extra {
+		pkt.Tags = append(pkt.Tags, raven.Tag{Key: tag, Value: "1"})
+	}
 
 	return pkt, nil
 }
 
-// syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) [email protected] 2019-05-23 16:08:14 UTC
-var longVersionRE = regexp.MustCompile(`syncthing\s+(v[^\s]+)\s+"([^"]+)"\s\(([^\s]+)\s+([^-]+)-([^)]+)\)\s+([^\s]+)`)
+// syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) [email protected] 2019-05-23 16:08:14 UTC [foo, bar]
+var longVersionRE = regexp.MustCompile(`syncthing\s+(v[^\s]+)\s+"([^"]+)"\s\(([^\s]+)\s+([^-]+)-([^)]+)\)\s+([^\s]+)[^\[]*(?:\[(.+)\])?$`)
 
 type version struct {
-	version  string // "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep"
-	tag      string // "v1.1.4-rc.1"
-	commit   string // "6aaae618", blank when absent
-	codename string // "Erbium Earthworm"
-	runtime  string // "go1.12.5"
-	goos     string // "darwin"
-	goarch   string // "amd64"
-	builder  string // "[email protected]"
+	version  string   // "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep"
+	tag      string   // "v1.1.4-rc.1"
+	commit   string   // "6aaae618", blank when absent
+	codename string   // "Erbium Earthworm"
+	runtime  string   // "go1.12.5"
+	goos     string   // "darwin"
+	goarch   string   // "amd64"
+	builder  string   // "[email protected]"
+	extra    []string // "foo", "bar"
 }
 
 func (v version) environment() string {
@@ -193,6 +197,7 @@ func parseVersion(line string) (version, error) {
 		goarch:   m[5],
 		builder:  m[6],
 	}
+
 	parts := strings.Split(v.version, "+")
 	v.tag = parts[0]
 	if len(parts) > 1 {
@@ -202,5 +207,13 @@ func parseVersion(line string) (version, error) {
 		}
 	}
 
+	if len(m) >= 8 && m[7] != "" {
+		tags := strings.Split(m[7], ",")
+		for i := range tags {
+			tags[i] = strings.TrimSpace(tags[i])
+		}
+		v.extra = tags
+	}
+
 	return v, nil
 }

+ 17 - 3
cmd/stcrashreceiver/sentry_test.go

@@ -30,16 +30,30 @@ func TestParseVersion(t *testing.T) {
 				builder:  "[email protected]",
 			},
 		},
+		{
+			longVersion: `syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) [email protected] 2019-05-23 16:08:14 UTC [foo, bar]`,
+			parsed: version{
+				version:  "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep",
+				tag:      "v1.1.4-rc.1",
+				commit:   "6aaae618",
+				codename: "Erbium Earthworm",
+				runtime:  "go1.12.5",
+				goos:     "darwin",
+				goarch:   "amd64",
+				builder:  "[email protected]",
+				extra:    []string{"foo", "bar"},
+			},
+		},
 	}
 
 	for _, tc := range cases {
 		v, err := parseVersion(tc.longVersion)
 		if err != nil {
-			t.Error(err)
+			t.Errorf("%s\nerror: %v\n", tc.longVersion, err)
 			continue
 		}
-		if v != tc.parsed {
-			t.Error(v)
+		if fmt.Sprint(v) != fmt.Sprint(tc.parsed) {
+			t.Errorf("%s\nA: %v\nE: %v\n", tc.longVersion, v, tc.parsed)
 		}
 	}
 }