@@ -40,27 +40,52 @@ where
40
40
}
41
41
42
42
pub async fn run ( & mut self , token : CancellationToken ) {
43
- info ! ( "starting upstream subscription" ) ;
43
+ // Added the URI to the log message for better identification
44
+ info ! (
45
+ message = "starting upstream subscription" ,
46
+ uri = self . uri. to_string( )
47
+ ) ;
44
48
loop {
45
49
select ! {
46
50
_ = token. cancelled( ) => {
47
- info!( "cancelled upstream subscription" ) ;
51
+ info!(
52
+ message = "cancelled upstream subscription" ,
53
+ uri = self . uri. to_string( )
54
+ ) ;
48
55
return ;
49
56
}
50
57
result = self . connect_and_listen( ) => {
51
58
match result {
52
59
Ok ( ( ) ) => {
53
- info!( message="upstream connection closed" ) ;
60
+ info!(
61
+ message = "upstream connection closed" ,
62
+ uri = self . uri. to_string( )
63
+ ) ;
54
64
}
55
65
Err ( e) => {
56
- error!( message="upstream websocket error" , error=e. to_string( ) ) ;
66
+ // Added URI to the error log for better debugging
67
+ error!(
68
+ message = "upstream websocket error" ,
69
+ uri = self . uri. to_string( ) ,
70
+ error = e. to_string( )
71
+ ) ;
57
72
self . metrics. upstream_errors. increment( 1 ) ;
73
+ // Decrement the active connections count when connection fails
74
+ self . metrics. upstream_connections. decrement( 1 ) ;
58
75
59
76
if let Some ( duration) = self . backoff. next_backoff( ) {
60
- warn!( message="recconecting" , seconds=duration. as_secs( ) ) ;
77
+ // Added URI to the warning message
78
+ warn!(
79
+ message = "reconnecting" ,
80
+ uri = self . uri. to_string( ) ,
81
+ seconds = duration. as_secs( )
82
+ ) ;
61
83
select! {
62
84
_ = token. cancelled( ) => {
63
- info!( message="cancelled subscriber during backoff" ) ;
85
+ info!(
86
+ message = "cancelled subscriber during backoff" ,
87
+ uri = self . uri. to_string( )
88
+ ) ;
64
89
return
65
90
}
66
91
_ = tokio:: time:: sleep( duration) => { }
@@ -79,8 +104,31 @@ where
79
104
uri = self . uri. to_string( )
80
105
) ;
81
106
82
- let ( ws_stream, _) = connect_async ( & self . uri ) . await ?;
83
- info ! ( message = "websocket connection established" ) ;
107
+ // Increment connection attempts counter for metrics
108
+ self . metrics . upstream_connection_attempts . increment ( 1 ) ;
109
+
110
+ // Modified connection with success/failure metrics tracking
111
+ let ( ws_stream, _) = match connect_async ( & self . uri ) . await {
112
+ Ok ( connection) => {
113
+ // Track successful connections
114
+ self . metrics . upstream_connection_successes . increment ( 1 ) ;
115
+ connection
116
+ } ,
117
+ Err ( e) => {
118
+ // Track failed connections
119
+ self . metrics . upstream_connection_failures . increment ( 1 ) ;
120
+ return Err ( e) ;
121
+ }
122
+ } ;
123
+
124
+ info ! (
125
+ message = "websocket connection established" ,
126
+ uri = self . uri. to_string( )
127
+ ) ;
128
+
129
+ // Increment active connections counter
130
+ self . metrics . upstream_connections . increment ( 1 ) ;
131
+ // Reset backoff timer on successful connection
84
132
self . backoff . reset ( ) ;
85
133
86
134
let ( _, mut read) = ws_stream. split ( ) ;
@@ -89,12 +137,20 @@ where
89
137
match message {
90
138
Ok ( msg) => {
91
139
let text = msg. to_text ( ) ?;
92
- trace ! ( message = "received message" , payload = text) ;
140
+ trace ! (
141
+ message = "received message" ,
142
+ uri = self . uri. to_string( ) ,
143
+ payload = text
144
+ ) ;
93
145
self . metrics . upstream_messages . increment ( 1 ) ;
94
146
( self . handler ) ( text. into ( ) ) ;
95
147
}
96
148
Err ( e) => {
97
- error ! ( message = "error receiving message" , error = e. to_string( ) ) ;
149
+ error ! (
150
+ message = "error receiving message" ,
151
+ uri = self . uri. to_string( ) ,
152
+ error = e. to_string( )
153
+ ) ;
98
154
return Err ( e) ;
99
155
}
100
156
}
0 commit comments