MisterRaindrop commented on code in PR #87: URL: https://github.com/apache/cloudberry-backup/pull/87#discussion_r3138111065
########## exporter/gpbckp_exporter.go: ########## @@ -0,0 +1,188 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package exporter + +import ( + "log/slog" + "net/http" + "os" + "time" + + "github.com/apache/cloudberry-backup/gpbackman/gpbckpconfig" + "github.com/apache/cloudberry-backup/history" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/exporter-toolkit/web" +) + +var ( + webFlagsConfig web.FlagConfig + webEndpoint string +) + +// SetPromPortAndPath sets HTTP endpoint parameters. +func SetPromPortAndPath(flagsConfig web.FlagConfig, endpoint string) { + webFlagsConfig = flagsConfig + webEndpoint = endpoint +} + +// StartPromEndpoint run HTTP endpoint. +func StartPromEndpoint(version string, logger *slog.Logger) { + go func(logger *slog.Logger) { + if webEndpoint == "" { + logger.Error("Metric endpoint is empty", "endpoint", webEndpoint) + } + http.Handle(webEndpoint, promhttp.Handler()) Review Comment: There seems to be an issue here. An error occurred above, yet the execution continues. This will likely cause a panic? ########## exporter/README.md: ########## @@ -0,0 +1,122 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +# gpbackup_exporter + +**gpbackup_exporter** is a Prometheus exporter for collecting metrics from gpbackup history database (`gpbackup_history.db`). + +## Metrics +### Backup metrics +| Metric | Description | Labels | Additional Info | +| ----------- | ------------------ | ------------- | --------------- | +| `gpbackup_backup_status` | backup status | backup_type, database_name, object_filtering, plugin, timestamp | Values description:<br> `0` - success,<br> `1` - failure.| +| `gpbackup_backup_deletion_status` | backup deletion status | backup_type, database_name, date_deleted, object_filtering, plugin, timestamp | Values description:<br> `0` - backup still exists,<br> `1` - backup was successfully deleted,<br> `2` - the deletion is in progress,<br> `3` - last delete attempt failed to delete backup from plugin storage,<br> `4` - last delete attempt failed to delete backup from local storage.| +| `gpbackup_backup_info` | backup info | backup_dir, backup_ver, backup_type, compression_type, database_name, database_ver, object_filtering, plugin, plugin_ver, timestamp, with_statistic | Values description:<br> `1` - info about backup is exist.| +| `gpbackup_backup_duration_seconds` | backup duration in seconds| backup_type, database_name, end_time, object_filtering, plugin, timestamp || + +### Last backup metrics +| Metric | Description | Labels | Additional Info | +| ----------- | ------------------ | ------------- | --------------- | +| `gpbackup_backup_since_last_completion_seconds`| seconds since the last completed backup | backup_type, database_name || + +### Exporter metrics + +| Metric | Description | Labels | Additional Info | +| ----------- | ------------------ | ------------- | --------------- | +| `gpbackup_exporter_build_info` | information about gpbackup exporter | branch, goarch, goos, goversion, revision, tags, version | | +| `gpbackup_exporter_status` | gpbackup exporter get data status | database_name | Values description:<br> `0` - errors occurred when fetching information from history database,<br> `1` - information successfully fetched from history database. | + +## Getting Started +Available configuration flags: + +```bash +./gpbackup_exporter --help +usage: gpbackup_exporter [<flags>] + + +Flags: + -h, --[no-]help Show context-sensitive help (also try --help-long and --help-man). + --web.telemetry-path="/metrics" + Path under which to expose metrics. + --web.listen-address=:19854 ... Review Comment: in gpbckp_exporter_test.go I see `WebListenAddresses: &([]string{":9854"}),` so 19854 or 9854? ########## gpbackup_exporter.go: ########## @@ -0,0 +1,172 @@ +//go:build gpbackup_exporter + +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package main + +import ( + "log/slog" + "os" + "os/signal" + "path/filepath" + "strings" + "syscall" + "time" + + "github.com/alecthomas/kingpin/v2" + "github.com/apache/cloudberry-backup/exporter" + "github.com/prometheus/client_golang/prometheus" + version_collector "github.com/prometheus/client_golang/prometheus/collectors/version" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" + "github.com/prometheus/common/version" + "github.com/prometheus/exporter-toolkit/web/kingpinflag" +) + +const exporterName = "gpbackup_exporter" + +func main() { + var ( + webPath = kingpin.Flag( + "web.telemetry-path", + "Path under which to expose metrics.", + ).Default("/metrics").String() + webAdditionalToolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":19854") + collectionInterval = kingpin.Flag( + "collect.interval", + "Collecting metrics interval in seconds.", + ).Default("600").Int() + collectionDepth = kingpin.Flag( + "collect.depth", + "Metrics depth collection in days. Metrics for backup older than this interval will not be collected. 0 - disable.", + ).Default("0").Int() + gpbckpHistoryFilePath = kingpin.Flag( + "gpbackup.history-file", + "Path to gpbackup_history.db.", + ).Default("").String() + gpbckpIncludeDB = kingpin.Flag( + "gpbackup.db-include", + "Specific db for collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpExcludeDB = kingpin.Flag( + "gpbackup.db-exclude", + "Specific db to exclude from collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpBackupType = kingpin.Flag( + "gpbackup.backup-type", + "Specific backup type for collecting metrics. One of: [full, incremental, data-only, metadata-only].", + ).Default("").String() + gpbckpBackupCollectDeleted = kingpin.Flag( + "gpbackup.collect-deleted", + "Collecting metrics for deleted backups.", + ).Default("false").Bool() + gpbckpBackupCollectFailed = kingpin.Flag( + "gpbackup.collect-failed", + "Collecting metrics for failed backups.", + ).Default("false").Bool() + ) + // Set logger config. + promslogConfig := &promslog.Config{} + // Add flags log.level and log.format from promslog package. + flag.AddFlags(kingpin.CommandLine, promslogConfig) + kingpin.Version(version.Print(exporterName)) + // Add short help flag. + kingpin.HelpFlag.Short('h') + // Load command line arguments. + kingpin.Parse() + // Setup signal catching. + sigs := make(chan os.Signal, 1) + // Catch listed signals. + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + // Set logger. + logger := promslog.New(promslogConfig) + // Method invoked upon seeing signal. + go func(logger *slog.Logger) { + s := <-sigs + logger.Warn( + "Stopping exporter", + "name", filepath.Base(os.Args[0]), + "signal", s) + os.Exit(1) Review Comment: is better os.Exit(0) ? ########## gpbackup_exporter.go: ########## @@ -0,0 +1,172 @@ +//go:build gpbackup_exporter + +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package main + +import ( + "log/slog" + "os" + "os/signal" + "path/filepath" + "strings" + "syscall" + "time" + + "github.com/alecthomas/kingpin/v2" + "github.com/apache/cloudberry-backup/exporter" + "github.com/prometheus/client_golang/prometheus" + version_collector "github.com/prometheus/client_golang/prometheus/collectors/version" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" + "github.com/prometheus/common/version" + "github.com/prometheus/exporter-toolkit/web/kingpinflag" +) + +const exporterName = "gpbackup_exporter" + +func main() { + var ( + webPath = kingpin.Flag( + "web.telemetry-path", + "Path under which to expose metrics.", + ).Default("/metrics").String() + webAdditionalToolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":19854") + collectionInterval = kingpin.Flag( + "collect.interval", + "Collecting metrics interval in seconds.", + ).Default("600").Int() + collectionDepth = kingpin.Flag( + "collect.depth", + "Metrics depth collection in days. Metrics for backup older than this interval will not be collected. 0 - disable.", + ).Default("0").Int() + gpbckpHistoryFilePath = kingpin.Flag( + "gpbackup.history-file", + "Path to gpbackup_history.db.", + ).Default("").String() + gpbckpIncludeDB = kingpin.Flag( + "gpbackup.db-include", + "Specific db for collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpExcludeDB = kingpin.Flag( + "gpbackup.db-exclude", + "Specific db to exclude from collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpBackupType = kingpin.Flag( + "gpbackup.backup-type", + "Specific backup type for collecting metrics. One of: [full, incremental, data-only, metadata-only].", + ).Default("").String() + gpbckpBackupCollectDeleted = kingpin.Flag( + "gpbackup.collect-deleted", + "Collecting metrics for deleted backups.", + ).Default("false").Bool() + gpbckpBackupCollectFailed = kingpin.Flag( + "gpbackup.collect-failed", + "Collecting metrics for failed backups.", + ).Default("false").Bool() + ) + // Set logger config. + promslogConfig := &promslog.Config{} + // Add flags log.level and log.format from promslog package. + flag.AddFlags(kingpin.CommandLine, promslogConfig) + kingpin.Version(version.Print(exporterName)) + // Add short help flag. + kingpin.HelpFlag.Short('h') + // Load command line arguments. + kingpin.Parse() + // Setup signal catching. + sigs := make(chan os.Signal, 1) + // Catch listed signals. + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + // Set logger. + logger := promslog.New(promslogConfig) + // Method invoked upon seeing signal. + go func(logger *slog.Logger) { + s := <-sigs + logger.Warn( + "Stopping exporter", + "name", filepath.Base(os.Args[0]), + "signal", s) + os.Exit(1) + }(logger) + logger.Info( + "Starting exporter", + "name", filepath.Base(os.Args[0]), + "version", version.Info()) + logger.Info("Build context", "build_context", version.BuildContext()) + logger.Info( + "History database file path", + "file", *gpbckpHistoryFilePath) + logger.Info( + "Collecting metrics for deleted and failed backups", + "deleted", *gpbckpBackupCollectDeleted, + "failed", *gpbckpBackupCollectFailed, + ) + if *collectionDepth > 0 { + logger.Info( + "Metrics depth collection in days", + "depth", *collectionDepth) + } + if strings.Join(*gpbckpIncludeDB, "") != "" { + for _, db := range *gpbckpIncludeDB { + logger.Info( + "Collecting metrics for specific DB", + "DB", db) + } + } + if strings.Join(*gpbckpExcludeDB, "") != "" { + for _, db := range *gpbckpExcludeDB { + logger.Info( + "Exclude collecting metrics for specific DB", + "DB", db) + } + } + if *gpbckpBackupType != "" { + logger.Info( + "Collecting metrics for specific backup type", + "type", *gpbckpBackupType) + } + // Setup parameters for exporter. + exporter.SetPromPortAndPath(*webAdditionalToolkitFlags, *webPath) + logger.Info( + "Use exporter parameters", + "endpoint", *webPath, + "config.file", *webAdditionalToolkitFlags.WebConfigFile, + ) + // Exporter build info metric. + prometheus.MustRegister(version_collector.NewCollector(exporterName)) + // Start web server. + exporter.StartPromEndpoint(version.Info(), logger) + for { + // Get information from gpbackup_history.db. + exporter.GetGPBackupInfo( + *gpbckpHistoryFilePath, + *gpbckpBackupType, + *gpbckpBackupCollectDeleted, + *gpbckpBackupCollectFailed, + *gpbckpIncludeDB, + *gpbckpExcludeDB, + *collectionDepth, + logger, + ) + // Sleep for 'collection.interval' seconds. + time.Sleep(time.Duration(*collectionInterval) * time.Second) Review Comment: Should we add a cancel mechanism here ########## gpbackup_exporter.go: ########## @@ -0,0 +1,172 @@ +//go:build gpbackup_exporter + +/* +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +*/ + +package main + +import ( + "log/slog" + "os" + "os/signal" + "path/filepath" + "strings" + "syscall" + "time" + + "github.com/alecthomas/kingpin/v2" + "github.com/apache/cloudberry-backup/exporter" + "github.com/prometheus/client_golang/prometheus" + version_collector "github.com/prometheus/client_golang/prometheus/collectors/version" + "github.com/prometheus/common/promslog" + "github.com/prometheus/common/promslog/flag" + "github.com/prometheus/common/version" + "github.com/prometheus/exporter-toolkit/web/kingpinflag" +) + +const exporterName = "gpbackup_exporter" + +func main() { + var ( + webPath = kingpin.Flag( + "web.telemetry-path", + "Path under which to expose metrics.", + ).Default("/metrics").String() + webAdditionalToolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":19854") + collectionInterval = kingpin.Flag( + "collect.interval", + "Collecting metrics interval in seconds.", + ).Default("600").Int() + collectionDepth = kingpin.Flag( + "collect.depth", + "Metrics depth collection in days. Metrics for backup older than this interval will not be collected. 0 - disable.", + ).Default("0").Int() + gpbckpHistoryFilePath = kingpin.Flag( + "gpbackup.history-file", + "Path to gpbackup_history.db.", + ).Default("").String() + gpbckpIncludeDB = kingpin.Flag( + "gpbackup.db-include", + "Specific db for collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpExcludeDB = kingpin.Flag( + "gpbackup.db-exclude", + "Specific db to exclude from collecting metrics. Can be specified several times.", + ).Default("").PlaceHolder("\"\"").Strings() + gpbckpBackupType = kingpin.Flag( + "gpbackup.backup-type", + "Specific backup type for collecting metrics. One of: [full, incremental, data-only, metadata-only].", + ).Default("").String() + gpbckpBackupCollectDeleted = kingpin.Flag( + "gpbackup.collect-deleted", + "Collecting metrics for deleted backups.", + ).Default("false").Bool() + gpbckpBackupCollectFailed = kingpin.Flag( + "gpbackup.collect-failed", + "Collecting metrics for failed backups.", + ).Default("false").Bool() + ) + // Set logger config. + promslogConfig := &promslog.Config{} + // Add flags log.level and log.format from promslog package. + flag.AddFlags(kingpin.CommandLine, promslogConfig) + kingpin.Version(version.Print(exporterName)) + // Add short help flag. + kingpin.HelpFlag.Short('h') + // Load command line arguments. + kingpin.Parse() + // Setup signal catching. + sigs := make(chan os.Signal, 1) + // Catch listed signals. + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + // Set logger. + logger := promslog.New(promslogConfig) + // Method invoked upon seeing signal. + go func(logger *slog.Logger) { + s := <-sigs + logger.Warn( + "Stopping exporter", + "name", filepath.Base(os.Args[0]), + "signal", s) + os.Exit(1) + }(logger) + logger.Info( + "Starting exporter", + "name", filepath.Base(os.Args[0]), + "version", version.Info()) + logger.Info("Build context", "build_context", version.BuildContext()) + logger.Info( + "History database file path", + "file", *gpbckpHistoryFilePath) + logger.Info( + "Collecting metrics for deleted and failed backups", + "deleted", *gpbckpBackupCollectDeleted, + "failed", *gpbckpBackupCollectFailed, + ) + if *collectionDepth > 0 { + logger.Info( + "Metrics depth collection in days", + "depth", *collectionDepth) + } + if strings.Join(*gpbckpIncludeDB, "") != "" { + for _, db := range *gpbckpIncludeDB { + logger.Info( + "Collecting metrics for specific DB", + "DB", db) + } + } + if strings.Join(*gpbckpExcludeDB, "") != "" { + for _, db := range *gpbckpExcludeDB { + logger.Info( + "Exclude collecting metrics for specific DB", + "DB", db) + } + } + if *gpbckpBackupType != "" { + logger.Info( + "Collecting metrics for specific backup type", + "type", *gpbckpBackupType) + } + // Setup parameters for exporter. + exporter.SetPromPortAndPath(*webAdditionalToolkitFlags, *webPath) + logger.Info( + "Use exporter parameters", + "endpoint", *webPath, + "config.file", *webAdditionalToolkitFlags.WebConfigFile, + ) + // Exporter build info metric. + prometheus.MustRegister(version_collector.NewCollector(exporterName)) + // Start web server. + exporter.StartPromEndpoint(version.Info(), logger) + for { + // Get information from gpbackup_history.db. + exporter.GetGPBackupInfo( + *gpbckpHistoryFilePath, + *gpbckpBackupType, + *gpbckpBackupCollectDeleted, + *gpbckpBackupCollectFailed, + *gpbckpIncludeDB, + *gpbckpExcludeDB, + *collectionDepth, + logger, + ) + // Sleep for 'collection.interval' seconds. + time.Sleep(time.Duration(*collectionInterval) * time.Second) Review Comment: Should we add a cancel mechanism here -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
