-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CPU and Memory usage to logs #2379
Signed-off-by: João Reigota <[email protected]>
- Loading branch information
1 parent
6391652
commit 79e08d1
Showing
11 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package global | ||
|
||
import ( | ||
"github.com/Checkmarx/kics/internal/metrics" | ||
) | ||
|
||
var ( | ||
Metric *metrics.Metrics | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/pkg/profile" | ||
) | ||
|
||
type cpuMetric struct{} | ||
|
||
// Start - start gathering metrics for CPU usage | ||
func (c cpuMetric) Start(location, path string) metricProfile { | ||
profCPU := profile.Start(profile.CPUProfile, profile.Quiet, profile.NoShutdownHook, profile.ProfilePath(path)) | ||
return profCPU | ||
} | ||
|
||
// Stop - stop gathering metrics for CPU usage | ||
func (c cpuMetric) Stop(profCPU metricProfile) { | ||
profCPU.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/pkg/profile" | ||
) | ||
|
||
type memMetric struct{} | ||
|
||
// Start - start gathering metrics for Memory usage | ||
func (c memMetric) Start(location, path string) metricProfile { | ||
profMem := profile.Start(profile.MemProfile, profile.Quiet, profile.NoShutdownHook, profile.ProfilePath(path)) | ||
return profMem | ||
} | ||
|
||
// Stop - stop gathering metrics for Memory usage | ||
func (c memMetric) Stop(profMem metricProfile) { | ||
profMem.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// Start - starts gathering metrics based on the type of metrics and writes metrics to string | ||
// Stop - stops gathering metrics for the type of metrics specified | ||
type metricType interface { | ||
Start(location, path string) metricProfile | ||
Stop(metricProfile) | ||
} | ||
|
||
type metricProfile interface{ Stop() } | ||
|
||
// Metrics - structure to keep information relevant to the metrics calculation | ||
type Metrics struct { | ||
metric metricType | ||
metricsID string | ||
location string | ||
profile metricProfile | ||
disable bool | ||
tempDIR string | ||
} | ||
|
||
// InitializeMetrics - creates a new instance of a Metrics based on the type of metrics specified | ||
func InitializeMetrics(metric *pflag.Flag) (*Metrics, error) { | ||
metricStr := metric.Value.String() | ||
var err error | ||
metrics := &Metrics{} | ||
switch strings.ToLower(metricStr) { | ||
case "cpu": | ||
metrics.metric = cpuMetric{} | ||
case "mem": | ||
metrics.metric = memMetric{} | ||
case "": | ||
metrics.disable = true | ||
default: | ||
metrics.disable = true | ||
err = fmt.Errorf("unknonwn metric: %s (available metrics: cpu, mem)", metricStr) | ||
} | ||
|
||
// Create temporary dir to keep pprof file | ||
if !metrics.disable { | ||
temp, errCreate := os.MkdirTemp(".", "*") | ||
if errCreate != nil { | ||
err = errCreate | ||
} | ||
|
||
metrics.tempDIR = temp | ||
metrics.metricsID = metricStr | ||
} | ||
|
||
return metrics, err | ||
} | ||
|
||
// Start - starts gathering metrics for the location specified | ||
func (m *Metrics) Start(location string) { | ||
if m.disable { | ||
return | ||
} | ||
log.Debug().Msgf("Started %s profiling for %s", m.metricsID, location) | ||
m.location = location | ||
m.profile = m.metric.Start(location, m.tempDIR) | ||
} | ||
|
||
// Stop - stops gathering metrics and logs the result | ||
func (m *Metrics) Stop() { | ||
if m.disable { | ||
return | ||
} | ||
profile := fmt.Sprintf("%s.pprof", strings.ToLower(m.metricsID)) | ||
log.Debug().Msgf("Stopped %s profiling for %s", m.metricsID, m.location) | ||
m.metric.Stop(m.profile) | ||
|
||
if err := m.readFile(filepath.Join(m.tempDIR, profile)); err != nil { | ||
log.Error().Msgf("failed to get metrics from %s: %s", m.location, err) | ||
} | ||
} | ||
|
||
func (m *Metrics) readFile(profile string) error { | ||
// Remove temporary directory | ||
defer func() { | ||
if err := os.RemoveAll(m.tempDIR); err != nil { | ||
log.Error().Msgf("failed to remove metric temporary dir %s: %s", m.tempDIR, err) | ||
} | ||
}() | ||
// Since profiling in golang creates a binary pprof file we need to call pprof tool | ||
// to the information we need | ||
cmd := exec.Command("go", "tool", "pprof", "-list", "total", profile) | ||
b, err := cmd.Output() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get information of total metric usage | ||
lines := strings.Split(string(b), "\n") | ||
log.Info().Msgf("Total %s usage for %s: %s", strings.ToUpper(m.metricsID), m.location, lines[0]) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters