Skip to content

Commit

Permalink
Merge pull request #3 from whywaita/feat/more-metrics
Browse files Browse the repository at this point in the history
Add shoes_lxd_multi_lxd_instance metrics
  • Loading branch information
whywaita authored Dec 9, 2021
2 parents 90a0561 + ce84d0b commit 942494f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
1 change: 0 additions & 1 deletion server/pkg/config/config_hostconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (s *HostConfigMap) Range(f func(key string, value HostConfig) bool) {
type HostConfig struct {
Cert tls.Certificate

LxdHostName string
LxdHost string
LxdClientCert string
LxdClientKey string
Expand Down
30 changes: 22 additions & 8 deletions server/pkg/lxdclient/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ func GetCPUOverCommitPercent(in Resource) uint64 {

// GetResource get Resource
func GetResource(client lxd.InstanceServer) (*Resource, error) {
cpuTotal, memoryTotal, err := ScrapeLXDHostResources(client)
cpuTotal, memoryTotal, _, err := ScrapeLXDHostResources(client)
if err != nil {
return nil, fmt.Errorf("failed to scrape total resource: %w", err)
}
cpuUsed, memoryUsed, err := ScrapeLXDHostAllocatedResources(client)
instances, err := GetAnyInstances(client)
if err != nil {
return nil, fmt.Errorf("failed to retrieve list of instance: %w", err)
}
cpuUsed, memoryUsed, err := ScrapeLXDHostAllocatedResources(instances)
if err != nil {
return nil, fmt.Errorf("failed to scrape allocated resource: %w", err)
}
Expand All @@ -43,22 +47,32 @@ func GetResource(client lxd.InstanceServer) (*Resource, error) {
}

// ScrapeLXDHostResources scrape all resources
func ScrapeLXDHostResources(client lxd.InstanceServer) (uint64, uint64, error) {
func ScrapeLXDHostResources(client lxd.InstanceServer) (uint64, uint64, string, error) {
resources, err := client.GetServerResources()
if err != nil {
return 0, 0, fmt.Errorf("failed to get server resource: %w", err)
return 0, 0, "", fmt.Errorf("failed to get server resource: %w", err)
}

server, _, err := client.GetServer()
if err != nil {
return 0, 0, "", fmt.Errorf("failed to get server: %w", err)
}

return resources.CPU.Total, resources.Memory.Total, nil
return resources.CPU.Total, resources.Memory.Total, server.Environment.ServerName, nil
}

// ScrapeLXDHostAllocatedResources scrape allocated resources
func ScrapeLXDHostAllocatedResources(client lxd.InstanceServer) (uint64, uint64, error) {
// GetAnyInstances get any instances from lxd
func GetAnyInstances(client lxd.InstanceServer) ([]api.Instance, error) {
instances, err := client.GetInstances(api.InstanceTypeAny)
if err != nil {
return 0, 0, fmt.Errorf("failed to retrieve instances: %w", err)
return nil, fmt.Errorf("failed to retrieve instances: %w", err)
}

return instances, nil
}

// ScrapeLXDHostAllocatedResources scrape allocated resources
func ScrapeLXDHostAllocatedResources(instances []api.Instance) (uint64, uint64, error) {
var allocatedCPU uint64
var allocatedMemory uint64
for _, instance := range instances {
Expand Down
37 changes: 31 additions & 6 deletions server/pkg/metric/scrape_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package metric
import (
"context"
"fmt"
"strconv"

"github.com/docker/go-units"

"github.com/whywaita/shoes-lxd-multi/server/pkg/lxdclient"

Expand Down Expand Up @@ -33,6 +36,11 @@ var (
"usage of memory",
[]string{"hostname"}, nil,
)
lxdInstance = prometheus.NewDesc(
prometheus.BuildFQName(namespace, lxdName, "instance"),
"LXD instances",
[]string{"instance_name", "stadium_name", "cpu", "memory"}, nil,
)
)

// ScraperLXD is scraper implement for LXD
Expand Down Expand Up @@ -64,24 +72,41 @@ func scrapeLXDHost(ctx context.Context, hostConfigs []config.HostConfig, ch chan
}

for _, host := range hosts {
allCPU, allMemory, err := lxdclient.ScrapeLXDHostResources(host.Client)
allCPU, allMemory, hostname, err := lxdclient.ScrapeLXDHostResources(host.Client)
if err != nil {
return fmt.Errorf("failed to scrape lxd resources: %w", err)
}

ch <- prometheus.MustNewConstMetric(
lxdHostMaxCPU, prometheus.GaugeValue, float64(allCPU), host.HostConfig.LxdHostName)
lxdHostMaxCPU, prometheus.GaugeValue, float64(allCPU), hostname)
ch <- prometheus.MustNewConstMetric(
lxdHostMaxMemory, prometheus.GaugeValue, float64(allMemory), host.HostConfig.LxdHostName)
lxdHostMaxMemory, prometheus.GaugeValue, float64(allMemory), hostname)

instances, err := lxdclient.GetAnyInstances(host.Client)
if err != nil {
return fmt.Errorf("failed to retrieve list of instance (host: %s): %w", hostname, err)
}

for _, instance := range instances {
memory, err := units.FromHumanSize(instance.Config["limits.memory"])
if err != nil {
return fmt.Errorf("failed to convert limits.memory: %w", err)
}

ch <- prometheus.MustNewConstMetric(
lxdInstance, prometheus.GaugeValue, 1,
instance.Name, hostname, instance.Config["limits.cpu"], strconv.FormatInt(memory, 10),
)
}

allocatedCPU, allocatedMemory, err := lxdclient.ScrapeLXDHostAllocatedResources(host.Client)
allocatedCPU, allocatedMemory, err := lxdclient.ScrapeLXDHostAllocatedResources(instances)
if err != nil {
return fmt.Errorf("failed to scrape instance info: %w", err)
}
ch <- prometheus.MustNewConstMetric(
lxdUsageCPU, prometheus.GaugeValue, float64(allocatedCPU), host.HostConfig.LxdHostName)
lxdUsageCPU, prometheus.GaugeValue, float64(allocatedCPU), hostname)
ch <- prometheus.MustNewConstMetric(
lxdUsageMemory, prometheus.GaugeValue, float64(allocatedMemory), host.HostConfig.LxdHostName)
lxdUsageMemory, prometheus.GaugeValue, float64(allocatedMemory), hostname)
}

return nil
Expand Down

0 comments on commit 942494f

Please sign in to comment.