1
1
use anyhow:: { Context , Result } ;
2
2
use spin_core:: { async_trait, wasmtime:: component:: Resource } ;
3
- use spin_world:: v2:: key_value;
3
+ use spin_factor_observe:: ObserveContext ;
4
+ use spin_world:: { v2:: key_value, wasi:: observe} ;
4
5
use std:: { collections:: HashSet , sync:: Arc } ;
5
6
use table:: Table ;
6
7
use tracing:: { instrument, Level } ;
@@ -36,22 +37,25 @@ pub struct KeyValueDispatch {
36
37
allowed_stores : HashSet < String > ,
37
38
manager : Arc < dyn StoreManager > ,
38
39
stores : Table < Arc < dyn Store > > ,
40
+ observe_context : Option < ObserveContext > ,
39
41
}
40
42
41
43
impl KeyValueDispatch {
42
44
pub fn new ( allowed_stores : HashSet < String > , manager : Arc < dyn StoreManager > ) -> Self {
43
- Self :: new_with_capacity ( allowed_stores, manager, DEFAULT_STORE_TABLE_CAPACITY )
45
+ Self :: new_with_capacity ( allowed_stores, manager, DEFAULT_STORE_TABLE_CAPACITY , None )
44
46
}
45
47
46
48
pub fn new_with_capacity (
47
49
allowed_stores : HashSet < String > ,
48
50
manager : Arc < dyn StoreManager > ,
49
51
capacity : u32 ,
52
+ observe_context : Option < ObserveContext > ,
50
53
) -> Self {
51
54
Self {
52
55
allowed_stores,
53
56
manager,
54
57
stores : Table :: new ( capacity) ,
58
+ observe_context,
55
59
}
56
60
}
57
61
@@ -71,6 +75,9 @@ impl key_value::Host for KeyValueDispatch {}
71
75
impl key_value:: HostStore for KeyValueDispatch {
72
76
#[ instrument( name = "spin_key_value.open" , skip( self ) , err( level = Level :: INFO ) , fields( otel. kind = "client" , kv. backend=self . manager. summary( & name) . unwrap_or( "unknown" . to_string( ) ) ) ) ]
73
77
async fn open ( & mut self , name : String ) -> Result < Result < Resource < key_value:: Store > , Error > > {
78
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
79
+ observe_context. reparent_tracing_span ( )
80
+ }
74
81
Ok ( async {
75
82
if self . allowed_stores . contains ( & name) {
76
83
let store = self
@@ -91,6 +98,9 @@ impl key_value::HostStore for KeyValueDispatch {
91
98
store : Resource < key_value:: Store > ,
92
99
key : String ,
93
100
) -> Result < Result < Option < Vec < u8 > > , Error > > {
101
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
102
+ observe_context. reparent_tracing_span ( )
103
+ }
94
104
let store = self . get_store ( store) ?;
95
105
Ok ( store. get ( & key) . await )
96
106
}
@@ -102,6 +112,9 @@ impl key_value::HostStore for KeyValueDispatch {
102
112
key : String ,
103
113
value : Vec < u8 > ,
104
114
) -> Result < Result < ( ) , Error > > {
115
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
116
+ observe_context. reparent_tracing_span ( )
117
+ }
105
118
let store = self . get_store ( store) ?;
106
119
Ok ( store. set ( & key, & value) . await )
107
120
}
@@ -112,6 +125,9 @@ impl key_value::HostStore for KeyValueDispatch {
112
125
store : Resource < key_value:: Store > ,
113
126
key : String ,
114
127
) -> Result < Result < ( ) , Error > > {
128
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
129
+ observe_context. reparent_tracing_span ( )
130
+ }
115
131
let store = self . get_store ( store) ?;
116
132
Ok ( store. delete ( & key) . await )
117
133
}
@@ -122,6 +138,9 @@ impl key_value::HostStore for KeyValueDispatch {
122
138
store : Resource < key_value:: Store > ,
123
139
key : String ,
124
140
) -> Result < Result < bool , Error > > {
141
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
142
+ observe_context. reparent_tracing_span ( )
143
+ }
125
144
let store = self . get_store ( store) ?;
126
145
Ok ( store. exists ( & key) . await )
127
146
}
@@ -131,6 +150,9 @@ impl key_value::HostStore for KeyValueDispatch {
131
150
& mut self ,
132
151
store : Resource < key_value:: Store > ,
133
152
) -> Result < Result < Vec < String > , Error > > {
153
+ if let Some ( observe_context) = self . observe_context . as_ref ( ) {
154
+ observe_context. reparent_tracing_span ( )
155
+ }
134
156
let store = self . get_store ( store) ?;
135
157
Ok ( store. get_keys ( ) . await )
136
158
}
0 commit comments