Skip to content

Commit 3379e30

Browse files
authored
feat(v2fix): expand analyzer coverage and harden suggested fix generation (#4393)
1 parent 7196cbb commit 3379e30

23 files changed

+1709
-197
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2024 Datadog, Inc.
5+
6+
package main
7+
8+
import (
9+
"context"
10+
11+
"gopkg.in/DataDog/dd-trace-go.v1/appsec"
12+
)
13+
14+
func main() {
15+
ctx := context.Background()
16+
17+
// Login success event
18+
appsec.TrackUserLoginSuccessEvent(ctx, "user123", nil) // want `appsec login event functions have been renamed`
19+
20+
// Login failure event
21+
appsec.TrackUserLoginFailureEvent(ctx, "user123", false, nil) // want `appsec login event functions have been renamed`
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- appsec login event functions have been renamed (remove 'Event' suffix) --
2+
// Unless explicitly stated otherwise all files in this repository are licensed
3+
// under the Apache License Version 2.0.
4+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
5+
// Copyright 2024 Datadog, Inc.
6+
7+
package main
8+
9+
import (
10+
"context"
11+
12+
"gopkg.in/DataDog/dd-trace-go.v1/appsec"
13+
)
14+
15+
func main() {
16+
ctx := context.Background()
17+
18+
// Login success event
19+
appsec.TrackUserLoginSuccessEvent(ctx, "user123", nil) // want `appsec login event functions have been renamed`
20+
21+
// Login failure event
22+
appsec.TrackUserLoginFailureEvent(ctx, "user123", false, nil) // want `appsec login event functions have been renamed`
23+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2024 Datadog, Inc.
5+
6+
package main
7+
8+
import (
9+
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
10+
)
11+
12+
func main() {
13+
tracer.Start()
14+
defer tracer.Stop()
15+
16+
parent := tracer.StartSpan("parent")
17+
defer parent.Finish()
18+
19+
extraOpt := tracer.ResourceName("resource")
20+
21+
// Simple ChildOf usage
22+
child := tracer.StartSpan("child", tracer.ChildOf(parent.Context())) // want `use StartChild instead of StartSpan with ChildOf`
23+
defer child.Finish()
24+
25+
// ChildOf with additional options
26+
child2 := tracer.StartSpan("child2", tracer.ChildOf(parent.Context()), tracer.ResourceName("resource")) // want `use StartChild instead of StartSpan with ChildOf`
27+
defer child2.Finish()
28+
29+
// ChildOf with non-call option (variable)
30+
child3 := tracer.StartSpan("child3", tracer.ChildOf(parent.Context()), extraOpt) // want `use StartChild instead of StartSpan with ChildOf`
31+
defer child3.Finish()
32+
33+
// ChildOf with binary expression in option argument (now supported)
34+
child5 := tracer.StartSpan("child5", tracer.ChildOf(parent.Context()), tracer.ResourceName("a"+"b")) // want `use StartChild instead of StartSpan with ChildOf`
35+
defer child5.Finish()
36+
37+
// ChildOf with a SpanContext (not a Span) - diagnostic but no fix
38+
parentCtx := parent.Context()
39+
child6 := tracer.StartSpan("child6", tracer.ChildOf(parentCtx)) // want `use StartChild instead of StartSpan with ChildOf`
40+
defer child6.Finish()
41+
42+
// StartSpan with binary expression as opName - diagnostic but no fix
43+
suffix := "op"
44+
child7 := tracer.StartSpan("a"+suffix, tracer.ChildOf(parent.Context())) // want `use StartChild instead of StartSpan with ChildOf`
45+
defer child7.Finish()
46+
47+
// ChildOf passed via variadic (ellipsis applies to ChildOf itself) - should not be rewritten
48+
parentOpts := []tracer.StartSpanOption{
49+
tracer.ChildOf(parent.Context()),
50+
}
51+
child4 := tracer.StartSpan("child4", parentOpts...)
52+
defer child4.Finish()
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- use StartChild instead of StartSpan with ChildOf --
2+
// Unless explicitly stated otherwise all files in this repository are licensed
3+
// under the Apache License Version 2.0.
4+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
5+
// Copyright 2024 Datadog, Inc.
6+
7+
package main
8+
9+
import (
10+
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
11+
)
12+
13+
func main() {
14+
tracer.Start()
15+
defer tracer.Stop()
16+
17+
parent := tracer.StartSpan("parent")
18+
defer parent.Finish()
19+
20+
extraOpt := tracer.ResourceName("resource")
21+
22+
// Simple ChildOf usage
23+
child := parent.StartChild("child") // want `use StartChild instead of StartSpan with ChildOf`
24+
defer child.Finish()
25+
26+
// ChildOf with additional options
27+
child2 := parent.StartChild("child2", tracer.ResourceName("resource")) // want `use StartChild instead of StartSpan with ChildOf`
28+
defer child2.Finish()
29+
30+
// ChildOf with non-call option (variable)
31+
child3 := parent.StartChild("child3", extraOpt) // want `use StartChild instead of StartSpan with ChildOf`
32+
defer child3.Finish()
33+
34+
// ChildOf with binary expression in option argument (now supported)
35+
child5 := parent.StartChild("child5", tracer.ResourceName("a"+"b")) // want `use StartChild instead of StartSpan with ChildOf`
36+
defer child5.Finish()
37+
38+
// ChildOf with a SpanContext (not a Span) - diagnostic but no fix
39+
parentCtx := parent.Context()
40+
child6 := tracer.StartSpan("child6", tracer.ChildOf(parentCtx)) // want `use StartChild instead of StartSpan with ChildOf`
41+
defer child6.Finish()
42+
43+
// StartSpan with binary expression as opName - diagnostic but no fix
44+
suffix := "op"
45+
child7 := tracer.StartSpan("a"+suffix, tracer.ChildOf(parent.Context())) // want `use StartChild instead of StartSpan with ChildOf`
46+
defer child7.Finish()
47+
48+
// ChildOf passed via variadic (ellipsis applies to ChildOf itself) - should not be rewritten
49+
parentOpts := []tracer.StartSpanOption{
50+
tracer.ChildOf(parent.Context()),
51+
}
52+
child4 := tracer.StartSpan("child4", parentOpts...)
53+
defer child4.Finish()
54+
}

tools/v2fix/_stage/ddtracetypes/ddtracetypes.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
1212
)
1313

14+
const N = 2
15+
1416
func main() {
1517
var (
1618
_ ddtrace.FinishConfig // want `the declared type is in the ddtrace/tracer package now`
@@ -23,8 +25,27 @@ func main() {
2325
_ ddtrace.StartSpanOption // want `the declared type is in the ddtrace/tracer package now`
2426
_ ddtrace.Tracer // want `the declared type is in the ddtrace/tracer package now`
2527
_ time.Time
28+
29+
// Composite type tests: pointer, slice, array
30+
_ *ddtrace.Span // want `the declared type is in the ddtrace/tracer package now`
31+
_ []ddtrace.Span // want `the declared type is in the ddtrace/tracer package now`
32+
_ [3]ddtrace.SpanLink // want `the declared type is in the ddtrace/tracer package now`
33+
34+
// Non-literal array length: diagnostic emitted but no fix (preserves original formatting)
35+
_ [N + 1]ddtrace.Span // want `the declared type is in the ddtrace/tracer package now`
36+
37+
// Composite SpanContext types should NOT be migrated (exclusion applies to unwrapped base type)
38+
_ *ddtrace.SpanContext
39+
_ []ddtrace.SpanContext
40+
_ [2]ddtrace.SpanContext
2641
)
2742
}
2843

2944
func spanConsumer(_ ddtrace.Span) { // want `the declared type is in the ddtrace/tracer package now`
3045
}
46+
47+
func pointerConsumer(_ *ddtrace.Span) { // want `the declared type is in the ddtrace/tracer package now`
48+
}
49+
50+
func sliceConsumer(_ []ddtrace.Span) { // want `the declared type is in the ddtrace/tracer package now`
51+
}

tools/v2fix/_stage/ddtracetypes/ddtracetypes.go.golden

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
1313
)
1414

15+
const N = 2
16+
1517
func main() {
1618
var (
1719
_ tracer.FinishConfig // want `the declared type is in the ddtrace/tracer package now`
@@ -24,8 +26,27 @@ func main() {
2426
_ tracer.StartSpanOption // want `the declared type is in the ddtrace/tracer package now`
2527
_ tracer.Tracer // want `the declared type is in the ddtrace/tracer package now`
2628
_ time.Time
29+
30+
// Composite type tests: pointer, slice, array
31+
_ *tracer.Span // want `the declared type is in the ddtrace/tracer package now`
32+
_ []tracer.Span // want `the declared type is in the ddtrace/tracer package now`
33+
_ [3]tracer.SpanLink // want `the declared type is in the ddtrace/tracer package now`
34+
35+
// Non-literal array length: diagnostic emitted but no fix (preserves original formatting)
36+
_ [N + 1]ddtrace.Span // want `the declared type is in the ddtrace/tracer package now`
37+
38+
// Composite SpanContext types should NOT be migrated (exclusion applies to unwrapped base type)
39+
_ *ddtrace.SpanContext
40+
_ []ddtrace.SpanContext
41+
_ [2]ddtrace.SpanContext
2742
)
2843
}
2944

3045
func spanConsumer(_ tracer.Span) { // want `the declared type is in the ddtrace/tracer package now`
3146
}
47+
48+
func pointerConsumer(_ *tracer.Span) { // want `the declared type is in the ddtrace/tracer package now`
49+
}
50+
51+
func sliceConsumer(_ []tracer.Span) { // want `the declared type is in the ddtrace/tracer package now`
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2023 Datadog, Inc.
5+
6+
// This test verifies that functions with the same names as dd-trace-go v1 functions
7+
// but from different packages are NOT flagged for migration.
8+
package main
9+
10+
// Local type and function definitions that shadow dd-trace-go names
11+
type Span struct {
12+
Name string
13+
}
14+
15+
func WithServiceName(name string) string {
16+
return name
17+
}
18+
19+
func TraceID() uint64 {
20+
return 0
21+
}
22+
23+
func WithDogstatsdAddress(addr string) string {
24+
return addr
25+
}
26+
27+
func ServiceRule(service string, rate float64) string {
28+
return service
29+
}
30+
31+
func main() {
32+
// These should NOT be flagged - they're local functions, not from dd-trace-go v1
33+
_ = WithServiceName("test")
34+
_ = TraceID()
35+
_ = WithDogstatsdAddress("localhost:8125")
36+
_ = ServiceRule("myservice", 0.5)
37+
38+
// Local type should NOT be flagged
39+
var _ Span
40+
}

tools/v2fix/_stage/go.sum

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/v2fix/_stage/samplingrules/samplingrules.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func tags() map[string]string {
1616
func main() {
1717
_ = tracer.ServiceRule("test-service", 1.0) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`
1818
_ = tracer.NameRule("http.request", 1.0) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`
19+
_ = tracer.RateRule(0.5) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`
1920
_ = tracer.NameServiceRule("http.request", "test-service", 1.0) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`
2021
_ = tracer.NameServiceRule("http.*", "test-*", 1.0) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`
2122
_ = tracer.ServiceRule("other-service-1", 0.0) // want `a deprecated sampling rule constructor function should be replaced with a tracer.Rule{...} struct literal`

0 commit comments

Comments
 (0)