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

     1  // Package api provides native Go-based API/SDK over HTTP(S).
     2  /*
     3   * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package api
     6  
     7  import (
     8  	"net/http"
     9  	"net/url"
    10  
    11  	"github.com/NVIDIA/aistore/api/apc"
    12  	"github.com/NVIDIA/aistore/core/meta"
    13  	"github.com/NVIDIA/aistore/ios"
    14  	"github.com/NVIDIA/aistore/stats"
    15  	jsoniter "github.com/json-iterator/go"
    16  )
    17  
    18  // How to compute throughputs:
    19  //
    20  // - AIS supports several enumerated "kinds" of metrics (for complete enum, see stats/api.go).
    21  // - By convention, metrics that have `KindThroughput` kind are named with ".bps" ("bytes per second") suffix.
    22  // - ".bps" metrics reported by api.GetClusterStats and api.GetDaemonStats are, in fact, cumulative byte numbers.
    23  // - It is the client's responsibility to compute the actual throughputs as only the client knows _when_ exactly
    24  //   the same ".bps" metric was queried the previous time.
    25  //
    26  // See also:
    27  // - api.GetClusterStats
    28  // - api.GetDaemonStats
    29  // - api.GetStatsAndStatus
    30  // - stats/api.go
    31  
    32  //
    33  // cluster ----------------------
    34  //
    35  
    36  func GetClusterStats(bp BaseParams) (res stats.Cluster, err error) {
    37  	bp.Method = http.MethodGet
    38  	reqParams := AllocRp()
    39  	{
    40  		reqParams.BaseParams = bp
    41  		reqParams.Path = apc.URLPathClu.S
    42  		reqParams.Query = url.Values{apc.QparamWhat: []string{apc.WhatNodeStats}}
    43  	}
    44  
    45  	var rawStats stats.ClusterRaw
    46  	_, err = reqParams.DoReqAny(&rawStats)
    47  	FreeRp(reqParams)
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	res.Proxy = rawStats.Proxy
    53  	res.Target = make(map[string]*stats.Node, len(rawStats.Target))
    54  	for tid := range rawStats.Target {
    55  		var ts stats.Node
    56  		if err := jsoniter.Unmarshal(rawStats.Target[tid], &ts); err == nil {
    57  			res.Target[tid] = &ts
    58  		}
    59  	}
    60  	return
    61  }
    62  
    63  //
    64  // node ----------------------
    65  //
    66  
    67  func anyStats(bp BaseParams, sid, what string, out any) (err error) {
    68  	bp.Method = http.MethodGet
    69  	reqParams := AllocRp()
    70  	{
    71  		reqParams.BaseParams = bp
    72  		reqParams.Path = apc.URLPathReverseDae.S
    73  		reqParams.Query = url.Values{apc.QparamWhat: []string{what}}
    74  		reqParams.Header = http.Header{apc.HdrNodeID: []string{sid}}
    75  	}
    76  	_, err = reqParams.DoReqAny(out)
    77  	FreeRp(reqParams)
    78  	return err
    79  }
    80  
    81  func GetDaemonStats(bp BaseParams, node *meta.Snode) (ds *stats.Node, err error) {
    82  	ds = &stats.Node{}
    83  	err = anyStats(bp, node.ID(), apc.WhatNodeStats, ds)
    84  	return ds, err
    85  }
    86  
    87  // returns both node's stats (as above) and extended status
    88  func GetStatsAndStatus(bp BaseParams, node *meta.Snode) (ds *stats.NodeStatus, err error) {
    89  	ds = &stats.NodeStatus{}
    90  	err = anyStats(bp, node.ID(), apc.WhatNodeStatsAndStatus, ds)
    91  	return ds, err
    92  }
    93  
    94  func GetStatsAndStatusV322(bp BaseParams, node *meta.Snode) (ds *stats.NodeStatusV322, err error) {
    95  	ds = &stats.NodeStatusV322{}
    96  	err = anyStats(bp, node.ID(), apc.WhatNodeStatsAndStatusV322, ds)
    97  	return ds, err
    98  }
    99  
   100  func GetDiskStats(bp BaseParams, tid string) (res ios.AllDiskStats, err error) {
   101  	err = anyStats(bp, tid, apc.WhatDiskStats, &res)
   102  	return res, err
   103  }
   104  
   105  //
   106  // reset (cluster | node) stats _or_ only error counters ------------
   107  //
   108  
   109  func ResetClusterStats(bp BaseParams, errorsOnly bool) (err error) {
   110  	return _putCluster(bp, apc.ActMsg{Action: apc.ActResetStats, Value: errorsOnly})
   111  }
   112  
   113  func ResetDaemonStats(bp BaseParams, node *meta.Snode, errorsOnly bool) error {
   114  	return _putDaemon(bp, node.ID(), apc.ActMsg{Action: apc.ActResetStats, Value: errorsOnly})
   115  }