Skip to content

Commit

Permalink
[chore] Fix lint in pkg/instrumentors (#116)
Browse files Browse the repository at this point in the history
* Fix lint in instrumentors

* Apply suggestions from code review

Co-authored-by: Robert Pająk <[email protected]>

* Update pkg/instrumentors/allocator/allocator_linux.go

---------

Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
MrAlias and pellared authored Apr 27, 2023
1 parent fac24ae commit 4101e5d
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 36 deletions.
23 changes: 13 additions & 10 deletions pkg/instrumentors/allocator/allocator_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ 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"
"golang.org/x/sys/unix"
)

// Allocator handles the allocation of the BPF file-system.
type Allocator struct{}

// New returns a new [Allocator].
func New() *Allocator {
return &Allocator{}
}

// Load loads the BPF file-system.
func (a *Allocator) Load(ctx *context.InstrumentorContext) error {
logger := log.Logger.WithName("allocator")
if ctx.TargetDetails.AllocationDetails != nil {
Expand All @@ -38,25 +42,24 @@ func (a *Allocator) Load(ctx *context.InstrumentorContext) error {
}
logger.V(0).Info("Loading allocator")

err := a.mountBpfFS()
err := a.mountBPFFs()
if err != nil {
return err
}

return nil
}

func (a *Allocator) mountBpfFS() error {
_, err := os.Stat(bpffs.BpfFsPath)
func (a *Allocator) mountBPFFs() error {
_, err := os.Stat(bpffs.BPFFsPath)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(bpffs.BpfFsPath, 0755); err != nil {
return err
}
} else {
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, "")
return unix.Mount(bpffs.BPFFsPath, bpffs.BPFFsPath, "bpf", 0, "")
}
11 changes: 11 additions & 0 deletions pkg/instrumentors/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ import (
"go.opentelemetry.io/auto/pkg/instrumentors/events"
)

// Instrumentor provides instrumentation for a Go package.
type Instrumentor interface {
// LibraryName returns the package name being instrumented.
LibraryName() string

// FuncNames returns the fully-qualified function names that are
// instrumented.
FuncNames() []string

// Load loads all instrumentation offsets.
Load(ctx *context.InstrumentorContext) error

// Run runs the events processing loop.
Run(eventsChan chan<- *events.Event)

// Close stops the Instrumentor.
Close()
}
4 changes: 2 additions & 2 deletions pkg/instrumentors/bpf/github.com/gorilla/mux/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type HttpEvent struct {
EndTime uint64
Method [100]byte
Path [100]byte
SpanContext context.EbpfSpanContext
SpanContext context.EBPFSpanContext
}

type gorillaMuxInstrumentor struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (g *gorillaMuxInstrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = spec.LoadAndAssign(g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BpfFsPath,
PinPath: bpffs.BPFFsPath,
},
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/instrumentors/bpf/google/golang/org/grpc/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type GrpcEvent struct {
EndTime uint64
Method [50]byte
Target [50]byte
SpanContext context.EbpfSpanContext
ParentSpanContext context.EbpfSpanContext
SpanContext context.EBPFSpanContext
ParentSpanContext context.EBPFSpanContext
}

type grpcInstrumentor struct {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (g *grpcInstrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = spec.LoadAndAssign(g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BpfFsPath,
PinPath: bpffs.BPFFsPath,
},
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/instrumentors/bpf/google/golang/org/grpc/server/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type GrpcEvent struct {
StartTime uint64
EndTime uint64
Method [100]byte
SpanContext context.EbpfSpanContext
ParentSpanContext context.EbpfSpanContext
SpanContext context.EBPFSpanContext
ParentSpanContext context.EBPFSpanContext
}

type grpcServerInstrumentor struct {
Expand Down Expand Up @@ -107,7 +107,7 @@ func (g *grpcServerInstrumentor) Load(ctx *context.InstrumentorContext) error {
g.bpfObjects = &bpfObjects{}
err = spec.LoadAndAssign(g.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BpfFsPath,
PinPath: bpffs.BPFFsPath,
},
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/instrumentors/bpf/net/http/server/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type HttpEvent struct {
EndTime uint64
Method [6]byte
Path [100]byte
SpanContext context.EbpfSpanContext
SpanContext context.EBPFSpanContext
}

type httpServerInstrumentor struct {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (h *httpServerInstrumentor) Load(ctx *context.InstrumentorContext) error {
h.bpfObjects = &bpfObjects{}
err = spec.LoadAndAssign(h.bpfObjects, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{
PinPath: bpffs.BpfFsPath,
PinPath: bpffs.BPFFsPath,
},
})
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/instrumentors/bpffs/bpfsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@

package bpffs

const (
BpfFsPath = "/sys/fs/bpf"
)
// BPFFsPath is the system path to the BPF file-system.
const BPFFsPath = "/sys/fs/bpf"
2 changes: 2 additions & 0 deletions pkg/instrumentors/context/inst_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package context

import (
"github.com/cilium/ebpf/link"

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

// InstrumentorContext holds the state of the auto-instrumentation system.
type InstrumentorContext struct {
TargetDetails *process.TargetDetails
Executable *link.Executable
Expand Down
4 changes: 3 additions & 1 deletion pkg/instrumentors/context/span_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package context

import "go.opentelemetry.io/otel/trace"

type EbpfSpanContext struct {
// EBPFSpanContext is the the span context representation within the eBPF
// instrumentation system.
type EBPFSpanContext struct {
TraceID trace.TraceID
SpanID trace.SpanID
}
1 change: 1 addition & 0 deletions pkg/instrumentors/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

// Event is a telemetry event that happens within an instrumented package.
type Event struct {
Library string
Name string
Expand Down
20 changes: 13 additions & 7 deletions pkg/instrumentors/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ import (
"go.opentelemetry.io/auto/pkg/process"
)

type instrumentorsManager struct {
// Manager handles the management of [Instrumentor] instances.
type Manager struct {
instrumentors map[string]Instrumentor
done chan bool
incomingEvents chan *events.Event
otelController *opentelemetry.Controller
allocator *allocator.Allocator
}

func NewManager(otelController *opentelemetry.Controller) (*instrumentorsManager, error) {
m := &instrumentorsManager{
// NewManager returns a new [Manager].
func NewManager(otelController *opentelemetry.Controller) (*Manager, error) {
m := &Manager{
instrumentors: make(map[string]Instrumentor),
done: make(chan bool, 1),
incomingEvents: make(chan *events.Event),
Expand All @@ -53,7 +55,7 @@ func NewManager(otelController *opentelemetry.Controller) (*instrumentorsManager
return m, nil
}

func (m *instrumentorsManager) registerInstrumentor(instrumentor Instrumentor) error {
func (m *Manager) registerInstrumentor(instrumentor Instrumentor) error {
if _, exists := m.instrumentors[instrumentor.LibraryName()]; exists {
return fmt.Errorf("library %s registered twice, aborting", instrumentor.LibraryName())
}
Expand All @@ -62,7 +64,9 @@ func (m *instrumentorsManager) registerInstrumentor(instrumentor Instrumentor) e
return nil
}

func (m *instrumentorsManager) GetRelevantFuncs() map[string]interface{} {
// GetRelevantFuncs returns the instrumented functions for all managed
// Instrumentors.
func (m *Manager) GetRelevantFuncs() map[string]interface{} {
funcsMap := make(map[string]interface{})
for _, i := range m.instrumentors {
for _, f := range i.FuncNames() {
Expand All @@ -73,7 +77,9 @@ func (m *instrumentorsManager) GetRelevantFuncs() map[string]interface{} {
return funcsMap
}

func (m *instrumentorsManager) FilterUnusedInstrumentors(target *process.TargetDetails) {
// FilterUnusedInstrumentors filterers Instrumentors whose functions are
// already instrumented out of the Manager.
func (m *Manager) FilterUnusedInstrumentors(target *process.TargetDetails) {
existingFuncMap := make(map[string]interface{})
for _, f := range target.Functions {
existingFuncMap[f.Name] = nil
Expand All @@ -95,7 +101,7 @@ func (m *instrumentorsManager) FilterUnusedInstrumentors(target *process.TargetD
}
}

func registerInstrumentors(m *instrumentorsManager) error {
func registerInstrumentors(m *Manager) error {
insts := []Instrumentor{
grpc.New(),
grpcServer.New(),
Expand Down
13 changes: 8 additions & 5 deletions pkg/instrumentors/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import (

"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"

"go.opentelemetry.io/auto/pkg/inject"
"go.opentelemetry.io/auto/pkg/instrumentors/context"
"go.opentelemetry.io/auto/pkg/log"
"go.opentelemetry.io/auto/pkg/process"
)

func (m *instrumentorsManager) Run(target *process.TargetDetails) error {
// Run runs the event processing loop for all managed Instrumentors.
func (m *Manager) Run(target *process.TargetDetails) error {
if len(m.instrumentors) == 0 {
log.Logger.V(0).Info("there are no avilable instrumentations for target process")
log.Logger.V(0).Info("there are no available instrumentations for target process")
return nil
}

Expand All @@ -52,7 +54,7 @@ func (m *instrumentorsManager) Run(target *process.TargetDetails) error {
}
}

func (m *instrumentorsManager) load(target *process.TargetDetails) error {
func (m *Manager) load(target *process.TargetDetails) error {
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
return err
Expand Down Expand Up @@ -93,13 +95,14 @@ func (m *instrumentorsManager) load(target *process.TargetDetails) error {
return nil
}

func (m *instrumentorsManager) cleanup() {
func (m *Manager) cleanup() {
close(m.incomingEvents)
for _, i := range m.instrumentors {
i.Close()
}
}

func (m *instrumentorsManager) Close() {
// Close closes m.
func (m *Manager) Close() {
m.done <- true
}

0 comments on commit 4101e5d

Please sign in to comment.