Skip to content

Commit

Permalink
ddtrace/tracer: go tracer tests lint. (#2587)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyuanzhao3 authored Mar 4, 2024
1 parent cb92842 commit ccc12dc
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ddtrace/tracer/abandonedspans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestDebugAbandonedSpansOff(t *testing.T) {
t.Run("default", func(t *testing.T) {
assert.False(tracer.config.debugAbandonedSpans)
assert.Equal(time.Duration(0), tracer.config.spanTimeout)
expected := fmt.Sprintf("Abandoned spans logs enabled.")
expected := "Abandoned spans logs enabled."
s := tracer.StartSpan("operation", StartTime(spanStart))
time.Sleep(100 * time.Millisecond)
assert.NotContains(tp.Logs(), expected)
Expand Down
5 changes: 2 additions & 3 deletions ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestSpanFromContext(t *testing.T) {
assert.False(ok)
_, ok = span.(*traceinternal.NoopSpan)
assert.True(ok)
span, ok = SpanFromContext(nil)
span, ok = SpanFromContext(context.TODO())
assert.False(ok)
_, ok = span.(*traceinternal.NoopSpan)
assert.True(ok)
Expand Down Expand Up @@ -86,7 +86,6 @@ func TestStartSpanFromContextRace(t *testing.T) {

// Start 100 goroutines that create child spans with StartSpanFromContext in parallel,
// with a shared options slice. The child spans should get parented to the correct spans
const contextKey = "key"
const numContexts = 100
options := make([]StartSpanOption, 0, 3)
outputValues := make(chan uint64, numContexts)
Expand Down Expand Up @@ -153,7 +152,7 @@ func TestStartSpanFromNilContext(t *testing.T) {
_, _, _, stop := startTestTracer(t)
defer stop()

child, ctx := StartSpanFromContext(nil, "http.request")
child, ctx := StartSpanFromContext(context.TODO(), "http.request")
assert := assert.New(t)
// ensure the returned context works
assert.Nil(ctx.Value("not_found_key"))
Expand Down
6 changes: 3 additions & 3 deletions ddtrace/tracer/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func TestRulesSampler(t *testing.T) {
{ServiceRule("test-service", 1.0)},
{NameRule("http.request", 1.0)},
{NameServiceRule("http.request", "test-service", 1.0)},
{{Service: regexp.MustCompile("^test-"), Name: regexp.MustCompile("http\\..*"), Rate: 1.0}},
{{Service: regexp.MustCompile("^test-"), Name: regexp.MustCompile(`http\..*`), Rate: 1.0}},
{ServiceRule("other-service-1", 0.0), ServiceRule("other-service-2", 0.0), ServiceRule("test-service", 1.0)},
{TagsResourceRule(
map[string]*regexp.Regexp{"hostname": regexp.MustCompile("hn-[0-9]+")},
Expand Down Expand Up @@ -566,8 +566,8 @@ func TestRulesSampler(t *testing.T) {
{ServiceRule("toast-service", 1.0)},
{NameRule("grpc.request", 1.0)},
{NameServiceRule("http.request", "toast-service", 1.0)},
{{Service: regexp.MustCompile("^toast-"), Name: regexp.MustCompile("http\\..*"), Rate: 1.0}},
{{Service: regexp.MustCompile("^test-"), Name: regexp.MustCompile("grpc\\..*"), Rate: 1.0}},
{{Service: regexp.MustCompile("^toast-"), Name: regexp.MustCompile(`http\..*`), Rate: 1.0}},
{{Service: regexp.MustCompile("^test-"), Name: regexp.MustCompile(`grpc\..*`), Rate: 1.0}},
{ServiceRule("other-service-1", 0.0), ServiceRule("other-service-2", 0.0), ServiceRule("toast-service", 1.0)},
{TagsResourceRule(
map[string]*regexp.Regexp{"hostname": regexp.MustCompile("hn--1")},
Expand Down
1 change: 0 additions & 1 deletion ddtrace/tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ func (n *nilStringer) String() string {
}

type panicStringer struct {
s string
}

// String causes panic which SetTag should not handle.
Expand Down
49 changes: 20 additions & 29 deletions ddtrace/tracer/spancontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,47 +78,38 @@ func testAsyncSpanRace(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-done:
root.Finish()
for i := 0; i < 500; i++ {
for range root.(*span).Metrics {
// this range simulates iterating over the metrics map
// as we do when encoding msgpack upon flushing.
continue
}
<-done
root.Finish()
for i := 0; i < 500; i++ {
for range root.(*span).Metrics {
// this range simulates iterating over the metrics map
// as we do when encoding msgpack upon flushing.
continue
}
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-done:
root.Finish()
for i := 0; i < 500; i++ {
for range root.(*span).Meta {
// this range simulates iterating over the meta map
// as we do when encoding msgpack upon flushing.
continue
}
<-done
root.Finish()
for i := 0; i < 500; i++ {
for range root.(*span).Meta {
// this range simulates iterating over the meta map
// as we do when encoding msgpack upon flushing.
continue
}
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-done:
for i := 0; i < 50; i++ {
// to trigger the bug, the child should be created after the root was finished,
// as its being flushed
child, _ := StartSpanFromContext(ctx, "child", Tag(ext.SamplingPriority, ext.PriorityUserKeep))
child.Finish()
}
return
<-done
for i := 0; i < 50; i++ {
// to trigger the bug, the child should be created after the root was finished,
// as its being flushed
child, _ := StartSpanFromContext(ctx, "child", Tag(ext.SamplingPriority, ext.PriorityUserKeep))
child.Finish()
}
}()
// closing will attempt trigger the two goroutines at approximately the same time.
Expand Down
8 changes: 4 additions & 4 deletions ddtrace/tracer/sqlcomment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func TestExtractOpenTelemetryTraceInformation(t *testing.T) {
// open-telemetry supports 128 bit trace ids
traceID := "5bd66ef5095369c7b0d1f8f4bd33716a"
ss := "c532cb4098ac3dd2"
upper, err := strconv.ParseUint(traceID[:16], 16, 64)
lower, err := strconv.ParseUint(traceID[16:], 16, 64)
spanID, err := strconv.ParseUint(ss, 16, 64)
upper, _ := strconv.ParseUint(traceID[:16], 16, 64)
lower, _ := strconv.ParseUint(traceID[16:], 16, 64)
spanID, _ := strconv.ParseUint(ss, 16, 64)
ps := "1"
priority, err := strconv.Atoi(ps)
require.NoError(t, err)
Expand Down Expand Up @@ -257,7 +257,7 @@ func FuzzSpanContextFromTraceComment(f *testing.F) {
b.WriteString(ts)
ts = b.String()

traceIDUpper, err := strconv.ParseUint(ts[:16], 16, 64)
traceIDUpper, _ := strconv.ParseUint(ts[:16], 16, 64)
traceIDLower, err := strconv.ParseUint(ts[16:], 16, 64)
if err != nil {
t.Skip()
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/textmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ func TestEnvVars(t *testing.T) {
ddTag := strings.SplitN(headers[tracestateHeader], ",", 2)[0]
assert.Contains(ddTag, "s:2")
assert.Contains(ddTag, "s:2")
assert.Regexp(regexp.MustCompile("dd=[\\w:,]+"), ddTag)
assert.Regexp(regexp.MustCompile(`dd=[\w:,]+`), ddTag)
assert.LessOrEqual(len(ddTag), 256)
})
}
Expand Down
7 changes: 0 additions & 7 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2244,13 +2244,6 @@ func (w *testTraceWriter) flush() {

func (w *testTraceWriter) stop() {}

func (w *testTraceWriter) reset() {
w.mu.Lock()
w.flushed = w.flushed[:0]
w.buf = w.buf[:0]
w.mu.Unlock()
}

// Buffered returns the spans buffered by the writer.
func (w *testTraceWriter) Buffered() []*span {
w.mu.RLock()
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestLogWriter(t *testing.T) {
s.Metrics["-inf"] = math.Inf(-1)
h.add([]*span{s})
h.flush()
json := string(buf.Bytes())
json := buf.String()
assert.NotContains(json, `"nan":`)
assert.NotContains(json, `"+inf":`)
assert.NotContains(json, `"-inf":`)
Expand Down

0 comments on commit ccc12dc

Please sign in to comment.