@@ -7,7 +7,6 @@ use crate::logs::ExportingEnabled;
7
7
use crate :: CliState ;
8
8
use ockam_core:: env:: { get_env_with_default, FromString } ;
9
9
use ockam_core:: errcode:: { Kind , Origin } ;
10
- use ockam_node:: Executor ;
11
10
use std:: env:: current_exe;
12
11
use std:: fmt:: { Display , Formatter } ;
13
12
use std:: net:: { SocketAddr , ToSocketAddrs } ;
@@ -116,14 +115,15 @@ impl ExportingConfiguration {
116
115
117
116
/// Create a tracing configuration for a user command running in the foreground.
118
117
/// (meaning that the process will shut down once the command has been executed)
119
- pub fn foreground ( state : & CliState ) -> ockam_core:: Result < ExportingConfiguration > {
120
- match opentelemetry_endpoint ( state) ? {
118
+ pub async fn foreground ( state : & CliState ) -> ockam_core:: Result < ExportingConfiguration > {
119
+ match opentelemetry_endpoint ( state) . await ? {
121
120
None => ExportingConfiguration :: off ( ) ,
122
121
Some ( endpoint) => Ok ( ExportingConfiguration {
123
122
enabled : exporting_enabled (
124
123
& endpoint,
125
124
opentelemetry_endpoint_foreground_connection_timeout ( ) ?,
126
- ) ?,
125
+ )
126
+ . await ?,
127
127
span_export_timeout : span_export_timeout ( ) ?,
128
128
log_export_timeout : log_export_timeout ( ) ?,
129
129
span_export_scheduled_delay : foreground_span_export_scheduled_delay ( ) ?,
@@ -139,14 +139,15 @@ impl ExportingConfiguration {
139
139
}
140
140
141
141
/// Create a tracing configuration for a background node
142
- pub fn background ( state : & CliState ) -> ockam_core:: Result < ExportingConfiguration > {
143
- match opentelemetry_endpoint ( state) ? {
142
+ pub async fn background ( state : & CliState ) -> ockam_core:: Result < ExportingConfiguration > {
143
+ match opentelemetry_endpoint ( state) . await ? {
144
144
None => ExportingConfiguration :: off ( ) ,
145
145
Some ( endpoint) => Ok ( ExportingConfiguration {
146
146
enabled : exporting_enabled (
147
147
& endpoint,
148
148
opentelemetry_endpoint_background_connection_timeout ( ) ?,
149
- ) ?,
149
+ )
150
+ . await ?,
150
151
span_export_timeout : span_export_timeout ( ) ?,
151
152
log_export_timeout : log_export_timeout ( ) ?,
152
153
span_export_scheduled_delay : background_span_export_scheduled_delay ( ) ?,
@@ -254,11 +255,11 @@ fn print_debug(message: impl Into<String>) {
254
255
/// - Exporting has not been deactivated by the user
255
256
/// - The opentelemetry endpoint is accessible
256
257
///
257
- fn exporting_enabled (
258
+ async fn exporting_enabled (
258
259
endpoint : & OpenTelemetryEndpoint ,
259
260
connection_check_timeout : Duration ,
260
261
) -> ockam_core:: Result < ExportingEnabled > {
261
- if is_endpoint_accessible ( & endpoint. url ( ) , connection_check_timeout) {
262
+ if is_endpoint_accessible ( & endpoint. url ( ) , connection_check_timeout) . await {
262
263
print_debug ( "Exporting is enabled" ) ;
263
264
Ok ( ExportingEnabled :: On )
264
265
} else {
@@ -275,23 +276,37 @@ fn exporting_enabled(
275
276
}
276
277
277
278
/// Return true if the endpoint can be accessed with a TCP connection
278
- fn is_endpoint_accessible ( url : & Url , connection_check_timeout : Duration ) -> bool {
279
+ async fn is_endpoint_accessible ( url : & Url , connection_check_timeout : Duration ) -> bool {
279
280
match to_socket_addr ( url) {
280
281
Some ( address) => {
281
282
let retries = FibonacciBackoff :: from_millis ( 100 ) ;
282
283
let now = Instant :: now ( ) ;
283
284
285
+ // TODO: Not sure we need to retry really, also maybe it could happen in the background
286
+ // to not slow things down
284
287
for timeout_duration in retries {
285
288
print_debug ( format ! (
286
289
"trying to connect to {address} in {timeout_duration:?}"
287
290
) ) ;
288
- if std:: net:: TcpStream :: connect_timeout ( & address, timeout_duration) . is_ok ( ) {
289
- return true ;
290
- } else {
291
- if now. elapsed ( ) >= connection_check_timeout {
292
- return false ;
293
- } ;
294
- std:: thread:: sleep ( timeout_duration) ;
291
+
292
+ let res = tokio:: time:: timeout (
293
+ timeout_duration,
294
+ tokio:: net:: TcpStream :: connect ( & address) ,
295
+ )
296
+ . await ;
297
+
298
+ match res {
299
+ Ok ( res) => {
300
+ if res. is_ok ( ) {
301
+ return true ;
302
+ }
303
+ }
304
+ Err ( _) => {
305
+ if now. elapsed ( ) >= connection_check_timeout {
306
+ return false ;
307
+ } ;
308
+ tokio:: time:: sleep ( timeout_duration) . await ;
309
+ }
295
310
}
296
311
}
297
312
false
@@ -324,36 +339,27 @@ fn to_socket_addr(url: &Url) -> Option<SocketAddr> {
324
339
/// Return the tracing endpoint, defined by an environment variable
325
340
/// If the endpoint can be established with an Ockam portal to the opentelemetry-relay created in the project
326
341
/// use that URL, otherwise use the HTTPS endpoint
327
- fn opentelemetry_endpoint ( state : & CliState ) -> ockam_core:: Result < Option < OpenTelemetryEndpoint > > {
328
- if !is_exporting_set ( ) ? {
329
- print_debug ( "Exporting is turned off" ) ;
330
- Ok ( None )
331
- } else {
332
- let state = state. clone ( ) ;
333
- match Executor :: execute_future ( async move {
334
- // if a project is defined try to use the OpenTelemetry portal
335
- // and if we allow traces to be exported via a portal
336
- if state. projects ( ) . get_default_project ( ) . await . is_ok ( )
337
- && is_exporting_via_portal_set ( ) ?
338
- {
339
- print_debug ( "A default project exists. Getting the project export endpoint" ) ;
340
- get_project_endpoint_url ( & state) . await
341
- } else {
342
- print_debug ( "A default project does not exist. Getting the default HTTPs endpoint" ) ;
343
- get_https_endpoint ( )
344
- }
345
- } ) {
346
- Ok ( Ok ( url) ) => Ok ( Some ( url) ) ,
347
- Ok ( Err ( e) ) => {
348
- print_debug ( format ! (
349
- "There was an issue when setting up the exporting of traces: {e:?}"
350
- ) ) ;
351
- Ok ( None )
352
- }
353
- Err ( e) => {
354
- print_debug ( format ! ( "There was an issue when running the code setting up the exporting of traces: {e:?}" ) ) ;
355
- Ok ( None )
356
- }
342
+ async fn opentelemetry_endpoint (
343
+ state : & CliState ,
344
+ ) -> ockam_core:: Result < Option < OpenTelemetryEndpoint > > {
345
+ let res = {
346
+ // if a project is defined try to use the OpenTelemetry portal
347
+ // and if we allow traces to be exported via a portal
348
+ if state. projects ( ) . get_default_project ( ) . await . is_ok ( ) && is_exporting_via_portal_set ( ) ? {
349
+ print_debug ( "A default project exists. Getting the project export endpoint" ) ;
350
+ get_project_endpoint_url ( state) . await
351
+ } else {
352
+ print_debug ( "A default project does not exist. Getting the default HTTPs endpoint" ) ;
353
+ get_https_endpoint ( )
354
+ }
355
+ } ;
356
+ match res {
357
+ Ok ( url) => Ok ( Some ( url) ) ,
358
+ Err ( e) => {
359
+ print_debug ( format ! (
360
+ "There was an issue when setting up the exporting of traces: {e:?}"
361
+ ) ) ;
362
+ Ok ( None )
357
363
}
358
364
}
359
365
}
0 commit comments