Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger: add klog functionality for segcache-rs #335

Merged
merged 48 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
88b21b3
logger: initial commit of klog functionality for segcache-rs
brayniac Aug 19, 2021
b8987ca
revert cas fix, moved to separate pr
brayniac Sep 1, 2021
c95c24c
change to mpmc crate and update rustcommon
brayniac Sep 2, 2021
11786ae
use rustcommon-time instead of chrono
brayniac Sep 2, 2021
dd5bf54
fix fuzz target
brayniac Sep 2, 2021
12bac98
merge master into feature branch
brayniac Sep 9, 2021
57a1355
address review feedback
brayniac Sep 9, 2021
6fee0c4
add license header
brayniac Sep 9, 2021
9e38240
fix pingserver metrics
brayniac Sep 9, 2021
e6f7028
revert expiry change
brayniac Sep 9, 2021
a543c3f
cleanup
brayniac Sep 9, 2021
084ecc9
fix year in license header
brayniac Sep 9, 2021
cb9e347
refactor logger crate
brayniac Sep 10, 2021
8d1d80d
fixes
brayniac Sep 10, 2021
49cbda5
add docs and comments
brayniac Sep 16, 2021
82e9307
actually use configured buffer sizes
brayniac Sep 16, 2021
8d47f45
remove flexi_logger from Cargo manifest
brayniac Sep 16, 2021
c45df04
klog formatting
brayniac Sep 16, 2021
602d287
refactor file logging backend
brayniac Sep 16, 2021
5e05ec6
fixes
brayniac Sep 16, 2021
b65499e
preallocate log buffers
brayniac Sep 16, 2021
e41c1be
rustfmt
brayniac Sep 16, 2021
fff5f7b
should be an empty vec, not a zero'd vec
brayniac Sep 16, 2021
c71344a
missing license header
brayniac Sep 16, 2021
266c5cd
use millisecond timestamps
brayniac Sep 16, 2021
670d934
use more consistent format
brayniac Sep 16, 2021
1475c47
reimplement logger
brayniac Sep 20, 2021
5033917
rustfmt
brayniac Sep 20, 2021
9075aff
add temporary helper function in logger crate
brayniac Sep 20, 2021
62a0b99
fix logging level and format
brayniac Sep 20, 2021
63d4538
add sampling functionality
brayniac Sep 21, 2021
dee9329
missing file
brayniac Sep 21, 2021
241777f
hide some implementation details and address clippy lints
brayniac Sep 21, 2021
e1a492c
cleanup
brayniac Sep 21, 2021
0f0b799
split examples
brayniac Sep 21, 2021
530a513
fix examples
brayniac Sep 21, 2021
cee9702
cleanup
brayniac Sep 21, 2021
1000896
docs / cleanup
brayniac Sep 21, 2021
9df4d40
update core after refactor of logging
brayniac Sep 21, 2021
3f6607f
add version numbers
brayniac Sep 21, 2021
6908000
add some crate level doc
brayniac Sep 21, 2021
c4d9a0d
add explanation for dropping new messages
brayniac Sep 21, 2021
ad638e6
add noplogger
brayniac Sep 23, 2021
db48514
add comments about choosing error level in convenience macro
brayniac Sep 23, 2021
6011154
helps to commit the noplogger file
brayniac Sep 23, 2021
c065945
fix rustdoc for nop
brayniac Sep 23, 2021
f1de38e
address review feedback
brayniac Sep 24, 2021
dcc84bd
wire up config options for queue depth and message size
brayniac Sep 24, 2021
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
22 changes: 22 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"src/rust/config",
"src/rust/core/server",
"src/rust/entrystore",
"src/rust/logger",
"src/rust/metrics",
"src/rust/macros",
"src/rust/protocol",
Expand Down
20 changes: 20 additions & 0 deletions config/pingserver.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,27 @@ nevent = 1024
[buf]

[debug]
# choose from: error, warn, info, debug, trace
log_level = "info"
# optionally, log to the file below instead of standard out
# log_file = "pingserver.log"
# backup file name for use with log rotation
log_backup = "pingserver.log.old"
# trigger log rotation when the file grows beyond this size (in bytes). Set this
# option to '0' to disable log rotation.
log_max_size = 1073741824

[klog]
# optionally, log commands to the file below
# file = "pingserver.cmd"
# backup file name for use with log rotation
backup = "pingserver.cmd.old"
# trigger log rotation when the file grows beyond this size (in bytes). Set this
# option to '0' to disable log rotation.
max_size = 1073741824
# specify the sampling ratio, 1 in N commands will be logged. Setting to '0'
# will disable command logging.
sample = 100

# NOTE: not currently implemented
[sockio]
Expand Down
25 changes: 25 additions & 0 deletions config/segcache.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,34 @@ time_type = "Memcache"
[debug]
# choose from: error, warn, info, debug, trace
log_level = "info"
# optionally, log to the file below instead of standard out
# log_file = "segcache.log"
# backup file name for use with log rotation
log_backup = "segcache.log.old"
# trigger log rotation when the file grows beyond this size (in bytes). Set this
# option to '0' to disable log rotation.
log_max_size = 1073741824

[klog]
# optionally, log commands to the file below
# file = "segcache.cmd"
# backup file name for use with log rotation
backup = "segcache.cmd.old"
# trigger log rotation when the file grows beyond this size (in bytes). Set this
# option to '0' to disable log rotation.
max_size = 1073741824
# specify the sampling ratio, 1 in N commands will be logged. Setting to '0'
# will disable command logging.
sample = 100

[sockio]

[tcp]

[tls]
# certificate chain used to validate client certificate
# certificate_chain = "client.chain"
# server certificate
# certificate = "server.crt"
# server private key
# private_key = "server.key"
42 changes: 35 additions & 7 deletions src/rust/config/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,50 @@
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

use crate::units::*;
use log::Level;
use serde::{Deserialize, Serialize};

// constants to define default values
const DEBUG_LOG_LEVEL: Level = Level::Info;
const DEBUG_LOG_FILE: Option<String> = None;
const DEBUG_LOG_NBUF: usize = 0;
const LOG_LEVEL: Level = Level::Info;
const LOG_FILE: Option<String> = None;
const LOG_BACKUP: Option<String> = None;
const LOG_MAX_SIZE: u64 = GB as u64;
const LOG_NBUF: usize = 0;

// helper functions
fn log_level() -> Level {
DEBUG_LOG_LEVEL
LOG_LEVEL
}

fn log_file() -> Option<String> {
DEBUG_LOG_FILE
LOG_FILE
}

fn log_backup() -> Option<String> {
LOG_BACKUP
}

fn log_max_size() -> u64 {
LOG_MAX_SIZE
}

fn log_nbuf() -> usize {
DEBUG_LOG_NBUF
LOG_NBUF
}

// struct definitions
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DebugConfig {
#[serde(with = "LevelDef")]
#[serde(default = "log_level")]
log_level: Level,
#[serde(default = "log_file")]
log_file: Option<String>,
#[serde(default = "log_backup")]
log_backup: Option<String>,
#[serde(default = "log_max_size")]
log_max_size: u64,
#[serde(default = "log_nbuf")]
log_nbuf: usize,
}
Expand All @@ -57,6 +72,17 @@ impl DebugConfig {
self.log_file.clone()
}

pub fn log_backup(&self) -> Option<String> {
match &self.log_backup {
Some(path) => Some(path.clone()),
None => self.log_file.as_ref().map(|path| format!("{}.old", path)),
}
}

pub fn log_max_size(&self) -> u64 {
self.log_max_size
}

pub fn log_nbuf(&self) -> usize {
self.log_nbuf
}
Expand All @@ -68,6 +94,8 @@ impl Default for DebugConfig {
Self {
log_level: log_level(),
log_file: log_file(),
log_backup: log_backup(),
log_max_size: log_max_size(),
log_nbuf: log_nbuf(),
}
}
Expand Down
123 changes: 123 additions & 0 deletions src/rust/config/src/klog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2021 Twitter, Inc.
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

use crate::units::*;
use serde::{Deserialize, Serialize};

////////////////////////////////////////////////////////////////////////////////
// constants to define default values
////////////////////////////////////////////////////////////////////////////////

// log to the file path
const FILE: Option<String> = None;

// log will rotate to the given backup path
const BACKUP: Option<String> = None;

// flush interval in milliseconds
const INTERVAL: usize = 100;

// max log size before rotate in bytes
const MAX_SIZE: u64 = GB as u64;

// buffer size in bytes
const NBUF: usize = 0;

// log 1 in every N commands
const SAMPLE: usize = 100;

////////////////////////////////////////////////////////////////////////////////
// helper functions
////////////////////////////////////////////////////////////////////////////////

fn file() -> Option<String> {
FILE
}

fn backup() -> Option<String> {
BACKUP
}

fn interval() -> usize {
INTERVAL
}

fn max_size() -> u64 {
MAX_SIZE
}

fn nbuf() -> usize {
NBUF
}

fn sample() -> usize {
SAMPLE
}

////////////////////////////////////////////////////////////////////////////////
// struct definitions
////////////////////////////////////////////////////////////////////////////////

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct KlogConfig {
#[serde(default = "file")]
file: Option<String>,
#[serde(default = "backup")]
backup: Option<String>,
#[serde(default = "interval")]
interval: usize,
#[serde(default = "nbuf")]
nbuf: usize,
#[serde(default = "sample")]
sample: usize,
#[serde(default = "max_size")]
max_size: u64,
}

////////////////////////////////////////////////////////////////////////////////
// implementation
////////////////////////////////////////////////////////////////////////////////

impl KlogConfig {
pub fn file(&self) -> Option<String> {
self.file.clone()
}

pub fn backup(&self) -> Option<String> {
match &self.backup {
Some(path) => Some(path.clone()),
None => self.file.as_ref().map(|path| format!("{}.old", path)),
}
}

pub fn interval(&self) -> usize {
self.interval
}

pub fn max_size(&self) -> u64 {
self.max_size
}

pub fn nbuf(&self) -> usize {
self.nbuf
}

pub fn sample(&self) -> usize {
self.sample
}
}

// trait implementations
impl Default for KlogConfig {
fn default() -> Self {
Self {
file: file(),
backup: backup(),
interval: interval(),
max_size: max_size(),
nbuf: nbuf(),
sample: sample(),
}
}
}
3 changes: 3 additions & 0 deletions src/rust/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod array;
mod buf;
mod dbuf;
mod debug;
mod klog;
mod pingserver;
pub mod seg;
mod segcache;
Expand All @@ -19,13 +20,15 @@ mod stats_log;
mod tcp;
pub mod time;
mod tls;
mod units;
mod worker;

pub use admin::AdminConfig;
pub use array::ArrayConfig;
pub use buf::BufConfig;
pub use dbuf::DbufConfig;
pub use debug::DebugConfig;
pub use klog::KlogConfig;
pub use pingserver::PingserverConfig;
pub use seg::SegConfig;
pub use segcache::SegcacheConfig;
Expand Down
7 changes: 7 additions & 0 deletions src/rust/config/src/pingserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct PingserverConfig {
#[serde(default)]
debug: DebugConfig,
#[serde(default)]
klog: KlogConfig,
#[serde(default)]
sockio: SockioConfig,
#[serde(default)]
tcp: TcpConfig,
Expand Down Expand Up @@ -114,6 +116,10 @@ impl PingserverConfig {
&self.debug
}

pub fn klog(&self) -> &KlogConfig {
&self.klog
}

pub fn sockio(&self) -> &SockioConfig {
&self.sockio
}
Expand Down Expand Up @@ -142,6 +148,7 @@ impl Default for PingserverConfig {

buf: Default::default(),
debug: Default::default(),
klog: Default::default(),
sockio: Default::default(),
tcp: Default::default(),
tls: Default::default(),
Expand Down
Loading