github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/metrics/query.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 query(config Config) (t trace.Query) { 11 queryConfig := config.WithSystem("query") 12 { 13 poolConfig := queryConfig.WithSystem("pool") 14 { 15 withConfig := poolConfig.WithSystem("with") 16 errs := withConfig.CounterVec("errs", "status") 17 latency := withConfig.TimerVec("latency") 18 attempts := withConfig.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}) 19 t.OnPoolWith = func( 20 info trace.QueryPoolWithStartInfo, 21 ) func( 22 info trace.QueryPoolWithDoneInfo, 23 ) { 24 if withConfig.Details()&trace.QueryPoolEvents == 0 { 25 return nil 26 } 27 28 start := time.Now() 29 30 return func(info trace.QueryPoolWithDoneInfo) { 31 attempts.With(nil).Record(float64(info.Attempts)) 32 if info.Error != nil { 33 errs.With(map[string]string{ 34 "status": errorBrief(info.Error), 35 }).Inc() 36 } 37 latency.With(nil).Record(time.Since(start)) 38 } 39 } 40 } 41 { 42 sizeConfig := poolConfig.WithSystem("size") 43 limit := sizeConfig.GaugeVec("limit") 44 idle := sizeConfig.GaugeVec("idle") 45 index := sizeConfig.GaugeVec("index") 46 t.OnPoolChange = func(stats trace.QueryPoolChange) { 47 if sizeConfig.Details()&trace.QueryPoolEvents == 0 { 48 return 49 } 50 51 limit.With(nil).Set(float64(stats.Limit)) 52 idle.With(nil).Set(float64(stats.Idle)) 53 index.With(nil).Set(float64(stats.Index)) 54 } 55 } 56 } 57 { 58 doConfig := queryConfig.WithSystem("do") 59 { 60 errs := doConfig.CounterVec("errs", "status") 61 attempts := doConfig.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}) 62 latency := doConfig.TimerVec("latency") 63 t.OnDo = func( 64 info trace.QueryDoStartInfo, 65 ) func( 66 trace.QueryDoDoneInfo, 67 ) { 68 start := time.Now() 69 70 return func(info trace.QueryDoDoneInfo) { 71 if doConfig.Details()&trace.QueryEvents != 0 { 72 errs.With(map[string]string{ 73 "status": errorBrief(info.Error), 74 }).Inc() 75 attempts.With(nil).Record(float64(info.Attempts)) 76 latency.With(nil).Record(time.Since(start)) 77 } 78 } 79 } 80 } 81 { 82 doTxConfig := doConfig.WithSystem("tx") 83 errs := doTxConfig.CounterVec("errs", "status") 84 attempts := doTxConfig.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}) 85 latency := doTxConfig.TimerVec("latency") 86 t.OnDoTx = func( 87 info trace.QueryDoTxStartInfo, 88 ) func( 89 trace.QueryDoTxDoneInfo, 90 ) { 91 start := time.Now() 92 93 return func(info trace.QueryDoTxDoneInfo) { 94 if doTxConfig.Details()&trace.QueryEvents != 0 { 95 attempts.With(nil).Record(float64(info.Attempts)) 96 errs.With(map[string]string{ 97 "status": errorBrief(info.Error), 98 }).Inc() 99 latency.With(nil).Record(time.Since(start)) 100 } 101 } 102 } 103 } 104 } 105 { 106 sessionConfig := queryConfig.WithSystem("session") 107 count := sessionConfig.GaugeVec("count") 108 { 109 createConfig := sessionConfig.WithSystem("create") 110 errs := createConfig.CounterVec("errs", "status") 111 latency := createConfig.TimerVec("latency") 112 t.OnSessionCreate = func( 113 info trace.QuerySessionCreateStartInfo, 114 ) func( 115 info trace.QuerySessionCreateDoneInfo, 116 ) { 117 start := time.Now() 118 119 return func(info trace.QuerySessionCreateDoneInfo) { 120 if createConfig.Details()&trace.QuerySessionEvents != 0 { 121 if info.Error == nil { 122 count.With(nil).Add(1) 123 } 124 errs.With(map[string]string{ 125 "status": errorBrief(info.Error), 126 }).Inc() 127 latency.With(nil).Record(time.Since(start)) 128 } 129 } 130 } 131 } 132 { 133 deleteConfig := sessionConfig.WithSystem("delete") 134 errs := deleteConfig.CounterVec("errs", "status") 135 latency := deleteConfig.TimerVec("latency") 136 t.OnSessionDelete = func(info trace.QuerySessionDeleteStartInfo) func(info trace.QuerySessionDeleteDoneInfo) { 137 start := time.Now() 138 139 return func(info trace.QuerySessionDeleteDoneInfo) { 140 if deleteConfig.Details()&trace.QuerySessionEvents != 0 { 141 count.With(nil).Add(-1) 142 errs.With(map[string]string{ 143 "status": errorBrief(info.Error), 144 }).Inc() 145 latency.With(nil).Record(time.Since(start)) 146 } 147 } 148 } 149 } 150 { 151 sessionExecConfig := sessionConfig.WithSystem("exec") 152 errs := sessionExecConfig.CounterVec("errs", "status") 153 latency := sessionExecConfig.TimerVec("latency") 154 t.OnSessionExec = func(info trace.QuerySessionExecStartInfo) func(info trace.QuerySessionExecDoneInfo) { 155 start := time.Now() 156 157 return func(info trace.QuerySessionExecDoneInfo) { 158 if sessionExecConfig.Details()&trace.QuerySessionEvents != 0 { 159 errs.With(map[string]string{ 160 "status": errorBrief(info.Error), 161 }).Inc() 162 latency.With(nil).Record(time.Since(start)) 163 } 164 } 165 } 166 } 167 { 168 sessionQueryConfig := sessionConfig.WithSystem("query") 169 errs := sessionQueryConfig.CounterVec("errs", "status") 170 latency := sessionQueryConfig.TimerVec("latency") 171 t.OnSessionQuery = func(info trace.QuerySessionQueryStartInfo) func(info trace.QuerySessionQueryDoneInfo) { 172 start := time.Now() 173 174 return func(info trace.QuerySessionQueryDoneInfo) { 175 if sessionQueryConfig.Details()&trace.QuerySessionEvents != 0 { 176 errs.With(map[string]string{ 177 "status": errorBrief(info.Error), 178 }).Inc() 179 latency.With(nil).Record(time.Since(start)) 180 } 181 } 182 } 183 } 184 { 185 beginConfig := sessionConfig.WithSystem("begin") 186 errs := beginConfig.CounterVec("errs", "status") 187 latency := beginConfig.TimerVec("latency") 188 t.OnSessionBegin = func(info trace.QuerySessionBeginStartInfo) func(info trace.QuerySessionBeginDoneInfo) { 189 start := time.Now() 190 191 return func(info trace.QuerySessionBeginDoneInfo) { 192 if beginConfig.Details()&trace.QuerySessionEvents != 0 { 193 errs.With(map[string]string{ 194 "status": errorBrief(info.Error), 195 }).Inc() 196 latency.With(nil).Record(time.Since(start)) 197 } 198 } 199 } 200 } 201 } 202 { 203 txConfig := queryConfig.WithSystem("tx") 204 { 205 txExecConfig := txConfig.WithSystem("exec") 206 errs := txExecConfig.CounterVec("errs", "status") 207 latency := txExecConfig.TimerVec("latency") 208 t.OnTxExec = func(info trace.QueryTxExecStartInfo) func(info trace.QueryTxExecDoneInfo) { 209 start := time.Now() 210 211 return func(info trace.QueryTxExecDoneInfo) { 212 if txExecConfig.Details()&trace.QuerySessionEvents != 0 { 213 errs.With(map[string]string{ 214 "status": errorBrief(info.Error), 215 }).Inc() 216 latency.With(nil).Record(time.Since(start)) 217 } 218 } 219 } 220 } 221 { 222 txQueryConfig := txConfig.WithSystem("query") 223 errs := txQueryConfig.CounterVec("errs", "status") 224 latency := txQueryConfig.TimerVec("latency") 225 t.OnTxQuery = func(info trace.QueryTxQueryStartInfo) func(info trace.QueryTxQueryDoneInfo) { 226 start := time.Now() 227 228 return func(info trace.QueryTxQueryDoneInfo) { 229 if txQueryConfig.Details()&trace.QuerySessionEvents != 0 { 230 errs.With(map[string]string{ 231 "status": errorBrief(info.Error), 232 }).Inc() 233 latency.With(nil).Record(time.Since(start)) 234 } 235 } 236 } 237 } 238 } 239 240 return t 241 }