Skip to content

Commit 5738927

Browse files
populate severity text based on severity_number if text is not available
fixed data types of flattened log fields as per the proto
1 parent 992a9dc commit 5738927

File tree

3 files changed

+69
-51
lines changed

3 files changed

+69
-51
lines changed

server/src/handlers/http/otel.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod proto;
2424
mod resource;
2525
use std::collections::BTreeMap;
2626

27+
use self::log::{LogRecordFlags, SeverityNumber};
28+
2729
// Value can be one of types - String, Bool, Int, Double, ArrayValue, AnyValue, KeyValueList, Byte
2830
fn collect_json_from_any_value(
2931
key: &String,
@@ -39,19 +41,19 @@ fn collect_json_from_any_value(
3941
if value.bool_val.is_some() {
4042
value_json.insert(
4143
key.to_string(),
42-
Value::String(value.bool_val.as_ref().unwrap().to_owned()),
44+
Value::Bool(value.bool_val.unwrap()),
4345
);
4446
}
4547
if value.int_val.is_some() {
4648
value_json.insert(
4749
key.to_string(),
48-
Value::String(value.int_val.as_ref().unwrap().to_owned()),
50+
Value::Number(serde_json::Number::from(value.int_val.unwrap())),
4951
);
5052
}
5153
if value.double_val.is_some() {
5254
value_json.insert(
5355
key.to_string(),
54-
Value::String(value.double_val.as_ref().unwrap().to_owned()),
56+
Value::Number(serde_json::Number::from_f64(value.double_val.unwrap()).unwrap()),
5557
);
5658
}
5759

@@ -125,7 +127,7 @@ pub fn flatten_otel_logs(body: Bytes) -> Vec<BTreeMap<String, Value>> {
125127
if resource.dropped_attributes_count.is_some() {
126128
otel_json.insert(
127129
"resource_dropped_attributes_count".to_string(),
128-
Value::String(resource.dropped_attributes_count.unwrap().to_string()),
130+
Value::Number(serde_json::Number::from(resource.dropped_attributes_count.unwrap())),
129131
);
130132
}
131133
}
@@ -166,12 +168,7 @@ pub fn flatten_otel_logs(body: Bytes) -> Vec<BTreeMap<String, Value>> {
166168
if instrumentation_scope.dropped_attributes_count.is_some() {
167169
otel_json.insert(
168170
"instrumentation_scope_dropped_attributes_count".to_string(),
169-
Value::String(
170-
instrumentation_scope
171-
.dropped_attributes_count
172-
.unwrap()
173-
.to_string(),
174-
),
171+
Value::Number(serde_json::Number::from(instrumentation_scope.dropped_attributes_count.unwrap())),
175172
);
176173
}
177174
}
@@ -199,12 +196,21 @@ pub fn flatten_otel_logs(body: Bytes) -> Vec<BTreeMap<String, Value>> {
199196
);
200197
}
201198
if log_record.severity_number.is_some() {
199+
let severity_number: i32 = log_record.severity_number.unwrap();
202200
log_record_json.insert(
203201
"severity_number".to_string(),
204-
Value::String(
205-
log_record.severity_number.as_ref().unwrap().to_string(),
202+
Value::Number(
203+
serde_json::Number::from(severity_number),
206204
),
207205
);
206+
if log_record.severity_text.is_none() {
207+
log_record_json.insert(
208+
"severity_text".to_string(),
209+
Value::String(
210+
SeverityNumber::as_str_name(severity_number).to_string(),
211+
),
212+
);
213+
}
208214
}
209215
if log_record.severity_text.is_some() {
210216
log_record_json.insert(
@@ -214,6 +220,7 @@ pub fn flatten_otel_logs(body: Bytes) -> Vec<BTreeMap<String, Value>> {
214220
),
215221
);
216222
}
223+
217224

218225
if log_record.name.is_some() {
219226
log_record_json.insert(
@@ -246,16 +253,25 @@ pub fn flatten_otel_logs(body: Bytes) -> Vec<BTreeMap<String, Value>> {
246253
if log_record.dropped_attributes_count.is_some() {
247254
log_record_json.insert(
248255
"log_record_dropped_attributes_count".to_string(),
249-
Value::String(
250-
log_record.dropped_attributes_count.unwrap().to_string(),
251-
),
256+
257+
Value::Number(serde_json::Number::from(log_record.dropped_attributes_count.unwrap())),
258+
252259
);
253260
}
254261

255262
if log_record.flags.is_some() {
263+
let flags: u32 = log_record.flags.unwrap();
256264
log_record_json.insert(
257-
"flags".to_string(),
258-
Value::String(log_record.flags.unwrap().to_string()),
265+
"flags_number".to_string(),
266+
Value::Number(
267+
serde_json::Number::from(flags),
268+
),
269+
);
270+
log_record_json.insert(
271+
"flags_string".to_string(),
272+
Value::String(
273+
LogRecordFlags::as_str_name(flags).to_string(),
274+
),
259275
);
260276
}
261277

server/src/handlers/http/otel/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ pub struct Value {
3232
#[serde(rename = "stringValue")]
3333
pub str_val: Option<String>,
3434
#[serde(rename = "boolValue")]
35-
pub bool_val: Option<String>,
35+
pub bool_val: Option<bool>,
3636
#[serde(rename = "intValue")]
37-
pub int_val: Option<String>,
37+
pub int_val: Option<i64>,
3838
#[serde(rename = "doubleValue")]
39-
pub double_val: Option<String>,
39+
pub double_val: Option<f64>,
4040
#[serde(rename = "arrayValue")]
4141
pub array_val: Option<ArrayValue>,
4242
#[serde(rename = "keyVauleList")]

server/src/handlers/http/otel/log.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,34 @@ impl SeverityNumber {
188188
///
189189
/// The values are not transformed in any way and thus are considered stable
190190
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
191-
pub fn as_str_name(&self) -> &'static str {
192-
match self {
193-
SeverityNumber::Unspecified => "SEVERITY_NUMBER_UNSPECIFIED",
194-
SeverityNumber::Trace => "SEVERITY_NUMBER_TRACE",
195-
SeverityNumber::Trace2 => "SEVERITY_NUMBER_TRACE2",
196-
SeverityNumber::Trace3 => "SEVERITY_NUMBER_TRACE3",
197-
SeverityNumber::Trace4 => "SEVERITY_NUMBER_TRACE4",
198-
SeverityNumber::Debug => "SEVERITY_NUMBER_DEBUG",
199-
SeverityNumber::Debug2 => "SEVERITY_NUMBER_DEBUG2",
200-
SeverityNumber::Debug3 => "SEVERITY_NUMBER_DEBUG3",
201-
SeverityNumber::Debug4 => "SEVERITY_NUMBER_DEBUG4",
202-
SeverityNumber::Info => "SEVERITY_NUMBER_INFO",
203-
SeverityNumber::Info2 => "SEVERITY_NUMBER_INFO2",
204-
SeverityNumber::Info3 => "SEVERITY_NUMBER_INFO3",
205-
SeverityNumber::Info4 => "SEVERITY_NUMBER_INFO4",
206-
SeverityNumber::Warn => "SEVERITY_NUMBER_WARN",
207-
SeverityNumber::Warn2 => "SEVERITY_NUMBER_WARN2",
208-
SeverityNumber::Warn3 => "SEVERITY_NUMBER_WARN3",
209-
SeverityNumber::Warn4 => "SEVERITY_NUMBER_WARN4",
210-
SeverityNumber::Error => "SEVERITY_NUMBER_ERROR",
211-
SeverityNumber::Error2 => "SEVERITY_NUMBER_ERROR2",
212-
SeverityNumber::Error3 => "SEVERITY_NUMBER_ERROR3",
213-
SeverityNumber::Error4 => "SEVERITY_NUMBER_ERROR4",
214-
SeverityNumber::Fatal => "SEVERITY_NUMBER_FATAL",
215-
SeverityNumber::Fatal2 => "SEVERITY_NUMBER_FATAL2",
216-
SeverityNumber::Fatal3 => "SEVERITY_NUMBER_FATAL3",
217-
SeverityNumber::Fatal4 => "SEVERITY_NUMBER_FATAL4",
191+
pub fn as_str_name(severity_number: i32) -> &'static str {
192+
match severity_number {
193+
0 => "SEVERITY_NUMBER_UNSPECIFIED",
194+
1 => "SEVERITY_NUMBER_TRACE",
195+
2 => "SEVERITY_NUMBER_TRACE2",
196+
3 => "SEVERITY_NUMBER_TRACE3",
197+
4 => "SEVERITY_NUMBER_TRACE4",
198+
5 => "SEVERITY_NUMBER_DEBUG",
199+
6 => "SEVERITY_NUMBER_DEBUG2",
200+
7 => "SEVERITY_NUMBER_DEBUG3",
201+
8 => "SEVERITY_NUMBER_DEBUG4",
202+
9 => "SEVERITY_NUMBER_INFO",
203+
10 => "SEVERITY_NUMBER_INFO2",
204+
11 => "SEVERITY_NUMBER_INFO3",
205+
12 => "SEVERITY_NUMBER_INFO4",
206+
13 => "SEVERITY_NUMBER_WARN",
207+
14 => "SEVERITY_NUMBER_WARN2",
208+
15 => "SEVERITY_NUMBER_WARN3",
209+
16 => "SEVERITY_NUMBER_WARN4",
210+
17 => "SEVERITY_NUMBER_ERROR",
211+
18 => "SEVERITY_NUMBER_ERROR2",
212+
19 => "SEVERITY_NUMBER_ERROR3",
213+
20 => "SEVERITY_NUMBER_ERROR4",
214+
21 => "SEVERITY_NUMBER_FATAL",
215+
22 => "SEVERITY_NUMBER_FATAL2",
216+
23 => "SEVERITY_NUMBER_FATAL3",
217+
24 => "SEVERITY_NUMBER_FATAL4",
218+
_ => "Invalid severity number",
218219
}
219220
}
220221
/// Creates an enum from field names used in the ProtoBuf definition.
@@ -269,10 +270,11 @@ impl LogRecordFlags {
269270
///
270271
/// The values are not transformed in any way and thus are considered stable
271272
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
272-
pub fn as_str_name(&self) -> &'static str {
273-
match self {
274-
LogRecordFlags::DoNotUse => "LOG_RECORD_FLAGS_DO_NOT_USE",
275-
LogRecordFlags::TraceFlagsMask => "LOG_RECORD_FLAGS_TRACE_FLAGS_MASK",
273+
pub fn as_str_name(flag: u32) -> &'static str {
274+
match flag {
275+
0 => "LOG_RECORD_FLAGS_DO_NOT_USE",
276+
255 => "LOG_RECORD_FLAGS_TRACE_FLAGS_MASK",
277+
_ => "Invalid flag",
276278
}
277279
}
278280
/// Creates an enum from field names used in the ProtoBuf definition.

0 commit comments

Comments
 (0)