|
| 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. |
| 4 | +use serde::de::{Error, MapAccess, Visitor}; |
| 5 | +use serde::{Deserialize, Deserializer, Serialize}; |
| 6 | +use serde_with::skip_serializing_none; |
| 7 | +use std::fmt::{self, Formatter}; |
| 8 | + |
| 9 | +/// There are two ways to define correlation between application spans and logs: |
| 10 | +/// |
| 11 | +/// 1. Follow the documentation on [how to inject a span ID in the application logs](<https://docs.datadoghq.com/tracing/connect_logs_and_traces>). |
| 12 | +/// Log integrations automatically handle all remaining setup steps by default. |
| 13 | +/// |
| 14 | +/// 2. Use the span remapper processor to define a log attribute as its associated span ID. |
| 15 | +#[non_exhaustive] |
| 16 | +#[skip_serializing_none] |
| 17 | +#[derive(Clone, Debug, PartialEq, Serialize)] |
| 18 | +pub struct LogsSpanRemapper { |
| 19 | + /// Whether or not the processor is enabled. |
| 20 | + #[serde(rename = "is_enabled")] |
| 21 | + pub is_enabled: Option<bool>, |
| 22 | + /// Name of the processor. |
| 23 | + #[serde(rename = "name")] |
| 24 | + pub name: Option<String>, |
| 25 | + /// Array of source attributes. |
| 26 | + #[serde(rename = "sources")] |
| 27 | + pub sources: Option<Vec<String>>, |
| 28 | + /// Type of logs span remapper. |
| 29 | + #[serde(rename = "type")] |
| 30 | + pub type_: crate::datadogV1::model::LogsSpanRemapperType, |
| 31 | + #[serde(flatten)] |
| 32 | + pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
| 33 | + #[serde(skip)] |
| 34 | + #[serde(default)] |
| 35 | + pub(crate) _unparsed: bool, |
| 36 | +} |
| 37 | + |
| 38 | +impl LogsSpanRemapper { |
| 39 | + pub fn new(type_: crate::datadogV1::model::LogsSpanRemapperType) -> LogsSpanRemapper { |
| 40 | + LogsSpanRemapper { |
| 41 | + is_enabled: None, |
| 42 | + name: None, |
| 43 | + sources: None, |
| 44 | + type_, |
| 45 | + additional_properties: std::collections::BTreeMap::new(), |
| 46 | + _unparsed: false, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + pub fn is_enabled(mut self, value: bool) -> Self { |
| 51 | + self.is_enabled = Some(value); |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + pub fn name(mut self, value: String) -> Self { |
| 56 | + self.name = Some(value); |
| 57 | + self |
| 58 | + } |
| 59 | + |
| 60 | + pub fn sources(mut self, value: Vec<String>) -> Self { |
| 61 | + self.sources = Some(value); |
| 62 | + self |
| 63 | + } |
| 64 | + |
| 65 | + pub fn additional_properties( |
| 66 | + mut self, |
| 67 | + value: std::collections::BTreeMap<String, serde_json::Value>, |
| 68 | + ) -> Self { |
| 69 | + self.additional_properties = value; |
| 70 | + self |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<'de> Deserialize<'de> for LogsSpanRemapper { |
| 75 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 76 | + where |
| 77 | + D: Deserializer<'de>, |
| 78 | + { |
| 79 | + struct LogsSpanRemapperVisitor; |
| 80 | + impl<'a> Visitor<'a> for LogsSpanRemapperVisitor { |
| 81 | + type Value = LogsSpanRemapper; |
| 82 | + |
| 83 | + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 84 | + f.write_str("a mapping") |
| 85 | + } |
| 86 | + |
| 87 | + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 88 | + where |
| 89 | + M: MapAccess<'a>, |
| 90 | + { |
| 91 | + let mut is_enabled: Option<bool> = None; |
| 92 | + let mut name: Option<String> = None; |
| 93 | + let mut sources: Option<Vec<String>> = None; |
| 94 | + let mut type_: Option<crate::datadogV1::model::LogsSpanRemapperType> = None; |
| 95 | + let mut additional_properties: std::collections::BTreeMap< |
| 96 | + String, |
| 97 | + serde_json::Value, |
| 98 | + > = std::collections::BTreeMap::new(); |
| 99 | + let mut _unparsed = false; |
| 100 | + |
| 101 | + while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
| 102 | + match k.as_str() { |
| 103 | + "is_enabled" => { |
| 104 | + if v.is_null() { |
| 105 | + continue; |
| 106 | + } |
| 107 | + is_enabled = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 108 | + } |
| 109 | + "name" => { |
| 110 | + if v.is_null() { |
| 111 | + continue; |
| 112 | + } |
| 113 | + name = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 114 | + } |
| 115 | + "sources" => { |
| 116 | + if v.is_null() { |
| 117 | + continue; |
| 118 | + } |
| 119 | + sources = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 120 | + } |
| 121 | + "type" => { |
| 122 | + type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 123 | + if let Some(ref _type_) = type_ { |
| 124 | + match _type_ { |
| 125 | + crate::datadogV1::model::LogsSpanRemapperType::UnparsedObject(_type_) => { |
| 126 | + _unparsed = true; |
| 127 | + }, |
| 128 | + _ => {} |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + &_ => { |
| 133 | + if let Ok(value) = serde_json::from_value(v.clone()) { |
| 134 | + additional_properties.insert(k, value); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?; |
| 140 | + |
| 141 | + let content = LogsSpanRemapper { |
| 142 | + is_enabled, |
| 143 | + name, |
| 144 | + sources, |
| 145 | + type_, |
| 146 | + additional_properties, |
| 147 | + _unparsed, |
| 148 | + }; |
| 149 | + |
| 150 | + Ok(content) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + deserializer.deserialize_any(LogsSpanRemapperVisitor) |
| 155 | + } |
| 156 | +} |
0 commit comments