11use super :: { Cas , SwapError } ;
22use anyhow:: { Context , Result } ;
33use spin_core:: { async_trait, wasmtime:: component:: Resource } ;
4+ use spin_factor_otel:: OtelContext ;
45use spin_resource_table:: Table ;
56use spin_telemetry:: traces:: { self , Blame } ;
67use spin_world:: v2:: key_value;
@@ -49,23 +50,26 @@ pub struct KeyValueDispatch {
4950 manager : Arc < dyn StoreManager > ,
5051 stores : Table < Arc < dyn Store > > ,
5152 compare_and_swaps : Table < Arc < dyn Cas > > ,
53+ otel_context : Option < OtelContext > ,
5254}
5355
5456impl KeyValueDispatch {
5557 pub fn new ( allowed_stores : HashSet < String > , manager : Arc < dyn StoreManager > ) -> Self {
56- Self :: new_with_capacity ( allowed_stores, manager, DEFAULT_STORE_TABLE_CAPACITY )
58+ Self :: new_with_capacity ( allowed_stores, manager, DEFAULT_STORE_TABLE_CAPACITY , None )
5759 }
5860
5961 pub fn new_with_capacity (
6062 allowed_stores : HashSet < String > ,
6163 manager : Arc < dyn StoreManager > ,
6264 capacity : u32 ,
65+ otel_context : Option < OtelContext > ,
6366 ) -> Self {
6467 Self {
6568 allowed_stores,
6669 manager,
6770 stores : Table :: new ( capacity) ,
6871 compare_and_swaps : Table :: new ( capacity) ,
72+ otel_context,
6973 }
7074 }
7175
@@ -113,6 +117,9 @@ impl key_value::Host for KeyValueDispatch {}
113117impl key_value:: HostStore for KeyValueDispatch {
114118 #[ instrument( name = "spin_key_value.open" , skip( self ) , err, fields( otel. kind = "client" , kv. backend=self . manager. summary( & name) . unwrap_or( "unknown" . to_string( ) ) ) ) ]
115119 async fn open ( & mut self , name : String ) -> Result < Result < Resource < key_value:: Store > , Error > > {
120+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
121+ otel_context. reparent_tracing_span ( )
122+ }
116123 Ok ( async {
117124 if self . allowed_stores . contains ( & name) {
118125 let store = self . manager . get ( & name) . await ?;
@@ -135,6 +142,9 @@ impl key_value::HostStore for KeyValueDispatch {
135142 store : Resource < key_value:: Store > ,
136143 key : String ,
137144 ) -> Result < Result < Option < Vec < u8 > > , Error > > {
145+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
146+ otel_context. reparent_tracing_span ( )
147+ }
138148 let store = self . get_store ( store) ?;
139149 Ok ( store. get ( & key) . await . map_err ( track_error_on_span) )
140150 }
@@ -146,6 +156,9 @@ impl key_value::HostStore for KeyValueDispatch {
146156 key : String ,
147157 value : Vec < u8 > ,
148158 ) -> Result < Result < ( ) , Error > > {
159+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
160+ otel_context. reparent_tracing_span ( )
161+ }
149162 let store = self . get_store ( store) ?;
150163 Ok ( store. set ( & key, & value) . await . map_err ( track_error_on_span) )
151164 }
@@ -156,6 +169,9 @@ impl key_value::HostStore for KeyValueDispatch {
156169 store : Resource < key_value:: Store > ,
157170 key : String ,
158171 ) -> Result < Result < ( ) , Error > > {
172+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
173+ otel_context. reparent_tracing_span ( )
174+ }
159175 let store = self . get_store ( store) ?;
160176 Ok ( store. delete ( & key) . await . map_err ( track_error_on_span) )
161177 }
@@ -166,6 +182,9 @@ impl key_value::HostStore for KeyValueDispatch {
166182 store : Resource < key_value:: Store > ,
167183 key : String ,
168184 ) -> Result < Result < bool , Error > > {
185+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
186+ otel_context. reparent_tracing_span ( )
187+ }
169188 let store = self . get_store ( store) ?;
170189 Ok ( store. exists ( & key) . await . map_err ( track_error_on_span) )
171190 }
@@ -175,6 +194,9 @@ impl key_value::HostStore for KeyValueDispatch {
175194 & mut self ,
176195 store : Resource < key_value:: Store > ,
177196 ) -> Result < Result < Vec < String > , Error > > {
197+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
198+ otel_context. reparent_tracing_span ( )
199+ }
178200 let store = self . get_store ( store) ?;
179201 Ok ( store. get_keys ( ) . await . map_err ( track_error_on_span) )
180202 }
0 commit comments