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: dynamically load injector configuration #393

Merged
merged 9 commits into from
Nov 22, 2024
Merged
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: 0 additions & 1 deletion _integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ require (
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.23 // indirect
github.com/aws/aws-sdk-go-v2/service/cloudfront v1.41.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.176.0 // indirect
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.35.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions _integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ github.com/aws/aws-sdk-go-v2/service/cloudfront v1.41.0 h1:sLXpWohpuSh6fSvI7q/D5
github.com/aws/aws-sdk-go-v2/service/cloudfront v1.41.0/go.mod h1:GM6Olux4KAMUmRw0XgadfpN1cOpm5eWYZ31PAj59JSk=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.37.0 h1:qgDx1ChCsz5tSxok9hxWES30bt4koYM1Xub4ONuNYDU=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.37.0/go.mod h1:P+1rrWglInpWvnBpN0pH8jIIhkLkBaolkRVG4X9Kous=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.176.0 h1:fWhkSvaQqa5eWiRwBw10FUnk1YatAQ9We4GdGxKiCtg=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.176.0/go.mod h1:ISODge3zgdwOEa4Ou6WM9PKbxJWJ15DYKnr2bfmCAIA=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.93.2 h1:c6a19AjfhEXKlEX63cnlWtSQ4nzENihHZOG0I3wH6BE=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.93.2/go.mod h1:VX22JN3HQXDtQ3uS4h4TtM+K11vydq58tpHTlsm8TL8=
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.35.5 h1:O7UMjjX8eAM4eLs303VramU8DW4FzTUJz1EsQKkxqc0=
github.com/aws/aws-sdk-go-v2/service/eventbridge v1.35.5/go.mod h1:U1Wwh1TVfPHB8sbmBt3yqH2etdYERX1quammRvGWtXs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 h1:TToQNkvGguu209puTojY/ozlqy2d/SFNcoLIqTFi42g=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ package tools

import (
_ "github.com/DataDog/orchestrion"
_ "github.com/DataDog/orchestrion/instrument"
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/urfave/cli/v2 v2.27.5
golang.org/x/mod v0.22.0
golang.org/x/perf v0.0.0-20241112183634-aa2227201f71
golang.org/x/sync v0.9.0
golang.org/x/sys v0.27.0
golang.org/x/term v0.26.0
golang.org/x/tools v0.27.0
Expand Down Expand Up @@ -334,7 +335,6 @@ require (
golang.org/x/image v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
Expand Down
2 changes: 1 addition & 1 deletion instrument/orchestrion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2023-present Datadog, Inc.
---
# yaml-language-server: $schema=../_docs/static/schema.json
# yaml-language-server: $schema=../internal/injector/config/schema.json
meta:
name: github.com/DataDog/orchestrion/instrument
description: |-
Expand Down
4 changes: 0 additions & 4 deletions internal/injector/aspect/advice/advice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ package advice
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
)

const pkgPath = "github.com/DataDog/orchestrion/internal/injector/aspect/advice"

// Advice is the interface abstracting actual AST changes performed by
// injections.
type Advice interface {
// AsCode produces a jen.Code representation of the receiver.
AsCode() jen.Code

// AddedImports returns the list of import paths the receiver may introduce in
// modified code.
AddedImports() []string
Expand Down
5 changes: 0 additions & 5 deletions internal/injector/aspect/advice/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/dst"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -49,10 +48,6 @@ func (a *assignValue) AddedImports() []string {
return a.Template.AddedImports()
}

func (a *assignValue) AsCode() jen.Code {
return jen.Qual(pkgPath, "AssignValue").Call(a.Template.AsCode())
}

func (a *assignValue) Hash(h *fingerprint.Hasher) error {
return h.Named("assign-value", a.Template)
}
Expand Down
5 changes: 0 additions & 5 deletions internal/injector/aspect/advice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/dst"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -48,10 +47,6 @@ func (a *prependStatements) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (a *prependStatements) AsCode() jen.Code {
return jen.Qual(pkgPath, "PrependStmts").Call(a.Template.AsCode())
}

func (a *prependStatements) Hash(h *fingerprint.Hasher) error {
return h.Named("prepend-statements", a.Template)
}
Expand Down
15 changes: 0 additions & 15 deletions internal/injector/aspect/advice/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/join"
"github.com/dave/dst"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -98,16 +97,6 @@ func (a *appendArgs) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (a *appendArgs) AsCode() jen.Code {
return jen.Qual(pkgPath, "AppendArgs").CallFunc(func(group *jen.Group) {
group.Line().Add(a.TypeName.AsCode())
for _, t := range a.Templates {
group.Line().Add(t.AsCode())
}
group.Empty().Line()
})
}

func (a *appendArgs) AddedImports() []string {
imports := make([]string, 0, len(a.Templates)+1)
if argTypeImportPath := a.TypeName.ImportPath(); argTypeImportPath != "" {
Expand Down Expand Up @@ -155,10 +144,6 @@ func (r *redirectCall) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (r *redirectCall) AsCode() jen.Code {
return jen.Qual(pkgPath, "ReplaceFunction").Call(jen.Lit(r.ImportPath), jen.Lit(r.Name))
}

func (r *redirectCall) Hash(h *fingerprint.Hasher) error {
return h.Named("replace-function", fingerprint.String(r.ImportPath), fingerprint.String(r.Name))
}
Expand Down
31 changes: 0 additions & 31 deletions internal/injector/aspect/advice/code/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"go/token"
"sort"
"strconv"
"strings"
"text/template"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/DataDog/orchestrion/internal/version"
"github.com/dave/dst"
"github.com/dave/dst/dstutil"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -192,35 +190,6 @@ func (t *Template) processImports(ctx context.AdviceContext, node dst.Decl) dst.
return res
}

func (t *Template) AsCode() jen.Code {
var lang *jen.Statement
if langStr := t.Lang.String(); langStr != "" {
lang = jen.Qual("github.com/DataDog/orchestrion/internal/injector/aspect/context", "MustParseGoLangVersion").Call(jen.Lit(langStr))
} else {
lang = jen.Qual("github.com/DataDog/orchestrion/internal/injector/aspect/context", "GoLangVersion").Block()
}

return jen.Qual("github.com/DataDog/orchestrion/internal/injector/aspect/advice/code", "MustTemplate").Call(
jen.Line().Lit(t.Source),
jen.Line().Map(jen.String()).String().ValuesFunc(func(g *jen.Group) {
// We sort the keys so the generated code order is consistent...
keys := make([]string, 0, len(t.Imports))
for k := range t.Imports {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
v := t.Imports[k]
g.Line().Add(jen.Lit(k).Op(":").Lit(v))
}
g.Empty().Line()
}),
jen.Line().Add(lang),
jen.Empty().Line(),
)
}

func (t *Template) Hash(h *fingerprint.Hasher) error {
return h.Named(
"template",
Expand Down
5 changes: 0 additions & 5 deletions internal/injector/aspect/advice/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package advice
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand All @@ -25,10 +24,6 @@ func (a addBlankImport) Apply(ctx context.AdviceContext) (bool, error) {
return added, nil
}

func (a addBlankImport) AsCode() jen.Code {
return jen.Qual(pkgPath, "AddBlankImport").Call(jen.Lit(string(a)))
}

func (a addBlankImport) AddedImports() []string {
return []string{string(a)}
}
Expand Down
16 changes: 0 additions & 16 deletions internal/injector/aspect/advice/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
package advice

import (
"sort"

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -53,19 +50,6 @@ func (a injectDeclarations) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (a injectDeclarations) AsCode() jen.Code {
return jen.Qual(pkgPath, "InjectDeclarations").Call(
a.Template.AsCode(),
jen.Index().String().ValuesFunc(func(g *jen.Group) {
sort.Strings(a.Links)
for _, link := range a.Links {
g.Line().Lit(link)
}
g.Line()
}),
)
}

func (a injectDeclarations) Hash(h *fingerprint.Hasher) error {
return h.Named(
"inject-declarations",
Expand Down
5 changes: 0 additions & 5 deletions internal/injector/aspect/advice/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/DataDog/orchestrion/internal/injector/aspect/join"
"github.com/dave/dst"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -49,10 +48,6 @@ func (a *addStructField) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (a *addStructField) AsCode() jen.Code {
return jen.Qual(pkgPath, "AddStructField").Call(jen.Lit(a.Name), a.TypeName.AsCode())
}

func (a *addStructField) Hash(h *fingerprint.Hasher) error {
return h.Named("add-struct-field", fingerprint.String(a.Name), a.TypeName)
}
Expand Down
5 changes: 0 additions & 5 deletions internal/injector/aspect/advice/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/dst"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -53,10 +52,6 @@ func (a *wrapExpression) Apply(ctx context.AdviceContext) (bool, error) {
return true, nil
}

func (a *wrapExpression) AsCode() jen.Code {
return jen.Qual(pkgPath, "WrapExpression").Call(a.Template.AsCode())
}

func (a *wrapExpression) Hash(h *fingerprint.Hasher) error {
return h.Named("wrap-expression", a.Template)
}
Expand Down
32 changes: 20 additions & 12 deletions internal/injector/aspect/aspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/advice"
"github.com/DataDog/orchestrion/internal/injector/aspect/join"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand All @@ -28,17 +27,6 @@ type Aspect struct {
ID string
}

func (a *Aspect) AsCode() (jp jen.Code, adv jen.Code) {
jp = a.JoinPoint.AsCode()
adv = jen.Index().Qual("github.com/DataDog/orchestrion/internal/injector/aspect/advice", "Advice").ValuesFunc(func(g *jen.Group) {
for _, a := range a.Advice {
g.Line().Add(a.AsCode())
}
g.Empty().Line()
})
return
}

func (a *Aspect) Hash(h *fingerprint.Hasher) error {
return h.Named(
"aspect",
Expand Down Expand Up @@ -67,6 +55,26 @@ func (a *Aspect) AddedImports() (imports []string) {
return
}

// InjectedPaths returns the list of import paths that may be injected by the
// supplied list of aspects. The output list is not sorted in any particular way
// but does not contain duplicted entries.
func InjectedPaths(list []*Aspect) []string {
var res []string
dedup := make(map[string]struct{})

for _, a := range list {
for _, path := range a.AddedImports() {
if _, dup := dedup[path]; dup {
continue
}
dedup[path] = struct{}{}
res = append(res, path)
}
}

return res
}

func (a *Aspect) UnmarshalYAML(node *yaml.Node) error {
var ti struct {
JoinPoint yaml.Node `yaml:"join-point"`
Expand Down
16 changes: 0 additions & 16 deletions internal/injector/aspect/join/all-of.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package join
import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

Expand All @@ -35,21 +34,6 @@ func (o allOf) Matches(ctx context.AspectContext) bool {
return len(o) > 0
}

func (o allOf) AsCode() jen.Code {
if len(o) == 1 {
return o[0].AsCode()
}

return jen.Qual(pkgPath, "AllOf").CallFunc(func(g *jen.Group) {
if len(o) > 0 {
for _, candidate := range o {
g.Line().Add(candidate.AsCode())
}
g.Line().Empty()
}
})
}

func (o allOf) Hash(h *fingerprint.Hasher) error {
return h.Named("all-of", fingerprint.List[Point](o))
}
Expand Down
Loading
Loading