mirror of
https://github.com/bytedance/g3.git
synced 2026-05-04 22:51:02 +00:00
g3proxy: use metrics name for escaper name
This commit is contained in:
parent
9aef683e80
commit
435d9b3fc4
95 changed files with 572 additions and 529 deletions
72
lib/g3-msgpack/src/value/metrics.rs
Normal file
72
lib/g3-msgpack/src/value/metrics.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright 2023 ByteDance and/or its affiliates.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use rmpv::ValueRef;
|
||||
|
||||
use g3_types::collection::WeightedValue;
|
||||
use g3_types::metrics::MetricsName;
|
||||
|
||||
pub fn as_metrics_name(v: &ValueRef) -> anyhow::Result<MetricsName> {
|
||||
if let ValueRef::String(s) = v {
|
||||
let s = s.as_str().ok_or_else(|| anyhow!("invalid utf-8 string"))?;
|
||||
let name = MetricsName::from_str(s).map_err(|e| anyhow!("invalid metrics name: {e}"))?;
|
||||
Ok(name)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"msgpack value type for 'metrics name' should be 'string'"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_weighted_metrics_name(v: &ValueRef) -> anyhow::Result<WeightedValue<MetricsName>> {
|
||||
match v {
|
||||
ValueRef::Map(map) => {
|
||||
let mut name = MetricsName::default();
|
||||
let mut weight = None;
|
||||
|
||||
for (k, v) in map {
|
||||
let key = as_metrics_name(k).context("all keys should be metrics name")?;
|
||||
match crate::key::normalize(key.as_str()).as_str() {
|
||||
"name" => {
|
||||
name = as_metrics_name(v)
|
||||
.context(format!("invalid metrics name value for key {key}"))?;
|
||||
}
|
||||
"weight" => {
|
||||
let f = crate::value::as_f64(v)
|
||||
.context(format!("invalid f64 value for key {key}"))?;
|
||||
weight = Some(f);
|
||||
}
|
||||
_ => {} // ignore all other keys
|
||||
}
|
||||
}
|
||||
|
||||
if name.is_empty() {
|
||||
Err(anyhow!("no name found"))
|
||||
} else if let Some(weight) = weight {
|
||||
Ok(WeightedValue::with_weight(name, weight))
|
||||
} else {
|
||||
Ok(WeightedValue::new(name))
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let s = as_metrics_name(v).context("invalid string value")?;
|
||||
Ok(WeightedValue::new(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
mod datetime;
|
||||
mod metrics;
|
||||
mod primary;
|
||||
mod uuid;
|
||||
|
||||
|
|
@ -23,6 +24,7 @@ mod rustls;
|
|||
|
||||
pub use self::uuid::as_uuid;
|
||||
pub use datetime::as_rfc3339_datetime;
|
||||
pub use metrics::{as_metrics_name, as_weighted_metrics_name};
|
||||
pub use primary::{as_f64, as_string, as_u32, as_weighted_name_string};
|
||||
|
||||
#[cfg(feature = "rustls")]
|
||||
|
|
|
|||
|
|
@ -81,33 +81,33 @@ pub fn as_f64(v: &ValueRef) -> anyhow::Result<f64> {
|
|||
}
|
||||
|
||||
pub fn as_weighted_name_string(v: &ValueRef) -> anyhow::Result<WeightedValue<String>> {
|
||||
const KEY_NAME: &str = "name";
|
||||
const KEY_WEIGHT: &str = "weight";
|
||||
|
||||
match v {
|
||||
ValueRef::Map(map) => {
|
||||
let mut name: Option<String> = None;
|
||||
let mut weight = WeightedValue::<String>::DEFAULT_WEIGHT;
|
||||
let mut name = String::new();
|
||||
let mut weight = None;
|
||||
|
||||
for (k, v) in map {
|
||||
let key = as_string(k).context("all keys should be string")?;
|
||||
match crate::key::normalize(key.as_str()).as_str() {
|
||||
KEY_NAME => {
|
||||
let v =
|
||||
"name" => {
|
||||
name =
|
||||
as_string(v).context(format!("invalid string value for key {key}"))?;
|
||||
name = Some(v);
|
||||
}
|
||||
KEY_WEIGHT => {
|
||||
weight = crate::value::as_f64(v)
|
||||
"weight" => {
|
||||
let f = crate::value::as_f64(v)
|
||||
.context(format!("invalid f64 value for key {key}"))?;
|
||||
weight = Some(f);
|
||||
}
|
||||
_ => {} // ignore all other keys
|
||||
}
|
||||
}
|
||||
|
||||
match name {
|
||||
Some(s) => Ok(WeightedValue::with_weight(s, weight)),
|
||||
None => Err(anyhow!("no required key {KEY_NAME} found")),
|
||||
if name.is_empty() {
|
||||
Err(anyhow!("no name found"))
|
||||
} else if let Some(weight) = weight {
|
||||
Ok(WeightedValue::with_weight(name, weight))
|
||||
} else {
|
||||
Ok(WeightedValue::new(name))
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue