Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to custom log level through command line flag and environment variable #842

Merged
merged 17 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adjusted log visibility levels to ensure is following the flag
vitorhugoro1 committed Jun 17, 2024

Verified

This commit was signed with the committer’s verified signature.
MrAlias Tyler Yahn
commit db79e4c3a752b6c9ef453a3b3f05193d9c10ef88
16 changes: 8 additions & 8 deletions internal/pkg/instrumentation/manager.go
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ func (m *Manager) FilterUnusedProbes(target *process.TargetDetails) {
}

if !funcsFound {
m.logger.V(-1).Info("no functions found for probe, removing", "name", name)
m.logger.V(1).Info("no functions found for probe, removing", "name", name)
delete(m.probes, name)
}
}
@@ -172,13 +172,13 @@ func (m *Manager) Run(ctx context.Context, target *process.TargetDetails) error
for {
select {
case <-ctx.Done():
m.logger.V(-1).Info("shutting down all probes due to context cancellation")
m.logger.V(1).Info("shutting down all probes due to context cancellation")
err := m.cleanup(target)
err = errors.Join(err, ctx.Err())
m.closingErrors <- err
return nil
case <-m.done:
m.logger.V(-1).Info("shutting down all probes due to signal")
m.logger.V(1).Info("shutting down all probes due to signal")
err := m.cleanup(target)
m.closingErrors <- err
return nil
@@ -205,23 +205,23 @@ func (m *Manager) load(target *process.TargetDetails) error {

// Load probes
for name, i := range m.probes {
m.logger.V(-1).Info("loading probe", "name", name)
m.logger.V(0).Info("loading probe", "name", name)
err := i.Load(exe, target)
if err != nil {
m.logger.Error(err, "error while loading probes, cleaning up", "name", name)
return errors.Join(err, m.cleanup(target))
}
}

m.logger.V(-1).Info("loaded probes to memory", "total_probes", len(m.probes))
m.logger.V(1).Info("loaded probes to memory", "total_probes", len(m.probes))
return nil
}

func (m *Manager) mount(target *process.TargetDetails) error {
if target.AllocationDetails != nil {
m.logger.V(-1).Info("Mounting bpffs", "allocations_details", target.AllocationDetails)
m.logger.V(1).Info("Mounting bpffs", "allocations_details", target.AllocationDetails)
} else {
m.logger.V(-1).Info("Mounting bpffs")
m.logger.V(1).Info("Mounting bpffs")
}
return bpffs.Mount(target)
}
@@ -233,7 +233,7 @@ func (m *Manager) cleanup(target *process.TargetDetails) error {
err = errors.Join(err, i.Close())
}

m.logger.V(-1).Info("Cleaning bpffs")
m.logger.V(1).Info("Cleaning bpffs")
return errors.Join(err, bpffs.Cleanup(target))
}

6 changes: 3 additions & 3 deletions internal/pkg/instrumentation/probe/probe.go
Original file line number Diff line number Diff line change
@@ -149,7 +149,7 @@ func (i *Base[BPFObj, BPFEvent]) buildObj(exec *link.Executable, td *process.Tar
links, err := up.Fn(up.Sym, exec, td, obj)
if err != nil {
if up.Optional {
i.Logger.V(-1).Info("failed to attach optional uprobe", "probe", i.ID, "symbol", up.Sym, "error", err)
i.Logger.V(1).Info("failed to attach optional uprobe", "probe", i.ID, "symbol", up.Sym, "error", err)
continue
}
return nil, err
@@ -175,7 +175,7 @@ func (i *Base[BPFObj, BPFEvent]) Run(dest chan<- *Event) {
}

if record.LostSamples != 0 {
i.Logger.V(-1).Info("perf event ring buffer full", "dropped", record.LostSamples)
i.Logger.V(1).Info("perf event ring buffer full", "dropped", record.LostSamples)
continue
}

@@ -211,7 +211,7 @@ func (i *Base[BPFObj, BPFEvent]) Close() error {
err = errors.Join(err, c.Close())
}
if err == nil {
i.Logger.V(-1).Info("Closed", "Probe", i.ID)
i.Logger.V(1).Info("Closed", "Probe", i.ID)
}
return err
}
4 changes: 2 additions & 2 deletions internal/pkg/opentelemetry/controller.go
Original file line number Diff line number Diff line change
@@ -52,11 +52,11 @@ func (c *Controller) getTracer(pkg string) trace.Tracer {
// Trace creates a trace span for event.
func (c *Controller) Trace(event *probe.Event) {
for _, se := range event.SpanEvents {
c.logger.V(-1).Info("got event", "kind", event.Kind.String(), "pkg", event.Package, "attrs", se.Attributes, "traceID", se.SpanContext.TraceID().String(), "spanID", se.SpanContext.SpanID().String())
c.logger.V(1).Info("got event", "kind", event.Kind.String(), "pkg", event.Package, "attrs", se.Attributes, "traceID", se.SpanContext.TraceID().String(), "spanID", se.SpanContext.SpanID().String())
ctx := context.Background()

if se.SpanContext == nil {
c.logger.V(-1).Info("got event without context - dropping")
c.logger.V(1).Info("got event without context - dropping")
return
}

8 changes: 4 additions & 4 deletions internal/pkg/process/allocate.go
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ func Allocate(logger logr.Logger, pid int) (*AllocationDetails, error) {
}

mapSize := uint64(os.Getpagesize() * nCPU * 8)
logger.V(-1).Info(
logger.V(1).Info(
"Requesting memory allocation",
"size", mapSize,
"page size", os.Getpagesize(),
@@ -59,7 +59,7 @@ func Allocate(logger logr.Logger, pid int) (*AllocationDetails, error) {
return nil, err
}

logger.V(-1).Info(
logger.V(1).Info(
"mmaped remote memory",
"start_addr", fmt.Sprintf("0x%x", addr),
"end_addr", fmt.Sprintf("0x%x", addr+mapSize),
@@ -81,7 +81,7 @@ func remoteAllocate(logger logr.Logger, pid int, mapSize uint64) (uint64, error)
}

defer func() {
logger.V(-1).Info("Detaching from process", "pid", pid)
logger.V(0).Info("Detaching from process", "pid", pid)
err := program.Detach()
if err != nil {
logger.Error(err, "Failed to detach ptrace", "pid", pid)
@@ -91,7 +91,7 @@ func remoteAllocate(logger logr.Logger, pid int, mapSize uint64) (uint64, error)
if err := program.SetMemLockInfinity(); err != nil {
logger.Error(err, "Failed to set memlock on process")
} else {
logger.V(-1).Info("Set memlock on process successfully")
logger.V(1).Info("Set memlock on process successfully")
}

fd := -1
4 changes: 2 additions & 2 deletions internal/pkg/process/analyze.go
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ func (a *Analyzer) Analyze(pid int, relevantFuncs map[string]interface{}) (*Targ
return nil, err
}
for _, fn := range funcs {
a.logger.V(-1).Info("found function", "function_name", fn)
a.logger.V(1).Info("found function", "function_name", fn)
}

result.Functions = funcs
@@ -145,7 +145,7 @@ func (a *Analyzer) findFunctions(elfF *elf.File, relevantFuncs map[string]interf
result, err := binary.FindFunctionsUnStripped(elfF, relevantFuncs)
if err != nil {
if errors.Is(err, elf.ErrNoSymbols) {
a.logger.V(-1).Info("No symbols found in binary, trying to find functions using .gosymtab")
a.logger.V(1).Info("No symbols found in binary, trying to find functions using .gosymtab")
return binary.FindFunctionsStripped(elfF, relevantFuncs)
}
return nil, err
6 changes: 3 additions & 3 deletions internal/pkg/process/discover.go
Original file line number Diff line number Diff line change
@@ -70,16 +70,16 @@ func (a *Analyzer) DiscoverProcessID(ctx context.Context, target *TargetArgs) (i
for {
select {
case <-ctx.Done():
a.logger.V(-1).Info("stopping process id discovery due to kill signal")
a.logger.V(1).Info("stopping process id discovery due to kill signal")
return 0, ErrInterrupted
case <-t.C:
pid, err := a.findProcessID(target, proc)
if err == nil {
a.logger.V(-1).Info("found process", "pid", pid)
a.logger.V(0).Info("found process", "pid", pid)
return pid, nil
}
if err == ErrProcessNotFound {
a.logger.V(-1).Info("process not found yet, trying again soon", "exe_path", target.ExePath)
a.logger.V(1).Info("process not found yet, trying again soon", "exe_path", target.ExePath)
} else {
a.logger.Error(err, "error while searching for process", "exe_path", target.ExePath)
}
8 changes: 4 additions & 4 deletions internal/pkg/process/ptrace/ptrace_linux.go
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ func NewTracedProgram(pid int, logger logr.Logger) (*TracedProgram, error) {
retryCount[tid]++
}
if retryCount[tid] < threadRetryLimit {
logger.V(-1).Info("retry attaching thread", "tid", tid, "retryCount", retryCount[tid], "limit", threadRetryLimit)
logger.V(1).Info("retry attaching thread", "tid", tid, "retryCount", retryCount[tid], "limit", threadRetryLimit)
continue
}

@@ -135,7 +135,7 @@ func NewTracedProgram(pid int, logger logr.Logger) (*TracedProgram, error) {
return nil, errors.WithStack(err)
}

logger.V(-1).Info("attach successfully", "tid", tid)
logger.V(1).Info("attach successfully", "tid", tid)
tids[tid] = true
tidMap[tid] = true
}
@@ -247,7 +247,7 @@ func (p *TracedProgram) Madvise(addr uint64, length uint64) error {
}

minVersion := version.Must(version.NewVersion("5.14"))
p.logger.V(-1).Info("Detected linux kernel version", "version", ver)
p.logger.V(1).Info("Detected linux kernel version", "version", ver)
if ver.GreaterThanOrEqual(minVersion) {
advice = syscall.MADV_WILLNEED | MadvisePopulateRead | MadvisePopulateWrite
}
@@ -259,6 +259,6 @@ func (p *TracedProgram) Madvise(addr uint64, length uint64) error {
// Mlock runs mlock syscall.
func (p *TracedProgram) Mlock(addr uint64, length uint64) error {
ret, err := p.Syscall(syscall.SYS_MLOCK, addr, length, 0, 0, 0, 0)
p.logger.V(-1).Info("mlock ret", "ret", ret)
p.logger.V(1).Info("mlock ret", "ret", ret)
return err
}