grow-apache-skywalking 0.2.0

Rust SDK for Apache SkyWalking by Grow Codes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Service code (Apache SkyWalking): the native query model and its SPEC §5 conversion.
//! The native SkyWalking query model (GraphQL: spans, traces, topology) and its conversion into
//! the SPEC §5 trace model. Native trace ids are opaque strings (`<hex>.<n>.<ms>`) and are kept
//! verbatim; native span ids become `"<segmentId>-<spanId>"`.

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::tracing::{
    sort_spans, AttrValue, Attributes, DependencyLink, Span, SpanEvent, SpanKind, SpanLink,
    SpanStatus, StatusCode, Trace,
};

/// A `{key, value}` pair (span tags, log tags, log data).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyValue {
    /// Key.
    pub key: String,
    /// Value (non-string values are stringified, `null` → `""`).
    pub value: String,
}

impl KeyValue {
    /// A pair.
    pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
        KeyValue {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// Cross-segment reference of a native span.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeSpanRef {
    /// Trace id.
    pub trace_id: String,
    /// Segment id of the parent.
    pub parent_segment_id: String,
    /// Span id of the parent within its segment.
    pub parent_span_id: i64,
    /// `CROSS_PROCESS` or `CROSS_THREAD`.
    #[serde(rename = "type")]
    pub ref_type: String,
}

/// A log entry recorded on a native span.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct NativeSpanLog {
    /// Epoch milliseconds.
    pub time: i64,
    /// Log fields.
    pub data: Vec<KeyValue>,
}

/// A span as stored by SkyWalking (agents, `/v3/segments`); identity = `(segment_id, span_id)`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeSpan {
    /// Opaque native trace id, or a hex id from an OTel context.
    pub trace_id: String,
    /// Segment id.
    pub segment_id: String,
    /// Span id within the segment.
    pub span_id: i64,
    /// Parent within the segment; `-1` = segment root (parent in `refs`).
    pub parent_span_id: i64,
    /// Cross-segment references.
    pub refs: Vec<NativeSpanRef>,
    /// Service name.
    pub service_code: String,
    /// Instance name.
    pub service_instance_name: String,
    /// Epoch milliseconds.
    pub start_time: i64,
    /// Epoch milliseconds.
    pub end_time: i64,
    /// Endpoint (operation) name.
    pub endpoint_name: String,
    /// `Entry`, `Exit` or `Local`.
    #[serde(rename = "type")]
    pub span_type: String,
    /// Remote address.
    pub peer: String,
    /// Component name.
    pub component: String,
    /// Error flag.
    pub is_error: bool,
    /// Layer (`Http`, `Database`, …).
    pub layer: String,
    /// Tags.
    pub tags: Vec<KeyValue>,
    /// Span logs.
    pub logs: Vec<NativeSpanLog>,
}

/// A native trace: every segment's spans.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeTrace {
    /// Trace id (verbatim).
    pub trace_id: String,
    /// Spans of every segment.
    pub spans: Vec<NativeSpan>,
}

/// A node of the service topology.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TopologyNode {
    /// Service id.
    pub id: String,
    /// Service name.
    pub name: String,
    /// Node type (component), may be empty.
    #[serde(rename = "type")]
    pub node_type: String,
    /// `false` for virtual nodes (e.g. `User`).
    pub is_real: bool,
    /// Layers.
    pub layers: Vec<String>,
}

/// An edge of the service topology.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TopologyCall {
    /// Edge id.
    pub id: String,
    /// Source node id.
    pub source: String,
    /// Target node id.
    pub target: String,
    /// `CLIENT` / `SERVER` / `PROXY`.
    pub detect_points: Vec<String>,
    /// Components seen at the source.
    pub source_components: Vec<String>,
    /// Components seen at the target.
    pub target_components: Vec<String>,
}

/// `getGlobalTopology` / `getServiceTopology` result.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Topology {
    /// Nodes.
    pub nodes: Vec<TopologyNode>,
    /// Edges.
    pub calls: Vec<TopologyCall>,
}

/// GraphQL selection used for native spans.
pub const NATIVE_SPAN_FIELDS: &str = "traceId segmentId spanId parentSpanId refs{traceId parentSegmentId parentSpanId type} serviceCode serviceInstanceName startTime endTime endpointName type peer component isError layer tags{key value} logs{time data{key value}}";

pub(crate) fn str_of(v: &Value, key: &str) -> String {
    v.get(key)
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_owned()
}

pub(crate) fn i64_of(v: &Value, key: &str, fallback: i64) -> i64 {
    v.get(key)
        .and_then(|x| x.as_i64().or_else(|| x.as_f64().map(|f| f as i64)))
        .unwrap_or(fallback)
}

pub(crate) fn objects(v: Option<&Value>) -> impl Iterator<Item = &Value> {
    v.and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter(|x| x.is_object())
}

pub(crate) fn strings(v: Option<&Value>) -> Vec<String> {
    v.and_then(Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(Value::as_str)
        .map(str::to_owned)
        .collect()
}

/// Decodes a `[{key, value}]` list (non-string values are stringified, `null` → `""`).
pub fn key_values(value: Option<&Value>) -> Vec<KeyValue> {
    objects(value)
        .map(|kv| KeyValue {
            key: str_of(kv, "key"),
            value: match kv.get("value") {
                Some(Value::String(s)) => s.clone(),
                None | Some(Value::Null) => String::new(),
                Some(other) => other.to_string(),
            },
        })
        .collect()
}

/// Decodes one native span from a GraphQL response (missing fields get neutral defaults).
pub fn decode_native_span(raw: &Value) -> NativeSpan {
    NativeSpan {
        trace_id: str_of(raw, "traceId"),
        segment_id: str_of(raw, "segmentId"),
        span_id: i64_of(raw, "spanId", 0),
        parent_span_id: i64_of(raw, "parentSpanId", -1),
        refs: objects(raw.get("refs"))
            .map(|r| NativeSpanRef {
                trace_id: str_of(r, "traceId"),
                parent_segment_id: str_of(r, "parentSegmentId"),
                parent_span_id: i64_of(r, "parentSpanId", 0),
                ref_type: str_of(r, "type"),
            })
            .collect(),
        service_code: str_of(raw, "serviceCode"),
        service_instance_name: str_of(raw, "serviceInstanceName"),
        start_time: i64_of(raw, "startTime", 0),
        end_time: i64_of(raw, "endTime", 0),
        endpoint_name: str_of(raw, "endpointName"),
        span_type: str_of(raw, "type"),
        peer: str_of(raw, "peer"),
        component: str_of(raw, "component"),
        is_error: raw.get("isError") == Some(&Value::Bool(true)),
        layer: str_of(raw, "layer"),
        tags: key_values(raw.get("tags")),
        logs: objects(raw.get("logs"))
            .map(|l| NativeSpanLog {
                time: i64_of(l, "time", 0),
                data: key_values(l.get("data")),
            })
            .collect(),
    }
}

/// Decodes a list of native spans into a [`NativeTrace`]; `None` when empty.
pub fn native_trace_from_spans(raw: Option<&Value>) -> Option<NativeTrace> {
    let spans: Vec<NativeSpan> = raw
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .map(decode_native_span)
        .collect();
    let trace_id = spans.first()?.trace_id.clone();
    Some(NativeTrace { trace_id, spans })
}

/// Decodes a topology result.
pub fn decode_topology(raw: &Value) -> Topology {
    Topology {
        nodes: objects(raw.get("nodes"))
            .map(|n| TopologyNode {
                id: str_of(n, "id"),
                name: str_of(n, "name"),
                node_type: str_of(n, "type"),
                is_real: n.get("isReal") == Some(&Value::Bool(true)),
                layers: strings(n.get("layers")),
            })
            .collect(),
        calls: objects(raw.get("calls"))
            .map(|c| TopologyCall {
                id: str_of(c, "id"),
                source: str_of(c, "source"),
                target: str_of(c, "target"),
                detect_points: strings(c.get("detectPoints")),
                source_components: strings(c.get("sourceComponents")),
                target_components: strings(c.get("targetComponents")),
            })
            .collect(),
    }
}

/// Composite SPEC span id of a native span: `<segmentId>-<spanId>`.
pub fn native_span_key(segment_id: &str, span_id: i64) -> String {
    format!("{segment_id}-{span_id}")
}

fn kind_of(span_type: &str) -> SpanKind {
    match span_type {
        "Entry" => SpanKind::Server,
        "Exit" => SpanKind::Client,
        "Local" => SpanKind::Internal,
        _ => SpanKind::Unspecified,
    }
}

fn ms_to_micros(ms: i64) -> u64 {
    (ms.max(0) as u64).saturating_mul(1000)
}

/// Converts a native span into the SPEC §5 model. Ids are kept verbatim: `span_id =
/// "<segmentId>-<spanId>"`; the parent is the same segment's `parentSpanId` when ≥ 0, else
/// `refs[0]` (`"<parentSegmentId>-<parentSpanId>"`); further refs become links. `kind`: Entry →
/// server, Exit → client, Local → internal. Times ms → µs. Status `error` when `isError`.
/// Attributes = tags plus `sw.component`, `sw.layer`, `sw.peer`, `sw.service_instance` (when
/// set). Span logs become events (name = the `event` data value, else `log`).
pub fn native_span_to_span(span: &NativeSpan) -> Span {
    let mut attributes = Attributes::new();
    for kv in &span.tags {
        attributes.insert(kv.key.clone(), AttrValue::String(kv.value.clone()));
    }
    for (key, value) in [
        ("sw.component", &span.component),
        ("sw.layer", &span.layer),
        ("sw.peer", &span.peer),
        ("sw.service_instance", &span.service_instance_name),
    ] {
        if !value.is_empty() {
            attributes.insert(key.to_owned(), AttrValue::String(value.clone()));
        }
    }
    let start = ms_to_micros(span.start_time);
    let end = ms_to_micros(span.end_time.max(span.start_time));
    let parent_span_id = if span.parent_span_id >= 0 {
        Some(native_span_key(&span.segment_id, span.parent_span_id))
    } else {
        span.refs
            .first()
            .map(|r| native_span_key(&r.parent_segment_id, r.parent_span_id))
    };
    let skip = usize::from(span.parent_span_id < 0);
    Span {
        trace_id: span.trace_id.clone(),
        span_id: native_span_key(&span.segment_id, span.span_id),
        parent_span_id,
        name: span.endpoint_name.clone(),
        kind: kind_of(&span.span_type),
        service_name: span.service_code.clone(),
        start_micros: start,
        end_micros: end,
        duration_micros: end - start,
        status: SpanStatus {
            code: if span.is_error {
                StatusCode::Error
            } else {
                StatusCode::Unset
            },
            message: None,
        },
        attributes,
        events: span
            .logs
            .iter()
            .map(|log| {
                let attributes: Attributes = log
                    .data
                    .iter()
                    .map(|kv| (kv.key.clone(), AttrValue::String(kv.value.clone())))
                    .collect();
                let name = log
                    .data
                    .iter()
                    .find(|kv| kv.key == "event")
                    .map(|kv| kv.value.clone())
                    .unwrap_or_else(|| "log".to_owned());
                SpanEvent {
                    name,
                    time_micros: ms_to_micros(log.time),
                    attributes,
                }
            })
            .collect(),
        links: span
            .refs
            .iter()
            .skip(skip)
            .map(|r| SpanLink {
                trace_id: r.trace_id.clone(),
                span_id: native_span_key(&r.parent_segment_id, r.parent_span_id),
                attributes: [(
                    "sw.ref_type".to_owned(),
                    AttrValue::String(r.ref_type.clone()),
                )]
                .into_iter()
                .collect(),
            })
            .collect(),
    }
}

/// Converts a native trace into a SPEC [`Trace`] (spans sorted); `None` when empty.
pub fn native_trace_to_trace(trace: &NativeTrace) -> Option<Trace> {
    if trace.spans.is_empty() {
        return None;
    }
    let mut spans: Vec<Span> = trace.spans.iter().map(native_span_to_span).collect();
    sort_spans(&mut spans);
    Some(Trace {
        trace_id: trace.trace_id.clone(),
        spans,
    })
}

/// Topology → dependency links (`parent` = source node name, `child` = target node name,
/// `call_count` = 0 because the topology carries no call counts). Sorted by parent, child.
pub fn topology_to_dependencies(topology: &Topology) -> Vec<DependencyLink> {
    let name = |id: &str| {
        topology
            .nodes
            .iter()
            .find(|n| n.id == id)
            .map(|n| n.name.clone())
            .unwrap_or_else(|| id.to_owned())
    };
    let mut links: Vec<DependencyLink> = topology
        .calls
        .iter()
        .map(|c| DependencyLink {
            parent: name(&c.source),
            child: name(&c.target),
            call_count: 0,
        })
        .collect();
    links.sort_by(|a, b| a.parent.cmp(&b.parent).then_with(|| a.child.cmp(&b.child)));
    links
}