solana_exporter/cmd/solana-exporter/desc_test.go

50 lines
1.2 KiB
Go
Raw Normal View History

2024-10-24 05:20:30 -07:00
package main
import (
"fmt"
"sort"
)
2024-10-29 04:04:49 -07:00
type (
LV struct {
labels []string
value float64
}
collectionTest struct {
Name string
ExpectedResponse string
}
)
2024-10-24 06:26:27 -07:00
func NewLV(value float64, labels ...string) LV {
return LV{labels, value}
}
func (c *GaugeDesc) expectedCollection(labeledValues ...LV) string {
2024-10-24 05:20:30 -07:00
helpLine := fmt.Sprintf("# HELP %s %s", c.Name, c.Help)
typeLine := fmt.Sprintf("# TYPE %s gauge", c.Name)
result := fmt.Sprintf("%s\n%s", helpLine, typeLine)
// we need to sort our variable labels:
sortedVariableLabels := make([]string, len(c.VariableLabels))
copy(sortedVariableLabels, c.VariableLabels)
sort.Strings(sortedVariableLabels)
for _, lv := range labeledValues {
description := ""
if len(lv.labels) > 0 {
for i, label := range lv.labels {
description += fmt.Sprintf("%s=\"%s\",", sortedVariableLabels[i], label)
}
// remove trailing comma + put in brackets:
description = fmt.Sprintf("{%s}", description[:len(description)-1])
}
result += fmt.Sprintf("\n%s%s %v", c.Name, description, lv.value)
}
return "\n" + result + "\n"
}
2024-10-24 06:26:27 -07:00
func (c *GaugeDesc) makeCollectionTest(labeledValues ...LV) collectionTest {
2024-10-24 05:20:30 -07:00
return collectionTest{Name: c.Name, ExpectedResponse: c.expectedCollection(labeledValues...)}
}