Skip to content

Commit

Permalink
fix telemetry (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug authored Nov 10, 2023
1 parent eb38b15 commit 24d5e20
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 141 deletions.
104 changes: 37 additions & 67 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = { workspace = true }

[features]
default = ["shadowsocks"]
tracing = ["console-subscriber", "tracing-timing"]
tracing = []
bench = ["criterion"]

[dependencies]
Expand Down Expand Up @@ -45,7 +45,8 @@ once_cell = "1.18.0"

# opentelemetry
opentelemetry = "0.21"
tracing-opentelemetry = "0.21"
opentelemetry_sdk = "0.21"
tracing-opentelemetry = "0.22"
opentelemetry-jaeger = { version = "0.20", features = ["collector_client", "hyper_collector_client", "rt-tokio"] }

crc32fast = "1.3.2"
Expand Down Expand Up @@ -96,8 +97,8 @@ maxminddb = "0.23.0"
public-suffix = "0.1.0"
murmur3 = "0.5.2"

console-subscriber = { version = "0.2.0", optional = true }
tracing-timing = { version = "0.6.0", optional = true }
console-subscriber = { version = "0.2.0" }
tracing-timing = { version = "0.6.0" }
criterion = { version = "0.5", features = ["html_reports", "async_tokio"], optional = true }

[dev-dependencies]
Expand Down
137 changes: 67 additions & 70 deletions clash_lib/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,76 +94,73 @@ pub fn setup_logging(
cwd: &str,
log_file: Option<String>,
) -> anyhow::Result<Option<WorkerGuard>> {
#[cfg(feature = "tracing")]
{
console_subscriber::init();
Ok(None)
}
#[cfg(not(feature = "tracing"))]
{
let filter = EnvFilter::builder()
.with_default_directive(
format!("clash={}", level)
.parse::<Directive>()
.unwrap()
.into(),
)
.from_env_lossy();

let jaeger = if let Ok(jager_endpoint) = std::env::var("JAGER_ENDPOINT") {
global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());

let tracer = opentelemetry_jaeger::new_collector_pipeline()
.with_service_name("clash-rs")
.with_endpoint(jager_endpoint)
.with_hyper()
.install_batch(opentelemetry::runtime::Tokio)?;

Some(tracing_opentelemetry::layer().with_tracer(tracer))
} else {
None
};

let ios_os_log = if cfg!(target_os = "ios") {
Some(OsLogger::new("com.watfaq.clash", "default"))
} else {
None
};

let (appender, g) = if let Some(log_file) = log_file {
let file_appender = tracing_appender::rolling::daily(cwd, log_file);
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
(Some(non_blocking), Some(guard))
} else {
(None, None)
};

let subscriber = tracing_subscriber::registry()
.with(jaeger)
.with(filter)
.with(collector)
.with(
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stdout().is_terminal())
.pretty()
.with_file(true)
.with_line_number(true)
.with_writer(move || -> Box<dyn std::io::Write> {
Box::new(W(appender.clone()))
})
.with_writer(std::io::stdout),
)
.with(ios_os_log);

tracing::subscriber::set_global_default(subscriber)
.map_err(|x| anyhow!("setup logging error: {}", x))?;

if let Ok(jager_endpiont) = std::env::var("JAGER_ENDPOINT") {
debug!("jager endpoint: {}", jager_endpiont);
}

Ok(g)
}
let filter = EnvFilter::builder()
.with_default_directive(
format!("clash={}", level)
.parse::<Directive>()
.unwrap()
.into(),
)
.from_env_lossy();

let jaeger = if let Ok(jager_endpoint) = std::env::var("JAGER_ENDPOINT") {
global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());

let tracer = opentelemetry_jaeger::new_collector_pipeline()
.with_service_name("clash-rs")
.with_endpoint(jager_endpoint)
.with_hyper()
.install_batch(opentelemetry_sdk::runtime::Tokio)?;

Some(tracing_opentelemetry::layer().with_tracer(tracer))
} else {
None
};

let ios_os_log = if cfg!(target_os = "ios") {
Some(OsLogger::new("com.watfaq.clash", "default"))
} else {
None
};

let (appender, g) = if let Some(log_file) = log_file {
let file_appender = tracing_appender::rolling::daily(cwd, log_file);
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
(Some(non_blocking), Some(guard))
} else {
(None, None)
};

let console_layer = if cfg!(feature = "tracing") {
Some(console_subscriber::spawn())
} else {
None
};

let subscriber = tracing_subscriber::registry()
.with(jaeger)
.with(filter)
.with(collector)
.with(console_layer)
.with(
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stdout().is_terminal())
.pretty()
.with_file(true)
.with_line_number(true)
.with_writer(move || -> Box<dyn std::io::Write> { Box::new(W(appender.clone())) })
.with_writer(std::io::stdout),
)
.with(ios_os_log);

tracing::subscriber::set_global_default(subscriber)
.map_err(|x| anyhow!("setup logging error: {}", x))?;

if let Ok(jager_endpiont) = std::env::var("JAGER_ENDPOINT") {
debug!("jager endpoint: {}", jager_endpiont);
}

Ok(g)
}

struct EventVisitor<'a>(&'a mut Vec<String>);
Expand Down

0 comments on commit 24d5e20

Please sign in to comment.