@@ -13,6 +13,7 @@ use std::cell::Cell;
13
13
use hyperactor:: clock:: ClockKind ;
14
14
use hyperactor:: clock:: RealClock ;
15
15
use hyperactor:: clock:: SimClock ;
16
+ use hyperactor_telemetry:: opentelemetry;
16
17
use hyperactor_telemetry:: swap_telemetry_clock;
17
18
use pyo3:: prelude:: * ;
18
19
use pyo3:: types:: PyTraceback ;
@@ -134,6 +135,73 @@ impl PySpan {
134
135
}
135
136
}
136
137
138
+ /// Add to a counter with the given name and attributes
139
+ #[ pyfunction]
140
+ #[ pyo3( signature = ( name, value, attributes = None ) ) ]
141
+ pub fn add_to_counter (
142
+ name : String ,
143
+ value : u64 ,
144
+ attributes : Option < Vec < ( String , String ) > > ,
145
+ ) -> PyResult < ( ) > {
146
+ let kv_pairs: Vec < opentelemetry:: KeyValue > = attributes
147
+ . unwrap_or_default ( )
148
+ . into_iter ( )
149
+ . map ( |( k, v) | opentelemetry:: KeyValue :: new ( k, v) )
150
+ . collect ( ) ;
151
+
152
+ println ! ( "Added {} to counter {}" , value, name) ;
153
+ let counter = hyperactor_telemetry:: meter ( "python" )
154
+ . u64_counter ( name)
155
+ . build ( ) ;
156
+
157
+ counter. add ( value, & kv_pairs) ;
158
+ Ok ( ( ) )
159
+ }
160
+
161
+ /// Add to an up/down counter with the given name and attributes
162
+ #[ pyfunction]
163
+ #[ pyo3( signature = ( name, value, attributes = None ) ) ]
164
+ pub fn add_to_up_down_counter (
165
+ name : String ,
166
+ value : i64 ,
167
+ attributes : Option < Vec < ( String , String ) > > ,
168
+ ) -> PyResult < ( ) > {
169
+ let kv_pairs: Vec < opentelemetry:: KeyValue > = attributes
170
+ . unwrap_or_default ( )
171
+ . into_iter ( )
172
+ . map ( |( k, v) | opentelemetry:: KeyValue :: new ( k, v) )
173
+ . collect ( ) ;
174
+
175
+ let counter = hyperactor_telemetry:: meter ( "python" )
176
+ . i64_up_down_counter ( name)
177
+ . build ( ) ;
178
+
179
+ counter. add ( value, & kv_pairs) ;
180
+ Ok ( ( ) )
181
+ }
182
+
183
+ /// Record a value to a gauge with the given name and attributes
184
+ #[ pyfunction]
185
+ #[ pyo3( signature = ( name, value, attributes = None ) ) ]
186
+ pub fn add_to_gauge (
187
+ name : String ,
188
+ value : f64 ,
189
+ attributes : Option < Vec < ( String , String ) > > ,
190
+ ) -> PyResult < ( ) > {
191
+ let kv_pairs: Vec < opentelemetry:: KeyValue > = attributes
192
+ . unwrap_or_default ( )
193
+ . into_iter ( )
194
+ . map ( |( k, v) | opentelemetry:: KeyValue :: new ( k, v) )
195
+ . collect ( ) ;
196
+
197
+ let gauge = hyperactor_telemetry:: meter ( "python" )
198
+ . f64_gauge ( name)
199
+ . build ( ) ;
200
+
201
+ gauge. record ( value, & kv_pairs) ;
202
+ Ok ( ( ) )
203
+ }
204
+
137
205
use pyo3:: Bound ;
138
206
use pyo3:: types:: PyModule ;
139
207
@@ -182,6 +250,28 @@ pub fn register_python_bindings(module: &Bound<'_, PyModule>) -> PyResult<()> {
182
250
) ?;
183
251
module. add_function ( use_sim_clock_fn) ?;
184
252
253
+ // Register telemetry functions
254
+ let add_to_counter_fn = wrap_pyfunction ! ( add_to_counter, module) ?;
255
+ add_to_counter_fn. setattr (
256
+ "__module__" ,
257
+ "monarch._rust_bindings.hyperactor_extension.telemetry" ,
258
+ ) ?;
259
+ module. add_function ( add_to_counter_fn) ?;
260
+
261
+ let add_to_up_down_counter_fn = wrap_pyfunction ! ( add_to_up_down_counter, module) ?;
262
+ add_to_up_down_counter_fn. setattr (
263
+ "__module__" ,
264
+ "monarch._rust_bindings.hyperactor_extension.telemetry" ,
265
+ ) ?;
266
+ module. add_function ( add_to_up_down_counter_fn) ?;
267
+
268
+ let add_to_gauge_fn = wrap_pyfunction ! ( add_to_gauge, module) ?;
269
+ add_to_gauge_fn. setattr (
270
+ "__module__" ,
271
+ "monarch._rust_bindings.hyperactor_extension.telemetry" ,
272
+ ) ?;
273
+ module. add_function ( add_to_gauge_fn) ?;
274
+
185
275
module. add_class :: < PySpan > ( ) ?;
186
276
Ok ( ( ) )
187
277
}
0 commit comments