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

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     9  )
    10  
    11  func table(config Config) (t trace.Table) {
    12  	config = config.WithSystem("table")
    13  	alive := config.GaugeVec("sessions", "node_id")
    14  	config = config.WithSystem("pool")
    15  	limit := config.GaugeVec("limit")
    16  	size := config.GaugeVec("size")
    17  	inflight := config.GaugeVec("inflight")
    18  	inflightLatency := config.WithSystem("inflight").TimerVec("latency")
    19  	wait := config.GaugeVec("wait")
    20  	waitLatency := config.WithSystem("wait").TimerVec("latency")
    21  	t.OnInit = func(info trace.TableInitStartInfo) func(trace.TableInitDoneInfo) {
    22  		return func(info trace.TableInitDoneInfo) {
    23  			limit.With(nil).Set(float64(info.Limit))
    24  		}
    25  	}
    26  	t.OnSessionNew = func(info trace.TableSessionNewStartInfo) func(trace.TableSessionNewDoneInfo) {
    27  		return func(info trace.TableSessionNewDoneInfo) {
    28  			if info.Error == nil && config.Details()&trace.TableSessionEvents != 0 {
    29  				alive.With(map[string]string{
    30  					"node_id": idToString(info.Session.NodeID()),
    31  				}).Add(1)
    32  			}
    33  		}
    34  	}
    35  	t.OnSessionDelete = func(info trace.TableSessionDeleteStartInfo) func(trace.TableSessionDeleteDoneInfo) {
    36  		if config.Details()&trace.TableSessionEvents != 0 {
    37  			alive.With(map[string]string{
    38  				"node_id": idToString(info.Session.NodeID()),
    39  			}).Add(-1)
    40  		}
    41  
    42  		return nil
    43  	}
    44  	t.OnPoolSessionAdd = func(info trace.TablePoolSessionAddInfo) {
    45  		if config.Details()&trace.TablePoolEvents != 0 {
    46  			size.With(nil).Add(1)
    47  		}
    48  	}
    49  	t.OnPoolSessionRemove = func(info trace.TablePoolSessionRemoveInfo) {
    50  		if config.Details()&trace.TablePoolEvents != 0 {
    51  			size.With(nil).Add(-1)
    52  		}
    53  	}
    54  	var inflightStarts sync.Map
    55  	t.OnPoolGet = func(info trace.TablePoolGetStartInfo) func(trace.TablePoolGetDoneInfo) {
    56  		wait.With(nil).Add(1)
    57  		start := time.Now()
    58  
    59  		return func(info trace.TablePoolGetDoneInfo) {
    60  			wait.With(nil).Add(-1)
    61  			if info.Error == nil && config.Details()&trace.TablePoolEvents != 0 {
    62  				inflight.With(nil).Add(1)
    63  				inflightStarts.Store(info.Session.ID(), time.Now())
    64  				waitLatency.With(nil).Record(time.Since(start))
    65  			}
    66  		}
    67  	}
    68  	t.OnPoolPut = func(info trace.TablePoolPutStartInfo) func(trace.TablePoolPutDoneInfo) {
    69  		if config.Details()&trace.TablePoolEvents != 0 {
    70  			inflight.With(nil).Add(-1)
    71  			start, ok := inflightStarts.LoadAndDelete(info.Session.ID())
    72  			if !ok {
    73  				panic(fmt.Sprintf("unknown session '%s'", info.Session.ID()))
    74  			}
    75  			inflightLatency.With(nil).Record(time.Since(start.(time.Time)))
    76  		}
    77  
    78  		return nil
    79  	}
    80  
    81  	return t
    82  }