Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cc = "1.0"
cf-rustracing = "1.2.1"
cf-rustracing-jaeger = "1.2.2"
clap = "4.4"
crossbeam-utils = { version = "0.8", default-features = false }
darling = "0.21"
erased-serde = "0.4"
futures-util = "0.3"
Expand Down
30 changes: 14 additions & 16 deletions foundations-macros/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ fn expand_from_parsed(args: MacroArgs, extern_: Mod) -> proc_macro2::TokenStream
fns,
} = extern_;

let reexports = quote! { #foundations::reexports_for_macros };

// This should be using `Span::def_site` but it is currently unstable.
let metrics_struct = Ident::new(&format!("__{mod_name}_Metrics"), Span::call_site());

Expand Down Expand Up @@ -152,8 +150,8 @@ fn expand_from_parsed(args: MacroArgs, extern_: Mod) -> proc_macro2::TokenStream
#(#label_set_structs)*

#[allow(non_upper_case_globals)]
static #metrics_struct: #reexports::once_cell::sync::Lazy<#metrics_struct> =
#reexports::once_cell::sync::Lazy::new(|| {
static #metrics_struct: ::std::sync::LazyLock<#metrics_struct> =
::std::sync::LazyLock::new(|| {
#init_registry
#init_opt_registry

Expand Down Expand Up @@ -424,8 +422,8 @@ mod tests {
struct __empty_Metrics {}

#[allow(non_upper_case_globals)]
static __empty_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__empty_Metrics> =
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| { __empty_Metrics {} });
static __empty_Metrics: ::std::sync::LazyLock<__empty_Metrics> =
::std::sync::LazyLock::new(|| { __empty_Metrics {} });
}
};

Expand Down Expand Up @@ -459,8 +457,8 @@ mod tests {
}

#[allow(non_upper_case_globals)]
static __oxy_Metrics: tarmac::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
tarmac::reexports_for_macros::once_cell::sync::Lazy::new(|| {
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
::std::sync::LazyLock::new(|| {
let registry = &mut *tarmac::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);

__oxy_Metrics {
Expand Down Expand Up @@ -516,8 +514,8 @@ mod tests {
}

#[allow(non_upper_case_globals)]
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
::std::sync::LazyLock::new(|| {
let opt_registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), true, true);

__oxy_Metrics {
Expand Down Expand Up @@ -577,8 +575,8 @@ mod tests {
}

#[allow(non_upper_case_globals)]
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
::std::sync::LazyLock::new(|| {
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, false);
let opt_registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), true, false);

Expand Down Expand Up @@ -682,8 +680,8 @@ mod tests {
}

#[allow(non_upper_case_globals)]
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
::std::sync::LazyLock::new(|| {
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);

__oxy_Metrics {
Expand Down Expand Up @@ -777,8 +775,8 @@ mod tests {
}

#[allow(non_upper_case_globals)]
static __oxy_Metrics: ::foundations::reexports_for_macros::once_cell::sync::Lazy<__oxy_Metrics> =
::foundations::reexports_for_macros::once_cell::sync::Lazy::new(|| {
static __oxy_Metrics: ::std::sync::LazyLock<__oxy_Metrics> =
::std::sync::LazyLock::new(|| {
let registry = &mut *::foundations::telemetry::metrics::internal::Registries::get_subsystem(stringify!(oxy), false, true);

__oxy_Metrics {
Expand Down
6 changes: 3 additions & 3 deletions foundations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ tokio-runtime-metrics = [
# Enables logging functionality.
logging = [
"dep:governor",
"dep:once_cell",
"dep:parking_lot",
"dep:slog-async",
"dep:slog-json",
Expand All @@ -111,13 +110,13 @@ logging = [
"dep:thread_local",
"dep:futures-util",
"dep:serde",
"dep:crossbeam-utils",
]

# Enables distributed tracing functionality.
tracing = [
"dep:foundations-macros",
"dep:governor",
"dep:once_cell",
"dep:parking_lot",
"dep:rand",
"dep:cf-rustracing-jaeger",
Expand All @@ -130,13 +129,13 @@ tracing = [
"dep:slab",
"dep:libc",
"dep:http",
"dep:crossbeam-utils",
]

# Enables metrics functionality.
metrics = [
"dep:foundations-macros",
"dep:erased-serde",
"dep:once_cell",
"dep:parking_lot",
"dep:prometheus-client",
"dep:prometheus",
Expand Down Expand Up @@ -188,6 +187,7 @@ foundations-macros = { workspace = true, optional = true, default-features = fal
cf-rustracing = { workspace = true, optional = true }
cf-rustracing-jaeger = { workspace = true, optional = true }
clap = { workspace = true, optional = true }
crossbeam-utils = { workspace = true, optional = true }
erased-serde = { workspace = true, optional = true }
futures-util = { workspace = true, optional = true }
governor = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion foundations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub mod security;
pub mod reexports_for_macros {
#[cfg(feature = "tracing")]
pub use cf_rustracing;
#[cfg(any(feature = "metrics", feature = "security"))]
#[cfg(feature = "security")]
pub use once_cell;
#[cfg(feature = "metrics")]
pub use parking_lot;
Expand Down
14 changes: 8 additions & 6 deletions foundations/src/telemetry/log/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::telemetry::log::retry_writer::RetryPipeWriter;
use crate::telemetry::scope::ScopeStack;
use crate::telemetry::settings::{LogFormat, LogOutput, LoggingSettings};
use crate::{BootstrapResult, ServiceInfo};
use once_cell::sync::{Lazy, OnceCell};
use crossbeam_utils::CachePadded;
use slog::{
Discard, Drain, FnValue, Fuse, LevelFilter, Logger, Never, OwnedKV, SendSyncRefUnwindSafeDrain,
SendSyncRefUnwindSafeKV, SendSyncUnwindSafeDrain,
Expand All @@ -23,15 +23,17 @@ use std::fs::File;
use std::io;
use std::io::BufWriter;
use std::panic::RefUnwindSafe;
use std::sync::Arc;
use std::sync::{Arc, LazyLock, OnceLock};

type FilteredDrain<D> = LevelFilter<
FieldFilteringDrain<FieldRedactFilterFactory, FieldFilteringDrain<FieldDedupFilterFactory, D>>,
>;

static HARNESS: OnceCell<LogHarness> = OnceCell::new();
// These singletons are accessed _very often_, and each access requires an atomic load to
// ensure initialization. Make sure nobody else invalidates our cache lines.
static HARNESS: CachePadded<OnceLock<LogHarness>> = CachePadded::new(OnceLock::new());

static NOOP_HARNESS: Lazy<LogHarness> = Lazy::new(|| {
static NOOP_HARNESS: CachePadded<LazyLock<LogHarness>> = CachePadded::new(LazyLock::new(|| {
let root_drain = Arc::new(Discard);
let noop_log =
LoggerWithKvNestingTracking::new(Logger::root(Arc::clone(&root_drain), slog::o!()));
Expand All @@ -42,7 +44,7 @@ static NOOP_HARNESS: Lazy<LogHarness> = Lazy::new(|| {
settings: Default::default(),
log_scope_stack: Default::default(),
}
});
}));

pub(crate) struct LogHarness {
pub(crate) root_log: SharedLog,
Expand All @@ -53,7 +55,7 @@ pub(crate) struct LogHarness {

impl LogHarness {
pub(crate) fn get() -> &'static Self {
HARNESS.get().unwrap_or(&NOOP_HARNESS)
HARNESS.get().unwrap_or_else(|| &**NOOP_HARNESS)
}

#[cfg(any(test, feature = "testing"))]
Expand Down
6 changes: 4 additions & 2 deletions foundations/src/telemetry/memory_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::fs::File;
use std::io::Read;
use std::os::raw::c_char;
use std::sync::mpsc::{self};
use std::sync::OnceLock;
use tempfile::NamedTempFile;
use tokio::sync::{oneshot, Mutex as AsyncMutex};

// TODO(once_cell_try): replace with `std::sync::OnceLock`
static PROFILER: OnceCell<Option<MemoryProfiler>> = OnceCell::new();
static HEAP_PROFILE_REQUEST_SENDER: OnceCell<
static HEAP_PROFILE_REQUEST_SENDER: OnceLock<
AsyncMutex<mpsc::Sender<oneshot::Sender<Result<String>>>>,
> = OnceCell::new();
> = OnceLock::new();

mod control {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions foundations/src/telemetry/metrics/internal.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::{info_metric, ExtraProducer, InfoMetric};
use crate::telemetry::settings::{MetricsSettings, ServiceNameFormat};
use crate::{Result, ServiceInfo};
use once_cell::sync::OnceCell;
use prometheus_client::encoding::text::{encode, EncodeMetric};
use prometheus_client::registry::Registry;
use prometools::serde::InfoGauge;
use std::any::TypeId;
use std::borrow::Cow;
use std::collections::HashMap;
use std::ops::DerefMut;
use std::sync::OnceLock;

static REGISTRIES: OnceCell<Registries> = OnceCell::new();
static REGISTRIES: OnceLock<Registries> = OnceLock::new();

enum MetricsServiceName {
Prefix(String),
Expand Down
35 changes: 19 additions & 16 deletions foundations/src/telemetry/tracing/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ use super::output_jaeger_thrift_udp;
use crate::telemetry::scope::ScopeStack;
use crate::telemetry::settings::{SamplingStrategy, TracesOutput, TracingSettings};
use crate::telemetry::tracing::live::ActiveRoots;
use crate::telemetry::tracing::rate_limit::RateLimitingProbabilisticSampler;
use crate::{BootstrapResult, ServiceInfo};
use cf_rustracing::sampler::{NullSampler, PassiveSampler, Sampler};
use cf_rustracing_jaeger::span::SpanReceiver;
use crossbeam_utils::CachePadded;
use futures_util::future::BoxFuture;
use once_cell::sync::{Lazy, OnceCell};
use std::sync::{LazyLock, OnceLock};

#[cfg(feature = "telemetry-otlp-grpc")]
use super::output_otlp_grpc;

use cf_rustracing::sampler::{PassiveSampler, Sampler};
#[cfg(feature = "testing")]
use std::borrow::Cow;

use crate::telemetry::tracing::rate_limit::RateLimitingProbabilisticSampler;

static HARNESS: OnceCell<TracingHarness> = OnceCell::new();
// These singletons are accessed _very often_, and each access requires an atomic load to
// ensure initialization. Make sure nobody else invalidates our cache lines.
static HARNESS: CachePadded<OnceLock<TracingHarness>> = CachePadded::new(OnceLock::new());

static NOOP_HARNESS: Lazy<TracingHarness> = Lazy::new(|| {
let (noop_tracer, _) = Tracer::new(RateLimitingProbabilisticSampler::default().boxed());
static NOOP_HARNESS: CachePadded<LazyLock<TracingHarness>> =
CachePadded::new(LazyLock::new(|| {
let (noop_tracer, _) = Tracer::new(NullSampler.boxed());

TracingHarness {
tracer: noop_tracer,
span_scope_stack: Default::default(),
TracingHarness {
tracer: noop_tracer,
span_scope_stack: Default::default(),

#[cfg(feature = "testing")]
test_tracer_scope_stack: Default::default(),
#[cfg(feature = "testing")]
test_tracer_scope_stack: Default::default(),

active_roots: Default::default(),
}
});
active_roots: Default::default(),
}
}));

pub(crate) struct TracingHarness {
tracer: Tracer,
Expand All @@ -46,7 +49,7 @@ pub(crate) struct TracingHarness {

impl TracingHarness {
pub(crate) fn get() -> &'static Self {
HARNESS.get().unwrap_or(&NOOP_HARNESS)
HARNESS.get().unwrap_or_else(|| &**NOOP_HARNESS)
}

#[cfg(feature = "testing")]
Expand Down
Loading
Loading