Skip to content

Commit

Permalink
removed server/metrics import
Browse files Browse the repository at this point in the history
  • Loading branch information
CharanSriramUni committed May 5, 2024
1 parent eb81d39 commit fba6844
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions cmd/incus/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/lxc/incus/v6/client"
cli "github.com/lxc/incus/v6/internal/cmd"
"github.com/lxc/incus/v6/internal/i18n"
"github.com/lxc/incus/v6/internal/server/metrics"
)

type cmdTop struct {
Expand Down Expand Up @@ -256,11 +255,11 @@ func (c *cmdTop) updateDisplay(d incus.InstanceServer, refreshInterval time.Dura
for i := 0; i < namesLen; i++ {
currentName := names[i]

cpuSeconds := metricSet.getMetricValue(metrics.CPUSecondsTotal, currentName)
memoryUsed := metricSet.getMetricValue(metrics.MemoryMemTotalBytes, currentName)
memoryAvail := metricSet.getMetricValue(metrics.MemoryMemAvailableBytes, currentName)
diskSize := metricSet.getMetricValue(metrics.FilesystemSizeBytes, currentName)
diskFree := metricSet.getMetricValue(metrics.FilesystemFreeBytes, currentName)
cpuSeconds := metricSet.getMetricValue(cpuSecondsTotal, currentName)
memoryUsed := metricSet.getMetricValue(memoryMemTotalBytes, currentName)
memoryAvail := metricSet.getMetricValue(memoryMemAvailableBytes, currentName)
diskSize := metricSet.getMetricValue(filesystemSizeBytes, currentName)
diskFree := metricSet.getMetricValue(filesystemFreeBytes, currentName)

data[i] = displayData{
instanceName: currentName,
Expand Down Expand Up @@ -296,18 +295,47 @@ func (c *cmdTop) updateDisplay(d incus.InstanceServer, refreshInterval time.Dura
return nil
}

type sample struct {
labels map[string]string
value float64
}

type metricType int

type metricSet struct {
set map[metrics.MetricType][]metrics.Sample
set map[metricType][]sample
labels map[string]string
}

func (ms *metricSet) getMetricValue(metricType metrics.MetricType, instanceName string) float64 {
const (
// CPUSecondsTotal represents the total CPU seconds used.
cpuSecondsTotal metricType = iota
// FilesystemAvailBytes represents the available bytes on a filesystem.
filesystemFreeBytes
// FilesystemSizeBytes represents the size in bytes of a filesystem.
filesystemSizeBytes
//MemoryMemAvailableBytes represents the amount of available memory.
memoryMemAvailableBytes
// MemoryMemTotalBytes represents the amount of used memory.
memoryMemTotalBytes
)

// MetricNames associates a metric type to its name.
var metricNames = map[metricType]string{
cpuSecondsTotal: "incus_cpu_seconds_total",
filesystemFreeBytes: "incus_filesystem_free_bytes",
filesystemSizeBytes: "incus_filesystem_size_bytes",
memoryMemAvailableBytes: "incus_memory_MemAvailable_bytes",
memoryMemTotalBytes: "incus_memory_MemTotal_bytes",
}

func (ms *metricSet) getMetricValue(metricType_ metricType, instanceName string) float64 {
value := 0.0

if samples, exists := ms.set[metricType]; exists { // Check if metricType exists
if samples, exists := ms.set[metricType_]; exists { // Check if metricType exists
for _, sample := range samples {
if sample.Labels["name"] == instanceName {
value += sample.Value
if sample.labels["name"] == instanceName {
value += sample.value
}
}
}
Expand All @@ -319,7 +347,7 @@ func (ms *metricSet) getMetricValue(metricType metrics.MetricType, instanceName
func parseMetricsFromString(input string) (*metricSet, []string, error) {
scanner := bufio.NewScanner(strings.NewReader(input))
metricSet := &metricSet{
set: make(map[metrics.MetricType][]metrics.Sample),
set: make(map[metricType][]sample),
labels: make(map[string]string),
}

Expand All @@ -338,18 +366,18 @@ func parseMetricsFromString(input string) (*metricSet, []string, error) {
return nil, nil, fmt.Errorf("Invalid metric value: %v", err)
}

metricType, found := findMetricTypeByName(metricName)
metricType_, found := findMetricTypeByName(metricName)
if !found {
continue
}

labels := parseLabels(labelPart)
sample := metrics.Sample{
Labels: labels,
Value: value,
sample := sample{
labels: labels,
value: value,
}

metricSet.set[metricType] = append(metricSet.set[metricType], sample)
metricSet.set[metricType_] = append(metricSet.set[metricType_], sample)
}

err := scanner.Err()
Expand All @@ -358,9 +386,9 @@ func parseMetricsFromString(input string) (*metricSet, []string, error) {
}

names := []string{}
if samples, exists := metricSet.set[metrics.MemoryMemTotalBytes]; exists { // Use a known metric type to gather names
if samples, exists := metricSet.set[memoryMemTotalBytes]; exists { // Use a known metric type to gather names
for _, sample := range samples {
names = append(names, sample.Labels["name"])
names = append(names, sample.labels["name"])
}
}

Expand All @@ -383,8 +411,8 @@ func parseLabels(input string) map[string]string {
return labels
}

func findMetricTypeByName(name string) (metrics.MetricType, bool) {
for typ, typName := range metrics.MetricNames {
func findMetricTypeByName(name string) (metricType, bool) {
for typ, typName := range metricNames {
if typName == name {
return typ, true
}
Expand Down

0 comments on commit fba6844

Please sign in to comment.