use crate::tracer::CompactSpanRecord;
use serde::{Deserialize, Serialize};
use torvyn_types::TraceId;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OtlpSpan {
pub trace_id: String,
pub span_id: String,
pub parent_span_id: String,
pub name: String,
pub start_time_unix_nano: u64,
pub end_time_unix_nano: u64,
pub status: OtlpStatus,
pub attributes: Vec<OtlpAttribute>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OtlpStatus {
pub code: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OtlpAttribute {
pub key: String,
pub value: OtlpValue,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OtlpValue {
#[serde(skip_serializing_if = "Option::is_none")]
pub string_value: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub int_value: Option<i64>,
}
pub fn spans_to_otlp(trace_id: TraceId, spans: &[CompactSpanRecord]) -> Vec<OtlpSpan> {
let trace_id_hex = format!("{trace_id}");
spans
.iter()
.map(|s| {
let status_code = match s.status_code {
0 => 1, _ => 2, };
OtlpSpan {
trace_id: trace_id_hex.clone(),
span_id: format!("{}", s.span_id),
parent_span_id: format!("{}", s.parent_span_id),
name: format!("component:{}", s.component_id),
start_time_unix_nano: s.start_ns,
end_time_unix_nano: s.end_ns,
status: OtlpStatus {
code: status_code,
message: None,
},
attributes: vec![
OtlpAttribute {
key: "torvyn.component.id".into(),
value: OtlpValue {
int_value: Some(s.component_id.as_u64() as i64),
string_value: None,
},
},
OtlpAttribute {
key: "torvyn.element.sequence".into(),
value: OtlpValue {
int_value: Some(s.element_sequence as i64),
string_value: None,
},
},
],
}
})
.collect()
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OtlpExportRequest {
pub resource_spans: Vec<OtlpResourceSpans>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OtlpResourceSpans {
pub scope_spans: Vec<OtlpScopeSpans>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OtlpScopeSpans {
pub spans: Vec<OtlpSpan>,
}
pub fn build_export_request(spans: Vec<OtlpSpan>) -> OtlpExportRequest {
OtlpExportRequest {
resource_spans: vec![OtlpResourceSpans {
scope_spans: vec![OtlpScopeSpans { spans }],
}],
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tracer::CompactSpanRecord;
use torvyn_types::{ComponentId, SpanId};
#[test]
fn test_spans_to_otlp() {
let trace_id = TraceId::new([0xab; 16]);
let spans = vec![CompactSpanRecord {
span_id: SpanId::new([1; 8]),
parent_span_id: SpanId::new([0; 8]),
component_id: ComponentId::new(42),
start_ns: 1000,
end_ns: 2000,
status_code: 0,
element_sequence: 1,
}];
let otlp = spans_to_otlp(trace_id, &spans);
assert_eq!(otlp.len(), 1);
assert_eq!(otlp[0].start_time_unix_nano, 1000);
assert_eq!(otlp[0].end_time_unix_nano, 2000);
assert_eq!(otlp[0].status.code, 1); }
#[test]
fn test_spans_to_otlp_error_status() {
let trace_id = TraceId::new([0xab; 16]);
let spans = vec![CompactSpanRecord {
span_id: SpanId::new([1; 8]),
parent_span_id: SpanId::new([0; 8]),
component_id: ComponentId::new(1),
start_ns: 0,
end_ns: 100,
status_code: 1, element_sequence: 0,
}];
let otlp = spans_to_otlp(trace_id, &spans);
assert_eq!(otlp[0].status.code, 2); }
#[test]
fn test_build_export_request() {
let otlp_spans = vec![OtlpSpan {
trace_id: "abc".into(),
span_id: "def".into(),
parent_span_id: "000".into(),
name: "test".into(),
start_time_unix_nano: 0,
end_time_unix_nano: 100,
status: OtlpStatus {
code: 1,
message: None,
},
attributes: vec![],
}];
let req = build_export_request(otlp_spans);
assert_eq!(req.resource_spans.len(), 1);
assert_eq!(req.resource_spans[0].scope_spans[0].spans.len(), 1);
let json = serde_json::to_string(&req).unwrap();
assert!(json.contains("test"));
}
}