1
1
use super :: { Cas , SwapError } ;
2
2
use anyhow:: { Context , Result } ;
3
3
use spin_core:: { async_trait, wasmtime:: component:: Resource } ;
4
+ use spin_factor_otel:: OtelContext ;
4
5
use spin_resource_table:: Table ;
5
6
use spin_telemetry:: traces:: { self , Blame } ;
6
7
use spin_world:: v2:: key_value;
@@ -49,23 +50,26 @@ pub struct KeyValueDispatch {
49
50
manager : Arc < dyn StoreManager > ,
50
51
stores : Table < Arc < dyn Store > > ,
51
52
compare_and_swaps : Table < Arc < dyn Cas > > ,
53
+ otel_context : Option < OtelContext > ,
52
54
}
53
55
54
56
impl KeyValueDispatch {
55
57
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 )
57
59
}
58
60
59
61
pub fn new_with_capacity (
60
62
allowed_stores : HashSet < String > ,
61
63
manager : Arc < dyn StoreManager > ,
62
64
capacity : u32 ,
65
+ otel_context : Option < OtelContext > ,
63
66
) -> Self {
64
67
Self {
65
68
allowed_stores,
66
69
manager,
67
70
stores : Table :: new ( capacity) ,
68
71
compare_and_swaps : Table :: new ( capacity) ,
72
+ otel_context,
69
73
}
70
74
}
71
75
@@ -113,6 +117,9 @@ impl key_value::Host for KeyValueDispatch {}
113
117
impl key_value:: HostStore for KeyValueDispatch {
114
118
#[ instrument( name = "spin_key_value.open" , skip( self ) , err, fields( otel. kind = "client" , kv. backend=self . manager. summary( & name) . unwrap_or( "unknown" . to_string( ) ) ) ) ]
115
119
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
+ }
116
123
Ok ( async {
117
124
if self . allowed_stores . contains ( & name) {
118
125
let store = self . manager . get ( & name) . await ?;
@@ -135,6 +142,9 @@ impl key_value::HostStore for KeyValueDispatch {
135
142
store : Resource < key_value:: Store > ,
136
143
key : String ,
137
144
) -> Result < Result < Option < Vec < u8 > > , Error > > {
145
+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
146
+ otel_context. reparent_tracing_span ( )
147
+ }
138
148
let store = self . get_store ( store) ?;
139
149
Ok ( store. get ( & key) . await . map_err ( track_error_on_span) )
140
150
}
@@ -146,6 +156,9 @@ impl key_value::HostStore for KeyValueDispatch {
146
156
key : String ,
147
157
value : Vec < u8 > ,
148
158
) -> Result < Result < ( ) , Error > > {
159
+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
160
+ otel_context. reparent_tracing_span ( )
161
+ }
149
162
let store = self . get_store ( store) ?;
150
163
Ok ( store. set ( & key, & value) . await . map_err ( track_error_on_span) )
151
164
}
@@ -156,6 +169,9 @@ impl key_value::HostStore for KeyValueDispatch {
156
169
store : Resource < key_value:: Store > ,
157
170
key : String ,
158
171
) -> Result < Result < ( ) , Error > > {
172
+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
173
+ otel_context. reparent_tracing_span ( )
174
+ }
159
175
let store = self . get_store ( store) ?;
160
176
Ok ( store. delete ( & key) . await . map_err ( track_error_on_span) )
161
177
}
@@ -166,6 +182,9 @@ impl key_value::HostStore for KeyValueDispatch {
166
182
store : Resource < key_value:: Store > ,
167
183
key : String ,
168
184
) -> Result < Result < bool , Error > > {
185
+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
186
+ otel_context. reparent_tracing_span ( )
187
+ }
169
188
let store = self . get_store ( store) ?;
170
189
Ok ( store. exists ( & key) . await . map_err ( track_error_on_span) )
171
190
}
@@ -175,6 +194,9 @@ impl key_value::HostStore for KeyValueDispatch {
175
194
& mut self ,
176
195
store : Resource < key_value:: Store > ,
177
196
) -> Result < Result < Vec < String > , Error > > {
197
+ if let Some ( otel_context) = self . otel_context . as_ref ( ) {
198
+ otel_context. reparent_tracing_span ( )
199
+ }
178
200
let store = self . get_store ( store) ?;
179
201
Ok ( store. get_keys ( ) . await . map_err ( track_error_on_span) )
180
202
}
0 commit comments