Skip to content

Commit

Permalink
make g3-syslog and more unix sockets unix only
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq committed May 12, 2024
1 parent e4fc5e5 commit 43123dc
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/g3-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ clap.workspace = true
quinn = { workspace = true, optional = true, features = ["runtime-tokio", "ring"] }
g3-types = { workspace = true, features = ["async-log"] }
g3-stdlog.workspace = true
g3-syslog.workspace = true
g3-fluentd.workspace = true
g3-runtime.workspace = true
g3-yaml = { workspace = true, features = ["syslog", "fluentd", "statsd", "sched"] }
Expand All @@ -46,6 +45,7 @@ g3-http = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
daemonize = "0.5"
g3-syslog.workspace = true

[target.'cfg(target_os = "linux")'.dependencies]
g3-journal.workspace = true
Expand Down
5 changes: 5 additions & 0 deletions lib/g3-daemon/src/log/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use yaml_rust::Yaml;
use g3_fluentd::FluentdClientConfig;
#[cfg(target_os = "linux")]
use g3_journal::JournalConfig;
#[cfg(unix)]
use g3_syslog::SyslogBuilder;

const DEFAULT_CHANNEL_SIZE: usize = 4096;
Expand All @@ -34,6 +35,7 @@ pub enum LogConfigDriver {
Discard,
#[cfg(target_os = "linux")]
Journal(JournalConfig),
#[cfg(unix)]
Syslog(SyslogBuilder),
Fluentd(Arc<FluentdClientConfig>),
}
Expand Down Expand Up @@ -70,6 +72,7 @@ impl LogConfig {
)
}

#[cfg(unix)]
pub fn default_syslog(program_name: &'static str) -> Self {
Self::with_driver(
LogConfigDriver::Syslog(SyslogBuilder::with_ident(program_name)),
Expand All @@ -94,6 +97,7 @@ impl LogConfig {
"discard" => Ok(LogConfig::default_discard(program_name)),
#[cfg(target_os = "linux")]
"journal" => Ok(LogConfig::default_journal(program_name)),
#[cfg(unix)]
"syslog" => Ok(LogConfig::default_syslog(program_name)),
"fluentd" => Ok(LogConfig::default_fluentd(program_name)),
_ => Err(anyhow!("invalid log config")),
Expand All @@ -107,6 +111,7 @@ impl LogConfig {
LogConfigDriver::Journal(JournalConfig::with_ident(program_name));
Ok(())
}
#[cfg(unix)]
"syslog" => {
let builder = g3_yaml::value::as_syslog_builder(v, program_name)
.context("invalid syslog config")?;
Expand Down
4 changes: 4 additions & 0 deletions lib/g3-statsd-client/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
#[cfg(unix)]
use std::os::unix::net::UnixDatagram;
#[cfg(unix)]
use std::path::PathBuf;
use std::time::Duration;

Expand All @@ -29,6 +31,7 @@ const UDP_DEFAULT_PORT: u16 = 8125;
#[derive(Debug, Clone)]
pub enum StatsdBackend {
Udp(SocketAddr, Option<IpAddr>),
#[cfg(unix)]
Unix(PathBuf),
}

Expand Down Expand Up @@ -79,6 +82,7 @@ impl StatsdClientConfig {
let socket = UdpSocket::bind(SocketAddr::new(bind_ip, 0))?;
StatsdMetricsSink::udp_with_capacity(*addr, socket, 1024)
}
#[cfg(unix)]
StatsdBackend::Unix(path) => {
let socket = UnixDatagram::unbound()?;
StatsdMetricsSink::unix_with_capacity(path.clone(), socket, 4096)
Expand Down
7 changes: 7 additions & 0 deletions lib/g3-statsd-client/src/sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

use std::io;
use std::net::{SocketAddr, UdpSocket};
#[cfg(unix)]
use std::os::unix::net::UnixDatagram;
#[cfg(unix)]
use std::path::PathBuf;
#[cfg(test)]
use std::rc::Rc;
Expand All @@ -31,13 +33,16 @@ use buf::BufMetricsSink;
mod udp;
use udp::UdpMetricsSink;

#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix::UnixMetricsSink;

enum MetricsSinkIo {
#[cfg(test)]
Buf(BufMetricsSink),
Udp(UdpMetricsSink),
#[cfg(unix)]
Unix(UnixMetricsSink),
}

Expand All @@ -47,6 +52,7 @@ impl MetricsSinkIo {
#[cfg(test)]
MetricsSinkIo::Buf(b) => b.send_msg(buf),
MetricsSinkIo::Udp(s) => s.send_msg(buf),
#[cfg(unix)]
MetricsSinkIo::Unix(s) => s.send_msg(buf),
}
}
Expand Down Expand Up @@ -80,6 +86,7 @@ impl StatsdMetricsSink {
}
}

#[cfg(unix)]
pub(crate) fn unix_with_capacity(
path: PathBuf,
socket: UnixDatagram,
Expand Down
4 changes: 3 additions & 1 deletion lib/g3-yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ rustls-pemfile = { workspace = true, optional = true }
openssl = { workspace = true, optional = true }
http = { workspace = true, optional = true }
g3-types.workspace = true
g3-syslog = { workspace = true, optional = true }
g3-fluentd = { workspace = true, optional = true }
g3-statsd-client = { workspace = true, optional = true }
g3-histogram = { workspace = true, optional = true }
Expand All @@ -37,6 +36,9 @@ g3-icap-client = { workspace = true, optional = true }
g3-geoip-types = { workspace = true, optional = true }
g3-ip-locate = { workspace = true, optional = true }

[target.'cfg(unix)'.dependencies]
g3-syslog = { workspace = true, optional = true }

[features]
default = []
syslog = ["dep:g3-syslog"]
Expand Down
4 changes: 2 additions & 2 deletions lib/g3-yaml/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ pub mod acl;
#[cfg(feature = "acl-rule")]
pub mod acl_set;

#[cfg(feature = "syslog")]
#[cfg(all(unix, feature = "syslog"))]
mod syslog;
#[cfg(feature = "syslog")]
#[cfg(all(unix, feature = "syslog"))]
pub use syslog::as_syslog_builder;

#[cfg(feature = "fluentd")]
Expand Down
4 changes: 4 additions & 0 deletions lib/g3-yaml/src/value/statsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use std::net::{IpAddr, SocketAddr};
#[cfg(unix)]
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -60,6 +61,7 @@ fn as_statsd_backend_udp(v: &Yaml) -> anyhow::Result<StatsdBackend> {
}
}

#[cfg(unix)]
fn as_statsd_backend_unix(v: &Yaml) -> anyhow::Result<StatsdBackend> {
match v {
Yaml::Hash(map) => {
Expand Down Expand Up @@ -103,6 +105,7 @@ pub fn as_statsd_client_config(
config.set_backend(target);
Ok(())
}
#[cfg(unix)]
"target_unix" | "backend_unix" => {
let target =
as_statsd_backend_unix(v).context(format!("invalid value for key {k}"))?;
Expand All @@ -118,6 +121,7 @@ pub fn as_statsd_client_config(
config.set_backend(target);
Ok(())
}
#[cfg(unix)]
"unix" => {
let target = as_statsd_backend_unix(v)
.context(format!("invalid value for key {k}"))?;
Expand Down

0 comments on commit 43123dc

Please sign in to comment.