Skip to content

Commit 44e46a8

Browse files
committed
tracing: add minimal format suitable for datadriven tests
I'd like to make the trace events emitted from deterministic multi-metric allocator tests part of the expected test outputs. I realized there wasn't a standardized deterministic and barebones output format, so I added one. Epic: none
1 parent ae0b3d7 commit 44e46a8

File tree

14 files changed

+420
-45
lines changed

14 files changed

+420
-45
lines changed

pkg/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ ALL_TESTS = [
835835
"//pkg/util/tracing/goexectrace:goexectrace_test",
836836
"//pkg/util/tracing/grpcinterceptor:grpcinterceptor_test",
837837
"//pkg/util/tracing/service:service_test",
838+
"//pkg/util/tracing/tracingpb:tracingpb_test",
838839
"//pkg/util/tracing:tracing_test",
839840
"//pkg/util/treeprinter:treeprinter_test",
840841
"//pkg/util/trigram:trigram_test",
@@ -2842,6 +2843,7 @@ GO_TARGETS = [
28422843
"//pkg/util/tracing/service:service",
28432844
"//pkg/util/tracing/service:service_test",
28442845
"//pkg/util/tracing/tracingpb:tracingpb",
2846+
"//pkg/util/tracing/tracingpb:tracingpb_test",
28452847
"//pkg/util/tracing/tracingservicepb:tracingservicepb",
28462848
"//pkg/util/tracing/tracingui:tracingui",
28472849
"//pkg/util/tracing/tracingutil:tracingutil",

pkg/util/tracing/tracingpb/BUILD.bazel

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_proto//proto:defs.bzl", "proto_library")
22
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
3-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
44

55
go_library(
66
name = "tracingpb",
@@ -51,3 +51,20 @@ go_proto_library(
5151
"@com_github_gogo_protobuf//gogoproto",
5252
],
5353
)
54+
55+
go_test(
56+
name = "tracingpb_test",
57+
srcs = [
58+
"condense_test.go",
59+
"recording_test.go",
60+
],
61+
data = glob(["testdata/**"]),
62+
embed = [":tracingpb"],
63+
deps = [
64+
"//pkg/testutils/datapathutils",
65+
"//pkg/testutils/echotest",
66+
"//pkg/util/log",
67+
"//pkg/util/tracing",
68+
"@com_github_cockroachdb_redact//:redact",
69+
],
70+
)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package tracingpb
7+
8+
import (
9+
"testing"
10+
11+
"github.com/cockroachdb/redact"
12+
)
13+
14+
func TestCondensePathLinePrefix(t *testing.T) {
15+
for _, test := range []struct {
16+
name string
17+
input string
18+
expected string
19+
}{
20+
{
21+
name: "no_directory",
22+
input: "foo.go:1234 message",
23+
expected: "message",
24+
},
25+
{
26+
name: "full_path",
27+
input: "pkg/foo/bar.go:123 message",
28+
expected: "message",
29+
},
30+
{
31+
// Won't redact because there are redaction markers in the path.
32+
// This doesn't happen in the real world but we want to make sure
33+
// condensePathLinePrefix doesn't mess with redaction at all.
34+
name: "redaction_in_path",
35+
input: "pkg/‹foo›/bar.go:123 message",
36+
expected: "pkg/‹foo›/bar.go:123 message",
37+
},
38+
{
39+
// Redaction in message is expected and okay, since
40+
// condensePathLinePrefix is written in a way to
41+
// preserve that.
42+
name: "redaction_in_message",
43+
input: "pkg/foo/bar.go:123 ‹sensitive› message",
44+
expected: "‹sensitive› message",
45+
},
46+
{
47+
// Not expecting it in the real world since we only call
48+
// condensePathLinePrefix in formatMinimal, but if we get formatFull, it
49+
// also works.
50+
name: "full_format",
51+
input: "event: pkg/foo/bar.go:1234 ‹sensitive› message",
52+
expected: "‹sensitive› message",
53+
},
54+
{
55+
name: "unredacted_trace",
56+
input: "‹pkg/foo/bar.go:1234 some message›",
57+
expected: "‹some message›",
58+
},
59+
{
60+
// Should never happen (there would be escaping). But either way, works
61+
// just the same.
62+
name: "unredacted_trace_nested_unescaped",
63+
input: "‹pkg/foo/bar.go:1234 some ‹message››",
64+
expected: "‹some ‹message››",
65+
},
66+
{
67+
name: "unredacted_trace_nested_escaped",
68+
input: "‹pkg/foo/bar.go:1234 " + string(redact.EscapeMarkers([]byte("some ‹message›"))) + "›",
69+
expected: "‹some ?message?›", // NB: this is verbatim the escaped string inserted above
70+
},
71+
} {
72+
t.Run(test.name, func(t *testing.T) {
73+
got := string(condensePathLinePrefix(redact.RedactableString(test.input)))
74+
if got != test.expected {
75+
t.Errorf("expected %q, got %q", test.expected, got)
76+
}
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)