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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Module for telemetry client configuration.
use std::time::Duration;

/// Configuration data used to initialize a new [`TelemetryClient`](../struct.TelemetryClient.html) with.
///
/// # Examples
///
/// Creating a telemetry client configuration with default settings
/// ```rust
/// # use appinsights::TelemetryConfig;
/// let config = TelemetryConfig::new("<instrumentation key>".to_string());
/// ```
///
/// Creating a telemetry client configuration with custom settings
/// ```rust
/// # use std::time::Duration;
/// # use appinsights::TelemetryConfig;
/// let config = TelemetryConfig::builder()
///     .i_key("<instrumentation key>")
///     .interval(Duration::from_secs(5))
///     .build();
/// ```
#[derive(Debug, PartialEq)]
pub struct TelemetryConfig {
    /// Instrumentation key for the client.
    i_key: String,

    /// Endpoint URL where data will be sent.
    endpoint: String,

    /// Maximum time to wait until send a batch of telemetry.
    interval: Duration,
}

impl TelemetryConfig {
    /// Creates a new telemetry configuration with specified instrumentation key and default values.
    pub fn new(i_key: String) -> Self {
        TelemetryConfig::builder().i_key(i_key).build()
    }

    /// Creates a new telemetry configuration builder with default parameters.
    pub fn builder() -> DefaultTelemetryConfigBuilder {
        DefaultTelemetryConfigBuilder::default()
    }

    /// Returns an instrumentation key for the client.
    pub fn i_key(&self) -> &str {
        &self.i_key
    }

    /// Returns endpoint URL where data will be sent.
    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// Returns maximum time to wait until send a batch of telemetry.
    pub fn interval(&self) -> Duration {
        self.interval
    }
}

/// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with required
/// instrumentation key and custom settings.
#[derive(Default)]
pub struct DefaultTelemetryConfigBuilder;

impl DefaultTelemetryConfigBuilder {
    /// Initializes a builder with an instrumentation key for the client.
    pub fn i_key<I>(self, i_key: I) -> TelemetryConfigBuilder
    where
        I: Into<String>,
    {
        TelemetryConfigBuilder {
            i_key: i_key.into(),
            endpoint: "https://dc.services.visualstudio.com/v2/track".into(),
            interval: Duration::from_secs(2),
        }
    }
}

/// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with custom settings.
pub struct TelemetryConfigBuilder {
    i_key: String,
    endpoint: String,
    interval: Duration,
}

impl TelemetryConfigBuilder {
    /// Initializes a builder with an instrumentation key for the client.
    pub fn i_key<I>(mut self, i_key: I) -> Self
    where
        I: Into<String>,
    {
        self.i_key = i_key.into();
        self
    }

    /// Initializes a builder with an endpoint URL where data will be sent.
    pub fn endpoint<E>(mut self, endpoint: E) -> Self
    where
        E: Into<String>,
    {
        self.endpoint = endpoint.into();
        self
    }

    /// Initializes a builder with a maximum time to wait until send a batch of telemetry.
    pub fn interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Constructs a new instance of a [`TelemetryConfig`](struct.TelemetryConfig.html) with custom settings.
    pub fn build(self) -> TelemetryConfig {
        TelemetryConfig {
            i_key: self.i_key,
            endpoint: self.endpoint,
            interval: self.interval,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_creates_config_with_default_values() {
        let config = TelemetryConfig::new("instrumentation key".into());

        assert_eq!(
            TelemetryConfig {
                i_key: "instrumentation key".into(),
                endpoint: "https://dc.services.visualstudio.com/v2/track".into(),
                interval: Duration::from_secs(2)
            },
            config
        )
    }

    #[test]
    fn it_builds_config_with_custom_parameters() {
        let config = TelemetryConfig::builder()
            .i_key("instrumentation key")
            .endpoint("https://google.com")
            .interval(Duration::from_micros(100))
            .build();

        assert_eq!(
            TelemetryConfig {
                i_key: "instrumentation key".into(),
                endpoint: "https://google.com".into(),
                interval: Duration::from_micros(100)
            },
            config
        );
    }
}