Skip to content

Commit

Permalink
feat: return memory usage, not just percent
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jun 20, 2024
1 parent 4e37709 commit b2fd331
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions cmd/sentinel/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/docker/docker/client"
)

var containerMetricsCsvHeader = "time,container_id,cpu_usage,memory_usage\n"
var containerMetricsCsvHeader = "time,container_id,cpu_usage_percent,memory_usage,memory_usage_percent\n"
var containerConfigCsvHeader = "time,id,image,name,state,health_status\n"

type Container struct {
Expand Down Expand Up @@ -125,17 +125,16 @@ func getOneContainerMetrics(containerID string, csv bool) (string, error) {
Time: getUnixTimeInMilliUTC(),
CPUUsagePercentage: calculateCPUPercent(v),
MemoryUsagePercentage: calculateMemoryPercent(v),
MemoryUsed: v.MemoryStats.Usage,
MemoryUsed: calculateMemoryUsed(v),
MemoryAvailable: v.MemoryStats.Limit,
NetworkUsage: metrics.NetworkUsage,
}

jsonData, err := json.MarshalIndent(metrics, "", " ")
if err != nil {
return "", err
}
if csv {
return fmt.Sprintf("%s,%f,%f\n", metrics.Time, metrics.CPUUsagePercentage, metrics.MemoryUsagePercentage), nil
return fmt.Sprintf("%s,%f,%d,%f\n", metrics.Time, metrics.CPUUsagePercentage, metrics.MemoryUsed, metrics.MemoryUsagePercentage), nil
}
return string(jsonData), nil
}
Expand Down Expand Up @@ -254,3 +253,6 @@ func calculateMemoryPercent(stat types.StatsJSON) float64 {
availableMemory := float64(stat.MemoryStats.Limit)
return (usedMemory / availableMemory) * 100.0
}
func calculateMemoryUsed(stat types.StatsJSON) uint64 {
return (stat.MemoryStats.Usage) / 1024 / 1024
}
2 changes: 1 addition & 1 deletion cmd/sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gin-gonic/gin"
)

var version string = "0.0.11"
var version string = "0.0.12"
var logsDir string = "/app/logs"
var metricsDir string = "/app/metrics"
var cpuMetricsFile string = metricsDir + "/cpu.csv"
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sentinel": {
"version": "0.0.11"
"version": "0.0.12"
}
}

0 comments on commit b2fd331

Please sign in to comment.