monty-pool 1.0.0

A secure, snapshotable Python sandbox written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Telemetry for the pool: spans describing what each session did, and
//! aggregate metrics over the fleet running them.
//!
//! This root is the host-facing surface both are delivered through — the
//! [`TelemetryAdapter`] bridge for a host whose SDK is in another language,
//! and [`TelemetryContext::for_logfire`] / [`Metrics::for_logfire`] for a Rust
//! host that already owns one. A foreign-language binding can either export
//! metrics aggregated by the statically linked SDK or stream raw measurements
//! into its host SDK. The recorders behind this surface are internal:
//! `tracing` mirrors the protocol into spans, `metrics` records instruments,
//! and `tracing_json` encodes span attributes the way the Logfire SDKs do.

pub(crate) mod metrics;
pub(crate) mod tracing;
mod tracing_json;

use std::{
    collections::{HashMap, HashSet},
    fmt,
    future::{Future, ready},
    str::FromStr,
    sync::{Arc, Mutex, MutexGuard, PoisonError},
    time::Duration,
};

use logfire::{
    ConfigureError, Logfire,
    config::{AdvancedOptions, MetricsOptions},
};
pub use metrics::{Measurement, MetricKind, MetricValue, Metrics};
use opentelemetry::{
    Context, InstrumentationScope,
    trace::{SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState},
};
use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest;
use opentelemetry_sdk::{
    error::OTelSdkResult,
    logs::{LogProcessor, SdkLogRecord},
    metrics::{PeriodicReader, Temporality, data::ResourceMetrics, exporter::PushMetricExporter},
    trace::{Span, SpanData, SpanProcessor},
};
use prost::Message;

/// Current host-adapter protocol version used outside the Python binding.
pub const TELEMETRY_ADAPTER_VERSION: u8 = 1;

/// Configured pipeline shared by all checkouts using one language adapter.
pub struct TelemetryAdapterHandle {
    /// Retains the configured provider and its processors for the handle's lifetime.
    logfire: Logfire,
    /// The one instrument set every pool built from this handle shares, so
    /// worker counters total over all of them.
    metrics: Metrics,
}

/// Distributed parent context and recorder for one checkout root.
#[derive(Clone)]
pub struct TelemetryContext {
    parent: Option<SpanContext>,
    logfire: Logfire,
}

impl TelemetryAdapterHandle {
    /// Validates host W3C fields and couples them to this adapter pipeline.
    pub fn context(
        &self,
        trace_id: &str,
        span_id: &str,
        trace_flags: u8,
        trace_state: &str,
    ) -> Result<TelemetryContext, String> {
        self.context_with_remote(trace_id, span_id, trace_flags, trace_state, true)
    }

    /// Validates host W3C fields while preserving whether the parent is remote.
    pub fn context_with_remote(
        &self,
        trace_id: &str,
        span_id: &str,
        trace_flags: u8,
        trace_state: &str,
        is_remote: bool,
    ) -> Result<TelemetryContext, String> {
        let trace_id = TraceId::from_hex(trace_id).map_err(|err| format!("invalid trace ID: {err}"))?;
        let span_id = SpanId::from_hex(span_id).map_err(|err| format!("invalid span ID: {err}"))?;
        self.context_from_ids(trace_id, span_id, trace_flags, trace_state, is_remote)
    }

    /// Validates numeric W3C fields without a text round-trip.
    pub fn context_from_ids(
        &self,
        trace_id: TraceId,
        span_id: SpanId,
        trace_flags: u8,
        trace_state: &str,
        is_remote: bool,
    ) -> Result<TelemetryContext, String> {
        let trace_state = TraceState::from_str(trace_state).map_err(|err| format!("invalid trace state: {err}"))?;
        if trace_id == TraceId::INVALID || span_id == SpanId::INVALID {
            return Err("trace and span IDs must be non-zero".to_owned());
        }
        Ok(TelemetryContext {
            parent: Some(SpanContext::new(
                trace_id,
                span_id,
                TraceFlags::new(trace_flags),
                is_remote,
                trace_state,
            )),
            logfire: self.logfire.clone(),
        })
    }

    /// Creates adapter context when no host span is active.
    #[must_use]
    pub fn unparented_context(&self) -> TelemetryContext {
        TelemetryContext {
            parent: None,
            logfire: self.logfire.clone(),
        }
    }

    /// Metrics handle for [`PoolConfig::metrics`](crate::PoolConfig::metrics).
    ///
    /// Unlike spans, metrics do not travel per checkout: they are aggregates
    /// covering untraced sessions and worker lifecycle events that belong to
    /// no session at all. Every call returns the same shared recorder, so the
    /// worker counters sum over all pools configured from this handle.
    #[must_use]
    pub fn metrics(&self) -> Metrics {
        self.metrics.clone()
    }

    /// Flushes the statically linked telemetry pipeline.
    ///
    /// Adapters receiving aggregated metrics should call this from their flush
    /// path. Raw measurements are delivered synchronously and instead follow
    /// the foreign SDK's flush lifecycle.
    pub fn force_flush(&self) -> OTelSdkResult {
        self.logfire.force_flush()
    }
}

impl TelemetryContext {
    /// Records the pool's spans straight into `logfire`: a Rust host already
    /// owns a configured SDK, so it needs no [`TelemetryAdapter`] bridge.
    /// Unparented, like [`TelemetryAdapterHandle::unparented_context`] — an
    /// in-process ambient span attaches through `tracing`, not W3C fields.
    #[must_use]
    pub fn for_logfire(logfire: Logfire) -> Self {
        Self { parent: None, logfire }
    }
}

impl TelemetryContext {
    /// Returns the configured recorder used while processing this checkout.
    pub(crate) fn logfire(&self) -> Logfire {
        self.logfire.clone()
    }

    /// W3C `traceparent` (and `tracestate`, when non-empty) headers naming the
    /// propagated span, for a remote worker's WebSocket upgrade request; empty
    /// when the host captured no span.
    pub(crate) fn propagation_headers(&self) -> Vec<(String, String)> {
        let Some(parent) = &self.parent else {
            return Vec::new();
        };
        let traceparent = format!(
            "00-{}-{}-{:02x}",
            parent.trace_id(),
            parent.span_id(),
            parent.trace_flags().to_u8()
        );
        let mut headers = vec![("traceparent".to_owned(), traceparent)];
        let tracestate = parent.trace_state().header();
        if !tracestate.is_empty() {
            headers.push(("tracestate".to_owned(), tracestate));
        }
        headers
    }

    /// Converts the propagated span into its OTel parent context.
    pub(crate) fn into_parent(self) -> Option<Context> {
        self.parent
            .map(|parent| Context::new().with_remote_span_context(parent))
    }
}

/// Receives Monty records without owning exporters or credentials.
/// Returning false disables the affected Monty root and calls `disable_root`.
pub trait TelemetryAdapter: Send + Sync + 'static {
    /// Starts a reconstructed host span keyed by its Rust OTel span ID.
    fn start_span(&self, span: &SpanData) -> bool;
    /// Starts a span with its complete parent context when the adapter needs it.
    fn start_span_with_parent(&self, span: &SpanData, parent: &Context) -> bool {
        let _ = parent;
        self.start_span(span)
    }
    /// Applies final data and ends a reconstructed host span.
    fn end_span(&self, span: &SpanData) -> bool;
    /// Emits a log under the reconstructed span identified by `parent_span_id`.
    fn emit_log(&self, parent_span_id: SpanId, record: &SdkLogRecord) -> bool;
    /// Discards host-side state after delivery for one Monty root becomes unreliable.
    /// Called after every in-flight callback for that root has returned.
    fn disable_root(&self, trace_id: TraceId, root_span_id: SpanId);
    /// Records one raw metric measurement in the foreign host's metrics SDK.
    ///
    /// Defaults to dropping the measurement because version-1 adapters receive
    /// aggregated OTLP batches instead.
    fn record_metric(&self, measurement: &Measurement<'_>) {
        let _ = measurement;
    }
    /// Exports one aggregated OTLP `ExportMetricsServiceRequest` protobuf.
    ///
    /// Version-1 adapters receive these batches. New adapters can instead use
    /// [`configure_telemetry_adapter_with_host_metrics`] so the foreign SDK
    /// owns aggregation and receives [`Self::record_metric`] calls.
    fn export_metrics(&self, payload: &[u8]) {
        let _ = payload;
    }
}

/// Configures the statically linked Logfire pipeline used by a language binding.
pub fn configure_telemetry_adapter(
    adapter: Arc<dyn TelemetryAdapter>,
) -> Result<TelemetryAdapterHandle, ConfigureError> {
    configure_adapter(adapter, false)
}

/// Configures tracing in Rust while the foreign host owns metrics aggregation.
///
/// Every measurement is delivered through [`TelemetryAdapter::record_metric`]
/// and therefore follows the host SDK's views, readers, and exporters.
pub fn configure_telemetry_adapter_with_host_metrics(
    adapter: Arc<dyn TelemetryAdapter>,
) -> Result<TelemetryAdapterHandle, ConfigureError> {
    configure_adapter(adapter, true)
}

fn configure_adapter(
    adapter: Arc<dyn TelemetryAdapter>,
    host_owns_metrics: bool,
) -> Result<TelemetryAdapterHandle, ConfigureError> {
    let processor = AdapterProcessor::new(Arc::clone(&adapter));
    let config = logfire::configure()
        .send_to_logfire(false)
        .with_console(None)
        .with_install_panic_handler(false)
        .with_additional_span_processor(processor.clone())
        .with_advanced_options(AdvancedOptions::default().with_log_processor(processor));
    let config = if host_owns_metrics {
        config.with_metrics(None)
    } else {
        let exporter = AdapterMetricExporter::new(Arc::clone(&adapter));
        let reader = PeriodicReader::builder(exporter).build();
        config.with_metrics(Some(MetricsOptions::default().with_additional_reader(reader)))
    };
    let logfire = config.finish()?;
    let metrics = if host_owns_metrics {
        Metrics::for_adapter(adapter)
    } else {
        Metrics::for_logfire(logfire.clone())
    };
    Ok(TelemetryAdapterHandle { logfire, metrics })
}

/// Exports aggregated SDK metrics through a foreign-language adapter.
struct AdapterMetricExporter {
    adapter: Arc<dyn TelemetryAdapter>,
}

impl AdapterMetricExporter {
    /// Creates an OTLP exporter around the language bridge.
    fn new(adapter: Arc<dyn TelemetryAdapter>) -> Self {
        Self { adapter }
    }
}

impl PushMetricExporter for AdapterMetricExporter {
    fn export(&self, metrics: &ResourceMetrics) -> impl Future<Output = OTelSdkResult> + Send {
        let request = ExportMetricsServiceRequest::from(metrics);
        self.adapter.export_metrics(&request.encode_to_vec());
        ready(Ok(()))
    }

    fn force_flush(&self) -> OTelSdkResult {
        Ok(())
    }

    fn shutdown_with_timeout(&self, _: Duration) -> OTelSdkResult {
        Ok(())
    }

    fn temporality(&self) -> Temporality {
        // Match Logfire's native OTLP exporters; the foreign SDK forwards the
        // aggregate rather than feeding it through another metrics SDK.
        Temporality::Delta
    }
}

/// Shared processor state for span ancestry and root-level disablement.
#[derive(Clone)]
struct AdapterProcessor(Arc<ProcessorState>);

/// Span ancestry shared by the span and log processor callbacks.
struct ProcessorState {
    adapter: Arc<dyn TelemetryAdapter>,
    routing: Mutex<RoutingState>,
}

/// Mutable ancestry and delivery state shared by processor callbacks.
struct RoutingState {
    /// Root containing each open Monty span.
    spans: HashMap<SpanKey, SpanKey>,
    /// Number of open Monty spans belonging to each root.
    open_spans: HashMap<SpanKey, usize>,
    /// Roots whose adapter stream can no longer be trusted.
    disabled: HashSet<SpanKey>,
    /// Callbacks which may still create or use adapter state.
    active_deliveries: HashMap<SpanKey, usize>,
    /// Disabled roots for which adapter cleanup was requested.
    cleanup_sent: HashSet<SpanKey>,
    /// Ended roots retained until their final child and callback completes.
    finished_roots: HashSet<SpanKey>,
}

/// Identifies an OTel span without assuming span IDs are process-global.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
struct SpanKey {
    trace_id: TraceId,
    span_id: SpanId,
}

impl AdapterProcessor {
    /// Creates the shared span/log processor around one language adapter.
    fn new(adapter: Arc<dyn TelemetryAdapter>) -> Self {
        Self(Arc::new(ProcessorState {
            adapter,
            routing: Mutex::new(RoutingState {
                spans: HashMap::new(),
                open_spans: HashMap::new(),
                disabled: HashSet::new(),
                active_deliveries: HashMap::new(),
                cleanup_sent: HashSet::new(),
                finished_roots: HashSet::new(),
            }),
        }))
    }

    /// Registers a callback so cleanup waits until every in-flight delivery completes.
    fn begin_delivery(routing: &mut RoutingState, root: SpanKey) -> bool {
        if routing.disabled.contains(&root) {
            false
        } else {
            *routing.active_deliveries.entry(root).or_default() += 1;
            true
        }
    }

    /// Completes a callback and requests cleanup once no delivery can add more state.
    fn finish_delivery(&self, root: SpanKey, delivered: bool) {
        let cleanup = {
            let mut routing = lock(&self.0.routing);
            let active = routing
                .active_deliveries
                .get_mut(&root)
                .expect("delivery registered before callback");
            *active -= 1;
            if *active == 0 {
                routing.active_deliveries.remove(&root);
            }
            if !delivered {
                routing.disabled.insert(root);
            }
            let cleanup = routing.disabled.contains(&root)
                && !routing.active_deliveries.contains_key(&root)
                && routing.cleanup_sent.insert(root);
            Self::forget_finished_root(&mut routing, root);
            cleanup
        };
        if cleanup {
            self.0.adapter.disable_root(root.trace_id, root.span_id);
        }
    }

    /// Forgets routing failures only after an ended root has no remaining callbacks.
    fn forget_finished_root(routing: &mut RoutingState, root: SpanKey) {
        if routing.finished_roots.contains(&root)
            && !routing.active_deliveries.contains_key(&root)
            && !routing.open_spans.contains_key(&root)
        {
            routing.disabled.remove(&root);
            routing.cleanup_sent.remove(&root);
            routing.finished_roots.remove(&root);
        }
    }
}

impl fmt::Debug for AdapterProcessor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("AdapterProcessor")
    }
}

impl SpanProcessor for AdapterProcessor {
    fn on_start(&self, span: &mut Span, parent: &Context) {
        let Some(data) = span.exported_data() else { return };
        let span_key = SpanKey {
            trace_id: data.span_context.trace_id(),
            span_id: data.span_context.span_id(),
        };
        let parent_key = SpanKey {
            trace_id: span_key.trace_id,
            span_id: parent.span().span_context().span_id(),
        };
        let (root, deliver) = {
            let mut routing = lock(&self.0.routing);
            let root = routing.spans.get(&parent_key).copied().unwrap_or(span_key);
            routing.spans.insert(span_key, root);
            *routing.open_spans.entry(root).or_default() += 1;
            let deliver = Self::begin_delivery(&mut routing, root);
            (root, deliver)
        };
        if deliver {
            let delivered = self.0.adapter.start_span_with_parent(&data, parent);
            self.finish_delivery(root, delivered);
        }
    }

    fn on_end(&self, span: SpanData) {
        let span_key = SpanKey {
            trace_id: span.span_context.trace_id(),
            span_id: span.span_context.span_id(),
        };
        let (root, deliver) = {
            let mut routing = lock(&self.0.routing);
            let Some(root) = routing.spans.remove(&span_key) else {
                return;
            };
            let open_spans = routing.open_spans.get_mut(&root).expect("span registered on start");
            *open_spans -= 1;
            if *open_spans == 0 {
                routing.open_spans.remove(&root);
            }
            let deliver = Self::begin_delivery(&mut routing, root);
            (root, deliver)
        };
        if deliver {
            let delivered = self.0.adapter.end_span(&span);
            self.finish_delivery(root, delivered);
        }
        let mut routing = lock(&self.0.routing);
        if root == span_key {
            routing.finished_roots.insert(root);
        }
        Self::forget_finished_root(&mut routing, root);
    }

    fn force_flush(&self) -> OTelSdkResult {
        Ok(())
    }
    fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
        Ok(())
    }
}

impl LogProcessor for AdapterProcessor {
    fn emit(&self, record: &mut SdkLogRecord, _instrumentation: &InstrumentationScope) {
        let Some(context) = record.trace_context() else { return };
        let parent = SpanKey {
            trace_id: context.trace_id,
            span_id: context.span_id,
        };
        let (root, deliver) = {
            let mut routing = lock(&self.0.routing);
            let Some(root) = routing.spans.get(&parent).copied() else {
                return;
            };
            let deliver = Self::begin_delivery(&mut routing, root);
            (root, deliver)
        };
        if deliver {
            let delivered = self.0.adapter.emit_log(context.span_id, record);
            self.finish_delivery(root, delivered);
        }
    }

    fn force_flush(&self) -> OTelSdkResult {
        Ok(())
    }
    fn shutdown_with_timeout(&self, _timeout: Duration) -> OTelSdkResult {
        Ok(())
    }
}

/// Locks processor state while recovering from an unrelated panic.
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}