github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/stats/proxy_stats.go (about)

     1  // Package stats provides methods and functionality to register, track, log,
     2  // and StatsD-notify statistics that, for the most part, include "counter" and "latency" kinds.
     3  /*
     4   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     5   */
     6  package stats
     7  
     8  import (
     9  	"time"
    10  
    11  	"github.com/NVIDIA/aistore/cmn"
    12  	"github.com/NVIDIA/aistore/cmn/atomic"
    13  	"github.com/NVIDIA/aistore/cmn/cos"
    14  	"github.com/NVIDIA/aistore/cmn/nlog"
    15  	"github.com/NVIDIA/aistore/core"
    16  	"github.com/NVIDIA/aistore/core/meta"
    17  )
    18  
    19  const numProxyStats = 24 // approx. initial
    20  
    21  // NOTE: currently, proxy's stats == common and hardcoded
    22  
    23  type Prunner struct {
    24  	runner
    25  }
    26  
    27  /////////////
    28  // Prunner //
    29  /////////////
    30  
    31  // interface guard
    32  var _ cos.Runner = (*Prunner)(nil)
    33  
    34  func (r *Prunner) Run() error { return r._run(r /*as statsLogger*/) }
    35  
    36  // have only common metrics - init only the Prometheus part if enabled
    37  func (r *Prunner) RegMetrics(node *meta.Snode) {
    38  	r.core.initProm(node)
    39  }
    40  
    41  // All stats that proxy currently has are CoreStats which are registered at startup
    42  func (r *Prunner) Init(p core.Node) *atomic.Bool {
    43  	r.core = &coreStats{}
    44  
    45  	r.core.init(numProxyStats)
    46  
    47  	r.regCommon(p.Snode()) // common metrics
    48  
    49  	r.core.statsTime = cmn.GCO.Get().Periodic.StatsTime.D()
    50  	r.ctracker = make(copyTracker, numProxyStats)
    51  
    52  	r.runner.name = "proxystats"
    53  	r.runner.daemon = p
    54  
    55  	r.runner.stopCh = make(chan struct{}, 4)
    56  
    57  	r.core.initMetricClient(p.Snode(), &r.runner)
    58  
    59  	r.sorted = make([]string, 0, numProxyStats)
    60  	return &r.runner.startedUp
    61  }
    62  
    63  //
    64  // statsLogger interface impl
    65  //
    66  
    67  func (r *Prunner) log(now int64, uptime time.Duration, _ *cmn.Config) {
    68  	s := r.core
    69  	s.updateUptime(uptime)
    70  	s.promLock()
    71  	idle := s.copyT(r.ctracker)
    72  	s.promUnlock()
    73  
    74  	if now >= r.next || !idle {
    75  		s.sgl.Reset() // sharing w/ CoreStats.copyT
    76  		r.ctracker.write(s.sgl, r.sorted, false /*target*/, idle)
    77  		if s.sgl.Len() > 3 { // skip '{}'
    78  			line := string(s.sgl.Bytes())
    79  			if line != r.prev {
    80  				nlog.Infoln(line)
    81  				r.prev = line
    82  			}
    83  		}
    84  		if idle {
    85  			r.next = now + maxStatsLogInterval
    86  		}
    87  	}
    88  }
    89  
    90  func (r *Prunner) statsTime(newval time.Duration) {
    91  	r.core.statsTime = newval
    92  }
    93  
    94  func (*Prunner) standingBy() bool { return false }