github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/metrics/table.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     7  )
     8  
     9  //nolint:funlen
    10  func table(config Config) (t trace.Table) {
    11  	config = config.WithSystem("table")
    12  	alive := config.GaugeVec("sessions", "node_id")
    13  	session := config.WithSystem("session")
    14  	config = config.WithSystem("pool")
    15  	limit := config.GaugeVec("limit")
    16  	index := config.GaugeVec("index")
    17  	idle := config.GaugeVec("idle")
    18  	wait := config.GaugeVec("wait")
    19  	createInProgress := config.GaugeVec("createInProgress")
    20  	get := config.CounterVec("get")
    21  	put := config.CounterVec("put")
    22  	with := config.GaugeVec("with")
    23  	t.OnInit = func(info trace.TableInitStartInfo) func(trace.TableInitDoneInfo) {
    24  		return func(info trace.TableInitDoneInfo) {
    25  			if config.Details()&trace.TableEvents != 0 {
    26  				limit.With(nil).Set(float64(info.Limit))
    27  			}
    28  		}
    29  	}
    30  	t.OnSessionNew = func(info trace.TableSessionNewStartInfo) func(trace.TableSessionNewDoneInfo) {
    31  		return func(info trace.TableSessionNewDoneInfo) {
    32  			if info.Error == nil && config.Details()&trace.TableSessionEvents != 0 {
    33  				alive.With(map[string]string{
    34  					"node_id": idToString(info.Session.NodeID()),
    35  				}).Add(1)
    36  			}
    37  		}
    38  	}
    39  	t.OnSessionDelete = func(info trace.TableSessionDeleteStartInfo) func(trace.TableSessionDeleteDoneInfo) {
    40  		if config.Details()&trace.TableSessionEvents != 0 {
    41  			alive.With(map[string]string{
    42  				"node_id": idToString(info.Session.NodeID()),
    43  			}).Add(-1)
    44  		}
    45  
    46  		return nil
    47  	}
    48  	t.OnPoolWith = func(info trace.TablePoolWithStartInfo) func(trace.TablePoolWithDoneInfo) {
    49  		if config.Details()&trace.TablePoolEvents != 0 {
    50  			with.With(nil).Add(1)
    51  		}
    52  
    53  		return func(info trace.TablePoolWithDoneInfo) {
    54  			if config.Details()&trace.TablePoolEvents != 0 {
    55  				with.With(nil).Add(-1)
    56  			}
    57  		}
    58  	}
    59  	t.OnPoolGet = func(info trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
    60  		return func(info trace.TablePoolGetDoneInfo) {
    61  			if info.Error == nil && config.Details()&trace.TablePoolEvents != 0 {
    62  				get.With(nil).Inc()
    63  			}
    64  		}
    65  	}
    66  	t.OnPoolPut = func(info trace.TablePoolPutStartInfo) func(trace.TablePoolPutDoneInfo) {
    67  		if config.Details()&trace.TablePoolEvents != 0 {
    68  			put.With(nil).Inc()
    69  		}
    70  
    71  		return nil
    72  	}
    73  	t.OnPoolStateChange = func(info trace.TablePoolStateChangeInfo) {
    74  		if config.Details()&trace.TablePoolEvents != 0 {
    75  			limit.With(nil).Set(float64(info.Limit))
    76  			index.With(nil).Set(float64(info.Index))
    77  			idle.With(nil).Set(float64(info.Idle))
    78  			wait.With(nil).Set(float64(info.Wait))
    79  			createInProgress.With(nil).Set(float64(info.CreateInProgress))
    80  		}
    81  	}
    82  	{
    83  		latency := session.WithSystem("query").TimerVec("latency")
    84  		errs := session.WithSystem("query").CounterVec("errs", "status")
    85  		attempts := session.WithSystem("query").HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10})
    86  		t.OnDo = func(info trace.TableDoStartInfo) func(trace.TableDoDoneInfo) {
    87  			start := time.Now()
    88  
    89  			return func(doneInfo trace.TableDoDoneInfo) {
    90  				if config.Details()&trace.TableSessionQueryEvents != 0 {
    91  					latency.With(nil).Record(time.Since(start))
    92  					errs.With(map[string]string{
    93  						"status": errorBrief(doneInfo.Error),
    94  					})
    95  					attempts.With(nil).Record(float64(doneInfo.Attempts))
    96  				}
    97  			}
    98  		}
    99  	}
   100  	{
   101  		latency := session.WithSystem("tx").TimerVec("latency")
   102  		errs := session.WithSystem("tx").CounterVec("errs", "status")
   103  		attempts := session.WithSystem("tx").HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10})
   104  		t.OnDoTx = func(info trace.TableDoTxStartInfo) func(trace.TableDoTxDoneInfo) {
   105  			start := time.Now()
   106  
   107  			return func(doneInfo trace.TableDoTxDoneInfo) {
   108  				if config.Details()&trace.TableSessionQueryEvents != 0 {
   109  					latency.With(nil).Record(time.Since(start))
   110  					errs.With(map[string]string{
   111  						"status": errorBrief(doneInfo.Error),
   112  					})
   113  					attempts.With(nil).Record(float64(doneInfo.Attempts))
   114  				}
   115  			}
   116  		}
   117  	}
   118  
   119  	return t
   120  }