mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-12-04 00:15:29 +00:00
fix(servers): spell Prometheus infinities as +Inf and keep Jaeger tags per trace (#10823)
* fix(servers): spell infinite Prometheus samples as +Inf and keep Jaeger process tags per trace Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * perf(servers): serialize non-finite Prometheus samples from static strings Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
@@ -772,8 +772,9 @@ fn traces_from_records(records: HttpRecordsOutput) -> Result<Vec<Trace>> {
|
||||
// maintain the mapping: trace_id -> spans.
|
||||
// use BTreeMap to retain order
|
||||
let mut trace_id_to_spans: BTreeMap<String, Vec<Span>> = BTreeMap::new();
|
||||
// maintain the mapping: service.name -> resource.attributes.
|
||||
let mut service_to_resource_attributes: HashMap<String, Vec<KeyValue>> = HashMap::new();
|
||||
// maintain the mapping: (trace_id, service.name) -> resource.attributes.
|
||||
let mut service_to_resource_attributes: HashMap<(String, String), Vec<KeyValue>> =
|
||||
HashMap::new();
|
||||
|
||||
let is_span_attributes_flatten = !records
|
||||
.schema
|
||||
@@ -1024,9 +1025,9 @@ fn traces_from_records(records: HttpRecordsOutput) -> Result<Vec<Trace>> {
|
||||
}
|
||||
|
||||
if let Some(service_name) = service_name {
|
||||
if !service_to_resource_attributes.contains_key(&service_name) {
|
||||
service_to_resource_attributes.insert(service_name.clone(), resource_tags);
|
||||
}
|
||||
service_to_resource_attributes
|
||||
.entry((span.trace_id.clone(), service_name.clone()))
|
||||
.or_insert(resource_tags);
|
||||
|
||||
if let Some(process) = trace_id_to_processes.get_mut(&span.trace_id) {
|
||||
if let Some(process_id) = process.get(&service_name) {
|
||||
@@ -1062,7 +1063,7 @@ fn traces_from_records(records: HttpRecordsOutput) -> Result<Vec<Trace>> {
|
||||
let mut process_id_to_process = HashMap::new();
|
||||
for (service_name, process_id) in processes.into_iter() {
|
||||
let tags = service_to_resource_attributes
|
||||
.remove(&service_name)
|
||||
.remove(&(trace.trace_id.clone(), service_name.clone()))
|
||||
.unwrap_or_default();
|
||||
process_id_to_process.insert(process_id, Process { service_name, tags });
|
||||
}
|
||||
@@ -1642,6 +1643,46 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_each_trace_gets_its_own_process_tags() {
|
||||
let column = |name: &str| ColumnSchema {
|
||||
name: name.to_string(),
|
||||
data_type: "String".to_string(),
|
||||
};
|
||||
let row = |trace_id: &str, host: &str| {
|
||||
vec![
|
||||
JsonValue::String(trace_id.to_string()),
|
||||
JsonValue::String("test-service".to_string()),
|
||||
JsonValue::String(host.to_string()),
|
||||
]
|
||||
};
|
||||
let records = HttpRecordsOutput {
|
||||
schema: OutputSchema {
|
||||
column_schemas: vec![
|
||||
column("trace_id"),
|
||||
column("service_name"),
|
||||
column("resource_attributes.host.name"),
|
||||
],
|
||||
},
|
||||
rows: vec![row("trace-1", "host-1"), row("trace-2", "host-2")],
|
||||
total_rows: 2,
|
||||
metrics: HashMap::new(),
|
||||
};
|
||||
|
||||
let traces = traces_from_records(records).unwrap();
|
||||
assert_eq!(traces.len(), 2);
|
||||
for (trace, host) in traces.iter().zip(["host-1", "host-2"]) {
|
||||
assert_eq!(
|
||||
trace.processes["p1"].tags,
|
||||
vec![KeyValue {
|
||||
key: "host.name".to_string(),
|
||||
value_type: ValueType::String,
|
||||
value: Value::String(host.to_string()),
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_jaeger_query_params() {
|
||||
// The tests is the tuple of `(test_query_params, expected)`.
|
||||
|
||||
@@ -86,7 +86,9 @@ impl Serialize for PromSampleValue {
|
||||
Self::Number(value) if value.is_finite() => {
|
||||
serializer.serialize_str(Buffer::new().format_finite(*value))
|
||||
}
|
||||
Self::Number(value) => serializer.collect_str(value),
|
||||
Self::Number(value) if value.is_nan() => serializer.serialize_str("NaN"),
|
||||
Self::Number(value) if value.is_sign_positive() => serializer.serialize_str("+Inf"),
|
||||
Self::Number(_) => serializer.serialize_str("-Inf"),
|
||||
Self::Text(value) => serializer.serialize_str(value),
|
||||
}
|
||||
}
|
||||
@@ -117,12 +119,12 @@ fn prometheus_native_histogram(histogram: &NativeHistogram) -> Result<PromNative
|
||||
///
|
||||
/// Finite sample strings use ryu's shortest-roundtrip representation and may
|
||||
/// differ textually from previous Rust/Prometheus wire formatting; values parse
|
||||
/// identically. Non-finite values retain Rust's `f64::to_string()` output.
|
||||
/// identically. Non-finite values use Prometheus' `+Inf`, `-Inf` and `NaN`.
|
||||
fn format_prometheus_sample_value(value: f64) -> String {
|
||||
if value.is_finite() {
|
||||
Buffer::new().format_finite(value).to_string()
|
||||
} else {
|
||||
value.to_string()
|
||||
format_prometheus_float(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1235,10 +1237,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_prometheus_sample_value_preserves_nonfinite_values() {
|
||||
fn format_prometheus_sample_value_spells_nonfinite_values_like_prometheus() {
|
||||
assert_eq!(format_prometheus_sample_value(f64::NAN), "NaN");
|
||||
assert_eq!(format_prometheus_sample_value(f64::INFINITY), "inf");
|
||||
assert_eq!(format_prometheus_sample_value(f64::NEG_INFINITY), "-inf");
|
||||
assert_eq!(format_prometheus_sample_value(f64::INFINITY), "+Inf");
|
||||
assert_eq!(format_prometheus_sample_value(f64::NEG_INFINITY), "-Inf");
|
||||
|
||||
// Parsing a NaN does not preserve its payload bits, so NaN is checked
|
||||
// by its required semantic spelling rather than by to_bits().
|
||||
@@ -1335,7 +1337,7 @@ mod tests {
|
||||
"status": "success",
|
||||
"data": {"resultType": "matrix", "result": [{
|
||||
"metric": {"host": "a"},
|
||||
"values": [[1.0, "-0.0"], [2.0, "NaN"], [3.0, "1e-7"], [4.0, "inf"], [5.0, "-inf"]]
|
||||
"values": [[1.0, "-0.0"], [2.0, "NaN"], [3.0, "1e-7"], [4.0, "+Inf"], [5.0, "-Inf"]]
|
||||
}]}
|
||||
}))
|
||||
.unwrap();
|
||||
@@ -1453,8 +1455,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_batches_to_data_preserves_infinity_output() {
|
||||
// NaN and infinities use Rust's `f64::to_string()` output.
|
||||
fn record_batches_to_data_formats_nonfinite_values() {
|
||||
let schema = Arc::new(Schema::new(vec![
|
||||
ColumnSchema::new(
|
||||
"timestamp",
|
||||
@@ -1493,7 +1494,7 @@ mod tests {
|
||||
assert_eq!(series.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::to_value(&series[0].values).unwrap(),
|
||||
serde_json::json!([[1.0, "inf"], [2.0, "-inf"], [3.0, "NaN"]])
|
||||
serde_json::json!([[1.0, "+Inf"], [2.0, "-Inf"], [3.0, "NaN"]])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2563,7 +2563,7 @@ pub async fn test_prometheus_native_promql_route(store_type: StorageType) {
|
||||
),
|
||||
(
|
||||
"100 / native_zero",
|
||||
json!({"resultType":"vector","result":[{"metric":{"host":"z"},"value":[1.0,"inf"]}]}),
|
||||
json!({"resultType":"vector","result":[{"metric":{"host":"z"},"value":[1.0,"+Inf"]}]}),
|
||||
),
|
||||
(
|
||||
"native_zero / 0",
|
||||
|
||||
Reference in New Issue
Block a user