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

Remove Profiler from runtime environment #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions cmd/agent/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ func (r *RootCommand) Do(ctx context.Context) error {
_ = r.env.Lock().Release()
}()

profiler, err := r.env.Profiler().Start(r.profilerConfig)
profiler, err := runtime.StartProfiler(r.profilerConfig)
if err != nil {
return fmt.Errorf("could not create profiler %w", err)
}

defer func() {
_ = profiler.Close()
_ = profiler.Stop()
}()

// set context for command
Expand Down
31 changes: 0 additions & 31 deletions pkg/runtime/fake.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runtime

import (
"io"
"strings"
)

Expand Down Expand Up @@ -94,29 +93,6 @@ func NewCallbackExecutor(callback ExecCallback) *CallbackExecutor {
}
}

// FakeProfiler is a noop profiler for testing
type FakeProfiler struct {
started bool
stopped bool
}

// NewFakeProfiler creates a new FakeProfiler
func NewFakeProfiler() *FakeProfiler {
return &FakeProfiler{}
}

// Start updates the FakeProfiler to registers it was started
func (p *FakeProfiler) Start(c ProfilerConfig) (io.Closer, error) {
p.started = true
return p, nil
}

// Close updates the FakeProfiler to registers it was stopped operation
func (p *FakeProfiler) Close() error {
p.stopped = true
return nil
}

// FakeLock implements a Lock for testing
type FakeLock struct {
locked bool
Expand Down Expand Up @@ -145,7 +121,6 @@ type FakeRuntime struct {
FakeArgs []string
FakeVars map[string]string
FakeExecutor *FakeExecutor
FakeProfiler *FakeProfiler
FakeLock *FakeLock
}

Expand All @@ -154,19 +129,13 @@ func NewFakeRuntime(args []string, vars map[string]string) *FakeRuntime {
return &FakeRuntime{
FakeArgs: args,
FakeVars: vars,
FakeProfiler: NewFakeProfiler(),
FakeExecutor: NewFakeExecutor(nil, nil),
FakeLock: NewFakeLock(),
}
}

// implement Runtime interface

// Profiler implements Profiler method from Runtime interface
func (f *FakeRuntime) Profiler() Profiler {
return f.FakeProfiler
}

// Executor implements Executor method from Runtime interface
func (f *FakeRuntime) Executor() Executor {
return f.FakeExecutor
Expand Down
19 changes: 8 additions & 11 deletions pkg/runtime/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package runtime

import (
"fmt"
"io"
"os"
goruntime "runtime"
"runtime/pprof"
Expand All @@ -20,10 +19,10 @@ type ProfilerConfig struct {
TraceFileName string
}

// Profiler defines the methods to control execution profiling
// Profiler defines the methods to control the execution profiling
type Profiler interface {
// Start initiates the tracing. If already active, has no effect
Start(ProfilerConfig) (io.Closer, error)
// Stops profiling
Stop() error
}

// profiler maintains the configuration state of the profiler
Expand All @@ -34,12 +33,10 @@ type profiler struct {
traceFile *os.File
}

// DefaultProfiler creates a Profiler instance
func DefaultProfiler() Profiler {
return &profiler{}
}

func (p *profiler) Start(config ProfilerConfig) (io.Closer, error) {
// StartProfiler starts the profiler with the given configuration, returning a Profiler
// to control its execution.
func StartProfiler(config ProfilerConfig) (Profiler, error) {
p := &profiler{}
var err error

if config.MemProfile {
Expand Down Expand Up @@ -94,7 +91,7 @@ func (p *profiler) Start(config ProfilerConfig) (io.Closer, error) {
return p, nil
}

func (p *profiler) Close() error {
func (p *profiler) Stop() error {
if p.CPUProfile {
pprof.StopCPUProfile()
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ type Environment interface {
Executor() Executor
// Lock returns an interface for a process lock
Lock() Lock
// Profiler return an execution profiler
Profiler() Profiler
// Vars returns the environment variables
Vars() map[string]string
// Args returns the arguments passed to the process
Expand All @@ -25,7 +23,6 @@ type Environment interface {
type environment struct {
executor Executor
lock Lock
profiler Profiler
vars map[string]string
args []string
}
Expand All @@ -47,7 +44,6 @@ func DefaultEnvironment() Environment {
vars := getEnv()
return &environment{
executor: DefaultExecutor(),
profiler: DefaultProfiler(),
lock: DefaultLock(),
vars: vars,
args: args,
Expand All @@ -62,10 +58,6 @@ func (e *environment) Lock() Lock {
return e.lock
}

func (e *environment) Profiler() Profiler {
return e.profiler
}

func (e *environment) Vars() map[string]string {
return e.vars
}
Expand Down