Skip to main content

obeli_sk_wasm_workers/
component_logger.rs

1use chrono::Utc;
2use concepts::{
3    ExecutionId,
4    prefixed_ulid::RunId,
5    storage::{LogEntry, LogInfoAppendRow, LogLevel},
6};
7use tokio::sync::mpsc;
8use tracing::{Span, debug, error, info, trace, warn};
9
10#[derive(Clone)]
11pub(crate) struct ComponentLogger {
12    pub(crate) span: Span,
13    pub(crate) execution_id: ExecutionId,
14    pub(crate) run_id: RunId,
15    pub(crate) logs_storage_config: Option<LogStrageConfig>,
16}
17
18#[derive(Clone, derive_more::Debug)]
19pub struct LogStrageConfig {
20    pub min_level: LogLevel,
21    #[debug(skip)]
22    pub log_sender: mpsc::Sender<LogInfoAppendRow>,
23}
24
25const TARGET: &str = "app";
26impl ComponentLogger {
27    pub(crate) fn log(&mut self, level: LogLevel, message: String) {
28        // publish via tracing subscriber
29        self.span.in_scope(|| match level {
30            LogLevel::Trace => trace!(target: TARGET, "{message}"),
31            LogLevel::Debug => debug!(target: TARGET, "{message}"),
32            LogLevel::Info => info!(target: TARGET, "{message}"),
33            LogLevel::Warn => warn!(target: TARGET, "{message}"),
34            LogLevel::Error => error!(target: TARGET, "{message}"),
35        });
36        // store
37        if let Some(logs_storage_config) = &mut self.logs_storage_config
38            && level as u8 >= logs_storage_config.min_level as u8
39        {
40            let res = logs_storage_config.log_sender.try_send(LogInfoAppendRow {
41                execution_id: self.execution_id.clone(),
42                run_id: self.run_id,
43                log_entry: LogEntry::Log {
44                    created_at: Utc::now(),
45                    level,
46                    message,
47                },
48            });
49            if let Err(err) = res {
50                debug!("Dropping message: {err:?}");
51            }
52        }
53    }
54}
55
56pub(crate) mod log_activities {
57
58    // Generate `obelisk::log::log`
59    wasmtime::component::bindgen!({
60        path: "host-wit-log/",
61            // interfaces: "import obelisk:log@1.0.0/log;", // Broken in 26.0.0
62        inline: "package any:any;
63                    world bindings {
64                        import obelisk:log/log@1.0.0;
65                    }",
66        world: "any:any/bindings",
67    });
68}