Skip to main content

libdd_crashtracker/crash_info/
metadata.rs

1// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use super::unknown_value::UnknownValue;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
9pub struct Metadata {
10    pub library_name: String,
11    pub library_version: String,
12    pub family: String,
13    #[serde(default, skip_serializing_if = "Vec::is_empty")]
14    /// A list of "key:value" tuples.
15    pub tags: Vec<String>,
16}
17
18impl Metadata {
19    pub fn new(
20        library_name: String,
21        library_version: String,
22        family: String,
23        tags: Vec<String>,
24    ) -> Self {
25        Self {
26            library_name,
27            library_version,
28            family,
29            tags,
30        }
31    }
32}
33
34impl UnknownValue for Metadata {
35    fn unknown_value() -> Self {
36        Self {
37            library_name: "unknown".to_string(),
38            library_version: "unknown".to_string(),
39            family: "unknown".to_string(),
40            tags: vec![],
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::Metadata;
48    use crate::crash_info::test_utils::TestInstance;
49
50    macro_rules! tag {
51        ($key:expr, $val:expr) => {
52            format!("{}:{}", $key, $val)
53        };
54    }
55
56    impl TestInstance for Metadata {
57        fn test_instance(seed: u64) -> Self {
58            Self {
59                library_name: "libdatadog".to_owned(),
60                library_version: format!("{}.{}.{}", seed, seed + 1, seed + 2),
61                family: "native".to_owned(),
62                tags: vec![
63                    tag!("service", "foo"),
64                    tag!("service_version", "bar"),
65                    tag!("runtime-id", "xyz"),
66                    tag!("language", "native"),
67                ],
68            }
69        }
70    }
71}