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

feat(injector): early filtering of aspects #435

Merged
merged 24 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
23 changes: 23 additions & 0 deletions internal/injector/aspect/join/all-of.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package join
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"gopkg.in/yaml.v3"
)

Expand All @@ -24,6 +25,28 @@ func (o allOf) ImpliesImported() (list []string) {
return
}

func (o allOf) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
sum := may.Match
for _, candidate := range o {
sum = sum.And(candidate.PackageMayMatch(ctx))
if sum == may.NeverMatch {
return may.NeverMatch
}
}
return sum
}

func (o allOf) FileMayMatch(ctx *may.FileContext) may.MatchType {
sum := may.Match
for _, candidate := range o {
sum = sum.And(candidate.FileMayMatch(ctx))
if sum == may.NeverMatch {
return may.NeverMatch
}
}
return sum
}

func (o allOf) Matches(ctx context.AspectContext) bool {
for _, candidate := range o {
if !candidate.Matches(ctx) {
Expand Down
9 changes: 9 additions & 0 deletions internal/injector/aspect/join/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"gopkg.in/yaml.v3"

_ "embed" // For go:embed
Expand All @@ -23,6 +24,14 @@
return nil
}

func (configuration) PackageMayMatch(_ *may.PackageContext) may.MatchType {
return may.Unknown

Check warning on line 28 in internal/injector/aspect/join/configuration.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/join/configuration.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}

func (configuration) FileMayMatch(_ *may.FileContext) may.MatchType {
return may.Unknown

Check warning on line 32 in internal/injector/aspect/join/configuration.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/join/configuration.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}

func (jp configuration) Matches(ctx context.AspectContext) bool {
for k, v := range jp {
cfg, found := ctx.Config(k)
Expand Down
17 changes: 17 additions & 0 deletions internal/injector/aspect/join/declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"github.com/dave/dst"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -56,6 +57,14 @@ func (i *declarationOf) ImpliesImported() []string {
return []string{i.ImportPath}
}

func (i *declarationOf) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
return ctx.PackageImports(i.ImportPath)
}

func (i *declarationOf) FileMayMatch(ctx *may.FileContext) may.MatchType {
return ctx.FileContains(i.Name)
}

func (i *declarationOf) Hash(h *fingerprint.Hasher) error {
return h.Named("declaration-of", fingerprint.String(i.ImportPath), fingerprint.String(i.Name))
}
Expand All @@ -68,6 +77,14 @@ func ValueDeclaration(typeName TypeName) *valueDeclaration {
return &valueDeclaration{typeName}
}

func (i *valueDeclaration) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
return ctx.PackageImports(i.TypeName.ImportPath())
}

func (*valueDeclaration) FileMayMatch(_ *may.FileContext) may.MatchType {
return may.Unknown
}

func (i *valueDeclaration) Matches(ctx context.AspectContext) bool {
parent := ctx.Chain().Parent()
if parent == nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/injector/aspect/join/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"github.com/dave/dst"
"gopkg.in/yaml.v3"
)
Expand All @@ -22,6 +23,14 @@ func Directive(name string) directive {
return directive(name)
}

func (directive) PackageMayMatch(_ *may.PackageContext) may.MatchType {
return may.Unknown
}

func (d directive) FileMayMatch(ctx *may.FileContext) may.MatchType {
return ctx.FileContains("//" + string(d))
}

func (d directive) Matches(ctx context.AspectContext) bool {
return d.matchesChain(ctx.Chain())
}
Expand Down
9 changes: 9 additions & 0 deletions internal/injector/aspect/join/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"github.com/dave/dst"
"gopkg.in/yaml.v3"
)
Expand All @@ -28,6 +29,14 @@ func (i *functionCall) ImpliesImported() []string {
return []string{i.ImportPath}
}

func (i *functionCall) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
return ctx.PackageImports(i.ImportPath)
}

func (i *functionCall) FileMayMatch(ctx *may.FileContext) may.MatchType {
return ctx.FileContains(i.Name)
}

func (i *functionCall) Matches(ctx context.AspectContext) bool {
call, ok := ctx.Node().(*dst.CallExpr)
if !ok {
Expand Down
80 changes: 78 additions & 2 deletions internal/injector/aspect/join/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"github.com/dave/dst"
"gopkg.in/yaml.v3"
)
Expand All @@ -27,6 +28,10 @@
fingerprint.Hashable

impliesImported() []string

packageMayMatch(ctx *may.PackageContext) may.MatchType
fileMayMatch(ctx *may.FileContext) may.MatchType

evaluate(functionInformation) bool
}

Expand All @@ -48,6 +53,28 @@
return
}

func (s *functionDeclaration) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
sum := may.Match
for _, candidate := range s.Options {
sum = sum.And(candidate.packageMayMatch(ctx))
if sum == may.NeverMatch {
return may.NeverMatch
}
}
return sum
}

func (s *functionDeclaration) FileMayMatch(ctx *may.FileContext) may.MatchType {
sum := may.Match
for _, candidate := range s.Options {
sum = sum.And(candidate.fileMayMatch(ctx))
if sum == may.NeverMatch {
return may.NeverMatch
}
}
return sum
}

func (s *functionDeclaration) Matches(ctx context.AspectContext) bool {
info := functionInformation{
ImportPath: ctx.ImportPath(),
Expand Down Expand Up @@ -88,6 +115,14 @@
return nil
}

func (functionName) packageMayMatch(_ *may.PackageContext) may.MatchType {
return may.Unknown
}

func (fo functionName) fileMayMatch(ctx *may.FileContext) may.MatchType {
return ctx.FileContains(string(fo))
}

func (fo functionName) evaluate(info functionInformation) bool {
return info.Name == string(fo)
}
Expand All @@ -107,6 +142,27 @@
return &signature{Arguments: args, Results: ret}
}

func (fo *signature) packageMayMatch(ctx *may.PackageContext) may.MatchType {
sum := may.Match
for _, candidate := range fo.Arguments {
sum = sum.And(ctx.PackageImports(candidate.ImportPath()))
if sum == may.NeverMatch {
return may.NeverMatch
}

Check warning on line 151 in internal/injector/aspect/join/function.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/join/function.go#L150-L151

Added lines #L150 - L151 were not covered by tests
}
for _, candidate := range fo.Results {
sum = sum.And(ctx.PackageImports(candidate.ImportPath()))
if sum == may.NeverMatch {
return may.NeverMatch
}

Check warning on line 157 in internal/injector/aspect/join/function.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/join/function.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}
return sum
}

func (*signature) fileMayMatch(_ *may.FileContext) may.MatchType {
return may.Unknown
}

func (fo *signature) impliesImported() (list []string) {
for _, tn := range fo.Arguments {
if path := tn.ImportPath(); path != "" {
Expand Down Expand Up @@ -169,12 +225,24 @@
return &receiver{typeName}
}

func (fo *receiver) packageMayMatch(ctx *may.PackageContext) may.MatchType {
if ctx.ImportPath == fo.TypeName.ImportPath() {
return may.Match
}

return may.NeverMatch
}

func (fo *receiver) fileMayMatch(ctx *may.FileContext) may.MatchType {
return ctx.FileContains(fo.TypeName.Name())
}

func (fo *receiver) evaluate(info functionInformation) bool {
return info.Receiver != nil && fo.TypeName.MatchesDefinition(info.Receiver, info.ImportPath)
}

func (*receiver) impliesImported() []string {
return nil
func (fo *receiver) impliesImported() []string {
return []string{fo.TypeName.ImportPath()}
}

func (fo *receiver) Hash(h *fingerprint.Hasher) error {
Expand All @@ -197,6 +265,14 @@
return s.Function.ImpliesImported()
}

func (s *functionBody) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
return s.Function.PackageMayMatch(ctx)
}

func (s *functionBody) FileMayMatch(ctx *may.FileContext) may.MatchType {
return s.Function.FileMayMatch(ctx)
}

func (s *functionBody) Matches(ctx context.AspectContext) bool {
parent := ctx.Parent()
if parent == nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/injector/aspect/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"github.com/dave/dst"
)

Expand All @@ -23,6 +24,12 @@ type Point interface {
// imported if the join point matches.
ImpliesImported() []string

// PackageMayMatch determines whether the join point may match the given package data from the importcfg file.
PackageMayMatch(ctx *may.PackageContext) may.MatchType

// FileMayMatch determines whether the join point may match the given file raw content
FileMayMatch(ctx *may.FileContext) may.MatchType

// Matches determines whether the injection should be performed on the given
// node or not. The node's ancestry is also provided to allow Point to make
// decisions based on parent nodes.
Expand Down Expand Up @@ -113,7 +120,7 @@ func (n TypeName) Matches(node dst.Expr) bool {
}
}

// MacthesDefinition determines whether the provided node matches the definition
// MatchesDefinition determines whether the provided node matches the definition
eliottness marked this conversation as resolved.
Show resolved Hide resolved
// of this TypeName. The `importPath` argument determines the context in which
// the assertion is made.
func (n TypeName) MatchesDefinition(node dst.Expr, importPath string) bool {
Expand Down
9 changes: 9 additions & 0 deletions internal/injector/aspect/join/not.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package join
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"gopkg.in/yaml.v3"
)

Expand All @@ -23,6 +24,14 @@ func (not) ImpliesImported() []string {
return nil
}

func (n not) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
return n.JoinPoint.PackageMayMatch(ctx).Not()
}

func (n not) FileMayMatch(ctx *may.FileContext) may.MatchType {
return n.JoinPoint.FileMayMatch(ctx).Not()
}

func (n not) Matches(ctx context.AspectContext) bool {
return !n.JoinPoint.Matches(ctx)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/injector/aspect/join/one-of.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package join
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/may"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -36,6 +37,28 @@ func (o oneOf) ImpliesImported() []string {
return list
}

func (o oneOf) PackageMayMatch(ctx *may.PackageContext) may.MatchType {
sum := may.NeverMatch
for _, candidate := range o {
sum = sum.Or(candidate.PackageMayMatch(ctx))
if sum == may.Match {
return may.Match
}
}
return sum
}

func (o oneOf) FileMayMatch(ctx *may.FileContext) may.MatchType {
sum := may.NeverMatch
for _, candidate := range o {
sum = sum.Or(candidate.FileMayMatch(ctx))
if sum == may.Match {
return may.Match
}
}
return sum
}

func (o oneOf) Matches(ctx context.AspectContext) bool {
for _, candidate := range o {
if candidate.Matches(ctx) {
Expand Down
Loading
Loading