Skip to content

Commit 5c22ab1

Browse files
fix: refactor, deepsource fixes
1 parent 2ba7b4d commit 5c22ab1

File tree

7 files changed

+675
-1452
lines changed

7 files changed

+675
-1452
lines changed

src/cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ impl FromArgMatches for Cli {
529529
self.kafka_host = m.get_one::<String>(Self::KAFKA_HOST).cloned();
530530
self.kafka_group = m.get_one::<String>(Self::KAFKA_GROUP).cloned();
531531
self.kafka_client_id = m.get_one::<String>(Self::KAFKA_CLIENT_ID).cloned();
532-
self.kafka_security_protocol = m.get_one::<SslProtocol>(Self::KAFKA_SECURITY_PROTOCOL).cloned();
532+
self.kafka_security_protocol = m
533+
.get_one::<SslProtocol>(Self::KAFKA_SECURITY_PROTOCOL)
534+
.cloned();
533535
self.kafka_partitions = m.get_one::<String>(Self::KAFKA_PARTITIONS).cloned();
534536

535537
self.tls_cert_path = m.get_one::<PathBuf>(Self::TLS_CERT).cloned();

src/handlers/http/otel.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,10 @@ pub fn collect_json_from_any_value(
2525
value: super::otel::proto::common::v1::Value,
2626
) -> BTreeMap<String, Value> {
2727
let mut value_json: BTreeMap<String, Value> = BTreeMap::new();
28-
if value.str_val.is_some() {
29-
value_json.insert(
30-
key.to_string(),
31-
Value::String(value.str_val.as_ref().unwrap().to_owned()),
32-
);
33-
}
34-
if value.bool_val.is_some() {
35-
value_json.insert(key.to_string(), Value::Bool(value.bool_val.unwrap()));
36-
}
37-
if value.int_val.is_some() {
38-
value_json.insert(
39-
key.to_string(),
40-
Value::String(value.int_val.as_ref().unwrap().to_owned()),
41-
);
42-
}
43-
if value.double_val.is_some() {
44-
value_json.insert(
45-
key.to_string(),
46-
Value::Number(serde_json::Number::from_f64(value.double_val.unwrap()).unwrap()),
47-
);
48-
}
28+
insert_if_some(&mut value_json, key, &value.str_val);
29+
insert_bool_if_some(&mut value_json, key, &value.bool_val);
30+
insert_if_some(&mut value_json, key, &value.int_val);
31+
insert_number_if_some(&mut value_json, key, &value.double_val);
4932

5033
//ArrayValue is a vector of AnyValue
5134
//traverse by recursively calling the same function
@@ -91,12 +74,7 @@ pub fn collect_json_from_any_value(
9174
}
9275
}
9376
}
94-
if value.bytes_val.is_some() {
95-
value_json.insert(
96-
key.to_string(),
97-
Value::String(value.bytes_val.as_ref().unwrap().to_owned()),
98-
);
99-
}
77+
insert_if_some(&mut value_json, key, &value.bytes_val);
10078

10179
value_json
10280
}
@@ -135,3 +113,36 @@ pub fn flatten_attributes(attributes: &Vec<KeyValue>) -> BTreeMap<String, Value>
135113
}
136114
attributes_json
137115
}
116+
117+
pub fn insert_if_some<T: ToString>(
118+
map: &mut BTreeMap<String, Value>,
119+
key: &str,
120+
option: &Option<T>,
121+
) {
122+
if let Some(value) = option {
123+
map.insert(key.to_string(), Value::String(value.to_string()));
124+
}
125+
}
126+
127+
pub fn insert_number_if_some(map: &mut BTreeMap<String, Value>, key: &str, option: &Option<f64>) {
128+
if let Some(value) = option {
129+
if let Some(number) = serde_json::Number::from_f64(*value) {
130+
map.insert(key.to_string(), Value::Number(number));
131+
}
132+
}
133+
}
134+
135+
pub fn insert_bool_if_some(map: &mut BTreeMap<String, Value>, key: &str, option: &Option<bool>) {
136+
if let Some(value) = option {
137+
map.insert(key.to_string(), Value::Bool(*value));
138+
}
139+
}
140+
141+
pub fn insert_attributes(map: &mut BTreeMap<String, Value>, attributes: &Option<Vec<KeyValue>>) {
142+
if let Some(attrs) = attributes {
143+
let attributes_json = flatten_attributes(attrs);
144+
for (key, value) in attributes_json {
145+
map.insert(key, value);
146+
}
147+
}
148+
}

src/handlers/http/otel/opentelemetry.proto.trace.v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub mod span {
193193
#[derive(Serialize, Deserialize, Debug)]
194194
pub struct Event {
195195
/// time_unix_nano is the time the event occurred.
196-
pub time_unix_nano: Option<u64>,
196+
pub time_unix_nano: Option<String>,
197197
/// name of the event.
198198
/// This field is semantically required to be set to non-empty string.
199199
pub name: Option<String>,

0 commit comments

Comments
 (0)