github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/plugins/cpumem/metrics.go (about) 1 package cpumem 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/mitchellh/mapstructure" 9 plugintypes "github.com/projecteru2/core/resource/plugins/types" 10 ) 11 12 // GetMetricsDescription . 13 func (p Plugin) GetMetricsDescription(context.Context) (*plugintypes.GetMetricsDescriptionResponse, error) { 14 resp := &plugintypes.GetMetricsDescriptionResponse{} 15 return resp, mapstructure.Decode([]map[string]any{ 16 { 17 "name": "cpu_map", 18 "help": "node available cpu.", 19 "type": "gauge", 20 "labels": []string{"podname", "nodename", "cpuid"}, 21 }, 22 { 23 "name": "cpu_used", 24 "help": "node used cpu.", 25 "type": "gauge", 26 "labels": []string{"podname", "nodename"}, 27 }, 28 { 29 "name": "memory_capacity", 30 "help": "node available memory.", 31 "type": "gauge", 32 "labels": []string{"podname", "nodename"}, 33 }, 34 { 35 "name": "memory_used", 36 "help": "node used memory.", 37 "type": "gauge", 38 "labels": []string{"podname", "nodename"}, 39 }, 40 }, resp) 41 } 42 43 // GetMetrics . 44 func (p Plugin) GetMetrics(ctx context.Context, podname, nodename string) (*plugintypes.GetMetricsResponse, error) { 45 nodeResourceInfo, err := p.doGetNodeResourceInfo(ctx, nodename) 46 if err != nil { 47 return nil, err 48 } 49 safeNodename := strings.ReplaceAll(nodename, ".", "_") 50 metrics := []map[string]any{ 51 { 52 "name": "memory_capacity", 53 "labels": []string{podname, nodename}, 54 "value": fmt.Sprintf("%+v", nodeResourceInfo.Capacity.Memory), 55 "key": fmt.Sprintf("core.node.%s.memory", safeNodename), 56 }, 57 { 58 "name": "memory_used", 59 "labels": []string{podname, nodename}, 60 "value": fmt.Sprintf("%+v", nodeResourceInfo.Usage.Memory), 61 "key": fmt.Sprintf("core.node.%s.memory.used", safeNodename), 62 }, 63 { 64 "name": "cpu_used", 65 "labels": []string{podname, nodename}, 66 "value": fmt.Sprintf("%+v", nodeResourceInfo.Usage.CPU), 67 "key": fmt.Sprintf("core.node.%s.cpu.used", safeNodename), 68 }, 69 } 70 71 for cpuID, pieces := range nodeResourceInfo.Usage.CPUMap { 72 metrics = append(metrics, map[string]any{ 73 "name": "cpu_map", 74 "labels": []string{podname, nodename, cpuID}, 75 "value": fmt.Sprintf("%+v", pieces), 76 "key": fmt.Sprintf("core.node.%s.cpu.%s", safeNodename, cpuID), 77 }) 78 } 79 80 resp := &plugintypes.GetMetricsResponse{} 81 return resp, mapstructure.Decode(metrics, resp) 82 }