Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Add memstat flag #2127

Merged
merged 1 commit into from
Mar 10, 2021
Merged
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
1 change: 1 addition & 0 deletions changelogs/unreleased/2127-GuessWhoSamFoo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added memstats flag
5 changes: 5 additions & 0 deletions internal/commands/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func newOctantCmd(version string, gitCommit string, buildTime string) *cobra.Com
if viper.GetBool("enable-opencensus") {
options = append(options, dash.WithOpenCensus())
}
if file := viper.GetString("memstats"); file != "" {
options = append(options, dash.WithMemStats())
}

klogVerbosity := viper.GetString("klog-verbosity")
var klogOpts []string
Expand Down Expand Up @@ -171,6 +174,8 @@ func newOctantCmd(version string, gitCommit string, buildTime string) *cobra.Com
octantCmd.Flags().StringP("proxy-frontend", "", "", "url to send frontend request to [DEV]")
octantCmd.Flags().String("ui-url", "", "dashboard url [DEV]")
octantCmd.Flags().String("browser-path", "", "the browser path to open the browser on")
octantCmd.Flags().String("memstats", "", "log memory usage to this file")
octantCmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".")

return octantCmd
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ import (

type Options struct {
EnableOpenCensus bool
EnableMemStats bool
DisableClusterOverview bool
KubeConfig string
Namespace string
Expand Down Expand Up @@ -82,6 +84,15 @@ func WithOpenCensus() RunnerOption {
}
}

func WithMemStats() RunnerOption {
return RunnerOption{
kubeConfigOption: kubeconfig.Noop(),
nonClusterOption: func(o *Options) {
o.EnableMemStats = true
},
}
}

func WithoutClusterOverview() RunnerOption {
return RunnerOption{
kubeConfigOption: kubeconfig.Noop(),
Expand Down Expand Up @@ -352,6 +363,14 @@ func (r *Runner) initAPI(ctx context.Context, logger log.Logger, opts ...RunnerO
}
}

fmt.Println(options.EnableMemStats)
if options.EnableMemStats {
if err := memStats(); err != nil {
logger.Infof("Enable MemStat")
return nil, nil, fmt.Errorf("enabling memstat: %w", err)
}
}

nsClient, err := clusterClient.NamespaceClient()
if err != nil {
return nil, nil, fmt.Errorf("failed to create namespace client: %w", err)
Expand Down Expand Up @@ -704,6 +723,38 @@ func enableOpenCensus() error {
return nil
}

func memStats() error {
memstats := viper.GetString("memstats")
if memstats != "" {
interval, err := time.ParseDuration(viper.GetString("meminterval"))
if err != nil {
interval, _ = time.ParseDuration("100ms")
}

fileMemStats, err := os.Create(memstats)
if err != nil {
return err
}

fileMemStats.WriteString("# Time\tHeapSys\tHeapAlloc\tHeapIdle\tHeapReleased\n")
go func() {
var stats runtime.MemStats
start := time.Now().UnixNano()
for {
runtime.ReadMemStats(&stats)
if fileMemStats != nil {
fileMemStats.WriteString(fmt.Sprintf("%d\t%d\t%d\t%d\t%d\n",
(time.Now().UnixNano()-start)/1000000, stats.HeapSys, stats.HeapAlloc, stats.HeapIdle, stats.HeapReleased))
time.Sleep(interval)
} else {
break
}
}
}()
}
return nil
}

// ValidateKubeConfig returns a valid file list of kube config(s)
func ValidateKubeConfig(logger log.Logger, kubeConfig string, fs afero.Fs) (string, error) {
fileList := []string{}
Expand Down