Skip to content

Commit c8de83a

Browse files
committed
add detection for Golang applications
1 parent 0e3b1f5 commit c8de83a

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

containers/container.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {
327327
if appType != "" {
328328
appTypes[appType] = struct{}{}
329329
}
330+
if process.isGolangApp {
331+
appTypes["golang"] = struct{}{}
332+
}
330333
switch {
331334
case isJvm(cmdline):
332335
jvm, jMetrics := jvmMetrics(pid)
@@ -1072,7 +1075,9 @@ func (c *Container) attachTlsUprobes(tracer *ebpftracer.Tracer, pid uint32) {
10721075
p.openSslUprobesChecked = true
10731076
}
10741077
if !p.goTlsUprobesChecked {
1075-
p.uprobes = append(p.uprobes, tracer.AttachGoTlsUprobes(pid)...)
1078+
uprobes, isGolangApp := tracer.AttachGoTlsUprobes(pid)
1079+
p.isGolangApp = isGolangApp
1080+
p.uprobes = append(p.uprobes, uprobes...)
10761081
p.goTlsUprobesChecked = true
10771082
}
10781083
}

containers/process.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Process struct {
2222
cancelFunc context.CancelFunc
2323

2424
dotNetMonitor *DotNetMonitor
25+
isGolangApp bool
2526

2627
uprobes []link.Link
2728
goTlsUprobesChecked bool

ebpftracer/tls.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ func (t *Tracer) AttachOpenSslUprobes(pid uint32) []link.Link {
108108
return links
109109
}
110110

111-
func (t *Tracer) AttachGoTlsUprobes(pid uint32) []link.Link {
111+
func (t *Tracer) AttachGoTlsUprobes(pid uint32) ([]link.Link, bool) {
112+
isGolangApp := false
112113
if t.disableL7Tracing {
113-
return nil
114+
return nil, isGolangApp
114115
}
115116

116117
path := proc.Path(pid, "exe")
@@ -133,53 +134,54 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) []link.Link {
133134
bi, err := buildinfo.ReadFile(path)
134135
if err != nil {
135136
log("failed to read build info", err)
136-
return nil
137+
return nil, isGolangApp
137138
}
139+
isGolangApp = true
138140

139141
name, err = os.Readlink(path)
140142
if err != nil {
141143
log("failed to read name", err)
142-
return nil
144+
return nil, isGolangApp
143145
}
144146
version = strings.Replace(bi.GoVersion, "go", "v", 1)
145147
if semver.Compare(version, minSupportedGoVersion) < 0 {
146148
log(fmt.Sprintf("go_versions below %s are not supported", minSupportedGoVersion), nil)
147-
return nil
149+
return nil, isGolangApp
148150
}
149151

150152
ef, err := elf.Open(path)
151153
if err != nil {
152154
log("failed to open as elf binary", err)
153-
return nil
155+
return nil, isGolangApp
154156
}
155157
defer ef.Close()
156158

157159
symbols, err := ef.Symbols()
158160
if err != nil {
159161
if errors.Is(err, elf.ErrNoSymbols) {
160162
log("no symbol section", nil)
161-
return nil
163+
return nil, isGolangApp
162164
}
163165
log("failed to read symbols", err)
164-
return nil
166+
return nil, isGolangApp
165167
}
166168

167169
textSection := ef.Section(".text")
168170
if textSection == nil {
169171
log("no text section", nil)
170-
return nil
172+
return nil, isGolangApp
171173
}
172174
textSectionData, err := textSection.Data()
173175
if err != nil {
174176
log("failed to read text section", err)
175-
return nil
177+
return nil, isGolangApp
176178
}
177179
textSectionLen := uint64(len(textSectionData) - 1)
178180

179181
exe, err := link.OpenExecutable(path)
180182
if err != nil {
181183
log("failed to open executable", err)
182-
return nil
184+
return nil, isGolangApp
183185
}
184186

185187
var links []link.Link
@@ -208,14 +210,14 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) []link.Link {
208210
l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_write_enter"], &link.UprobeOptions{Address: address})
209211
if err != nil {
210212
log("failed to attach write_enter uprobe", err)
211-
return nil
213+
return nil, isGolangApp
212214
}
213215
links = append(links, l)
214216
case goTlsReadSymbol:
215217
l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_read_enter"], &link.UprobeOptions{Address: address})
216218
if err != nil {
217219
log("failed to attach read_enter uprobe", err)
218-
return nil
220+
return nil, isGolangApp
219221
}
220222
links = append(links, l)
221223
sStart := s.Value - textSection.Addr
@@ -227,23 +229,23 @@ func (t *Tracer) AttachGoTlsUprobes(pid uint32) []link.Link {
227229
returnOffsets := getReturnOffsets(ef.Machine, sBytes)
228230
if len(returnOffsets) == 0 {
229231
log("failed to attach read_exit uprobe", fmt.Errorf("no return offsets found"))
230-
return nil
232+
return nil, isGolangApp
231233
}
232234
for _, offset := range returnOffsets {
233235
l, err := exe.Uprobe(s.Name, t.uprobes["go_crypto_tls_read_exit"], &link.UprobeOptions{Address: address, Offset: uint64(offset)})
234236
if err != nil {
235237
log("failed to attach read_exit uprobe", err)
236-
return nil
238+
return nil, isGolangApp
237239
}
238240
links = append(links, l)
239241
}
240242
}
241243
}
242244
if len(links) == 0 {
243-
return nil
245+
return nil, isGolangApp
244246
}
245247
log("crypto/tls uprobes attached", nil)
246-
return links
248+
return links, isGolangApp
247249
}
248250

249251
func getSslLibPathAndVersion(pid uint32) (string, string) {

0 commit comments

Comments
 (0)