-
Notifications
You must be signed in to change notification settings - Fork 500
/
context_propagation.go
202 lines (190 loc) · 5.37 KB
/
context_propagation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package lib // import "go.opentelemetry.io/contrib/instrgen/lib"
import (
"fmt"
"go/ast"
"golang.org/x/tools/go/packages"
)
func isFunPartOfCallGraph(fun FuncDescriptor, callgraph map[FuncDescriptor][]FuncDescriptor) bool {
// TODO this is not optimap o(n)
for k, v := range callgraph {
if k.TypeHash() == fun.TypeHash() {
return true
}
for _, e := range v {
if fun.TypeHash() == e.TypeHash() {
return true
}
}
}
return false
}
// ContextPropagationPass.
type ContextPropagationPass struct{}
// Execute.
func (pass *ContextPropagationPass) Execute(
node *ast.File,
analysis *PackageAnalysis,
pkg *packages.Package,
pkgs []*packages.Package,
) []Import {
var imports []Import
addImports := false
// below variable is used
// when callexpr is inside var decl
// instead of functiondecl
currentFun := FuncDescriptor{}
emitEmptyContext := func(callExpr *ast.CallExpr, fun FuncDescriptor, ctxArg *ast.Ident) {
addImports = true
if currentFun != (FuncDescriptor{}) {
visited := map[FuncDescriptor]bool{}
if isPath(analysis.Callgraph, currentFun, analysis.RootFunctions[0], visited) {
callExpr.Args = append([]ast.Expr{ctxArg}, callExpr.Args...)
} else {
contextTodo := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{
Name: "__atel_context",
},
Sel: &ast.Ident{
Name: "TODO",
},
},
Lparen: 62,
Ellipsis: 0,
}
callExpr.Args = append([]ast.Expr{contextTodo}, callExpr.Args...)
}
return
}
contextTodo := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{
Name: "__atel_context",
},
Sel: &ast.Ident{
Name: "TODO",
},
},
Lparen: 62,
Ellipsis: 0,
}
callExpr.Args = append([]ast.Expr{contextTodo}, callExpr.Args...)
}
emitCallExpr := func(ident *ast.Ident, n ast.Node, ctxArg *ast.Ident, pkgPath string) {
if callExpr, ok := n.(*ast.CallExpr); ok {
funId := pkgPath + "." + pkg.TypesInfo.Uses[ident].Name()
fun := FuncDescriptor{
Id: funId,
DeclType: pkg.TypesInfo.Uses[ident].Type().String(),
CustomInjection: false,
}
found := analysis.FuncDecls[fun]
// inject context parameter only
// to these functions for which function decl
// exists
if found {
visited := map[FuncDescriptor]bool{}
if isPath(analysis.Callgraph, fun, analysis.RootFunctions[0], visited) {
fmt.Println("\t\t\tContextPropagation FuncCall:", funId, pkg.TypesInfo.Uses[ident].Type().String())
emitEmptyContext(callExpr, fun, ctxArg)
}
}
}
}
ast.Inspect(node, func(n ast.Node) bool {
ctxArg := &ast.Ident{
Name: "__atel_child_tracing_ctx",
}
ctxField := &ast.Field{
Names: []*ast.Ident{
{
Name: "__atel_tracing_ctx",
},
},
Type: &ast.SelectorExpr{
X: &ast.Ident{
Name: "__atel_context",
},
Sel: &ast.Ident{
Name: "Context",
},
},
}
switch xNode := n.(type) {
case *ast.FuncDecl:
pkgPath := GetPkgPathForFunction(pkg, pkgs, xNode, analysis.Interfaces)
funId := pkgPath + "." + pkg.TypesInfo.Defs[xNode.Name].Name()
fun := FuncDescriptor{
Id: funId,
DeclType: pkg.TypesInfo.Defs[xNode.Name].Type().String(),
CustomInjection: false,
}
currentFun = fun
// inject context only
// functions available in the call graph
if !isFunPartOfCallGraph(fun, analysis.Callgraph) {
break
}
if Contains(analysis.RootFunctions, fun) {
break
}
visited := map[FuncDescriptor]bool{}
if isPath(analysis.Callgraph, fun, analysis.RootFunctions[0], visited) {
fmt.Println("\t\t\tContextPropagation FuncDecl:", funId,
pkg.TypesInfo.Defs[xNode.Name].Type().String())
addImports = true
xNode.Type.Params.List = append([]*ast.Field{ctxField}, xNode.Type.Params.List...)
}
case *ast.CallExpr:
if ident, ok := xNode.Fun.(*ast.Ident); ok {
if pkg.TypesInfo.Uses[ident] == nil {
return false
}
pkgPath := GetPkgNameFromUsesTable(pkg, ident)
emitCallExpr(ident, n, ctxArg, pkgPath)
}
if sel, ok := xNode.Fun.(*ast.SelectorExpr); ok {
if pkg.TypesInfo.Uses[sel.Sel] == nil {
return false
}
pkgPath := GetPkgNameFromUsesTable(pkg, sel.Sel)
if sel.X != nil {
pkgPath = GetSelectorPkgPath(sel, pkg, pkgPath)
}
emitCallExpr(sel.Sel, n, ctxArg, pkgPath)
}
case *ast.TypeSpec:
iname := xNode.Name
iface, ok := xNode.Type.(*ast.InterfaceType)
if !ok {
return true
}
for _, method := range iface.Methods.List {
funcType, ok := method.Type.(*ast.FuncType)
if !ok {
return true
}
visited := map[FuncDescriptor]bool{}
pkgPath := GetPkgNameFromDefsTable(pkg, method.Names[0])
funId := pkgPath + "." + iname.Name + "." + pkg.TypesInfo.Defs[method.Names[0]].Name()
fun := FuncDescriptor{
Id: funId,
DeclType: pkg.TypesInfo.Defs[method.Names[0]].Type().String(),
CustomInjection: false,
}
if isPath(analysis.Callgraph, fun, analysis.RootFunctions[0], visited) {
fmt.Println("\t\t\tContext Propagation InterfaceType", fun.Id, fun.DeclType)
addImports = true
funcType.Params.List = append([]*ast.Field{ctxField}, funcType.Params.List...)
}
}
}
return true
})
if addImports {
imports = append(imports, Import{"__atel_context", "context", Add})
}
return imports
}