code.vegaprotocol.io/vega@v0.79.0/core/stats/stats.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package stats
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/logging"
    24  	proto "code.vegaprotocol.io/vega/protos/vega"
    25  	"code.vegaprotocol.io/vega/version"
    26  
    27  	tmversion "github.com/cometbft/cometbft/version"
    28  )
    29  
    30  // Stats ties together all other package level application stats types.
    31  type Stats struct {
    32  	log          *logging.Logger
    33  	cfg          Config
    34  	Blockchain   *Blockchain
    35  	version      string
    36  	versionHash  string
    37  	chainVersion string
    38  	uptime       time.Time
    39  	currentEpoch types.Epoch
    40  }
    41  
    42  // New instantiates a new Stats.
    43  func New(log *logging.Logger, cfg Config) *Stats {
    44  	log = log.Named(namedLogger)
    45  	log.SetLevel(cfg.Level.Get())
    46  	return &Stats{
    47  		log:          log,
    48  		cfg:          cfg,
    49  		Blockchain:   &Blockchain{},
    50  		version:      version.Get(),
    51  		versionHash:  version.GetCommitHash(),
    52  		chainVersion: tmversion.TMCoreSemVer,
    53  		uptime:       time.Now(),
    54  	}
    55  }
    56  
    57  // ReloadConf updates the internal configuration.
    58  func (s *Stats) ReloadConf(cfg Config) {
    59  	s.log.Info("reloading configuration")
    60  	if s.log.GetLevel() != cfg.Level.Get() {
    61  		s.log.Info("updating log level",
    62  			logging.String("old", s.log.GetLevel().String()),
    63  			logging.String("new", cfg.Level.String()),
    64  		)
    65  		s.log.SetLevel(cfg.Level.Get())
    66  	}
    67  
    68  	s.cfg = cfg
    69  }
    70  
    71  func (s *Stats) OnEpochRestore(_ context.Context, epoch types.Epoch) {
    72  	s.currentEpoch = epoch
    73  }
    74  
    75  func (s *Stats) OnEpochEvent(_ context.Context, epoch types.Epoch) {
    76  	if epoch.Action == proto.EpochAction_EPOCH_ACTION_START {
    77  		s.currentEpoch = epoch
    78  	}
    79  }
    80  
    81  func (s *Stats) GetEpochSeq() uint64 {
    82  	return s.currentEpoch.Seq
    83  }
    84  
    85  func (s *Stats) GetEpochStartTime() time.Time {
    86  	return s.currentEpoch.StartTime
    87  }
    88  
    89  func (s *Stats) GetEpochExpireTime() time.Time {
    90  	return s.currentEpoch.ExpireTime
    91  }
    92  
    93  // GetChainVersion returns the version of the chain in use by vega.
    94  func (s *Stats) GetChainVersion() string {
    95  	return s.chainVersion
    96  }
    97  
    98  // GetVersion return the version of vega which is currently running.
    99  func (s *Stats) GetVersion() string {
   100  	return s.version
   101  }
   102  
   103  // GetVersionHash return the hash of the commit this vega
   104  // binary was compiled from.
   105  func (s *Stats) GetVersionHash() string {
   106  	return s.versionHash
   107  }
   108  
   109  func (s *Stats) GetUptime() time.Time {
   110  	return s.uptime
   111  }
   112  
   113  func (s *Stats) Height() uint64 {
   114  	return s.Blockchain.Height()
   115  }
   116  
   117  func (s *Stats) BlockHash() string {
   118  	return s.Blockchain.Hash()
   119  }