Skip to content

Commit

Permalink
Log validation and prevalidation times for blocks to files (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Dec 18, 2023
1 parent 647c0ce commit c453729
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ main
bin/
GeoLite2-*.mmdb
.idea/*
*.log
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
logLevel string
requestTimeout time.Duration
disableCentralHarvesterCollection bool
logBlockTimes bool
)

cobra.OnInitialize(initConfig)
Expand All @@ -46,13 +47,15 @@ func init() {
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "How verbose the logs should be. panic, fatal, error, warn, info, debug, trace")
rootCmd.PersistentFlags().DurationVar(&requestTimeout, "rpc-timeout", 10*time.Second, "How long RPC requests will wait before timing out")
rootCmd.PersistentFlags().BoolVar(&disableCentralHarvesterCollection, "disable-central-harvester-collection", false, "Disables collection of harvester information via the farmer. Useful for very large farms where this request is very expensive, or cases where chia-exporter is already installed on all harvesters")
rootCmd.PersistentFlags().BoolVar(&logBlockTimes, "log-block-times", false, "Enables logging of block (pre)validation times to log files.")

cobra.CheckErr(viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname")))
cobra.CheckErr(viper.BindPFlag("metrics-port", rootCmd.PersistentFlags().Lookup("metrics-port")))
cobra.CheckErr(viper.BindPFlag("maxmind-db-path", rootCmd.PersistentFlags().Lookup("maxmind-db-path")))
cobra.CheckErr(viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level")))
cobra.CheckErr(viper.BindPFlag("rpc-timeout", rootCmd.PersistentFlags().Lookup("rpc-timeout")))
cobra.CheckErr(viper.BindPFlag("disable-central-harvester-collection", rootCmd.PersistentFlags().Lookup("disable-central-harvester-collection")))
cobra.CheckErr(viper.BindPFlag("log-block-times", rootCmd.PersistentFlags().Lookup("log-block-times")))
}

// initConfig reads in config file and ENV variables if set.
Expand Down
10 changes: 10 additions & 0 deletions internal/metrics/fullnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/chia-network/go-chia-libs/pkg/types"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

wrappedPrometheus "github.com/chia-network/go-modules/pkg/prometheus"

Expand Down Expand Up @@ -316,6 +317,15 @@ func (s *FullNodeServiceMetrics) Block(resp *types.WebsocketResponse) {
s.preValidationTime.Set(block.PreValidationTime)
s.validationTime.Set(block.ValidationTime)

if viper.GetBool("log-block-times") {
if err = utils.LogToFile("pre-validation-time.log", fmt.Sprintf("%d:%f", block.Height, block.PreValidationTime)); err != nil {
log.Error(err.Error())
}
if err = utils.LogToFile("validation-time.log", fmt.Sprintf("%d:%f", block.Height, block.ValidationTime)); err != nil {
log.Error(err.Error())
}
}

if block.TransactionBlock {
s.blockCost.Set(float64(block.BlockCost.OrEmpty()))
s.blockFees.Set(float64(block.BlockFees.OrEmpty()))
Expand Down
33 changes: 33 additions & 0 deletions internal/utils/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"os"
"path/filepath"

"github.com/chia-network/go-chia-libs/pkg/config"
log "github.com/sirupsen/logrus"
)

// LogToFile logs a message to a given file
func LogToFile(filename, message string) error {
rootPath, err := config.GetChiaRootPath()
if err != nil {
return err
}
path := filepath.Join(rootPath, "log", filename)
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Errorf("Error closing file: %s\n", err.Error())
}
}(file)

if _, err := file.WriteString(message + "\n"); err != nil {
return err
}
return nil
}

0 comments on commit c453729

Please sign in to comment.