@@ -8,6 +8,12 @@ import (
88 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
99)
1010
11+ // endpointKey represents a key for mapping endpoints to their properties.
12+ type endpointKey struct {
13+ localDC bool // a boolean indicating if the endpoint is in the local data center
14+ az string // a string representing the availability zone of the endpoint
15+ }
16+
1117// driver makes driver with New publishing
1218func driver (config Config ) (t trace.Driver ) {
1319 config = config .WithSystem ("driver" )
@@ -19,21 +25,37 @@ func driver(config Config) (t trace.Driver) {
1925 requests := config .WithSystem ("conn" ).CounterVec ("requests" , "status" , "method" , "endpoint" , "node_id" )
2026 tli := config .CounterVec ("transaction_locks_invalidated" )
2127
22- type endpointKey struct {
23- localDC bool
24- az string
25- }
2628 knownEndpoints := make (map [endpointKey ]struct {})
29+ driverConnEvents := config .Details () & trace .DriverConnEvents
30+ driverBalancerEvents := config .Details () & trace .DriverBalancerEvents
2731
28- t .OnConnInvoke = func (info trace.DriverConnInvokeStartInfo ) func (trace.DriverConnInvokeDoneInfo ) {
32+ t .OnConnInvoke = connInvoke (driverConnEvents , requests , tli )
33+ t .OnConnNewStream = connNewStream (driverConnEvents , requests )
34+ t .OnConnBan = connBan (driverConnEvents , banned )
35+ t .OnBalancerClusterDiscoveryAttempt = balancerClusterDiscoveryAttempt (balancersDiscoveries )
36+ t .OnBalancerUpdate = balancerUpdate (driverBalancerEvents , balancerUpdates , knownEndpoints , endpoints )
37+ t .OnConnDial = connDial (driverConnEvents , conns )
38+ t .OnConnClose = connClose (driverConnEvents , conns )
39+
40+ return t
41+ }
42+
43+ // connInvoke is a function that returns a callback function to be called
44+ // when a driver connection invoke starts and when it is done.
45+ func connInvoke (
46+ driverConnEvents trace.Details ,
47+ requests CounterVec ,
48+ tli CounterVec ,
49+ ) func (info trace.DriverConnInvokeStartInfo ) func (trace.DriverConnInvokeDoneInfo ) {
50+ return func (info trace.DriverConnInvokeStartInfo ) func (trace.DriverConnInvokeDoneInfo ) {
2951 var (
3052 method = info .Method
3153 endpoint = info .Endpoint .Address ()
3254 nodeID = info .Endpoint .NodeID ()
3355 )
3456
3557 return func (info trace.DriverConnInvokeDoneInfo ) {
36- if config . Details () & trace . DriverConnEvents != 0 {
58+ if driverConnEvents != 0 {
3759 requests .With (map [string ]string {
3860 "status" : errorBrief (info .Error ),
3961 "method" : string (method ),
@@ -46,7 +68,16 @@ func driver(config Config) (t trace.Driver) {
4668 }
4769 }
4870 }
49- t .OnConnNewStream = func (info trace.DriverConnNewStreamStartInfo ) func (
71+ }
72+
73+ // connNewStream receives the `driverConnEvents` and `requests` parameters and returns a closure function.
74+ func connNewStream (
75+ driverConnEvents trace.Details ,
76+ requests CounterVec ,
77+ ) func (
78+ info trace.DriverConnNewStreamStartInfo ,
79+ ) func (trace.DriverConnNewStreamRecvInfo ) func (trace.DriverConnNewStreamDoneInfo ) {
80+ return func (info trace.DriverConnNewStreamStartInfo ) func (
5081 trace.DriverConnNewStreamRecvInfo ,
5182 ) func (
5283 trace.DriverConnNewStreamDoneInfo ,
@@ -59,7 +90,7 @@ func driver(config Config) (t trace.Driver) {
5990
6091 return func (info trace.DriverConnNewStreamRecvInfo ) func (trace.DriverConnNewStreamDoneInfo ) {
6192 return func (info trace.DriverConnNewStreamDoneInfo ) {
62- if config . Details () & trace . DriverConnEvents != 0 {
93+ if driverConnEvents != 0 {
6394 requests .With (map [string ]string {
6495 "status" : errorBrief (info .Error ),
6596 "method" : string (method ),
@@ -70,8 +101,15 @@ func driver(config Config) (t trace.Driver) {
70101 }
71102 }
72103 }
73- t .OnConnBan = func (info trace.DriverConnBanStartInfo ) func (trace.DriverConnBanDoneInfo ) {
74- if config .Details ()& trace .DriverConnEvents != 0 {
104+ }
105+
106+ // connBan is a function that returns a closure wrapping the logic for tracing a connection ban event.
107+ func connBan (
108+ driverConnEvents trace.Details ,
109+ banned GaugeVec ,
110+ ) func (info trace.DriverConnBanStartInfo ) func (trace.DriverConnBanDoneInfo ) {
111+ return func (info trace.DriverConnBanStartInfo ) func (trace.DriverConnBanDoneInfo ) {
112+ if driverConnEvents != 0 {
75113 banned .With (map [string ]string {
76114 "endpoint" : info .Endpoint .Address (),
77115 "node_id" : idToString (info .Endpoint .NodeID ()),
@@ -81,7 +119,15 @@ func driver(config Config) (t trace.Driver) {
81119
82120 return nil
83121 }
84- t .OnBalancerClusterDiscoveryAttempt = func (info trace.DriverBalancerClusterDiscoveryAttemptStartInfo ) func (
122+ }
123+
124+ // balancerClusterDiscoveryAttempt performs balancer cluster discovery attempt.
125+ func balancerClusterDiscoveryAttempt (
126+ balancersDiscoveries CounterVec ,
127+ ) func (
128+ info trace.DriverBalancerClusterDiscoveryAttemptStartInfo ,
129+ ) func (trace.DriverBalancerClusterDiscoveryAttemptDoneInfo ) {
130+ return func (info trace.DriverBalancerClusterDiscoveryAttemptStartInfo ) func (
85131 trace.DriverBalancerClusterDiscoveryAttemptDoneInfo ,
86132 ) {
87133 eventType := repeater .EventType (* info .Context )
@@ -93,11 +139,20 @@ func driver(config Config) (t trace.Driver) {
93139 }).Inc ()
94140 }
95141 }
96- t .OnBalancerUpdate = func (info trace.DriverBalancerUpdateStartInfo ) func (trace.DriverBalancerUpdateDoneInfo ) {
142+ }
143+
144+ // balancerUpdate updates the balancer with new endpoint information.
145+ func balancerUpdate (
146+ driverBalancerEvents trace.Details ,
147+ balancerUpdates CounterVec ,
148+ knownEndpoints map [endpointKey ]struct {},
149+ endpoints GaugeVec ,
150+ ) func (info trace.DriverBalancerUpdateStartInfo ) func (trace.DriverBalancerUpdateDoneInfo ) {
151+ return func (info trace.DriverBalancerUpdateStartInfo ) func (trace.DriverBalancerUpdateDoneInfo ) {
97152 eventType := repeater .EventType (* info .Context )
98153
99154 return func (info trace.DriverBalancerUpdateDoneInfo ) {
100- if config . Details () & trace . DriverBalancerEvents != 0 {
155+ if driverBalancerEvents != 0 {
101156 balancerUpdates .With (map [string ]string {
102157 "cause" : eventType ,
103158 }).Inc ()
@@ -128,12 +183,20 @@ func driver(config Config) (t trace.Driver) {
128183 }
129184 }
130185 }
131- t .OnConnDial = func (info trace.DriverConnDialStartInfo ) func (trace.DriverConnDialDoneInfo ) {
186+ }
187+
188+ // connDial is a function that returns a closure function to handle the event when a driver connection dialing starts
189+ // and completes.
190+ func connDial (
191+ driverConnEvents trace.Details ,
192+ conns GaugeVec ,
193+ ) func (info trace.DriverConnDialStartInfo ) func (trace.DriverConnDialDoneInfo ) {
194+ return func (info trace.DriverConnDialStartInfo ) func (trace.DriverConnDialDoneInfo ) {
132195 endpoint := info .Endpoint .Address ()
133196 nodeID := info .Endpoint .NodeID ()
134197
135198 return func (info trace.DriverConnDialDoneInfo ) {
136- if config . Details () & trace . DriverConnEvents != 0 {
199+ if driverConnEvents != 0 {
137200 if info .Error == nil {
138201 conns .With (map [string ]string {
139202 "endpoint" : endpoint ,
@@ -143,8 +206,15 @@ func driver(config Config) (t trace.Driver) {
143206 }
144207 }
145208 }
146- t .OnConnClose = func (info trace.DriverConnCloseStartInfo ) func (trace.DriverConnCloseDoneInfo ) {
147- if config .Details ()& trace .DriverConnEvents != 0 {
209+ }
210+
211+ // connClose is a function that returns a closure accepting a trace.DriverConnCloseStartInfo parameter
212+ func connClose (
213+ driverConnEvents trace.Details ,
214+ conns GaugeVec ,
215+ ) func (info trace.DriverConnCloseStartInfo ) func (trace.DriverConnCloseDoneInfo ) {
216+ return func (info trace.DriverConnCloseStartInfo ) func (trace.DriverConnCloseDoneInfo ) {
217+ if driverConnEvents != 0 {
148218 conns .With (map [string ]string {
149219 "endpoint" : info .Endpoint .Address (),
150220 "node_id" : idToString (info .Endpoint .NodeID ()),
@@ -153,6 +223,4 @@ func driver(config Config) (t trace.Driver) {
153223
154224 return nil
155225 }
156-
157- return t
158226}
0 commit comments