mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-04 00:15:29 +00:00
fix(servers): map NaN OTLP log attributes to null and clamp v0 span durations (#10789)
An OTLP log attribute holding a NaN double panicked the pipeline conversion, and a v0 trace span whose end precedes its start underflowed duration_nano. Also remove the duplicated new-column conversion in the log path, reuse the v1 fixed trace column indexes in v2, drop the uncalled v1::write_span_to_row, and fix stale comments. Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -20,39 +20,14 @@ pub enum TraceCoerceError {
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
// For now we support the following coercions:
|
||||
// - Int64 to Float64
|
||||
// - Int64 to String
|
||||
// - Float64 to String
|
||||
// - Boolean to String
|
||||
// The following coercions are supported with parse, which could fail:
|
||||
// If fails, we will return TraceCoerceError::Unsupported.
|
||||
// - String to Int64
|
||||
// - String to Float64
|
||||
// - String to Boolean
|
||||
// The empty string and "-" coerce to null instead of failing: instrumentations
|
||||
// fill numeric attributes from absent headers this way (PSR-7 `getHeaderLine`
|
||||
// returns ""), and rejecting the value would reject the whole span.
|
||||
//
|
||||
// Lossless signed-to-unsigned integer casts. These let the built-in data
|
||||
// models move from unsigned to signed integers while existing unsigned tables
|
||||
// keep accepting new signed ingest without an `ALTER TABLE`: an existing
|
||||
// UInt64/UInt32 column coerces an incoming Int64/Int32 request into the
|
||||
// existing type:
|
||||
// - Int64 to UInt64 (e.g. trace `duration_nano` on the v1 path): checked,
|
||||
// so negative values are rejected rather than silently wrapping. Counts and
|
||||
// durations are non-negative by construction; a negative request is a
|
||||
// malformed value (e.g. a span whose end precedes its start).
|
||||
// - Int32 to UInt32 (e.g. log `trace_flags`): bit-preserving, because
|
||||
// `trace_flags` is a bit field whose high bits may legitimately be set
|
||||
// (e.g. the W3C sampled flag); bit patterns must round-trip exactly.
|
||||
// String -> numeric/boolean parses and may fail. The empty string and "-"
|
||||
// coerce to null instead: instrumentations fill numeric attributes from absent
|
||||
// headers this way (PSR-7 `getHeaderLine` returns ""), and rejecting the value
|
||||
// would reject the whole span.
|
||||
|
||||
/// The signed→unsigned integer coercions that let the built-in data models
|
||||
/// move from unsigned to signed integers while existing unsigned tables keep
|
||||
/// accepting new signed ingest without an `ALTER TABLE` (see the pair
|
||||
/// descriptions in the module-level comment above). Kept as one predicate so
|
||||
/// the trace and log ingest paths share the same supported pair set and
|
||||
/// cannot drift.
|
||||
/// Signed -> unsigned pairs that let existing unsigned built-in columns accept
|
||||
/// the signed types new tables use, without an `ALTER TABLE`. Shared by the
|
||||
/// trace and log paths.
|
||||
pub fn is_supported_signed_to_unsigned_coercion(
|
||||
request_type: ColumnDataType,
|
||||
target_type: ColumnDataType,
|
||||
|
||||
+56
-162
@@ -57,11 +57,11 @@ pub const LOG_TABLE_NAME: &str = "opentelemetry_logs";
|
||||
const EVENT_NAME_COLUMN: &str = "event_name";
|
||||
const OBSERVED_TIMESTAMP_COLUMN: &str = "observed_timestamp";
|
||||
|
||||
/// Convert OpenTelemetry metrics to GreptimeDB insert requests
|
||||
/// Convert OpenTelemetry logs to GreptimeDB insert requests
|
||||
///
|
||||
/// See
|
||||
/// <https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/metrics/v1/metrics.proto>
|
||||
/// for data structure of OTLP metrics.
|
||||
/// <https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/logs/v1/logs.proto>
|
||||
/// for data structure of OTLP logs.
|
||||
///
|
||||
/// Returns `InsertRequests` and total number of rows to ingest
|
||||
pub async fn to_grpc_insert_requests(
|
||||
@@ -618,102 +618,22 @@ fn decide_column_schema_and_convert_value(
|
||||
);
|
||||
}
|
||||
|
||||
let column_info = match value {
|
||||
JsonbValue::String(s) => Ok(Some((
|
||||
GreptimeValue {
|
||||
value_data: Some(ValueData::StringValue(s.into())),
|
||||
},
|
||||
ColumnDataType::String,
|
||||
SemanticType::Tag,
|
||||
None,
|
||||
))),
|
||||
JsonbValue::Number(n) => match n {
|
||||
JsonbNumber::Int64(i) => Ok(Some((
|
||||
GreptimeValue {
|
||||
value_data: Some(ValueData::I64Value(i)),
|
||||
},
|
||||
ColumnDataType::Int64,
|
||||
SemanticType::Tag,
|
||||
None,
|
||||
))),
|
||||
JsonbNumber::Float64(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "FLOAT".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbNumber::Decimal64(_) | JsonbNumber::Decimal128(_) | JsonbNumber::Decimal256(_) => {
|
||||
UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "DECIMAL".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail()
|
||||
}
|
||||
JsonbNumber::UInt64(u) => {
|
||||
let value = jsonb_uint64_to_log_value(u, column_name)?;
|
||||
Ok(Some((
|
||||
GreptimeValue {
|
||||
value_data: Some(ValueData::I64Value(value)),
|
||||
},
|
||||
ColumnDataType::Int64,
|
||||
SemanticType::Tag,
|
||||
None,
|
||||
)))
|
||||
}
|
||||
},
|
||||
JsonbValue::Bool(b) => Ok(Some((
|
||||
GreptimeValue {
|
||||
value_data: Some(ValueData::BoolValue(b)),
|
||||
},
|
||||
ColumnDataType::Boolean,
|
||||
SemanticType::Tag,
|
||||
None,
|
||||
))),
|
||||
JsonbValue::Array(_) | JsonbValue::Object(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "Json".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::Binary(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "Binary".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::Date(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "Date".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::Timestamp(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "Timestamp".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::TimestampTz(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "TimestampTz".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::Interval(_) => UnsupportedJsonDataTypeForTagSnafu {
|
||||
ty: "Interval".to_string(),
|
||||
key: column_name,
|
||||
}
|
||||
.fail(),
|
||||
JsonbValue::Null => Ok(None),
|
||||
let Some((value_data, column_type)) = jsonb_value_to_log_value_data(column_name, value, false)?
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
column_info.map(|c| {
|
||||
c.map(|(value, column_type, semantic_type, datatype_extension)| {
|
||||
(
|
||||
ColumnSchema {
|
||||
column_name: column_name.to_string(),
|
||||
datatype: column_type as i32,
|
||||
semantic_type: semantic_type as i32,
|
||||
datatype_extension,
|
||||
options: None,
|
||||
},
|
||||
value,
|
||||
)
|
||||
})
|
||||
})
|
||||
Ok(Some((
|
||||
ColumnSchema {
|
||||
column_name: column_name.to_string(),
|
||||
datatype: column_type as i32,
|
||||
semantic_type: SemanticType::Tag as i32,
|
||||
datatype_extension: None,
|
||||
options: None,
|
||||
},
|
||||
GreptimeValue {
|
||||
value_data: Some(value_data),
|
||||
},
|
||||
)))
|
||||
}
|
||||
|
||||
fn decide_existing_column_schema_and_convert_value(
|
||||
@@ -729,7 +649,6 @@ fn decide_existing_column_schema_and_convert_value(
|
||||
let value_data = coerce_log_value_data(
|
||||
Some(value_data),
|
||||
existing_column.datatype,
|
||||
existing_column.schema.semantic_type(),
|
||||
request_type,
|
||||
existing_column.is_json_binary(),
|
||||
column_name,
|
||||
@@ -846,7 +765,6 @@ fn align_rows_with_existing_schema(
|
||||
};
|
||||
|
||||
let target_type = existing_column.datatype;
|
||||
let semantic_type = existing_column.schema.semantic_type();
|
||||
let target_is_json_binary = existing_column.is_json_binary();
|
||||
for row in rows.iter_mut() {
|
||||
let Some(value) = row.values.get_mut(column_idx) else {
|
||||
@@ -855,7 +773,6 @@ fn align_rows_with_existing_schema(
|
||||
value.value_data = coerce_log_value_data(
|
||||
value.value_data.take(),
|
||||
target_type,
|
||||
semantic_type,
|
||||
request_type,
|
||||
target_is_json_binary,
|
||||
&schema.column_name,
|
||||
@@ -871,7 +788,6 @@ fn align_rows_with_existing_schema(
|
||||
fn coerce_log_value_data(
|
||||
value_data: Option<ValueData>,
|
||||
target_type: ColumnDataType,
|
||||
_semantic_type: SemanticType,
|
||||
request_type: ColumnDataType,
|
||||
target_is_json_binary: bool,
|
||||
column_name: &str,
|
||||
@@ -895,14 +811,8 @@ fn coerce_log_value_data(
|
||||
return align_timestamp_value(value_data, target_unit, column_name, table_name).map(Some);
|
||||
}
|
||||
|
||||
// Lossless signed -> unsigned integer cast for built-in fields moving from
|
||||
// unsigned to signed types (e.g. an existing UInt64/UInt32 log column
|
||||
// receiving new Int64/Int32 ingest). Lets existing tables keep their
|
||||
// unsigned columns without an `ALTER`. In the mutually-exclusive `else` of
|
||||
// the String branch so the move stays local to non-String targets; the
|
||||
// shared predicate keeps the supported pairs identical to the trace path
|
||||
// (the generic String -> numeric coercions keep their pre-existing
|
||||
// rejection on this path).
|
||||
// Only the signed -> unsigned pairs shared with traces apply to non-String
|
||||
// targets; String -> numeric stays rejected on the log path.
|
||||
if target_type == ColumnDataType::String {
|
||||
if let Ok(value_data) =
|
||||
coerce_value_data(&Some(value_data.clone()), target_type, request_type)
|
||||
@@ -1266,7 +1176,8 @@ fn any_value_to_vrl_value(value: any_value::Value) -> VrlValue {
|
||||
match value {
|
||||
any_value::Value::StringValue(s) => VrlValue::Bytes(s.into()),
|
||||
any_value::Value::IntValue(i) => VrlValue::Integer(i),
|
||||
any_value::Value::DoubleValue(d) => VrlValue::Float(NotNan::new(d).unwrap()),
|
||||
// VRL has no NaN float; like other unrepresentable values, NaN becomes null.
|
||||
any_value::Value::DoubleValue(d) => NotNan::new(d).map_or(VrlValue::Null, VrlValue::Float),
|
||||
any_value::Value::BoolValue(b) => VrlValue::Boolean(b),
|
||||
any_value::Value::ArrayValue(array_value) => {
|
||||
let values = array_value
|
||||
@@ -1612,49 +1523,25 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_existing_uint32_trace_flags_preserves_nonzero_flags_value() {
|
||||
// A non-zero trace_flags bit pattern (e.g. the W3C sampled flag as sent
|
||||
// by common encoders) round-trips exactly through the Int32 -> UInt32
|
||||
// coercion into an existing unsigned column. Guards that the cast is
|
||||
// bit-preserving for realistic values, not just the default 0.
|
||||
fn test_existing_uint32_trace_flags_preserves_flag_bits() {
|
||||
// `trace_flags` is a bit field: the Int32 -> UInt32 coercion must keep
|
||||
// the exact bit pattern, including the sign bit.
|
||||
let existing = existing_uint32_trace_flags_schema();
|
||||
for flags in [256, 0x8000_0000] {
|
||||
let rows = parse_with_select(
|
||||
request_with_log_attrs_and_flags(vec![], flags),
|
||||
"",
|
||||
Some(&existing),
|
||||
)
|
||||
.unwrap();
|
||||
let idx = column_index(&rows, "trace_flags");
|
||||
|
||||
let rows = parse_with_select(
|
||||
request_with_log_attrs_and_flags(vec![], 256),
|
||||
"",
|
||||
Some(&existing),
|
||||
)
|
||||
.unwrap();
|
||||
let idx = column_index(&rows, "trace_flags");
|
||||
|
||||
assert_eq!(rows.schema[idx].datatype, ColumnDataType::Uint32 as i32);
|
||||
assert_eq!(
|
||||
rows.rows[0].values[idx].value_data,
|
||||
Some(ValueData::U32Value(256))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_existing_uint32_trace_flags_preserves_high_bit_flags_value() {
|
||||
// The full u32 range round-trips: a value with the sign bit set proves
|
||||
// the Int32 -> UInt32 coercion is bit-exact (`as i32` then `as u32`
|
||||
// preserve the pattern), so no flags value is corrupted on the
|
||||
// unsigned -> signed transition.
|
||||
let existing = existing_uint32_trace_flags_schema();
|
||||
|
||||
let rows = parse_with_select(
|
||||
request_with_log_attrs_and_flags(vec![], 0x8000_0000),
|
||||
"",
|
||||
Some(&existing),
|
||||
)
|
||||
.unwrap();
|
||||
let idx = column_index(&rows, "trace_flags");
|
||||
|
||||
assert_eq!(rows.schema[idx].datatype, ColumnDataType::Uint32 as i32);
|
||||
assert_eq!(
|
||||
rows.rows[0].values[idx].value_data,
|
||||
Some(ValueData::U32Value(0x8000_0000))
|
||||
);
|
||||
assert_eq!(rows.schema[idx].datatype, ColumnDataType::Uint32 as i32);
|
||||
assert_eq!(
|
||||
rows.rows[0].values[idx].value_data,
|
||||
Some(ValueData::U32Value(flags))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1905,20 +1792,27 @@ mod tests {
|
||||
"Interval",
|
||||
),
|
||||
] {
|
||||
let schema_error =
|
||||
decide_column_schema_and_convert_value("attr", value.clone(), None, "logs")
|
||||
.expect_err("extended JSONB type must be rejected");
|
||||
let value_error = jsonb_value_to_log_value_data("attr", value, true)
|
||||
let error = decide_column_schema_and_convert_value("attr", value, None, "logs")
|
||||
.expect_err("extended JSONB type must be rejected");
|
||||
for error in [schema_error, value_error] {
|
||||
assert!(matches!(error,
|
||||
Error::UnsupportedJsonDataTypeForTag { key, ty, .. }
|
||||
if key == "attr" && ty == expected_type
|
||||
));
|
||||
}
|
||||
assert!(matches!(error,
|
||||
Error::UnsupportedJsonDataTypeForTag { key, ty, .. }
|
||||
if key == "attr" && ty == expected_type
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nan_double_becomes_null_vrl_value() {
|
||||
assert_eq!(
|
||||
any_value_to_vrl_value(OtlpValue::DoubleValue(f64::NAN)),
|
||||
VrlValue::Null
|
||||
);
|
||||
assert_eq!(
|
||||
any_value_to_vrl_value(OtlpValue::DoubleValue(1.5)),
|
||||
VrlValue::Float(NotNan::new(1.5).unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_jsonb_uint64_exceeding_i64_range_rejected() {
|
||||
// A JSON number that only fits in u64 must be rejected, not wrapped:
|
||||
|
||||
@@ -101,16 +101,14 @@ pub fn write_span_to_row(writer: &mut TableData, span: TraceSpan) -> Result<()>
|
||||
span.end_in_nanosecond as i64,
|
||||
)),
|
||||
),
|
||||
// The v0 data model is frozen: `duration_nano` stays UInt64. The
|
||||
// signed→unsigned ingest compatibility layer only runs on the v1 path
|
||||
// (see `trace_ingest.rs`), so flipping v0 here would break writes into
|
||||
// every pre-existing v0 table at mito's schema check. New v1 tables
|
||||
// are signed; v0 tables keep the legacy unsigned column indefinitely.
|
||||
// v0 stays UInt64: the signed->unsigned coercion only runs on the v1 path, so
|
||||
// a signed column would break writes into existing v0 tables.
|
||||
make_column_data(
|
||||
DURATION_NANO_COLUMN,
|
||||
ColumnDataType::Uint64,
|
||||
Some(ValueData::U64Value(
|
||||
span.end_in_nanosecond - span.start_in_nanosecond,
|
||||
span.end_in_nanosecond
|
||||
.saturating_sub(span.start_in_nanosecond),
|
||||
)),
|
||||
),
|
||||
make_string_column_data(TRACE_ID_COLUMN, Some(span.trace_id)),
|
||||
@@ -171,7 +169,6 @@ pub fn write_span_to_row(writer: &mut TableData, span: TraceSpan) -> Result<()>
|
||||
fn write_trace_services_to_row(writer: &mut TableData, services: HashSet<String>) -> Result<()> {
|
||||
for service_name in services {
|
||||
let mut row = writer.alloc_one_row();
|
||||
// Write the timestamp as 0.
|
||||
row_writer::write_ts_to_nanos(
|
||||
writer,
|
||||
TIMESTAMP_COLUMN,
|
||||
@@ -201,7 +198,6 @@ fn write_trace_operations_to_row(
|
||||
) -> Result<()> {
|
||||
for (service_name, span_name, span_kind) in operations {
|
||||
let mut row = writer.alloc_one_row();
|
||||
// Write the timestamp as 0.
|
||||
row_writer::write_ts_to_nanos(
|
||||
writer,
|
||||
TIMESTAMP_COLUMN,
|
||||
@@ -261,24 +257,13 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_trace_table_data_from_span_subset() {
|
||||
let spans = [
|
||||
make_span("svc-a", "trace-a", "span-a"),
|
||||
make_span("svc-b", "trace-b", "span-b"),
|
||||
];
|
||||
|
||||
let writer = build_trace_table_data(&spans[..1]).unwrap();
|
||||
let (_, rows) = writer.into_schema_and_rows();
|
||||
assert_eq!(rows.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_v0_duration_nano_stays_uint64() {
|
||||
// The v0 data model is frozen on the unsigned `duration_nano`. This
|
||||
// pins the schema so the unsigned→signed transition of the built-in
|
||||
// models (which flips only v1) cannot silently leak into v0 and break
|
||||
// writes into pre-existing v0 tables.
|
||||
let writer = build_trace_table_data(&[make_span("svc-a", "trace-a", "span-a")]).unwrap();
|
||||
fn test_v0_duration_nano_is_clamped_uint64() {
|
||||
// v0 is frozen on the unsigned `duration_nano`; a span ending before it
|
||||
// starts clamps to 0 instead of underflowing.
|
||||
let mut reversed = make_span("svc-a", "trace-b", "span-b");
|
||||
reversed.start_in_nanosecond = 5;
|
||||
let writer =
|
||||
build_trace_table_data(&[make_span("svc-a", "trace-a", "span-a"), reversed]).unwrap();
|
||||
let (schema, rows) = writer.into_schema_and_rows();
|
||||
|
||||
let idx = schema
|
||||
@@ -287,6 +272,7 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(schema[idx].datatype, ColumnDataType::Uint64 as i32);
|
||||
assert_eq!(rows[0].values[idx].value_data, Some(ValueData::U64Value(1)));
|
||||
assert_eq!(rows[1].values[idx].value_data, Some(ValueData::U64Value(0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -43,24 +43,25 @@ const APPROXIMATE_COLUMN_COUNT: usize = 30;
|
||||
// Use a timestamp(2100-01-01 00:00:00) as large as possible.
|
||||
const MAX_TIMESTAMP: i64 = 4102444800000000000;
|
||||
|
||||
struct FixedTraceColumnIndexes {
|
||||
timestamp: usize,
|
||||
timestamp_end: usize,
|
||||
duration_nano: usize,
|
||||
parent_span_id: usize,
|
||||
trace_id: usize,
|
||||
span_id: usize,
|
||||
span_kind: usize,
|
||||
span_name: usize,
|
||||
span_status_code: usize,
|
||||
span_status_message: usize,
|
||||
trace_state: usize,
|
||||
scope_name: usize,
|
||||
scope_version: usize,
|
||||
/// Column indexes of the span columns shared by the v1 and v2 data models.
|
||||
pub(super) struct FixedTraceColumnIndexes {
|
||||
pub(super) timestamp: usize,
|
||||
pub(super) timestamp_end: usize,
|
||||
pub(super) duration_nano: usize,
|
||||
pub(super) parent_span_id: usize,
|
||||
pub(super) trace_id: usize,
|
||||
pub(super) span_id: usize,
|
||||
pub(super) span_kind: usize,
|
||||
pub(super) span_name: usize,
|
||||
pub(super) span_status_code: usize,
|
||||
pub(super) span_status_message: usize,
|
||||
pub(super) trace_state: usize,
|
||||
pub(super) scope_name: usize,
|
||||
pub(super) scope_version: usize,
|
||||
}
|
||||
|
||||
impl FixedTraceColumnIndexes {
|
||||
fn resolve(writer: &mut TableData) -> Result<Self> {
|
||||
pub(super) fn resolve(writer: &mut TableData) -> Result<Self> {
|
||||
let timestamp = writer.ensure_column(time_index_column_schema(
|
||||
TIMESTAMP_COLUMN,
|
||||
ColumnDataType::TimestampNanosecond,
|
||||
@@ -330,12 +331,6 @@ pub fn build_aux_table_requests(
|
||||
Ok(multi_table_writer.into_row_insert_requests())
|
||||
}
|
||||
|
||||
pub fn write_span_to_row(writer: &mut TableData, span: TraceSpan) -> Result<()> {
|
||||
let row_index = writer.num_rows();
|
||||
let fixed_columns = FixedTraceColumnIndexes::resolve(writer)?;
|
||||
write_span_to_row_inner(writer, span, row_index, &fixed_columns, None)
|
||||
}
|
||||
|
||||
/// Computes the span duration as the signed `duration_nano` value written by
|
||||
/// the v1 data model.
|
||||
///
|
||||
@@ -475,7 +470,6 @@ fn write_span_to_row_inner(
|
||||
fn write_trace_services_to_row(writer: &mut TableData, services: HashSet<String>) -> Result<()> {
|
||||
for service_name in services {
|
||||
let mut row = writer.alloc_one_row();
|
||||
// Write the timestamp as 0.
|
||||
row_writer::write_ts_to_nanos(
|
||||
writer,
|
||||
TIMESTAMP_COLUMN,
|
||||
@@ -502,7 +496,6 @@ fn write_trace_operations_to_row(
|
||||
) -> Result<()> {
|
||||
for (service_name, span_name, span_kind) in operations {
|
||||
let mut row = writer.alloc_one_row();
|
||||
// Write the timestamp as 0.
|
||||
row_writer::write_ts_to_nanos(
|
||||
writer,
|
||||
TIMESTAMP_COLUMN,
|
||||
|
||||
@@ -18,13 +18,10 @@ use snafu::ensure;
|
||||
|
||||
use crate::error::{Result, TimestampOverflowSnafu};
|
||||
use crate::otlp::trace::span::TraceSpan;
|
||||
use crate::otlp::trace::v1::span_duration_nano;
|
||||
use crate::otlp::trace::v1::{FixedTraceColumnIndexes, span_duration_nano};
|
||||
use crate::otlp::trace::{
|
||||
DURATION_NANO_COLUMN, PARENT_SPAN_ID_COLUMN, RESOURCE_ATTRIBUTES_COLUMN,
|
||||
SCOPE_ATTRIBUTES_COLUMN, SCOPE_NAME_COLUMN, SCOPE_VERSION_COLUMN, SERVICE_NAME_COLUMN,
|
||||
SPAN_ATTRIBUTES_COLUMN, SPAN_EVENTS_COLUMN, SPAN_ID_COLUMN, SPAN_KIND_COLUMN,
|
||||
SPAN_LINKS_COLUMN, SPAN_NAME_COLUMN, SPAN_STATUS_CODE, SPAN_STATUS_MESSAGE_COLUMN,
|
||||
TIMESTAMP_COLUMN, TIMESTAMP_END_COLUMN, TRACE_ID_COLUMN, TRACE_STATE_COLUMN,
|
||||
RESOURCE_ATTRIBUTES_COLUMN, SCOPE_ATTRIBUTES_COLUMN, SERVICE_NAME_COLUMN,
|
||||
SPAN_ATTRIBUTES_COLUMN, SPAN_EVENTS_COLUMN, SPAN_LINKS_COLUMN,
|
||||
};
|
||||
use crate::row_writer::{self, MultiTableData, TableData};
|
||||
|
||||
@@ -33,20 +30,8 @@ use crate::row_writer::{self, MultiTableData, TableData};
|
||||
// columns, 1 service name, 3 JSON2 attribute columns, and 2 JSON event/link columns.
|
||||
const APPROXIMATE_COLUMN_COUNT: usize = 19;
|
||||
|
||||
struct FixedTraceColumnIndexes {
|
||||
timestamp: usize,
|
||||
timestamp_end: usize,
|
||||
duration_nano: usize,
|
||||
parent_span_id: usize,
|
||||
trace_id: usize,
|
||||
span_id: usize,
|
||||
span_kind: usize,
|
||||
span_name: usize,
|
||||
span_status_code: usize,
|
||||
span_status_message: usize,
|
||||
trace_state: usize,
|
||||
scope_name: usize,
|
||||
scope_version: usize,
|
||||
struct V2TraceColumnIndexes {
|
||||
span: FixedTraceColumnIndexes,
|
||||
service_name: usize,
|
||||
span_attributes: usize,
|
||||
scope_attributes: usize,
|
||||
@@ -55,34 +40,11 @@ struct FixedTraceColumnIndexes {
|
||||
span_links: usize,
|
||||
}
|
||||
|
||||
impl FixedTraceColumnIndexes {
|
||||
impl V2TraceColumnIndexes {
|
||||
fn resolve(writer: &mut TableData) -> Result<Self> {
|
||||
let timestamp = writer.ensure_column(api::v1::helper::time_index_column_schema(
|
||||
TIMESTAMP_COLUMN,
|
||||
ColumnDataType::TimestampNanosecond,
|
||||
))?;
|
||||
let mut field = |name: &str, datatype: ColumnDataType| {
|
||||
writer.ensure_column(ColumnSchema {
|
||||
column_name: name.to_string(),
|
||||
datatype: datatype as i32,
|
||||
semantic_type: SemanticType::Field as i32,
|
||||
..Default::default()
|
||||
})
|
||||
};
|
||||
let span = FixedTraceColumnIndexes::resolve(writer)?;
|
||||
Ok(Self {
|
||||
timestamp,
|
||||
timestamp_end: field(TIMESTAMP_END_COLUMN, ColumnDataType::TimestampNanosecond)?,
|
||||
duration_nano: field(DURATION_NANO_COLUMN, ColumnDataType::Int64)?,
|
||||
parent_span_id: field(PARENT_SPAN_ID_COLUMN, ColumnDataType::String)?,
|
||||
trace_id: field(TRACE_ID_COLUMN, ColumnDataType::String)?,
|
||||
span_id: field(SPAN_ID_COLUMN, ColumnDataType::String)?,
|
||||
span_kind: field(SPAN_KIND_COLUMN, ColumnDataType::String)?,
|
||||
span_name: field(SPAN_NAME_COLUMN, ColumnDataType::String)?,
|
||||
span_status_code: field(SPAN_STATUS_CODE, ColumnDataType::String)?,
|
||||
span_status_message: field(SPAN_STATUS_MESSAGE_COLUMN, ColumnDataType::String)?,
|
||||
trace_state: field(TRACE_STATE_COLUMN, ColumnDataType::String)?,
|
||||
scope_name: field(SCOPE_NAME_COLUMN, ColumnDataType::String)?,
|
||||
scope_version: field(SCOPE_VERSION_COLUMN, ColumnDataType::String)?,
|
||||
span,
|
||||
service_name: writer.ensure_column(ColumnSchema {
|
||||
column_name: SERVICE_NAME_COLUMN.to_string(),
|
||||
datatype: ColumnDataType::String as i32,
|
||||
@@ -122,7 +84,7 @@ fn build_trace_table_data(spans: &[TraceSpan]) -> Result<TableData> {
|
||||
if spans.is_empty() {
|
||||
return Ok(writer);
|
||||
}
|
||||
let columns = FixedTraceColumnIndexes::resolve(&mut writer)?;
|
||||
let columns = V2TraceColumnIndexes::resolve(&mut writer)?;
|
||||
for span in spans {
|
||||
write_span_to_row(&mut writer, span, &columns)?;
|
||||
}
|
||||
@@ -132,7 +94,7 @@ fn build_trace_table_data(spans: &[TraceSpan]) -> Result<TableData> {
|
||||
fn write_span_to_row(
|
||||
writer: &mut TableData,
|
||||
span: &TraceSpan,
|
||||
columns: &FixedTraceColumnIndexes,
|
||||
columns: &V2TraceColumnIndexes,
|
||||
) -> Result<()> {
|
||||
ensure!(
|
||||
span.start_in_nanosecond <= i64::MAX as u64,
|
||||
@@ -151,56 +113,59 @@ fn write_span_to_row(
|
||||
let duration = span_duration_nano(span);
|
||||
for (index, value) in [
|
||||
(
|
||||
columns.timestamp,
|
||||
columns.span.timestamp,
|
||||
Some(ValueData::TimestampNanosecondValue(
|
||||
span.start_in_nanosecond as i64,
|
||||
)),
|
||||
),
|
||||
(
|
||||
columns.timestamp_end,
|
||||
columns.span.timestamp_end,
|
||||
Some(ValueData::TimestampNanosecondValue(
|
||||
span.end_in_nanosecond as i64,
|
||||
)),
|
||||
),
|
||||
(columns.duration_nano, Some(ValueData::I64Value(duration))),
|
||||
(
|
||||
columns.parent_span_id,
|
||||
columns.span.duration_nano,
|
||||
Some(ValueData::I64Value(duration)),
|
||||
),
|
||||
(
|
||||
columns.span.parent_span_id,
|
||||
span.parent_span_id.clone().map(ValueData::StringValue),
|
||||
),
|
||||
(
|
||||
columns.trace_id,
|
||||
columns.span.trace_id,
|
||||
Some(ValueData::StringValue(span.trace_id.clone())),
|
||||
),
|
||||
(
|
||||
columns.span_id,
|
||||
columns.span.span_id,
|
||||
Some(ValueData::StringValue(span.span_id.clone())),
|
||||
),
|
||||
(
|
||||
columns.span_kind,
|
||||
columns.span.span_kind,
|
||||
Some(ValueData::StringValue(span.span_kind.clone())),
|
||||
),
|
||||
(
|
||||
columns.span_name,
|
||||
columns.span.span_name,
|
||||
Some(ValueData::StringValue(span.span_name.clone())),
|
||||
),
|
||||
(
|
||||
columns.span_status_code,
|
||||
columns.span.span_status_code,
|
||||
Some(ValueData::StringValue(span.span_status_code.clone())),
|
||||
),
|
||||
(
|
||||
columns.span_status_message,
|
||||
columns.span.span_status_message,
|
||||
Some(ValueData::StringValue(span.span_status_message.clone())),
|
||||
),
|
||||
(
|
||||
columns.trace_state,
|
||||
columns.span.trace_state,
|
||||
Some(ValueData::StringValue(span.trace_state.clone())),
|
||||
),
|
||||
(
|
||||
columns.scope_name,
|
||||
columns.span.scope_name,
|
||||
Some(ValueData::StringValue(span.scope_name.clone())),
|
||||
),
|
||||
(
|
||||
columns.scope_version,
|
||||
columns.span.scope_version,
|
||||
Some(ValueData::StringValue(span.scope_version.clone())),
|
||||
),
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user