@@ -16,7 +16,7 @@ use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Chann
16
16
use super :: BoxInterceptor ;
17
17
18
18
pub ( crate ) struct TonicTracesClient {
19
- inner : Option < ClientInner > ,
19
+ inner : Mutex < Option < ClientInner > > ,
20
20
#[ allow( dead_code) ]
21
21
// <allow dead> would be removed once we support set_resource for metrics.
22
22
resource : opentelemetry_proto:: transform:: common:: tonic:: ResourceAttributesWithSchema ,
@@ -49,18 +49,18 @@ impl TonicTracesClient {
49
49
otel_debug ! ( name: "TonicsTracesClientBuilt" ) ;
50
50
51
51
TonicTracesClient {
52
- inner : Some ( ClientInner {
52
+ inner : Mutex :: new ( Some ( ClientInner {
53
53
client,
54
54
interceptor : Mutex :: new ( interceptor) ,
55
- } ) ,
55
+ } ) ) ,
56
56
resource : Default :: default ( ) ,
57
57
}
58
58
}
59
59
}
60
60
61
61
impl SpanExporter for TonicTracesClient {
62
62
async fn export ( & self , batch : Vec < SpanData > ) -> OTelSdkResult {
63
- let ( mut client, metadata, extensions) = match & self . inner {
63
+ let ( mut client, metadata, extensions) = match self . inner . lock ( ) . await . as_ref ( ) {
64
64
Some ( inner) => {
65
65
let ( m, e, _) = inner
66
66
. interceptor
@@ -100,9 +100,15 @@ impl SpanExporter for TonicTracesClient {
100
100
}
101
101
102
102
fn shutdown ( & self ) -> OTelSdkResult {
103
- // For tonic client, we don't need to do anything special for shutdown
104
- // as it's already using atomic operations for state management
105
- Ok ( ( ) )
103
+ match self . inner . try_lock ( ) {
104
+ Ok ( mut guard) => match guard. take ( ) {
105
+ Some ( _) => Ok ( ( ) ) , // Successfully took `inner`, indicating a successful shutdown.
106
+ None => Err ( OTelSdkError :: AlreadyShutdown ) , // `inner` was already `None`, meaning it's already shut down.
107
+ } ,
108
+ Err ( _) => Err ( OTelSdkError :: InternalFailure (
109
+ "Failed to acquire lock for shutdown" . to_string ( ) ,
110
+ ) ) ,
111
+ }
106
112
}
107
113
108
114
fn set_resource ( & mut self , resource : & opentelemetry_sdk:: Resource ) {
0 commit comments