Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: [v2] add process_contribs tool #3323

Draft
wants to merge 2 commits into
base: v2-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ use (
./tools/fixmodules
./tools/v2check
./tools/v2check/_stage
./tools/process_contribs
)
522 changes: 522 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions instrumentation/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ func (i *Instrumentation) HTTPHeadersAsTags() HeaderTags {
func (i *Instrumentation) ActiveSpanKey() any {
return internal.ActiveSpanKey
}

func (i *Instrumentation) Disabled() bool {
// TODO
// either:
// internal.BoolEnv("DD_TRACE_"+i.info.EnvVarPrefix+"_INTEGRATION_DISABLED", false)
// or:
// check if internal.SliceEnv("DD_TRACE_DISABLED_INTEGRATIONS") contains i.pkg
return false
}
19 changes: 19 additions & 0 deletions tools/process_contribs/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/DataDog/dd-trace-go/v2/tools/process_contribs

go 1.23.0

require (
github.com/dave/dst v0.27.3
github.com/hashicorp/go-multierror v1.1.1
github.com/stretchr/testify v1.10.0
golang.org/x/tools v0.1.12
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
26 changes: 26 additions & 0 deletions tools/process_contribs/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/dave/dst v0.27.3 h1:P1HPoMza3cMEquVf9kKy8yXsFirry4zEnWOdYPOoIzY=
github.com/dave/dst v0.27.3/go.mod h1:jHh6EOibnHgcUW3WjKHisiooEkYwqpHLBSX1iOBhEyc=
github.com/dave/jennifer v1.5.0 h1:HmgPN93bVDpkQyYbqhCHj5QlgvUkvEOzMyEvKLgCRrg=
github.com/dave/jennifer v1.5.0/go.mod h1:4MnyiFIlZS3l5tSDn8VnzE6ffAhYBMB2SZntBsZGUok=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88 changes: 88 additions & 0 deletions tools/process_contribs/internal/comment/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package comment

import (
"fmt"
"regexp"
)

var entrypointCommentRegex = regexp.MustCompile(`^(?:\/\/)?\s*ddtrace:entrypoint:(\S+)(?:\s+(.*))?$`)

type Comment struct {
Command string
Arguments map[string]string
}

func (c Comment) String() string {
return fmt.Sprintf("command: %s | arguments: %v", c.Command, c.Arguments)
}

func ParseComment(s string) (Comment, bool) {
// Match the command
match := entrypointCommentRegex.FindStringSubmatch(s)
if match == nil {
return Comment{}, false
}

// Extract command and named arguments
command := match[1]
namedArgs := parseNamedArgs(match[2])
namedArgs["__raw_args"] = match[2]

entry := Comment{
Command: command,
Arguments: namedArgs,
}
return entry, true
}

func parseNamedArgs(raw string) map[string]string {
namedArgs := make(map[string]string)

var key, value string
inQuotes := false
readingValue := false

for i := 0; i < len(raw); i++ {
char := raw[i]

switch {
case char == '"' && readingValue:
// Toggle quote state when inside a value
inQuotes = !inQuotes

case char == ':' && !readingValue:
// Found key-value separator
readingValue = true

case char == ' ' && !inQuotes:
// End of a key-value pair (only if not inside quotes)
if key != "" && value != "" {
namedArgs[key] = value
}
key, value = "", ""
readingValue = false

default:
// Append to key or value
if readingValue {
value += string(char)
} else {
key += string(char)
}
}
}

// Store last key-value pair
if key != "" && value != "" {
namedArgs[key] = value
}

// Remove surrounding quotes from values
for k, v := range namedArgs {
if len(v) > 1 && v[0] == '"' && v[len(v)-1] == '"' {
namedArgs[k] = v[1 : len(v)-1]
}
}

return namedArgs
}
76 changes: 76 additions & 0 deletions tools/process_contribs/internal/comment/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package comment_test

import (
"github.com/DataDog/dd-trace-go/v2/tools/process_contribs/internal/comment"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseComment(t *testing.T) {
testCases := []struct {
name string
input string
want comment.Comment
wantOk bool
}{
{
name: "parameters",
input: "ddtrace:entrypoint:wrap-custom-type skip-methods:WithContext",
want: comment.Comment{
Command: "wrap-custom-type",
Arguments: map[string]string{"skip-methods": "WithContext", "__raw_args": "skip-methods:WithContext"},
},
wantOk: true,
},
{
name: "parameters_with_colon",
input: `ddtrace:entrypoint:wrap-custom-type config:"value:with:colons" another:arg`,
want: comment.Comment{
Command: "wrap-custom-type",
Arguments: map[string]string{"config": "value:with:colons", "another": "arg", "__raw_args": "config:\"value:with:colons\" another:arg"},
},
wantOk: true,
},
{
name: "parameters_with_space",
input: `ddtrace:entrypoint:test arg1:"a value with spaces" key2:value2`,
want: comment.Comment{
Command: "test",
Arguments: map[string]string{"arg1": "a value with spaces", "key2": "value2", "__raw_args": "arg1:\"a value with spaces\" key2:value2"},
},
wantOk: true,
},
{
name: "invalid_format",
input: "invalid format",
want: comment.Comment{},
wantOk: false,
},
{
name: "no_arguments",
input: "ddtrace:entrypoint:wrap-custom-type",
want: comment.Comment{
Command: "wrap-custom-type",
Arguments: map[string]string{"__raw_args": ""},
},
wantOk: true,
},
{
name: "with_comment_prefix_and_args",
input: "//ddtrace:entrypoint:wrap-custom-type skip-methods:WithContext",
want: comment.Comment{
Command: "wrap-custom-type",
Arguments: map[string]string{"skip-methods": "WithContext", "__raw_args": "skip-methods:WithContext"},
},
wantOk: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, ok := comment.ParseComment(tc.input)
assert.Equal(t, tc.wantOk, ok)
assert.Equal(t, tc.want, result)
})
}
}
Loading
Loading