mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 11:21:24 +00:00
Promote console from compile flag to runtime flag
This commit is contained in:
parent
f13f870a92
commit
2ad536db17
4 changed files with 31 additions and 33 deletions
|
@ -10,7 +10,6 @@ edition = "2021"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
console = ["console-subscriber"]
|
||||
default = ["object-storage"]
|
||||
object-storage = ["reqwest", "rust-s3"]
|
||||
io-uring = [
|
||||
|
@ -30,7 +29,7 @@ async-trait = "0.1.51"
|
|||
awc = { version = "3.0.0", default-features = false, features = ["rustls"] }
|
||||
base64 = "0.13.0"
|
||||
config = "0.12.0"
|
||||
console-subscriber = { version = "0.1", optional = true }
|
||||
console-subscriber = "0.1"
|
||||
dashmap = "5.1.0"
|
||||
futures-util = "0.3.17"
|
||||
mime = "0.3.1"
|
||||
|
|
|
@ -82,7 +82,6 @@ pub(crate) struct Overrides {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
sled_cache_capacity: Option<u64>,
|
||||
|
||||
#[cfg(feature = "console")]
|
||||
#[structopt(
|
||||
long,
|
||||
help = "Specify the number of events the console subscriber is allowed to buffer"
|
||||
|
@ -122,19 +121,11 @@ impl Overrides {
|
|||
&& self.max_image_height.is_none()
|
||||
&& self.max_image_area.is_none()
|
||||
&& self.sled_cache_capacity.is_none()
|
||||
&& self.default_console_settings()
|
||||
&& self.console_buffer_capacity.is_none()
|
||||
&& self.api_key.is_none()
|
||||
&& self.opentelemetry_url.is_none()
|
||||
&& self.store.is_none()
|
||||
}
|
||||
|
||||
fn default_console_settings(&self) -> bool {
|
||||
#[cfg(feature = "console")]
|
||||
return self.console_buffer_capacity.is_none();
|
||||
|
||||
#[cfg(not(feature = "console"))]
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
|
@ -209,8 +200,7 @@ pub(crate) struct Config {
|
|||
max_image_height: usize,
|
||||
max_image_area: usize,
|
||||
sled_cache_capacity: u64,
|
||||
#[cfg(feature = "console")]
|
||||
console_buffer_capacity: usize,
|
||||
console_buffer_capacity: Option<usize>,
|
||||
api_key: Option<String>,
|
||||
opentelemetry_url: Option<Url>,
|
||||
store: Store,
|
||||
|
@ -226,8 +216,6 @@ pub(crate) struct Defaults {
|
|||
max_image_height: usize,
|
||||
max_image_area: usize,
|
||||
sled_cache_capacity: u64,
|
||||
#[cfg(feature = "console")]
|
||||
console_buffer_capacity: usize,
|
||||
store: Store,
|
||||
}
|
||||
|
||||
|
@ -241,8 +229,6 @@ impl Defaults {
|
|||
max_image_height: 10_000,
|
||||
max_image_area: 40_000_000,
|
||||
sled_cache_capacity: 1024 * 1024 * 64, // 16 times smaller than sled's default of 1GB
|
||||
#[cfg(feature = "console")]
|
||||
console_buffer_capacity: 1024 * 128,
|
||||
store: Store::FileStore { path: None },
|
||||
}
|
||||
}
|
||||
|
@ -298,8 +284,7 @@ impl Config {
|
|||
self.sled_cache_capacity
|
||||
}
|
||||
|
||||
#[cfg(feature = "console")]
|
||||
pub(crate) fn console_buffer_capacity(&self) -> usize {
|
||||
pub(crate) fn console_buffer_capacity(&self) -> Option<usize> {
|
||||
self.console_buffer_capacity
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#[cfg(feature = "console")]
|
||||
use console_subscriber::ConsoleLayer;
|
||||
use opentelemetry::{
|
||||
sdk::{propagation::TraceContextPropagator, Resource},
|
||||
|
@ -9,14 +8,15 @@ use tracing::subscriber::set_global_default;
|
|||
use tracing_error::ErrorLayer;
|
||||
use tracing_log::LogTracer;
|
||||
use tracing_subscriber::{
|
||||
filter::Targets, fmt::format::FmtSpan, layer::SubscriberExt, Layer, Registry,
|
||||
filter::Targets, fmt::format::FmtSpan, layer::SubscriberExt, registry::LookupSpan, Layer,
|
||||
Registry,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
pub(super) fn init_tracing(
|
||||
servic_name: &'static str,
|
||||
opentelemetry_url: Option<&Url>,
|
||||
#[cfg(feature = "console")] buffer_capacity: usize,
|
||||
buffer_capacity: Option<usize>,
|
||||
) -> anyhow::Result<()> {
|
||||
LogTracer::init()?;
|
||||
|
||||
|
@ -30,20 +30,35 @@ pub(super) fn init_tracing(
|
|||
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
|
||||
.with_filter(targets.clone());
|
||||
|
||||
#[cfg(feature = "console")]
|
||||
let subscriber = Registry::default()
|
||||
.with(format_layer)
|
||||
.with(ErrorLayer::default());
|
||||
|
||||
if let Some(buffer_capacity) = buffer_capacity {
|
||||
let console_layer = ConsoleLayer::builder()
|
||||
.with_default_env()
|
||||
.event_buffer_capacity(buffer_capacity)
|
||||
.server_addr(([0, 0, 0, 0], 6669))
|
||||
.spawn();
|
||||
|
||||
let subscriber = Registry::default()
|
||||
.with(format_layer)
|
||||
.with(ErrorLayer::default());
|
||||
|
||||
#[cfg(feature = "console")]
|
||||
let subscriber = subscriber.with(console_layer);
|
||||
|
||||
with_otel(subscriber, targets, servic_name, opentelemetry_url)
|
||||
} else {
|
||||
with_otel(subscriber, targets, servic_name, opentelemetry_url)
|
||||
}
|
||||
}
|
||||
|
||||
fn with_otel<S>(
|
||||
subscriber: S,
|
||||
targets: Targets,
|
||||
servic_name: &'static str,
|
||||
opentelemetry_url: Option<&Url>,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
S: SubscriberExt + Send + Sync,
|
||||
for<'a> S: LookupSpan<'a>,
|
||||
{
|
||||
if let Some(url) = opentelemetry_url {
|
||||
let tracer =
|
||||
opentelemetry_otlp::new_pipeline()
|
||||
|
|
|
@ -885,7 +885,6 @@ async fn main() -> anyhow::Result<()> {
|
|||
init_tracing(
|
||||
"pict-rs",
|
||||
CONFIG.opentelemetry_url(),
|
||||
#[cfg(feature = "console")]
|
||||
CONFIG.console_buffer_capacity(),
|
||||
)?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue