New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor metrics doc generator #11467
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do wee need to use a metrics collector? can you use the list methods from each component monitoring package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we query the end point and add metrics manually that's why I asked if we can avoid that. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
domainstats "kubevirt.io/kubevirt/pkg/monitoring/domainstats/prometheus" // import for prometheus metrics | ||
_ "kubevirt.io/kubevirt/pkg/virt-controller/watch" | ||
) | ||
|
||
type metric struct { | ||
name string | ||
description string | ||
mType string | ||
} | ||
|
||
type metricList []metric | ||
|
||
func getMetricsNotIncludeInEndpointByDefault() metricList { | ||
metrics := metricList{ | ||
{ | ||
name: domainstats.MigrateVmiDataProcessedMetricName, | ||
description: "The total Guest OS data processed and migrated to the new VM.", | ||
mType: "Gauge", | ||
}, | ||
{ | ||
name: domainstats.MigrateVmiDataRemainingMetricName, | ||
description: "The remaining guest OS data to be migrated to the new VM.", | ||
mType: "Gauge", | ||
}, | ||
{ | ||
name: domainstats.MigrateVmiDirtyMemoryRateMetricName, | ||
description: "The rate of memory being dirty in the Guest OS.", | ||
mType: "Gauge", | ||
}, | ||
{ | ||
name: domainstats.MigrateVmiMemoryTransferRateMetricName, | ||
description: "The rate at which the memory is being transferred.", | ||
mType: "Gauge", | ||
}, | ||
} | ||
|
||
return metrics | ||
} | ||
|
||
// Len implements sort.Interface.Len | ||
func (m metricList) Len() int { | ||
return len(m) | ||
} | ||
|
||
// Less implements sort.Interface.Less | ||
func (m metricList) Less(i, j int) bool { | ||
return m[i].name < m[j].name | ||
} | ||
|
||
// Swap implements sort.Interface.Swap | ||
func (m metricList) Swap(i, j int) { | ||
m[i], m[j] = m[j], m[i] | ||
} | ||
|
||
func parseMetricDesc(line string) (string, string) { | ||
split := strings.Split(line, " ") | ||
name := split[2] | ||
split[3] = strings.Title(split[3]) | ||
description := strings.Join(split[3:], " ") | ||
return name, description | ||
} | ||
|
||
func parseMetricType(scan *bufio.Scanner, name string) string { | ||
for scan.Scan() { | ||
typeLine := scan.Text() | ||
if strings.HasPrefix(typeLine, "# TYPE ") { | ||
split := strings.Split(typeLine, " ") | ||
if split[2] == name { | ||
return strings.Title(split[3]) | ||
} | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func parseVirtMetrics(r io.Reader, metrics *metricList) error { | ||
scan := bufio.NewScanner(r) | ||
for scan.Scan() { | ||
helpLine := scan.Text() | ||
if strings.HasPrefix(helpLine, "# HELP ") { | ||
if strings.Contains(helpLine, "kubevirt_") { | ||
metName, metDesc := parseMetricDesc(helpLine) | ||
metType := parseMetricType(scan, metName) | ||
*metrics = append(*metrics, metric{name: metName, description: metDesc, mType: metType}) | ||
} | ||
} | ||
} | ||
|
||
if scan.Err() != nil { | ||
return fmt.Errorf("failed to parse metrics from prometheus endpoint, %w", scan.Err()) | ||
} | ||
|
||
sort.Sort(metrics) | ||
|
||
// remove duplicates | ||
for i := 0; i < len(*metrics)-1; i++ { | ||
if (*metrics)[i].name == (*metrics)[i+1].name { | ||
*metrics = append((*metrics)[:i], (*metrics)[i+1:]...) | ||
i-- | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this one was fixed by make generate I did use a space in the beginning