Skip to content
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

[+] move helpers to metrics definition #374

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/metrics/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type (

Metric struct {
SQLs SQLs
MasterOnly bool `yaml:",omitempty"`
StandbyOnly bool `yaml:",omitempty"`
InitSQL string `yaml:"init_sql,omitempty"`
MasterOnly bool `yaml:",omitempty"`
StandbyOnly bool `yaml:",omitempty"`
MetricPrometheusAttrs `yaml:",inline,omitempty"`
MetricAttrs `yaml:",inline,omitempty"`
}
Expand Down Expand Up @@ -87,7 +88,7 @@ func ReadMetricsFromFolder(folder string) (metricsMap Metrics, err error) {
regexMetricNameFilter := regexp.MustCompile(metricNamePattern)
regexIsDigitOrPunctuation := regexp.MustCompile(`^[\d\.]+$`)

fmt.Printf("Searching for metrics from path %s ...", folder)
fmt.Printf("Searching for metrics from path %s ...\n", folder)

for _, metricFolder := range metricFolders {
if metricFolder.Name() == FileBasedMetricHelpersDir ||
Expand Down Expand Up @@ -170,6 +171,7 @@ type Preset struct {
// Expects "preset metrics" definition file named preset-config.yaml to be present in provided --metrics folder
func ReadPresetMetricsConfigFromFolder(folder string) (presets Presets, err error) {
var presetMetrics []byte
fmt.Printf("Searching for presents from path %s ...\n", folder)
if presetMetrics, err = os.ReadFile(path.Join(folder, PresetConfigYAMLFile)); err != nil {
return
}
Expand All @@ -185,6 +187,23 @@ func WriteMetricsToFile(metricDefs any, filename string) error {
return os.WriteFile(filename, yamlData, 0644)
}

func moveHelpersToMetrics(helpers, metrics Metrics) {
new_helper:
for helperName, h := range helpers {
for metricName, m := range metrics {
for _, sql := range m.SQLs {
if strings.Contains(sql, helperName) {
for _, v := range h.SQLs {
m.InitSQL = v
metrics[metricName] = m
continue new_helper
}
}
}
}
}
}

func main() {
// Define command-line flags
src := flag.String("src", "", "pgwatch v2 metric folder, e.g. `./metrics/sql`")
Expand Down Expand Up @@ -212,16 +231,15 @@ func main() {
if err != nil {
panic(err)
}
moveHelpersToMetrics(helpers, metrics)
presets, err := ReadPresetMetricsConfigFromFolder(*src)
if err != nil {
panic(err)
}
err = WriteMetricsToFile(struct {
Helpers Metrics `yaml:"helpers,omitempty"`
Metrics Metrics `yaml:"metrics,omitempty"`
Presets Presets `yaml:"presets,omitempty"`
}{
helpers,
metrics,
presets,
}, *dst)
Expand Down
Loading