Skip to content

Commit

Permalink
BPF FS: add support for multiple processes (#211)
Browse files Browse the repository at this point in the history
* BPF FS: add support for multiple processes

* add entry to changelog

* fix checks

* Update pkg/instrumentors/bpffs/bpfsfs.go

Co-authored-by: Mike Goldsmith <[email protected]>

* rollback Dockerfile

---------

Co-authored-by: Mike Goldsmith <[email protected]>
  • Loading branch information
edeNFed and MikeGoldsmith authored Jul 23, 2023
1 parent 6cf75fd commit 298c38b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Remove the HTTP path from span names in `net/http`, `gin-gonic/gin`, and `gorilla/mux` instrumentations. ([#161](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/161))
- Update generated offsets. ([#186](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/186))
- Reduce Docker image size by using different base image. ([#182](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/182))
- Support for multiple processes in BPF FS. ([#211](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/211))

## [v0.2.1-alpha] - 2023-05-15

Expand Down
20 changes: 1 addition & 19 deletions pkg/instrumentors/allocator/allocator_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
package allocator

import (
"os"

"golang.org/x/sys/unix"

"go.opentelemetry.io/auto/pkg/instrumentors/bpffs"
"go.opentelemetry.io/auto/pkg/instrumentors/context"
"go.opentelemetry.io/auto/pkg/log"
Expand All @@ -42,24 +38,10 @@ func (a *Allocator) Load(ctx *context.InstrumentorContext) error {
}
logger.V(0).Info("Loading allocator")

err := a.mountBPFFs()
err := bpffs.Mount(ctx.TargetDetails)
if err != nil {
return err
}

return nil
}

func (a *Allocator) mountBPFFs() error {
_, err := os.Stat(bpffs.BPFFsPath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
if err := os.MkdirAll(bpffs.BPFFsPath, 0755); err != nil {
return err
}
}

return unix.Mount(bpffs.BPFFsPath, bpffs.BPFFsPath, "bpf", 0, "")
}
2 changes: 1 addition & 1 deletion pkg/instrumentors/bpf/github.com/gin-gonic/gin/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (h *Instrumentor) Load(ctx *context.InstrumentorContext) error {
h.bpfObjects = &bpfObjects{}
err = utils.LoadEBPFObjects(spec, h.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentors/bpf/github.com/gorilla/mux/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (g *Instrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = utils.LoadEBPFObjects(spec, g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentors/bpf/google/golang/org/grpc/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (g *Instrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = utils.LoadEBPFObjects(spec, g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (g *Instrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = utils.LoadEBPFObjects(spec, g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentors/bpf/net/http/client/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (h *Instrumentor) Load(ctx *context.InstrumentorContext) error {
h.bpfObjects = &bpfObjects{}
err = spec.LoadAndAssign(h.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentors/bpf/net/http/server/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (h *Instrumentor) Load(ctx *context.InstrumentorContext) error {
h.bpfObjects = &bpfObjects{}
err = utils.LoadEBPFObjects(spec, h.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BPFFsPath,
PinPath: bpffs.PathForTargetApplication(ctx.TargetDetails),
},
})
if err != nil {
Expand Down
52 changes: 51 additions & 1 deletion pkg/instrumentors/bpffs/bpfsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,55 @@

package bpffs

import (
"fmt"
"os"

"go.opentelemetry.io/auto/pkg/log"

"golang.org/x/sys/unix"

"go.opentelemetry.io/auto/pkg/process"
)

// BPFFsPath is the system path to the BPF file-system.
const BPFFsPath = "/sys/fs/bpf"
const bpfFsPath = "/sys/fs/bpf"

// PathForTargetApplication returns the path to the BPF file-system for the given target.
func PathForTargetApplication(target *process.TargetDetails) string {
return fmt.Sprintf("%s/%d", bpfFsPath, target.PID)
}

// Mount mounts the BPF file-system for the given target.
func Mount(target *process.TargetDetails) error {
if !isBPFFSMounted() {
// Directory does not exist, create it and mount
if err := os.MkdirAll(bpfFsPath, 0755); err != nil {
return err
}

err := unix.Mount(bpfFsPath, bpfFsPath, "bpf", 0, "")
if err != nil {
return err
}
}

// create directory with read, write and execute permissions
return os.Mkdir(PathForTargetApplication(target), 0755)
}

func isBPFFSMounted() bool {
var stat unix.Statfs_t
err := unix.Statfs(bpfFsPath, &stat)
if err != nil {
log.Logger.Error(err, "failed to statfs bpf filesystem")
return false
}

return stat.Type == unix.BPF_FS_MAGIC
}

// Cleanup removes the BPF file-system for the given target.
func Cleanup(target *process.TargetDetails) error {
return os.RemoveAll(PathForTargetApplication(target))
}

0 comments on commit 298c38b

Please sign in to comment.