Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
refactor runtimes a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 18, 2024
1 parent 3fa1fd7 commit f702378
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
32 changes: 24 additions & 8 deletions core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ use crate::{
props::Props, rng::Rng, timestamp::Timestamp,
};

pub static SHARED: Ambient = Ambient::new();
pub static INTERNAL: Ambient = Ambient::new();
static SHARED: AmbientSlot = AmbientSlot::new();
static INTERNAL: AmbientSlot = AmbientSlot::new();

pub fn shared() -> &'static AmbientRuntime<'static> {
SHARED.get()
}

pub fn shared_slot() -> &'static AmbientSlot {
&SHARED
}

pub fn internal() -> &'static AmbientRuntime<'static> {
INTERNAL.get()
}

pub fn internal_slot() -> &'static AmbientSlot {
&INTERNAL
}

#[derive(Debug, Clone, Copy)]
pub struct Runtime<TEmitter = Empty, TFilter = Empty, TCtxt = Empty, TClock = Empty, TRng = Empty> {
Expand Down Expand Up @@ -290,7 +306,7 @@ mod std_support {
}
}

pub struct Ambient(OnceLock<AmbientSync>);
pub struct AmbientSlot(OnceLock<AmbientSync>);

struct AmbientSync {
value: AmbientSyncValue,
Expand Down Expand Up @@ -324,9 +340,9 @@ mod std_support {
unsafe impl Send for AmbientSync where AmbientSyncValue: Send {}
unsafe impl Sync for AmbientSync where AmbientSyncValue: Sync {}

impl Ambient {
impl AmbientSlot {
pub const fn new() -> Self {
Ambient(OnceLock::new())
AmbientSlot(OnceLock::new())
}

pub fn init<TEmitter, TFilter, TCtxt, TClock, TRng>(
Expand Down Expand Up @@ -403,11 +419,11 @@ pub use self::std_support::*;
mod no_std_support {
use super::*;

pub struct Ambient {}
pub struct AmbientSlot {}

impl Ambient {
impl AmbientSlot {
pub const fn new() -> Self {
Ambient {}
AmbientSlot {}
}

pub fn get(&self) -> &Runtime {
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn base_push_ctxt<C: Ctxt>(ctxt: C, props: impl Props) -> Frame<C> {

#[track_caller]
pub fn emit(evt: &Event<impl Props>) {
let ambient = emit_core::runtime::SHARED.get();
let ambient = emit_core::runtime::shared();

let tpl = evt.tpl();
let props = evt.props();
Expand All @@ -85,34 +85,34 @@ pub type StartTimer = Timer<&'static emit_core::runtime::AmbientRuntime<'static>

#[track_caller]
pub fn now() -> Option<Timestamp> {
emit_core::runtime::SHARED.get().now()
emit_core::runtime::shared().now()
}

#[track_caller]
pub fn push_ctxt(props: impl Props) -> PushCtxt {
base_push_ctxt(emit_core::runtime::SHARED.get(), props)
base_push_ctxt(emit_core::runtime::shared(), props)
}

#[track_caller]
pub fn current_ctxt() -> PushCtxt {
base_push_ctxt(emit_core::runtime::SHARED.get(), empty::Empty)
base_push_ctxt(emit_core::runtime::shared(), empty::Empty)
}

#[track_caller]
pub fn start_timer() -> StartTimer {
Timer::start(emit_core::runtime::SHARED.get())
Timer::start(emit_core::runtime::shared())
}

#[track_caller]
pub fn new_span_id() -> Option<SpanId> {
emit_core::runtime::SHARED.get().gen_span_id()
emit_core::runtime::shared().gen_span_id()
}

#[track_caller]
pub fn current_span_id() -> Option<SpanId> {
let mut span_id = None;

emit_core::runtime::SHARED.get().with_current(|ctxt| {
emit_core::runtime::shared().with_current(|ctxt| {
span_id = ctxt.pull();
});

Expand All @@ -121,14 +121,14 @@ pub fn current_span_id() -> Option<SpanId> {

#[track_caller]
pub fn new_trace_id() -> Option<TraceId> {
emit_core::runtime::SHARED.get().gen_trace_id()
emit_core::runtime::shared().gen_trace_id()
}

#[track_caller]
pub fn current_trace_id() -> Option<TraceId> {
let mut trace_id = None;

emit_core::runtime::SHARED.get().with_current(|ctxt| {
emit_core::runtime::shared().with_current(|ctxt| {
trace_id = ctxt.pull();
});

Expand Down
4 changes: 2 additions & 2 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ pub fn __private_emit(
tpl: Template,
props: impl Props,
) {
let rt = crate::runtime::SHARED.get();
let rt = crate::runtime::shared();

base_emit(
rt.emitter().and(to),
Expand Down Expand Up @@ -489,7 +489,7 @@ pub fn __private_in_ctxt_rt<C: Ctxt>(

#[track_caller]
pub fn __private_in_ctxt(props: impl Props) -> Frame<&'static (dyn ErasedCtxt + Send + Sync)> {
let rt = crate::runtime::SHARED.get();
let rt = crate::runtime::shared();

base_push_ctxt(rt.ctxt(), props)
}
Expand Down
11 changes: 6 additions & 5 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ where
{
#[must_use = "call `blocking_flush` at the end of `main` to ensure events are flushed."]
pub fn init(self) -> Init<&'static TEmitter, &'static TCtxt> {
self.init_rt(&emit_core::runtime::SHARED)
self.init_slot(emit_core::runtime::shared_slot())
}

#[must_use = "call `blocking_flush` at the end of `main` to ensure events are flushed."]
pub fn init_internal(self) -> Init<&'static TEmitter, &'static TCtxt> {
self.init_rt(&emit_core::runtime::INTERNAL)
self.init_slot(emit_core::runtime::internal_slot())
}

fn init_rt(
#[must_use = "call `blocking_flush` at the end of `main` to ensure events are flushed."]
pub fn init_slot(
self,
rt: &'static emit_core::runtime::Ambient,
slot: &'static emit_core::runtime::AmbientSlot,
) -> Init<&'static TEmitter, &'static TCtxt> {
let ambient = rt
let ambient = slot
.init(
emit_core::runtime::Runtime::new()
.with_emitter(self.emitter)
Expand Down
4 changes: 2 additions & 2 deletions targets/otlp/src/data/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
)
.unwrap();

emit::debug!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
emit::debug!(rt: emit::runtime::internal(), "received {#[emit::as_debug] response}");
}
Err(body) => {
let response =
crate::data::generated::collector::logs::v1::ExportLogsPartialSuccess::decode(body)
.unwrap();

emit::warn!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
emit::warn!(rt: emit::runtime::internal(), "received {#[emit::as_debug] response}");
}
}
}
4 changes: 2 additions & 2 deletions targets/otlp/src/data/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
)
.unwrap();

emit::debug!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
emit::debug!(rt: emit::runtime::internal(), "received {#[emit::as_debug] response}");
}
Err(body) => {
let response =
Expand All @@ -85,7 +85,7 @@ pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
)
.unwrap();

emit::warn!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
emit::warn!(rt: emit::runtime::internal(), "received {#[emit::as_debug] response}");
}
}
}

0 comments on commit f702378

Please sign in to comment.