datadog_api_client/datadogV2/model/
model_observability_pipeline_sample_processor.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/// The `sample` processor allows probabilistic sampling of logs at a fixed rate.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct ObservabilityPipelineSampleProcessor {
14    /// The unique identifier for this component. Used to reference this component in other parts of the pipeline (for example, as the `input` to downstream components).
15    #[serde(rename = "id")]
16    pub id: String,
17    /// A Datadog search query used to determine which logs this processor targets.
18    #[serde(rename = "include")]
19    pub include: String,
20    /// A list of component IDs whose output is used as the `input` for this component.
21    #[serde(rename = "inputs")]
22    pub inputs: Vec<String>,
23    /// The percentage of logs to sample.
24    #[serde(rename = "percentage")]
25    pub percentage: Option<f64>,
26    /// Number of events to sample (1 in N).
27    #[serde(rename = "rate")]
28    pub rate: Option<i64>,
29    /// The processor type. The value should always be `sample`.
30    #[serde(rename = "type")]
31    pub type_: crate::datadogV2::model::ObservabilityPipelineSampleProcessorType,
32    #[serde(flatten)]
33    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
34    #[serde(skip)]
35    #[serde(default)]
36    pub(crate) _unparsed: bool,
37}
38
39impl ObservabilityPipelineSampleProcessor {
40    pub fn new(
41        id: String,
42        include: String,
43        inputs: Vec<String>,
44        type_: crate::datadogV2::model::ObservabilityPipelineSampleProcessorType,
45    ) -> ObservabilityPipelineSampleProcessor {
46        ObservabilityPipelineSampleProcessor {
47            id,
48            include,
49            inputs,
50            percentage: None,
51            rate: None,
52            type_,
53            additional_properties: std::collections::BTreeMap::new(),
54            _unparsed: false,
55        }
56    }
57
58    pub fn percentage(mut self, value: f64) -> Self {
59        self.percentage = Some(value);
60        self
61    }
62
63    pub fn rate(mut self, value: i64) -> Self {
64        self.rate = Some(value);
65        self
66    }
67
68    pub fn additional_properties(
69        mut self,
70        value: std::collections::BTreeMap<String, serde_json::Value>,
71    ) -> Self {
72        self.additional_properties = value;
73        self
74    }
75}
76
77impl<'de> Deserialize<'de> for ObservabilityPipelineSampleProcessor {
78    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
79    where
80        D: Deserializer<'de>,
81    {
82        struct ObservabilityPipelineSampleProcessorVisitor;
83        impl<'a> Visitor<'a> for ObservabilityPipelineSampleProcessorVisitor {
84            type Value = ObservabilityPipelineSampleProcessor;
85
86            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
87                f.write_str("a mapping")
88            }
89
90            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
91            where
92                M: MapAccess<'a>,
93            {
94                let mut id: Option<String> = None;
95                let mut include: Option<String> = None;
96                let mut inputs: Option<Vec<String>> = None;
97                let mut percentage: Option<f64> = None;
98                let mut rate: Option<i64> = None;
99                let mut type_: Option<
100                    crate::datadogV2::model::ObservabilityPipelineSampleProcessorType,
101                > = None;
102                let mut additional_properties: std::collections::BTreeMap<
103                    String,
104                    serde_json::Value,
105                > = std::collections::BTreeMap::new();
106                let mut _unparsed = false;
107
108                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
109                    match k.as_str() {
110                        "id" => {
111                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
112                        }
113                        "include" => {
114                            include = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
115                        }
116                        "inputs" => {
117                            inputs = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
118                        }
119                        "percentage" => {
120                            if v.is_null() {
121                                continue;
122                            }
123                            percentage = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
124                        }
125                        "rate" => {
126                            if v.is_null() {
127                                continue;
128                            }
129                            rate = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
130                        }
131                        "type" => {
132                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
133                            if let Some(ref _type_) = type_ {
134                                match _type_ {
135                                    crate::datadogV2::model::ObservabilityPipelineSampleProcessorType::UnparsedObject(_type_) => {
136                                        _unparsed = true;
137                                    },
138                                    _ => {}
139                                }
140                            }
141                        }
142                        &_ => {
143                            if let Ok(value) = serde_json::from_value(v.clone()) {
144                                additional_properties.insert(k, value);
145                            }
146                        }
147                    }
148                }
149                let id = id.ok_or_else(|| M::Error::missing_field("id"))?;
150                let include = include.ok_or_else(|| M::Error::missing_field("include"))?;
151                let inputs = inputs.ok_or_else(|| M::Error::missing_field("inputs"))?;
152                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
153
154                let content = ObservabilityPipelineSampleProcessor {
155                    id,
156                    include,
157                    inputs,
158                    percentage,
159                    rate,
160                    type_,
161                    additional_properties,
162                    _unparsed,
163                };
164
165                Ok(content)
166            }
167        }
168
169        deserializer.deserialize_any(ObservabilityPipelineSampleProcessorVisitor)
170    }
171}