2022-05-31 23:29:19 +00:00
|
|
|
package querytracer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2023-11-14 19:58:28 +00:00
|
|
|
"sync"
|
2022-05-31 23:29:19 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTracerDisabled(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(false, "test")
|
2022-05-31 23:29:19 +00:00
|
|
|
if qt.Enabled() {
|
|
|
|
t.Fatalf("query tracer must be disabled")
|
|
|
|
}
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild := qt.NewChild("child done %d", 456)
|
2022-05-31 23:29:19 +00:00
|
|
|
if qtChild.Enabled() {
|
|
|
|
t.Fatalf("query tracer must be disabled")
|
|
|
|
}
|
|
|
|
qtChild.Printf("foo %d", 123)
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("parent %d", 789)
|
|
|
|
if err := qt.AddJSON([]byte("foobar")); err != nil {
|
|
|
|
t.Fatalf("unexpected error in AddJSON: %s", err)
|
|
|
|
}
|
2022-06-08 18:05:17 +00:00
|
|
|
qt.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
s := qt.String()
|
|
|
|
if s != "" {
|
|
|
|
t.Fatalf("unexpected trace; got %s; want empty", s)
|
|
|
|
}
|
|
|
|
s = qt.ToJSON()
|
|
|
|
if s != "" {
|
|
|
|
t.Fatalf("unexpected json trace; got %s; want empty", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracerEnabled(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(true, "test")
|
2022-05-31 23:29:19 +00:00
|
|
|
if !qt.Enabled() {
|
|
|
|
t.Fatalf("query tracer must be enabled")
|
|
|
|
}
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild := qt.NewChild("child done %d", 456)
|
2022-05-31 23:29:19 +00:00
|
|
|
if !qtChild.Enabled() {
|
|
|
|
t.Fatalf("child query tracer must be enabled")
|
|
|
|
}
|
|
|
|
qtChild.Printf("foo %d", 123)
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("parent %d", 789)
|
2022-06-08 18:05:17 +00:00
|
|
|
qt.Donef("foo %d", 33)
|
2022-05-31 23:29:19 +00:00
|
|
|
s := qt.String()
|
2022-12-09 01:21:38 +00:00
|
|
|
sExpected := `- 0ms: : test: foo 33
|
2022-05-31 23:29:19 +00:00
|
|
|
| - 0ms: child done 456
|
|
|
|
| | - 0ms: foo 123
|
|
|
|
| - 0ms: parent 789
|
|
|
|
`
|
|
|
|
if !areEqualTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracerMultiline(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(true, "line1\nline2")
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("line3\nline4\n")
|
2022-06-08 18:05:17 +00:00
|
|
|
qt.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
s := qt.String()
|
2022-12-09 01:21:38 +00:00
|
|
|
sExpected := `- 0ms: : line1
|
2022-05-31 23:29:19 +00:00
|
|
|
| line2
|
|
|
|
| - 0ms: line3
|
|
|
|
| | line4
|
|
|
|
`
|
|
|
|
if !areEqualTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTracerToJSON(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(true, "test")
|
2022-05-31 23:29:19 +00:00
|
|
|
if !qt.Enabled() {
|
|
|
|
t.Fatalf("query tracer must be enabled")
|
|
|
|
}
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild := qt.NewChild("child done %d", 456)
|
2022-05-31 23:29:19 +00:00
|
|
|
if !qtChild.Enabled() {
|
|
|
|
t.Fatalf("child query tracer must be enabled")
|
|
|
|
}
|
|
|
|
qtChild.Printf("foo %d", 123)
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("parent %d", 789)
|
2022-06-08 18:05:17 +00:00
|
|
|
qt.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
s := qt.ToJSON()
|
2022-12-09 01:21:38 +00:00
|
|
|
sExpected := `{"duration_msec":0,"message":": test","children":[` +
|
2022-05-31 23:29:19 +00:00
|
|
|
`{"duration_msec":0,"message":"child done 456","children":[` +
|
|
|
|
`{"duration_msec":0,"message":"foo 123"}]},` +
|
|
|
|
`{"duration_msec":0,"message":"parent 789"}]}`
|
|
|
|
if !areEqualJSONTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceAddJSON(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild := New(true, "child")
|
2022-05-31 23:29:19 +00:00
|
|
|
qtChild.Printf("foo")
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
jsonTrace := qtChild.ToJSON()
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(true, "parent")
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("first_line")
|
|
|
|
if err := qt.AddJSON([]byte(jsonTrace)); err != nil {
|
|
|
|
t.Fatalf("unexpected error in AddJSON: %s", err)
|
|
|
|
}
|
|
|
|
qt.Printf("last_line")
|
|
|
|
if err := qt.AddJSON(nil); err != nil {
|
|
|
|
t.Fatalf("unexpected error in AddJSON(nil): %s", err)
|
|
|
|
}
|
2022-06-08 18:05:17 +00:00
|
|
|
qt.Done()
|
2022-05-31 23:29:19 +00:00
|
|
|
s := qt.String()
|
2022-12-09 02:18:06 +00:00
|
|
|
sExpected := `- 0ms: : parent
|
2022-05-31 23:29:19 +00:00
|
|
|
| - 0ms: first_line
|
2022-12-09 02:18:06 +00:00
|
|
|
| - 0ms: : child
|
2022-05-31 23:29:19 +00:00
|
|
|
| | - 0ms: foo
|
|
|
|
| - 0ms: last_line
|
|
|
|
`
|
|
|
|
if !areEqualTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonS := qt.ToJSON()
|
2022-12-09 02:18:06 +00:00
|
|
|
jsonSExpected := `{"duration_msec":0,"message":": parent","children":[` +
|
2022-05-31 23:29:19 +00:00
|
|
|
`{"duration_msec":0,"message":"first_line"},` +
|
2022-12-09 02:18:06 +00:00
|
|
|
`{"duration_msec":0,"message":": child","children":[` +
|
2022-05-31 23:29:19 +00:00
|
|
|
`{"duration_msec":0,"message":"foo"}]},` +
|
|
|
|
`{"duration_msec":0,"message":"last_line"}]}`
|
|
|
|
if !areEqualJSONTracesSkipDuration(jsonS, jsonSExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", jsonS, jsonSExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceMissingDonef(t *testing.T) {
|
2022-06-08 18:05:17 +00:00
|
|
|
qt := New(true, "parent")
|
2022-05-31 23:29:19 +00:00
|
|
|
qt.Printf("parent printf")
|
2022-06-08 18:05:17 +00:00
|
|
|
qtChild := qt.NewChild("child")
|
2022-05-31 23:29:19 +00:00
|
|
|
qtChild.Printf("child printf")
|
|
|
|
qt.Printf("another parent printf")
|
|
|
|
s := qt.String()
|
2023-11-14 19:58:28 +00:00
|
|
|
sExpected := `- 0.000ms: missing Tracer.Done() call for the trace with message=: parent
|
|
|
|
`
|
|
|
|
if !areEqualTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceConcurrent(t *testing.T) {
|
|
|
|
qt := New(true, "parent")
|
|
|
|
childLocal := qt.NewChild("local")
|
|
|
|
childLocal.Printf("abc")
|
|
|
|
childLocal.Done()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 3; i++ {
|
2024-08-19 19:15:33 +00:00
|
|
|
child := qt.NewChild("child %d", i)
|
2023-11-14 19:58:28 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for j := 0; j < 100; j++ {
|
2024-08-19 19:15:33 +00:00
|
|
|
child.Printf("message %d", j)
|
2023-11-14 19:58:28 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
qt.Done()
|
|
|
|
// Verify that it is safe to call qt.String() when child traces aren't done yet
|
|
|
|
s := qt.String()
|
|
|
|
wg.Wait()
|
|
|
|
sExpected := `- 0.008ms: : parent
|
|
|
|
| - 0.002ms: local
|
|
|
|
| | - 0.000ms: abc
|
|
|
|
| - 0.000ms: missing Tracer.Done() call for the trace with message=child 0
|
|
|
|
| - 0.000ms: missing Tracer.Done() call for the trace with message=child 1
|
|
|
|
| - 0.000ms: missing Tracer.Done() call for the trace with message=child 2
|
2022-05-31 23:29:19 +00:00
|
|
|
`
|
|
|
|
if !areEqualTracesSkipDuration(s, sExpected) {
|
|
|
|
t.Fatalf("unexpected trace\ngot\n%s\nwant\n%s", s, sExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestZeroDurationInTrace(t *testing.T) {
|
|
|
|
s := `- 123ms: missing Tracer.Donef() call
|
|
|
|
| - 0ms: parent printf
|
|
|
|
| - 54ms: missing Tracer.Donef() call
|
|
|
|
| | - 45ms: child printf
|
|
|
|
| - 0ms: another parent printf
|
|
|
|
`
|
|
|
|
result := zeroDurationsInTrace(s)
|
|
|
|
resultExpected := `- 0ms: missing Tracer.Donef() call
|
|
|
|
| - 0ms: parent printf
|
|
|
|
| - 0ms: missing Tracer.Donef() call
|
|
|
|
| | - 0ms: child printf
|
|
|
|
| - 0ms: another parent printf
|
|
|
|
`
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestZeroJSONDurationInTrace(t *testing.T) {
|
|
|
|
s := `{"duration_msec":123,"message":"parent","children":[` +
|
|
|
|
`{"duration_msec":0,"message":"first_line"},` +
|
|
|
|
`{"duration_msec":434,"message":"child","children":[` +
|
|
|
|
`{"duration_msec":343,"message":"foo"}]},` +
|
|
|
|
`{"duration_msec":0,"message":"last_line"}]}`
|
|
|
|
result := zeroJSONDurationsInTrace(s)
|
|
|
|
resultExpected := `{"duration_msec":0,"message":"parent","children":[` +
|
|
|
|
`{"duration_msec":0,"message":"first_line"},` +
|
|
|
|
`{"duration_msec":0,"message":"child","children":[` +
|
|
|
|
`{"duration_msec":0,"message":"foo"}]},` +
|
|
|
|
`{"duration_msec":0,"message":"last_line"}]}`
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func areEqualTracesSkipDuration(s1, s2 string) bool {
|
|
|
|
s1 = zeroDurationsInTrace(s1)
|
|
|
|
s2 = zeroDurationsInTrace(s2)
|
|
|
|
return s1 == s2
|
|
|
|
}
|
|
|
|
|
|
|
|
func zeroDurationsInTrace(s string) string {
|
2024-07-17 10:18:38 +00:00
|
|
|
return skipDurationRe.ReplaceAllLiteralString(s, " 0ms: ")
|
2022-05-31 23:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var skipDurationRe = regexp.MustCompile(" [0-9.]+ms: ")
|
|
|
|
|
|
|
|
func areEqualJSONTracesSkipDuration(s1, s2 string) bool {
|
|
|
|
s1 = zeroJSONDurationsInTrace(s1)
|
|
|
|
s2 = zeroJSONDurationsInTrace(s2)
|
|
|
|
return s1 == s2
|
|
|
|
}
|
|
|
|
|
|
|
|
func zeroJSONDurationsInTrace(s string) string {
|
2024-07-17 10:18:38 +00:00
|
|
|
return skipJSONDurationRe.ReplaceAllLiteralString(s, `"duration_msec":0`)
|
2022-05-31 23:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var skipJSONDurationRe = regexp.MustCompile(`"duration_msec":[0-9.]+`)
|