feat: add Metrics and Tracing to Config (#28)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2023-05-16 17:51:09 +08:00 committed by GitHub
parent 8a3dc7faaa
commit 8edbbf7128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View File

@ -20,6 +20,7 @@ use crate::config::{
}; };
use serde::Deserialize; use serde::Deserialize;
use std::fs; use std::fs;
use std::net::{Ipv4Addr, SocketAddr};
use std::path::PathBuf; use std::path::PathBuf;
use tracing::info; use tracing::info;
use validator::Validate; use validator::Validate;
@ -67,6 +68,11 @@ pub fn default_dfdaemon_lock_path() -> PathBuf {
default_lock_dir().join("dfdaemon.lock") default_lock_dir().join("dfdaemon.lock")
} }
// default_dfdaemon_tracing_addr is the default address to report tracing log.
pub fn default_dfdaemon_tracing_addr() -> SocketAddr {
SocketAddr::from((Ipv4Addr::LOCALHOST, 14268))
}
// Error is the error for Config. // Error is the error for Config.
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
@ -77,6 +83,35 @@ pub enum Error {
YAML(#[from] serde_yaml::Error), YAML(#[from] serde_yaml::Error),
} }
// Tracing is the tracing configuration for dfdaemon.
#[derive(Debug, Clone, Validate, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Tracing {
// enable indicates whether enable tracing.
pub enable: bool,
// addr is the address to report tracing log.
pub addr: SocketAddr,
}
// Tracing implements default value for Tracing.
impl Default for Tracing {
fn default() -> Self {
Tracing {
enable: false,
addr: default_dfdaemon_tracing_addr(),
}
}
}
// Metrics is the metrics configuration for dfdaemon.
#[derive(Debug, Clone, Default, Validate, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Metrics {
// enable indicates whether enable metrics.
pub enable: bool,
}
// Config is the configuration for dfdaemon. // Config is the configuration for dfdaemon.
#[derive(Debug, Clone, Validate, Deserialize)] #[derive(Debug, Clone, Validate, Deserialize)]
#[serde(default, rename_all = "camelCase")] #[serde(default, rename_all = "camelCase")]
@ -95,6 +130,12 @@ pub struct Config {
// lock_path is the file lock path for dfdaemon service. // lock_path is the file lock path for dfdaemon service.
pub lock_dir: PathBuf, pub lock_dir: PathBuf,
// metrics is the metrics configuration for dfdaemon.
pub metrics: Metrics,
// tracing is the tracing configuration for dfdaemon.
pub tracing: Tracing,
} }
// Default implements default value for Config. // Default implements default value for Config.
@ -106,6 +147,8 @@ impl Default for Config {
cache_dir: default_dfdaemon_cache_dir(), cache_dir: default_dfdaemon_cache_dir(),
root_dir: default_root_dir(), root_dir: default_root_dir(),
lock_dir: default_lock_dir(), lock_dir: default_lock_dir(),
metrics: Metrics::default(),
tracing: Tracing::default(),
} }
} }
} }

15
src/tracing/mod.rs Normal file
View File

@ -0,0 +1,15 @@
/*
* Copyright 2023 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/