esdiag 0.16.4

Elastic Stack diagnostic collector and processor
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

use super::super::Identifiers;
use super::{DiagPath, Manifest};
use crate::data::Product;
use chrono::{DateTime, SecondsFormat, TimeZone, Utc};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::RwLock;

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct RequestedApi {
    /// Final HTTP response status observed for this API request, if available
    pub status: Option<u16>,
    /// Number of retry attempts performed before the final response
    #[serde(default)]
    pub retries: u32,
    /// Total time spent waiting for collected response bodies for this API
    pub response_time_ms: u64,
    /// Total size in bytes of collected response bodies for this API
    pub response_size_bytes: u64,
}

#[derive(Deserialize, Serialize)]
pub struct DiagnosticManifest {
    /// Diagnostic bundle variation
    pub mode: Option<String>,
    /// Elastic Stack component name
    pub product: Product,
    /// Command-line flags used when running the diagnostic collector
    pub flags: Option<String>,
    /// Diagnostic collector version
    pub diagnostic: Option<String>,
    /// Diagnostic type (relates to product, not mode)
    pub r#type: Option<String>,
    /// Where the diagnostic was run from
    pub runner: Option<String>,
    /// Elastic Stack version
    pub version: Option<String>,
    /// Datetime when the diagnostic was collected
    #[serde(rename = "timestamp")]
    pub collection_date: String,
    /// Collection time in milliseconds since the Unix epoch
    pub collection_date_millis: Option<u64>,
    /// Platform diagnostic bundles can contain multiple diagnostics from different components
    pub included_diagnostics: Option<Vec<DiagPath>>,
    /// Name for human-readable IDs
    #[serde(skip_deserializing)]
    pub name: String,
    #[serde(skip_deserializing)]
    diagnostic_id: RwLock<Option<String>>,
    /// Additional identifiers not included in the diagnostic itself
    pub identifiers: Option<Identifiers>,
    /// APIs requested during this run keyed by API name
    pub requested_apis: Option<BTreeMap<String, RequestedApi>>,
    /// Deprecated compatibility list of API names collected during this run.
    #[serde(default)]
    pub collected_apis: Option<Vec<String>>,
}

impl Clone for DiagnosticManifest {
    fn clone(&self) -> Self {
        let diagnostic_id = if let Ok(id) = self.diagnostic_id.read() {
            RwLock::new(id.clone())
        } else {
            RwLock::new(None)
        };
        Self {
            mode: self.mode.clone(),
            product: self.product.clone(),
            flags: self.flags.clone(),
            diagnostic: self.diagnostic.clone(),
            r#type: self.r#type.clone(),
            runner: self.runner.clone(),
            version: self.version.clone(),
            collection_date: self.collection_date.clone(),
            collection_date_millis: self.collection_date_millis,
            included_diagnostics: self.included_diagnostics.clone(),
            name: self.name.clone(),
            diagnostic_id,
            identifiers: self.identifiers.clone(),
            requested_apis: self.requested_apis.clone(),
            collected_apis: self.collected_apis.clone(),
        }
    }
}

impl DiagnosticManifest {
    fn parse_collection_date_millis(collection_date: &str) -> Option<u64> {
        if let Ok(date) = DateTime::parse_from_rfc3339(collection_date) {
            u64::try_from(date.timestamp_millis()).ok()
        } else if let Ok(date) = DateTime::parse_from_str(collection_date, "%Y-%m-%dT%H:%M:%S%.3f%z") {
            u64::try_from(date.timestamp_millis()).ok()
        } else {
            None
        }
    }

    fn valid_collection_date_millis(collection_date_millis: u64) -> Option<u64> {
        if collection_date_millis == 0 {
            return None;
        }

        let millis = i64::try_from(collection_date_millis).ok()?;
        Utc.timestamp_millis_opt(millis).single()?;
        Some(collection_date_millis)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new(
        collection_date: String,
        diagnostic: Option<String>,
        flags: Option<String>,
        included_diagnostics: Option<Vec<DiagPath>>,
        mode: Option<String>,
        product: Product,
        r#type: Option<String>,
        runner: Option<String>,
        version: Option<String>,
    ) -> Self {
        let collection_date_millis = Some(Self::parse_collection_date_millis(&collection_date).unwrap_or_else(|| {
            tracing::warn!("Failed to parse collection date: {}", &collection_date);
            u64::try_from(chrono::Utc::now().timestamp_millis()).unwrap_or_default()
        }));
        let diagnostic_id = RwLock::new(None);
        let name = r#type.clone().unwrap_or("diagnostic".to_string());

        Self {
            collection_date,
            collection_date_millis,
            diagnostic,
            diagnostic_id,
            flags,
            included_diagnostics,
            identifiers: None,
            requested_apis: None,
            collected_apis: None,
            mode,
            name,
            product,
            r#type,
            runner,
            version,
        }
    }

    pub fn collection_date_in_millis(&self) -> u64 {
        self.collection_date_millis
            .and_then(Self::valid_collection_date_millis)
            .unwrap_or_else(|| {
                Self::parse_collection_date_millis(&self.collection_date).unwrap_or_else(|| {
                    tracing::warn!("Failed to parse collection date: {}", &self.collection_date);
                    u64::try_from(chrono::Utc::now().timestamp_millis()).unwrap_or_default()
                })
            })
    }

    pub fn diagnostic_id(&self, uuid: &str) -> String {
        let mut id = self
            .diagnostic_id
            .write()
            .expect("Failed to obtain write lock for diagnostic id");

        match id.as_ref() {
            Some(id) => id.clone(),
            None => {
                let collection_date_string = Utc
                    .timestamp_millis_opt(self.collection_date_in_millis() as i64)
                    .single()
                    .unwrap_or_else(Utc::now)
                    .to_rfc3339_opts(SecondsFormat::Secs, true);

                // Human readable ID
                *id = Some(format!(
                    "{}@{}~{}",
                    self.name,
                    &collection_date_string[..10], // Trim to only date
                    &uuid[..4]
                ));
                id.clone().unwrap()
            }
        }
    }

    pub fn with_name(self, name: String) -> Self {
        Self { name, ..self }
    }

    pub fn with_identifiers(self, identifiers: Identifiers) -> Self {
        Self {
            identifiers: Some(identifiers),
            ..self
        }
    }

    pub fn with_requested_apis(self, requested_apis: BTreeMap<String, RequestedApi>) -> Self {
        let collected_apis = requested_apis
            .iter()
            .filter(|(_, api)| api.status.is_some_and(|status| (200..300).contains(&status)))
            .map(|(name, _)| name.clone())
            .collect();
        Self {
            requested_apis: Some(requested_apis),
            collected_apis: Some(collected_apis),
            ..self
        }
    }

    #[deprecated(note = "use with_requested_apis to include per-request metadata")]
    pub fn with_collected_apis(self, collected_apis: Vec<String>) -> Self {
        Self {
            collected_apis: Some(collected_apis),
            ..self
        }
    }
}

impl DiagnosticManifest {
    pub const FILENAME: &'static str = "diagnostic_manifest.json";
}

impl From<Manifest> for DiagnosticManifest {
    fn from(manifest: Manifest) -> Self {
        let product = match manifest.diag_type.as_deref() {
            Some("eck-diagnostics") => Product::ECK,
            Some("k8s-platform-diagnostics") => Product::KubernetesPlatform,
            _ => Product::Elasticsearch,
        };
        DiagnosticManifest::new(
            manifest.collection_date,
            manifest.diag_version,
            manifest.diagnostic_inputs,
            manifest.included_diagnostics,
            Some("compatible".to_string()),
            product,
            manifest.diag_type,
            manifest.runner,
            manifest
                .product_version
                .map(|v| v.original_value.map(|v| v.to_string()).unwrap_or_default()),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::{DiagnosticManifest, RequestedApi};
    use crate::data::Product;
    use std::collections::BTreeMap;

    #[test]
    fn new_sets_collection_date_millis_from_timestamp() {
        let manifest = DiagnosticManifest::new(
            "2026-04-25T20:18:43.610Z".to_string(),
            Some("esdiag-0.16.0".to_string()),
            None,
            None,
            Some("support".to_string()),
            Product::Elasticsearch,
            Some("elasticsearch_diagnostic".to_string()),
            Some("esdiag".to_string()),
            Some("8.19.3".to_string()),
        );

        assert_eq!(manifest.collection_date_millis, Some(1_777_148_323_610));
    }

    #[test]
    fn parse_collection_date_millis_rejects_pre_epoch_timestamp() {
        assert_eq!(
            DiagnosticManifest::parse_collection_date_millis("1969-12-31T23:59:59Z"),
            None
        );
    }

    #[test]
    fn collection_date_in_millis_uses_stored_value_first() {
        let mut manifest = DiagnosticManifest::new(
            "2026-04-25T20:18:43.610Z".to_string(),
            Some("esdiag-0.16.0".to_string()),
            None,
            None,
            Some("support".to_string()),
            Product::Elasticsearch,
            Some("elasticsearch_diagnostic".to_string()),
            Some("esdiag".to_string()),
            Some("8.19.3".to_string()),
        );
        manifest.collection_date = "not-a-date".to_string();

        assert_eq!(manifest.collection_date_in_millis(), 1_777_148_323_610);
    }

    #[test]
    fn collection_date_in_millis_treats_zero_as_missing() {
        let mut manifest = DiagnosticManifest::new(
            "2026-04-25T20:18:43.610Z".to_string(),
            Some("esdiag-0.16.0".to_string()),
            None,
            None,
            Some("support".to_string()),
            Product::Elasticsearch,
            Some("elasticsearch_diagnostic".to_string()),
            Some("esdiag".to_string()),
            Some("8.19.3".to_string()),
        );
        manifest.collection_date_millis = Some(0);

        assert_eq!(manifest.collection_date_in_millis(), 1_777_148_323_610);
    }

    #[test]
    fn collection_date_in_millis_rejects_out_of_range_stored_value() {
        let mut manifest = DiagnosticManifest::new(
            "2026-04-25T20:18:43.610Z".to_string(),
            Some("esdiag-0.16.0".to_string()),
            None,
            None,
            Some("support".to_string()),
            Product::Elasticsearch,
            Some("elasticsearch_diagnostic".to_string()),
            Some("esdiag".to_string()),
            Some("8.19.3".to_string()),
        );
        manifest.collection_date_millis = Some(u64::MAX);

        assert_eq!(manifest.collection_date_in_millis(), 1_777_148_323_610);
        assert_eq!(
            manifest.diagnostic_id("abcd1234"),
            "elasticsearch_diagnostic@2026-04-25~abcd"
        );
    }

    #[test]
    fn requested_api_retries_defaults_for_legacy_manifests() {
        let manifest: DiagnosticManifest = serde_json::from_str(
            r#"{
              "mode": "support",
              "product": "elasticsearch",
              "flags": null,
              "diagnostic": "esdiag-0.16.0",
              "type": "elasticsearch_diagnostic",
              "runner": "esdiag",
              "version": "6.8.23",
              "timestamp": "2026-04-25T20:52:09.948Z",
              "collection_date_millis": 1777150329948,
              "included_diagnostics": null,
              "name": "elasticsearch_diagnostic",
              "diagnostic_id": null,
              "identifiers": null,
              "requested_apis": {
                "nodes": {
                  "status": 200,
                  "response_time_ms": 191,
                  "response_size_bytes": 14005
                }
              }
            }"#,
        )
        .expect("legacy diagnostic_manifest.json should deserialize");

        let requested_apis = manifest.requested_apis.expect("requested APIs");
        let nodes = requested_apis.get("nodes").expect("nodes API metadata");
        assert_eq!(nodes.retries, 0);
        assert_eq!(nodes.status, Some(200));
        assert_eq!(nodes.response_time_ms, 191);
        assert_eq!(nodes.response_size_bytes, 14005);
    }

    #[test]
    fn collected_apis_deserializes_for_legacy_consumers() {
        let manifest: DiagnosticManifest = serde_json::from_str(
            r#"{
              "mode": "support",
              "product": "elasticsearch",
              "flags": null,
              "diagnostic": "esdiag-0.16.0",
              "type": "elasticsearch_diagnostic",
              "runner": "esdiag",
              "version": "6.8.23",
              "timestamp": "2026-04-25T20:52:09.948Z",
              "collection_date_millis": 1777150329948,
              "included_diagnostics": null,
              "name": "elasticsearch_diagnostic",
              "diagnostic_id": null,
              "identifiers": null,
              "collected_apis": ["nodes"]
            }"#,
        )
        .expect("legacy diagnostic_manifest.json should deserialize");

        assert_eq!(manifest.collected_apis, Some(vec!["nodes".to_string()]));
    }

    #[test]
    fn requested_apis_serializes_deprecated_collected_apis_list() {
        let manifest = DiagnosticManifest::new(
            "2026-04-25T20:18:43.610Z".to_string(),
            Some("esdiag-0.16.0".to_string()),
            None,
            None,
            Some("support".to_string()),
            Product::Elasticsearch,
            Some("elasticsearch_diagnostic".to_string()),
            Some("esdiag".to_string()),
            Some("8.19.3".to_string()),
        )
        .with_requested_apis(BTreeMap::from([
            (
                "nodes".to_string(),
                RequestedApi {
                    status: Some(200),
                    retries: 0,
                    response_time_ms: 191,
                    response_size_bytes: 14005,
                },
            ),
            (
                "missing_api".to_string(),
                RequestedApi {
                    status: Some(404),
                    retries: 0,
                    response_time_ms: 12,
                    response_size_bytes: 42,
                },
            ),
        ]));

        let value = serde_json::to_value(&manifest).expect("serialize manifest");
        assert_eq!(value["requested_apis"]["nodes"]["status"], 200);
        assert_eq!(value["requested_apis"]["missing_api"]["status"], 404);
        assert_eq!(value["collected_apis"], serde_json::json!(["nodes"]));
    }
}