datadog_api_client/datadogV1/model/
model_logs_grok_parser.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9/// Create custom grok rules to parse the full message or [a specific attribute of your raw event](<https://docs.datadoghq.com/logs/log_configuration/parsing/#advanced-settings>).
10/// For more information, see the [parsing section](<https://docs.datadoghq.com/logs/log_configuration/parsing>).
11#[non_exhaustive]
12#[skip_serializing_none]
13#[derive(Clone, Debug, PartialEq, Serialize)]
14pub struct LogsGrokParser {
15    /// Set of rules for the grok parser.
16    #[serde(rename = "grok")]
17    pub grok: crate::datadogV1::model::LogsGrokParserRules,
18    /// Whether or not the processor is enabled.
19    #[serde(rename = "is_enabled")]
20    pub is_enabled: Option<bool>,
21    /// Name of the processor.
22    #[serde(rename = "name")]
23    pub name: Option<String>,
24    /// List of sample logs to test this grok parser.
25    #[serde(rename = "samples")]
26    pub samples: Option<Vec<String>>,
27    /// Name of the log attribute to parse.
28    #[serde(rename = "source")]
29    pub source: String,
30    /// Type of logs grok parser.
31    #[serde(rename = "type")]
32    pub type_: crate::datadogV1::model::LogsGrokParserType,
33    #[serde(flatten)]
34    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
35    #[serde(skip)]
36    #[serde(default)]
37    pub(crate) _unparsed: bool,
38}
39
40impl LogsGrokParser {
41    pub fn new(
42        grok: crate::datadogV1::model::LogsGrokParserRules,
43        source: String,
44        type_: crate::datadogV1::model::LogsGrokParserType,
45    ) -> LogsGrokParser {
46        LogsGrokParser {
47            grok,
48            is_enabled: None,
49            name: None,
50            samples: None,
51            source,
52            type_,
53            additional_properties: std::collections::BTreeMap::new(),
54            _unparsed: false,
55        }
56    }
57
58    pub fn is_enabled(mut self, value: bool) -> Self {
59        self.is_enabled = Some(value);
60        self
61    }
62
63    pub fn name(mut self, value: String) -> Self {
64        self.name = Some(value);
65        self
66    }
67
68    pub fn samples(mut self, value: Vec<String>) -> Self {
69        self.samples = Some(value);
70        self
71    }
72
73    pub fn additional_properties(
74        mut self,
75        value: std::collections::BTreeMap<String, serde_json::Value>,
76    ) -> Self {
77        self.additional_properties = value;
78        self
79    }
80}
81
82impl<'de> Deserialize<'de> for LogsGrokParser {
83    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
84    where
85        D: Deserializer<'de>,
86    {
87        struct LogsGrokParserVisitor;
88        impl<'a> Visitor<'a> for LogsGrokParserVisitor {
89            type Value = LogsGrokParser;
90
91            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
92                f.write_str("a mapping")
93            }
94
95            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
96            where
97                M: MapAccess<'a>,
98            {
99                let mut grok: Option<crate::datadogV1::model::LogsGrokParserRules> = None;
100                let mut is_enabled: Option<bool> = None;
101                let mut name: Option<String> = None;
102                let mut samples: Option<Vec<String>> = None;
103                let mut source: Option<String> = None;
104                let mut type_: Option<crate::datadogV1::model::LogsGrokParserType> = None;
105                let mut additional_properties: std::collections::BTreeMap<
106                    String,
107                    serde_json::Value,
108                > = std::collections::BTreeMap::new();
109                let mut _unparsed = false;
110
111                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
112                    match k.as_str() {
113                        "grok" => {
114                            grok = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
115                        }
116                        "is_enabled" => {
117                            if v.is_null() {
118                                continue;
119                            }
120                            is_enabled = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
121                        }
122                        "name" => {
123                            if v.is_null() {
124                                continue;
125                            }
126                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
127                        }
128                        "samples" => {
129                            if v.is_null() {
130                                continue;
131                            }
132                            samples = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
133                        }
134                        "source" => {
135                            source = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
136                        }
137                        "type" => {
138                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
139                            if let Some(ref _type_) = type_ {
140                                match _type_ {
141                                    crate::datadogV1::model::LogsGrokParserType::UnparsedObject(
142                                        _type_,
143                                    ) => {
144                                        _unparsed = true;
145                                    }
146                                    _ => {}
147                                }
148                            }
149                        }
150                        &_ => {
151                            if let Ok(value) = serde_json::from_value(v.clone()) {
152                                additional_properties.insert(k, value);
153                            }
154                        }
155                    }
156                }
157                let grok = grok.ok_or_else(|| M::Error::missing_field("grok"))?;
158                let source = source.ok_or_else(|| M::Error::missing_field("source"))?;
159                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
160
161                let content = LogsGrokParser {
162                    grok,
163                    is_enabled,
164                    name,
165                    samples,
166                    source,
167                    type_,
168                    additional_properties,
169                    _unparsed,
170                };
171
172                Ok(content)
173            }
174        }
175
176        deserializer.deserialize_any(LogsGrokParserVisitor)
177    }
178}