triblespace 0.24.0

The Triblespace: A lightweight knowledge base for 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
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::core::metadata;
use crate::core::repo::pile::Pile;
use crate::core::repo::{Repository, Workspace};
use crate::prelude::blobschemas::LongString;
use crate::prelude::valueschemas::{Blake3, GenId, Handle, ShortString, U256BE};
use crate::prelude::*;
use ed25519_dalek::SigningKey;
use rand_core06::OsRng;
use thread_local::ThreadLocal;
use tracing::Subscriber;
use tracing_subscriber::layer::{Context, Layer};
use tracing_subscriber::prelude::*;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::EnvFilter;

const ENV_TELEMETRY_PILE: &str = "TELEMETRY_PILE";
const ENV_PILE: &str = "PILE";
const ENV_TELEMETRY_BRANCH: &str = "TELEMETRY_BRANCH";
const ENV_TELEMETRY_FLUSH_MS: &str = "TELEMETRY_FLUSH_MS";

pub mod schema {
    use super::*;

    attributes! {
        "3E062AA7E3554C8F2DB94883CE639BFE" as pub session: GenId;
        "146E5AA2F7CB3D8B654BC7742A13CAB3" as pub parent: GenId;
        "CCB0147D20C4C6FCAC0E3D87FAFF71D1" as pub name: Handle<Blake3, LongString>;
        "8A4BE2C4D0E90D2B9EE0E1A07ECA2CFA" as pub category: ShortString;
        "E11A84A30CC112650DC860B66B8BD8A9" as pub begin_ns: U256BE;
        "2786FA563372FB6EF469EC7710719A49" as pub end_ns: U256BE;
        "7593602383D0B0D21BBE382A67E5BD9F" as pub duration_ns: U256BE;
        "7E96DD9A0B5002796B645ED25F5E99AC" as pub source: Handle<Blake3, LongString>;
    }

    #[allow(non_upper_case_globals)]
    pub const kind_session: Id = crate::macros::id_hex!("2701F7019B865D461F0169B1303026D6");
    #[allow(non_upper_case_globals)]
    pub const kind_span: Id = crate::macros::id_hex!("0AF9FEB9A2BFEB1BE8A8229829181085");

    #[allow(non_upper_case_globals)]
    pub const telemetry_metadata: Id = crate::macros::id_hex!("BCFDE38F7E452924C72803239392EA05");

    pub fn build_telemetry_metadata<B>(blobs: &mut B) -> std::result::Result<Fragment, B::PutError>
    where
        B: BlobStore<Blake3>,
    {
        let attrs = describe(blobs)?;

        let mut protocol = entity! { ExclusiveId::force_ref(&telemetry_metadata) @
            metadata::name: blobs.put("triblespace_telemetry")?,
            metadata::description: blobs.put(
                "Span-based profiling events emitted by TribleSpace telemetry.",
            )?,
            metadata::tag: metadata::KIND_PROTOCOL,
            metadata::attribute*: attrs,
        };

        protocol += entity! { ExclusiveId::force_ref(&kind_session) @
            metadata::name: blobs.put("telemetry_session")?,
            metadata::description: blobs.put(
                "A profiling session. Groups spans emitted during one telemetry run.",
            )?,
            metadata::tag: metadata::KIND_TAG,
        };
        protocol += entity! { ExclusiveId::force_ref(&kind_span) @
            metadata::name: blobs.put("telemetry_span")?,
            metadata::description: blobs.put(
                "A begin/end span with optional parent links.",
            )?,
            metadata::tag: metadata::KIND_TAG,
        };

        Ok(protocol)
    }
}

fn is_valid_short(value: &str) -> bool {
    value.as_bytes().len() <= 32 && !value.as_bytes().iter().any(|b| *b == 0)
}

struct ThreadTelemetry {
    workspace: Workspace<Pile<Blake3>>,
    last_flush: Instant,
}

struct TelemetryInner {
    repo: Mutex<Option<Repository<Pile<Blake3>>>>,
    workspaces: ThreadLocal<Arc<Mutex<ThreadTelemetry>>>,
    registry: Mutex<Vec<Arc<Mutex<ThreadTelemetry>>>>,
    session: Id,
    base: Instant,
    branch_id: Id,
    flush_interval: Duration,
    shutdown: AtomicBool,
}

impl TelemetryInner {
    fn now_ns(&self) -> u64 {
        self.base.elapsed().as_nanos() as u64
    }

    fn get_or_init_thread(&self) -> &Arc<Mutex<ThreadTelemetry>> {
        self.workspaces.get_or(|| {
            let mut repo_guard = self.repo.lock().expect("telemetry repo lock");
            let repo = repo_guard.as_mut().expect("telemetry repo not closed");
            let ws = repo
                .pull(self.branch_id)
                .expect("telemetry pull workspace");
            let arc = Arc::new(Mutex::new(ThreadTelemetry {
                workspace: ws,
                last_flush: Instant::now(),
            }));
            self.registry.lock().expect("telemetry registry lock").push(arc.clone());
            arc
        })
    }

    fn maybe_flush(&self, state: &mut ThreadTelemetry) {
        if state.last_flush.elapsed() < self.flush_interval {
            return;
        }
        let mut repo_guard = self.repo.lock().expect("telemetry repo lock");
        if let Some(repo) = repo_guard.as_mut() {
            if let Err(e) = repo.push(&mut state.workspace) {
                log::warn!("telemetry flush failed: {e:?}");
            }
        }
        state.last_flush = Instant::now();
    }
}

#[derive(Debug, Clone, Copy)]
struct TelemetrySpanData {
    span: Id,
    start_ns: u64,
}

#[derive(Default)]
struct FieldCapture {
    source: Option<String>,
}

impl tracing::field::Visit for FieldCapture {
    fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
        match field.name() {
            "source" if !value.is_empty() => self.source = Some(value.to_string()),
            _ => {}
        }
    }

    fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
        match field.name() {
            "source" => {
                let mut raw = format!("{value:?}");
                if raw.starts_with('"') && raw.ends_with('"') && raw.len() >= 2 {
                    raw = raw[1..raw.len() - 1].to_string();
                }
                if !raw.is_empty() {
                    self.source = Some(raw);
                }
            }
            _ => {}
        }
    }
}

/// Tracing layer that turns spans into TribleSpace telemetry.
///
/// Construct via [`Telemetry::layer_from_env`] and attach to your application's subscriber.
pub struct TelemetryLayer {
    inner: Arc<TelemetryInner>,
}

impl TelemetryLayer {
    fn parent_id<S>(
        &self,
        attrs: &tracing::span::Attributes<'_>,
        ctx: &Context<'_, S>,
    ) -> Option<Id>
    where
        S: Subscriber + for<'a> LookupSpan<'a>,
    {
        if let Some(parent) = attrs.parent() {
            if let Some(span) = ctx.span(parent) {
                if let Some(data) = span.extensions().get::<TelemetrySpanData>() {
                    return Some(data.span);
                }
            }
        }

        if let Some(id) = ctx.current_span().id() {
            if let Some(span) = ctx.span(id) {
                if let Some(data) = span.extensions().get::<TelemetrySpanData>() {
                    return Some(data.span);
                }
            }
        }

        None
    }
}

impl<S> Layer<S> for TelemetryLayer
where
    S: Subscriber + for<'a> LookupSpan<'a>,
{
    fn on_new_span(
        &self,
        attrs: &tracing::span::Attributes<'_>,
        id: &tracing::span::Id,
        ctx: Context<'_, S>,
    ) {
        if self.inner.shutdown.load(Ordering::Relaxed) {
            return;
        }

        let Some(span) = ctx.span(id) else {
            return;
        };

        let meta = attrs.metadata();
        let mut fields = FieldCapture::default();
        attrs.record(&mut fields);

        let start_ns = self.inner.now_ns();
        let span_id = *ufoid();
        let parent = self.parent_id(attrs, &ctx);

        span.extensions_mut().insert(TelemetrySpanData {
            span: span_id,
            start_ns,
        });

        let thread_state = self.inner.get_or_init_thread();
        let mut state = thread_state.lock().expect("telemetry thread state lock");

        let target = meta.target();
        let category = target.split("::").next().unwrap_or(target);
        let category = if !category.is_empty() && is_valid_short(category) {
            category
        } else {
            "span"
        };

        span_begin(
            &mut state.workspace,
            self.inner.session,
            span_id,
            parent,
            start_ns,
            category,
            meta.name(),
            fields.source,
        );
    }

    fn on_close(&self, id: tracing::span::Id, ctx: Context<'_, S>) {
        if self.inner.shutdown.load(Ordering::Relaxed) {
            return;
        }

        let Some(span) = ctx.span(&id) else {
            return;
        };
        let Some(data) = span.extensions().get::<TelemetrySpanData>().copied() else {
            return;
        };

        let end_ns = self.inner.now_ns();

        let thread_state = self.inner.get_or_init_thread();
        let mut state = thread_state.lock().expect("telemetry thread state lock");

        span_end(
            &mut state.workspace,
            data.span,
            end_ns,
            end_ns.saturating_sub(data.start_ns),
        );

        self.inner.maybe_flush(&mut state);
    }
}

pub struct Telemetry {
    inner: Arc<TelemetryInner>,
}

impl Telemetry {
    /// Start a telemetry sink and return a layer that writes spans into it.
    ///
    /// This does **not** install a tracing subscriber. Embed the returned layer into your
    /// application's subscriber, and keep the returned [`Telemetry`] guard alive to
    /// flush and close the sink on shutdown.
    pub fn layer_from_env(session_name: &str) -> Option<(TelemetryLayer, Self)> {
        let pile_path = std::env::var(ENV_TELEMETRY_PILE)
            .ok()
            .or_else(|| std::env::var(ENV_PILE).ok())?;
        let pile_path = pile_path.trim();
        if pile_path.is_empty() {
            return None;
        }
        let pile_path = PathBuf::from(pile_path);

        let branch_hex = std::env::var(ENV_TELEMETRY_BRANCH).ok()?;
        let branch_hex = branch_hex.trim();
        if branch_hex.len() != 32 {
            log::warn!(
                "TELEMETRY_BRANCH must be a 32-char hex ID, got {} chars",
                branch_hex.len()
            );
            return None;
        }
        let branch_id = Id::from_hex(branch_hex)?;

        let flush_ms = std::env::var(ENV_TELEMETRY_FLUSH_MS)
            .ok()
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(250);
        let flush_interval = Duration::from_millis(flush_ms.max(10));

        let base = Instant::now();
        let session_id = *ufoid();

        // Open and restore pile.
        if let Some(parent) = pile_path.parent().filter(|p| !p.as_os_str().is_empty()) {
            std::fs::create_dir_all(parent).ok()?;
        }
        let mut pile = Pile::<Blake3>::open(&pile_path).ok()?;
        if pile.restore().is_err() {
            let _ = pile.close();
            return None;
        }

        let signing_key = SigningKey::generate(&mut OsRng);
        let metadata_set: TribleSet = schema::build_telemetry_metadata(&mut pile)
            .ok()?
            .into();
        let mut repo = Repository::new(pile, signing_key, metadata_set).ok()?;

        // Commit session start entity.
        let mut ws = repo.pull(branch_id).ok()?;
        let session_entity = ExclusiveId::force_ref(&session_id);
        let mut init = TribleSet::new();
        init += entity! { session_entity @
            metadata::tag: schema::kind_session,
            schema::category: "session",
            schema::name: ws.put(session_name.to_string()),
            schema::begin_ns: 0u64,
        };
        ws.commit(init, "telemetry session");
        if repo.push(&mut ws).is_err() {
            let _ = repo.close();
            return None;
        }

        let inner = Arc::new(TelemetryInner {
            repo: Mutex::new(Some(repo)),
            workspaces: ThreadLocal::new(),
            registry: Mutex::new(Vec::new()),
            session: session_id,
            base,
            branch_id,
            flush_interval,
            shutdown: AtomicBool::new(false),
        });

        let layer = TelemetryLayer {
            inner: inner.clone(),
        };

        Some((layer, Self { inner }))
    }

    /// Convenience for standalone processes: start telemetry and install a global subscriber
    /// (only if none exists).
    pub fn install_global_from_env(session_name: &str) -> Option<Self> {
        let (layer, guard) = Self::layer_from_env(session_name)?;

        let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));
        let subscriber = tracing_subscriber::registry().with(filter).with(layer);

        if tracing::subscriber::set_global_default(subscriber).is_err() {
            log::warn!("triblespace telemetry disabled: tracing subscriber already set");
            drop(guard);
            return None;
        }

        Some(guard)
    }
}

impl Drop for Telemetry {
    fn drop(&mut self) {
        self.inner.shutdown.store(true, Ordering::Relaxed);

        // Flush all thread-local workspaces.
        let registry = self.inner.registry.lock().expect("telemetry registry lock");
        {
            let mut repo_guard = self.inner.repo.lock().expect("telemetry repo lock");
            if let Some(repo) = repo_guard.as_mut() {
                for state_arc in registry.iter() {
                    let mut state = state_arc.lock().expect("telemetry thread state lock");
                    if let Err(e) = repo.push(&mut state.workspace) {
                        log::warn!("telemetry shutdown flush failed: {e:?}");
                    }
                }

                // Commit session end entity.
                let end_ns = self.inner.now_ns();
                if let Ok(mut ws) = repo.pull(self.inner.branch_id) {
                    let session_entity = ExclusiveId::force_ref(&self.inner.session);
                    let mut end = TribleSet::new();
                    end += entity! { session_entity @
                        schema::end_ns: end_ns,
                        schema::duration_ns: end_ns,
                    };
                    ws.commit(end, "telemetry session end");
                    if let Err(e) = repo.push(&mut ws) {
                        log::warn!("telemetry session end push failed: {e:?}");
                    }
                }
            }
        }
        drop(registry);

        // Close the pile.
        let mut repo_guard = self.inner.repo.lock().expect("telemetry repo lock");
        if let Some(repo) = repo_guard.take() {
            if let Err(e) = repo.close() {
                log::warn!("telemetry pile close failed: {e:?}");
            }
        }
    }
}

fn span_begin(
    ws: &mut Workspace<Pile<Blake3>>,
    session: Id,
    span_id: Id,
    parent: Option<Id>,
    at_ns: u64,
    category: &str,
    name: &str,
    source: Option<String>,
) {
    let span_entity = ExclusiveId::force_ref(&span_id);
    let mut tribles = TribleSet::new();
    tribles += entity! { span_entity @
        metadata::tag: schema::kind_span,
        schema::session: session,
        schema::category: category,
        schema::name: ws.put(name.to_string()),
        schema::begin_ns: at_ns,
    };
    if let Some(parent) = parent {
        tribles += entity! { span_entity @ schema::parent: parent };
    }
    if let Some(source) = source {
        tribles += entity! { span_entity @ schema::source: ws.put(source) };
    }
    ws.commit(tribles, "telemetry span");
}

fn span_end(ws: &mut Workspace<Pile<Blake3>>, span_id: Id, at_ns: u64, duration_ns: u64) {
    let span_entity = ExclusiveId::force_ref(&span_id);
    let mut tribles = TribleSet::new();
    tribles += entity! { span_entity @
        schema::end_ns: at_ns,
        schema::duration_ns: duration_ns,
    };
    ws.commit(tribles, "telemetry span end");
}