-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
137 lines (110 loc) · 3.18 KB
/
sensor.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
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020
package main
import (
"bufio"
"fmt"
"go/ast"
"html/template"
"io"
"regexp"
"strings"
"golang.org/x/tools/go/ast/astutil"
)
const instanaGoFileName = "instana_go_dependency.go"
var headLineRegexp = regexp.MustCompile(`^// Code generated by go-instana.*, DO NOT EDIT\.$`)
// lookupInstanaSensorInPackage searches for the first instana.Sensor instance available in the package
// scope and returns its name
func lookupInstanaSensorInPackage(pkg *ast.Package) string {
if n := lookupInstanaSensor(pkg.Scope); n != "" {
return n
}
for _, f := range pkg.Files {
if !astutil.UsesImport(f, SensorPackage) {
continue
}
if n := lookupInstanaSensor(f.Scope); n != "" {
return n
}
}
return ""
}
func lookupInstanaSensor(sc *ast.Scope) string {
if sc == nil {
return ""
}
for _, obj := range sc.Objects {
if obj.Kind != ast.Var {
continue
}
// Is this a var declaration?
valSpec, ok := obj.Decl.(*ast.ValueSpec)
if !ok {
continue
}
// Does it have type specified? If so, this might be a global sensor
// variable initialized later. We need to check whether it's an instana.Sensor
if valSpec.Type != nil {
if pkg, typ := extractSelectorPackageAndName(valSpec.Type); pkg == "instana" && typ == "Sensor" {
return obj.Name
}
}
// Inline initialization? Let's have a look if there is an instana.NewSensor*() in the values list
for i, val := range valSpec.Values {
if fnCall, ok := val.(*ast.CallExpr); ok {
pkg, fnName := extractSelectorPackageAndName(fnCall.Fun)
if pkg == "instana" && strings.HasPrefix(fnName, "NewSensor") {
return valSpec.Names[i].Name
}
}
}
}
return ""
}
var instanaGoTmpl = template.Must(template.New(instanaGoFileName).Parse(`// Code generated by go-instana, DO NOT EDIT.
package {{ .Package }}
import (
{{if .AddSensor}}instana "{{ .InstanaPackage }}"{{ end }}
{{ range .InstrumentationPackages }}
_ "{{ . }}"{{ end }}
)
{{if .AddSensor}}
var {{ .SensorName }} = instana.NewSensor(""){{ end }}
`))
type instanaGoTmplArgs struct {
Version string
Package string
InstanaPackage string
SensorName string
InstrumentationPackages []string
AddSensor bool
}
// writeInstanaGoFile puts the sensor initialization
// code inside it to the `instanaGoFileName` file.
func writeInstanaGoFile(wr io.Writer, pkgName string, addSensor bool, instrumentationPackages []string) (bool, error) {
if !addSensor && len(instrumentationPackages) == 0 {
return false, nil
}
if err := instanaGoTmpl.Execute(wr, instanaGoTmplArgs{
Package: pkgName,
InstanaPackage: SensorPackage,
SensorName: "__instanaSensor",
InstrumentationPackages: instrumentationPackages,
AddSensor: addSensor,
}); err != nil {
return false, fmt.Errorf("failed to write to file: %w", err)
}
return true, nil
}
func isGeneratedByGoInstana(r io.Reader) bool {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
return headLineRegexp.MatchString(line)
}
return false
}