summaryrefslogtreecommitdiff
path: root/libs/shared/src/opentelemetry.rs
blob: ca6542dac0138c8e6f7e4386bb4103eeb9b49724 (plain)
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
use std::ops::{Deref, DerefMut};

use opentelemetry_otlp::{ExportConfig, Protocol};
use serde::{de::Visitor, Deserialize};

#[derive(Debug, Default)]
#[repr(transparent)]
pub struct ExportConfigDeserialize(ExportConfig);
impl Clone for ExportConfigDeserialize {
    fn clone(&self) -> Self {
        Self(ExportConfig {
            endpoint: self.0.endpoint.clone(),
            protocol: self.0.protocol,
            timeout: self.0.timeout,
        })
    }
}

impl From<ExportConfigDeserialize> for ExportConfig {
    fn from(val: ExportConfigDeserialize) -> Self {
        val.0
    }
}

impl Deref for ExportConfigDeserialize {
    type Target = ExportConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ExportConfigDeserialize {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'de> Deserialize<'de> for ExportConfigDeserialize {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize, Debug)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Fields {
            Endpoint,
            Timeout,
        }

        struct OpenTelemetryExportConfigVisitor;
        impl<'de> Visitor<'de> for OpenTelemetryExportConfigVisitor {
            type Value = ExportConfigDeserialize;
            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("struct OpenTelemetryExportConfig")
            }

            fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let mut export_config = ExportConfigDeserialize::default();
                export_config.0.protocol = Protocol::Grpc;
                while let Some(name) = map.next_key::<Fields>()? {
                    match name {
                        Fields::Endpoint => {
                            export_config.0.endpoint = map.next_value()?;
                        }
                        Fields::Timeout => {
                            export_config.0.timeout = map.next_value()?;
                        }
                    }
                }

                Ok(export_config)
            }
        }

        deserializer.deserialize_struct(
            "OpenTelemetryExportConfig",
            &["endpoint", "protocol", "timeout"],
            OpenTelemetryExportConfigVisitor,
        )
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Configuration {
    pub traces: Option<ExportConfigDeserialize>,
    pub metrics: Option<ExportConfigDeserialize>,
}