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

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     7  )
     8  
     9  // databaseSQL makes trace.DatabaseSQL with measuring `database/sql` events
    10  func databaseSQL(config Config) (t trace.DatabaseSQL) {
    11  	config = config.WithSystem("database").WithSystem("sql")
    12  	conns := config.GaugeVec("conns")
    13  	inflight := config.WithSystem("conns").GaugeVec("inflight")
    14  	query := config.CounterVec("query", "status", "query_mode")
    15  	queryLatency := config.WithSystem("query").TimerVec("latency", "query_mode")
    16  	exec := config.CounterVec("exec", "status", "query_mode")
    17  	execLatency := config.WithSystem("exec").TimerVec("latency", "query_mode")
    18  
    19  	config = config.WithSystem("tx")
    20  	txBegin := config.CounterVec("begin", "status")
    21  	txBeginLatency := config.WithSystem("begin").TimerVec("latency")
    22  	txExec := config.CounterVec("exec", "status")
    23  	txExecLatency := config.WithSystem("exec").TimerVec("latency")
    24  	txQuery := config.CounterVec("query", "status")
    25  	txQueryLatency := config.WithSystem("query").TimerVec("latency")
    26  	txCommit := config.CounterVec("commit", "status")
    27  	txCommitLatency := config.WithSystem("commit").TimerVec("latency")
    28  	txRollback := config.CounterVec("rollback", "status")
    29  	txRollbackLatency := config.WithSystem("rollback").TimerVec("latency")
    30  	t.OnConnectorConnect = func(info trace.DatabaseSQLConnectorConnectStartInfo) func(
    31  		trace.DatabaseSQLConnectorConnectDoneInfo,
    32  	) {
    33  		if config.Details()&trace.DatabaseSQLConnectorEvents != 0 {
    34  			return func(info trace.DatabaseSQLConnectorConnectDoneInfo) {
    35  				if info.Error == nil {
    36  					conns.With(nil).Add(1)
    37  				}
    38  			}
    39  		}
    40  
    41  		return nil
    42  	}
    43  	t.OnConnClose = func(info trace.DatabaseSQLConnCloseStartInfo) func(trace.DatabaseSQLConnCloseDoneInfo) {
    44  		if config.Details()&trace.DatabaseSQLConnectorEvents != 0 {
    45  			return func(info trace.DatabaseSQLConnCloseDoneInfo) {
    46  				conns.With(nil).Add(-1)
    47  			}
    48  		}
    49  
    50  		return nil
    51  	}
    52  	t.OnConnBegin = func(info trace.DatabaseSQLConnBeginStartInfo) func(trace.DatabaseSQLConnBeginDoneInfo) {
    53  		start := time.Now()
    54  		if config.Details()&trace.DatabaseSQLTxEvents != 0 {
    55  			return func(info trace.DatabaseSQLConnBeginDoneInfo) {
    56  				txBegin.With(map[string]string{
    57  					"status": errorBrief(info.Error),
    58  				}).Inc()
    59  				txBeginLatency.With(nil).Record(time.Since(start))
    60  			}
    61  		}
    62  
    63  		return nil
    64  	}
    65  	t.OnTxCommit = func(info trace.DatabaseSQLTxCommitStartInfo) func(trace.DatabaseSQLTxCommitDoneInfo) {
    66  		start := time.Now()
    67  
    68  		return func(info trace.DatabaseSQLTxCommitDoneInfo) {
    69  			if config.Details()&trace.DatabaseSQLTxEvents != 0 {
    70  				txCommit.With(map[string]string{
    71  					"status": errorBrief(info.Error),
    72  				}).Inc()
    73  				txCommitLatency.With(nil).Record(time.Since(start))
    74  			}
    75  		}
    76  	}
    77  	t.OnTxExec = func(info trace.DatabaseSQLTxExecStartInfo) func(trace.DatabaseSQLTxExecDoneInfo) {
    78  		start := time.Now()
    79  
    80  		return func(info trace.DatabaseSQLTxExecDoneInfo) {
    81  			if config.Details()&trace.DatabaseSQLTxEvents != 0 {
    82  				status := errorBrief(info.Error)
    83  				txExec.With(map[string]string{
    84  					"status": status,
    85  				}).Inc()
    86  				txExecLatency.With(nil).Record(time.Since(start))
    87  			}
    88  		}
    89  	}
    90  	t.OnTxQuery = func(info trace.DatabaseSQLTxQueryStartInfo) func(trace.DatabaseSQLTxQueryDoneInfo) {
    91  		start := time.Now()
    92  
    93  		return func(info trace.DatabaseSQLTxQueryDoneInfo) {
    94  			if config.Details()&trace.DatabaseSQLTxEvents != 0 {
    95  				status := errorBrief(info.Error)
    96  				txQuery.With(map[string]string{
    97  					"status": status,
    98  				}).Inc()
    99  				txQueryLatency.With(nil).Record(time.Since(start))
   100  			}
   101  		}
   102  	}
   103  	t.OnTxRollback = func(info trace.DatabaseSQLTxRollbackStartInfo) func(trace.DatabaseSQLTxRollbackDoneInfo) {
   104  		start := time.Now()
   105  
   106  		return func(info trace.DatabaseSQLTxRollbackDoneInfo) {
   107  			if config.Details()&trace.DatabaseSQLTxEvents != 0 {
   108  				txRollback.With(map[string]string{
   109  					"status": errorBrief(info.Error),
   110  				}).Inc()
   111  				txRollbackLatency.With(nil).Record(time.Since(start))
   112  			}
   113  		}
   114  	}
   115  	t.OnConnExec = func(info trace.DatabaseSQLConnExecStartInfo) func(trace.DatabaseSQLConnExecDoneInfo) {
   116  		if config.Details()&trace.DatabaseSQLEvents != 0 {
   117  			inflight.With(nil).Add(1)
   118  		}
   119  		var (
   120  			mode  = info.Mode
   121  			start = time.Now()
   122  		)
   123  
   124  		return func(info trace.DatabaseSQLConnExecDoneInfo) {
   125  			if config.Details()&trace.DatabaseSQLEvents != 0 {
   126  				inflight.With(nil).Add(-1)
   127  			}
   128  			if config.Details()&trace.DatabaseSQLConnEvents != 0 {
   129  				status := errorBrief(info.Error)
   130  				exec.With(map[string]string{
   131  					"status":     status,
   132  					"query_mode": mode,
   133  				}).Inc()
   134  				execLatency.With(map[string]string{
   135  					"query_mode": mode,
   136  				}).Record(time.Since(start))
   137  			}
   138  		}
   139  	}
   140  	t.OnConnQuery = func(info trace.DatabaseSQLConnQueryStartInfo) func(trace.DatabaseSQLConnQueryDoneInfo) {
   141  		if config.Details()&trace.DatabaseSQLEvents != 0 {
   142  			inflight.With(nil).Add(1)
   143  		}
   144  		var (
   145  			mode  = info.Mode
   146  			start = time.Now()
   147  		)
   148  
   149  		return func(info trace.DatabaseSQLConnQueryDoneInfo) {
   150  			if config.Details()&trace.DatabaseSQLEvents != 0 {
   151  				inflight.With(nil).Add(-1)
   152  			}
   153  			if config.Details()&trace.DatabaseSQLConnEvents != 0 {
   154  				status := errorBrief(info.Error)
   155  				query.With(map[string]string{
   156  					"status":     status,
   157  					"query_mode": mode,
   158  				}).Inc()
   159  				queryLatency.With(map[string]string{
   160  					"query_mode": mode,
   161  				}).Record(time.Since(start))
   162  			}
   163  		}
   164  	}
   165  
   166  	return t
   167  }