|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/ydb-platform/ydb-go-sdk/v3/trace" |
| 7 | +) |
| 8 | + |
| 9 | +func query(config Config) (t trace.Query) { |
| 10 | + queryConfig := config.WithSystem("query") |
| 11 | + { |
| 12 | + doConfig := queryConfig.WithSystem("do") |
| 13 | + { |
| 14 | + intermediateErrs := doConfig.WithSystem("intermediate").CounterVec("errs", "status") |
| 15 | + errs := doConfig.CounterVec("errs", "status") |
| 16 | + attempts := doConfig.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}) |
| 17 | + latency := doConfig.TimerVec("latency") |
| 18 | + t.OnDo = func( |
| 19 | + info trace.QueryDoStartInfo, |
| 20 | + ) func( |
| 21 | + info trace.QueryDoIntermediateInfo, |
| 22 | + ) func( |
| 23 | + trace.QueryDoDoneInfo, |
| 24 | + ) { |
| 25 | + start := time.Now() |
| 26 | + |
| 27 | + return func(info trace.QueryDoIntermediateInfo) func(trace.QueryDoDoneInfo) { |
| 28 | + if info.Error != nil && doConfig.Details()&trace.QueryEvents != 0 { |
| 29 | + intermediateErrs.With(map[string]string{ |
| 30 | + "status": errorBrief(info.Error), |
| 31 | + }).Inc() |
| 32 | + } |
| 33 | + |
| 34 | + return func(info trace.QueryDoDoneInfo) { |
| 35 | + if doConfig.Details()&trace.QueryEvents != 0 { |
| 36 | + attempts.With(nil).Record(float64(info.Attempts)) |
| 37 | + errs.With(map[string]string{ |
| 38 | + "status": errorBrief(info.Error), |
| 39 | + }).Inc() |
| 40 | + latency.With(nil).Record(time.Since(start)) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + { |
| 47 | + doTxConfig := doConfig.WithSystem("tx") |
| 48 | + intermediateErrs := doTxConfig.WithSystem("intermediate").CounterVec("errs", "status") |
| 49 | + errs := doTxConfig.CounterVec("errs", "status") |
| 50 | + attempts := doTxConfig.HistogramVec("attempts", []float64{0, 1, 2, 3, 4, 5, 7, 10}) |
| 51 | + latency := doTxConfig.TimerVec("latency") |
| 52 | + t.OnDoTx = func( |
| 53 | + info trace.QueryDoTxStartInfo, |
| 54 | + ) func( |
| 55 | + info trace.QueryDoTxIntermediateInfo, |
| 56 | + ) func( |
| 57 | + trace.QueryDoTxDoneInfo, |
| 58 | + ) { |
| 59 | + start := time.Now() |
| 60 | + |
| 61 | + return func(info trace.QueryDoTxIntermediateInfo) func(trace.QueryDoTxDoneInfo) { |
| 62 | + if info.Error != nil && doTxConfig.Details()&trace.QueryEvents != 0 { |
| 63 | + intermediateErrs.With(map[string]string{ |
| 64 | + "status": errorBrief(info.Error), |
| 65 | + }).Inc() |
| 66 | + } |
| 67 | + |
| 68 | + return func(info trace.QueryDoTxDoneInfo) { |
| 69 | + if doTxConfig.Details()&trace.QueryEvents != 0 { |
| 70 | + attempts.With(nil).Record(float64(info.Attempts)) |
| 71 | + errs.With(map[string]string{ |
| 72 | + "status": errorBrief(info.Error), |
| 73 | + }).Inc() |
| 74 | + latency.With(nil).Record(time.Since(start)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + { |
| 82 | + sessionConfig := queryConfig.WithSystem("session") |
| 83 | + { |
| 84 | + createConfig := sessionConfig.WithSystem("create") |
| 85 | + errs := createConfig.CounterVec("errs", "status") |
| 86 | + latency := createConfig.TimerVec("latency") |
| 87 | + t.OnCreateSession = func( |
| 88 | + info trace.QueryCreateSessionStartInfo, |
| 89 | + ) func( |
| 90 | + info trace.QueryCreateSessionDoneInfo, |
| 91 | + ) { |
| 92 | + start := time.Now() |
| 93 | + |
| 94 | + return func(info trace.QueryCreateSessionDoneInfo) { |
| 95 | + if info.Error != nil && createConfig.Details()&trace.QuerySessionEvents != 0 { |
| 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 | + deleteConfig := sessionConfig.WithSystem("delete") |
| 106 | + errs := deleteConfig.CounterVec("errs", "status") |
| 107 | + latency := deleteConfig.TimerVec("latency") |
| 108 | + t.OnCreateSession = func( |
| 109 | + info trace.QueryCreateSessionStartInfo, |
| 110 | + ) func( |
| 111 | + info trace.QueryCreateSessionDoneInfo, |
| 112 | + ) { |
| 113 | + start := time.Now() |
| 114 | + |
| 115 | + return func(info trace.QueryCreateSessionDoneInfo) { |
| 116 | + if info.Error != nil && deleteConfig.Details()&trace.QuerySessionEvents != 0 { |
| 117 | + errs.With(map[string]string{ |
| 118 | + "status": errorBrief(info.Error), |
| 119 | + }).Inc() |
| 120 | + latency.With(nil).Record(time.Since(start)) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return t |
| 128 | +} |
0 commit comments