Skip to content

Commit

Permalink
rename MetricsTag to MetricTag
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq committed Jan 15, 2025
1 parent 609024f commit 5cb4d9a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
6 changes: 3 additions & 3 deletions g3keymess/src/control/capnp/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use anyhow::anyhow;
use capnp::capability::Promise;
use capnp_rpc::pry;

use g3_types::metrics::{MetricsTagName, MetricsTagValue};
use g3_types::metrics::{MetricTagName, MetricTagValue};

use g3keymess_proto::proc_capnp::proc_control;
use g3keymess_proto::server_capnp::server_control;
Expand Down Expand Up @@ -161,9 +161,9 @@ impl proc_control::Server for ProcControlImpl {

fn do_add_metrics_tag(name: &str, value: &str) -> anyhow::Result<()> {
let name =
MetricsTagName::from_str(name).map_err(|e| anyhow!("invalid metrics tag name: {e}"))?;
MetricTagName::from_str(name).map_err(|e| anyhow!("invalid metrics tag name: {e}"))?;
let value =
MetricsTagValue::from_str(value).map_err(|e| anyhow!("invalid metrics tag value: {e}"))?;
MetricTagValue::from_str(value).map_err(|e| anyhow!("invalid metrics tag value: {e}"))?;

// add for server metrics
crate::serve::foreach_server(|_, s| {
Expand Down
6 changes: 3 additions & 3 deletions g3keymess/src/control/capnp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::anyhow;
use capnp::capability::Promise;
use capnp_rpc::pry;

use g3_types::metrics::{MetricsTagName, MetricsTagValue, NodeName};
use g3_types::metrics::{MetricTagName, MetricTagValue, NodeName};

use g3keymess_proto::server_capnp::server_control;

Expand All @@ -41,8 +41,8 @@ impl ServerControlImpl {

fn do_add_metrics_tag(&self, name: &str, value: &str) -> anyhow::Result<()> {
let name =
MetricsTagName::from_str(name).map_err(|e| anyhow!("invalid metrics tag name: {e}"))?;
let value = MetricsTagValue::from_str(value)
MetricTagName::from_str(name).map_err(|e| anyhow!("invalid metrics tag name: {e}"))?;
let value = MetricTagValue::from_str(value)
.map_err(|e| anyhow!("invalid metrics tag value: {e}"))?;
self.server.add_dynamic_metrics_tag(name, value);
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions g3keymess/src/serve/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use tokio::sync::{broadcast, Semaphore};

use g3_daemon::listen::ListenStats;
use g3_daemon::server::ServerQuitPolicy;
use g3_types::metrics::{MetricsTagName, MetricsTagValue, NodeName, StaticMetricsTags};
use g3_types::metrics::{MetricTagName, MetricTagValue, NodeName, StaticMetricsTags};

use super::{
KeyServerDurationRecorder, KeyServerDurationStats, KeyServerRuntime, KeyServerStats,
Expand Down Expand Up @@ -164,7 +164,7 @@ impl KeyServer {
self.prepare_reload(config)
}

pub(crate) fn add_dynamic_metrics_tag(&self, name: MetricsTagName, value: MetricsTagValue) {
pub(crate) fn add_dynamic_metrics_tag(&self, name: MetricTagName, value: MetricTagValue) {
let dynamic_tags = self.dynamic_metrics_tags.load();
let mut dynamic_tags = dynamic_tags.as_ref().clone();
dynamic_tags.insert(name, value);
Expand Down
2 changes: 1 addition & 1 deletion lib/g3-types/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod name;
pub use name::NodeName;

mod tag;
pub use tag::{MetricsTagName, MetricsTagValue, StaticMetricsTags};
pub use tag::{MetricTagName, MetricTagValue, StaticMetricsTags};

#[derive(Debug, Error)]
pub enum ParseError {
Expand Down
37 changes: 17 additions & 20 deletions lib/g3-types/src/metrics/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,64 @@ use smol_str::SmolStr;
use super::{chars_allowed_in_opentsdb, ParseError};

#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct MetricsTagName(SmolStr);
pub struct MetricTagName(SmolStr);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MetricsTagValue(SmolStr);
pub struct MetricTagValue(SmolStr);

pub type StaticMetricsTags = BTreeMap<MetricsTagName, MetricsTagValue>;
pub type StaticMetricsTags = BTreeMap<MetricTagName, MetricTagValue>;

impl MetricsTagName {
impl MetricTagName {
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl AsRef<str> for MetricsTagName {
impl AsRef<str> for MetricTagName {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl FromStr for MetricsTagName {
impl FromStr for MetricTagName {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
chars_allowed_in_opentsdb(s)?;
Ok(MetricsTagName(s.into()))
Ok(MetricTagName(s.into()))
}
}

impl fmt::Display for MetricsTagName {
impl fmt::Display for MetricTagName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl MetricsTagValue {
impl MetricTagValue {
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl AsRef<str> for MetricsTagValue {
impl AsRef<str> for MetricTagValue {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl FromStr for MetricsTagValue {
impl FromStr for MetricTagValue {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
chars_allowed_in_opentsdb(s)?;
Ok(MetricsTagValue(s.into()))
Ok(MetricTagValue(s.into()))
}
}

impl fmt::Display for MetricsTagValue {
impl fmt::Display for MetricTagValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
Expand All @@ -92,18 +92,15 @@ mod tests {

#[test]
fn t_metrics_name() {
assert_eq!(MetricsTagName::from_str("abc-1").unwrap().as_str(), "abc-1");
assert_eq!(MetricTagName::from_str("abc-1").unwrap().as_str(), "abc-1");

assert!(MetricsTagName::from_str("a=b").is_err());
assert!(MetricTagName::from_str("a=b").is_err());
}

#[test]
fn t_metrics_value() {
assert_eq!(
MetricsTagValue::from_str("abc-1").unwrap().as_str(),
"abc-1"
);
assert_eq!(MetricTagValue::from_str("abc-1").unwrap().as_str(), "abc-1");

assert!(MetricsTagValue::from_str("a=b").is_err());
assert!(MetricTagValue::from_str("a=b").is_err());
}
}
6 changes: 3 additions & 3 deletions lib/g3-yaml/src/value/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use anyhow::{anyhow, Context};
use yaml_rust::Yaml;

use g3_types::collection::WeightedValue;
use g3_types::metrics::{MetricsTagName, MetricsTagValue, NodeName, StaticMetricsTags};
use g3_types::metrics::{MetricTagName, MetricTagValue, NodeName, StaticMetricsTags};

pub fn as_metrics_name(v: &Yaml) -> anyhow::Result<NodeName> {
if let Yaml::String(s) = v {
Expand All @@ -38,9 +38,9 @@ pub fn as_static_metrics_tags(v: &Yaml) -> anyhow::Result<StaticMetricsTags> {
if let Yaml::Hash(map) = v {
let mut tags = BTreeMap::new();
crate::foreach_kv(map, |k, v| {
let name = MetricsTagName::from_str(k).context("invalid metrics tag name")?;
let name = MetricTagName::from_str(k).context("invalid metrics tag name")?;
let value_s = crate::value::as_string(v).context("invalid metrics tag yaml value")?;
let value = MetricsTagValue::from_str(&value_s).context("invalid metrics tag value")?;
let value = MetricTagValue::from_str(&value_s).context("invalid metrics tag value")?;

if tags.insert(name, value).is_some() {
Err(anyhow!("found duplicate value for tag name {k}"))
Expand Down

0 comments on commit 5cb4d9a

Please sign in to comment.