Skip to content

Commit

Permalink
util/log: allow custom crash report tags
Browse files Browse the repository at this point in the history
Today it can be difficult to trace back a sentry event to the CC cluster where
it originated, especially for serverless clusters. This change enables a new
environment variable (COCKROACH_CRASH_REPORT_TAGS), which allows
the database operator to provide additional information that will be included
in the sentry event.

Release note: None

Epic: none
  • Loading branch information
PJ Tatlow committed Jul 13, 2023
1 parent b6b5d28 commit 68a82d5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/util/log/logcrash/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_test(
"//pkg/testutils",
"//pkg/testutils/serverutils",
"//pkg/util",
"//pkg/util/envutil",
"//pkg/util/leaktest",
"//pkg/util/randutil",
"//pkg/util/timeutil",
Expand All @@ -49,6 +50,7 @@ go_test(
"@com_github_kr_pretty//:pretty",
"@com_github_pmezard_go_difflib//difflib",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
] + select({
"@io_bazel_rules_go//go/platform:aix": [
"@org_golang_x_sys//unix",
Expand Down
17 changes: 17 additions & 0 deletions pkg/util/log/logcrash/crash_reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package logcrash
import (
"context"
"fmt"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -263,9 +264,25 @@ func SetupCrashReporter(ctx context.Context, cmd string) {
"buildchannel": info.Channel,
"envchannel": info.EnvChannel,
})

})
}

func getTagsFromEnvironment() map[string]string {
tags := map[string]string{}
rawTags := envutil.EnvOrDefaultString("COCKROACH_CRASH_REPORT_TAGS", "")
if len(rawTags) > 0 {
envTags := strings.Split(rawTags, ";")
for _, tag := range envTags {
parts := strings.Split(tag, "=")
if len(parts) == 2 {
tags[parts[0]] = parts[1]
}
}
}
return tags
}

func uptimeTag(now time.Time) string {
uptime := now.Sub(startTime)
switch {
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/log/logcrash/crash_reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/testify/require"
)

// Renumber lines so they're stable no matter what changes above. (We
Expand Down Expand Up @@ -379,6 +381,25 @@ func TestUptimeTag(t *testing.T) {
}
}

func TestGetTagsFromEnvironment(t *testing.T) {
testCases := []struct {
envTags string
expectedTags map[string]string
}{
{"", map[string]string{}},
{"a=b", map[string]string{"a": "b"}},
{"a=123", map[string]string{"a": "123"}},
{"a=123;b=FOOBAR", map[string]string{"a": "123", "b": "FOOBAR"}},
{"a=123;b=FOOBAR;", map[string]string{"a": "123", "b": "FOOBAR"}},
}
for _, tc := range testCases {
func() {
defer envutil.TestSetEnv(t, "COCKROACH_CRASH_REPORT_TAGS", tc.envTags)()
require.Equal(t, tc.expectedTags, getTagsFromEnvironment())
}()
}
}

// makeTypeAssertionErr returns a runtime.Error with the message:
//
// interface conversion: interface {} is nil, not int
Expand Down

0 comments on commit 68a82d5

Please sign in to comment.