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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::time::Duration as StdDuration;

use chrono::{DateTime, SecondsFormat, Utc};

use crate::context::TelemetryContext;
use crate::contracts::*;
use crate::telemetry::{ContextTags, Measurements, Properties, Telemetry};
use crate::time::{self, Duration};
use crate::uuid::Uuid;

/// Represents interactions of the monitored component with a remote component/service like SQL or an HTTP endpoint.
///
/// # Examples
/// ```rust, no_run
/// # use appinsights::TelemetryClient;
/// # let client = TelemetryClient::new("<instrumentation key>".to_string());
/// use appinsights::telemetry::{Telemetry, RemoteDependencyTelemetry};
/// use std::time::Duration;
///
/// // create a telemetry item
/// let mut telemetry = RemoteDependencyTelemetry::new(
///     "GET https://api.github.com/dmolokanov/appinsights-rs".to_string(),
///     "HTTP".into(),
///     Duration::from_secs(2),
///     "api.github.com".to_string(),
///     true,
/// );
///
/// // attach custom properties, measurements and context tags
/// telemetry.properties_mut().insert("component".to_string(), "data_processor".to_string());
/// telemetry.tags_mut().insert("os_version".to_string(), "linux x86_64".to_string());
/// telemetry.measurements_mut().insert("body_size".to_string(), 115.0);
///
/// // submit telemetry item to server
/// client.track(telemetry);
/// ```
pub struct RemoteDependencyTelemetry {
    /// Identifier of a dependency call instance.
    /// It is used for correlation with the request telemetry item corresponding to this dependency call.
    id: Option<Uuid>,

    /// Name of the command that initiated this dependency call. Low cardinality value.
    /// Examples are stored procedure name and URL path template.
    name: String,

    /// Duration of the remote call.
    duration: Duration,

    /// Result code of a dependency call.
    /// Examples are SQL error code and HTTP status code.
    result_code: Option<String>,

    /// Indication of successful or unsuccessful call.
    success: bool,

    /// Command initiated by this dependency call.
    /// Examples are SQL statement and HTTP URL's with all the query parameters.
    data: Option<String>,

    /// Dependency type name. Very low cardinality.
    /// Examples are SQL, Azure table and HTTP.
    dependency_type: String,

    /// Target site of a dependency call.
    /// Examples are server name, host address.
    target: String,

    /// The time stamp when this telemetry was measured.
    timestamp: DateTime<Utc>,

    /// Custom properties.
    properties: Properties,

    /// Telemetry context containing extra, optional tags.
    tags: ContextTags,

    /// Custom measurements.
    measurements: Measurements,
}

impl RemoteDependencyTelemetry {
    /// Creates a new telemetry item with specified name, dependency type, target site and success status.
    pub fn new(name: String, dependency_type: String, duration: StdDuration, target: String, success: bool) -> Self {
        Self {
            id: Option::default(),
            name,
            duration: duration.into(),
            result_code: Option::default(),
            success,
            data: Option::default(),
            dependency_type,
            target,
            timestamp: time::now(),
            properties: Properties::default(),
            tags: ContextTags::default(),
            measurements: Measurements::default(),
        }
    }

    /// Returns custom measurements to submit with the telemetry item.
    pub fn measurements(&self) -> &Measurements {
        &self.measurements
    }

    /// Returns mutable reference to custom measurements.
    pub fn measurements_mut(&mut self) -> &mut Measurements {
        &mut self.measurements
    }
}

impl Telemetry for RemoteDependencyTelemetry {
    /// Returns the time when this telemetry was measured.
    fn timestamp(&self) -> DateTime<Utc> {
        self.timestamp
    }

    /// Returns custom properties to submit with the telemetry item.
    fn properties(&self) -> &Properties {
        &self.properties
    }

    /// Returns mutable reference to custom properties.
    fn properties_mut(&mut self) -> &mut Properties {
        &mut self.properties
    }

    /// Returns context data containing extra, optional tags. Overrides values found on client telemetry context.
    fn tags(&self) -> &ContextTags {
        &self.tags
    }

    /// Returns mutable reference to custom tags.
    fn tags_mut(&mut self) -> &mut ContextTags {
        &mut self.tags
    }
}

impl From<(TelemetryContext, RemoteDependencyTelemetry)> for Envelope {
    fn from((context, telemetry): (TelemetryContext, RemoteDependencyTelemetry)) -> Self {
        Self {
            name: "Microsoft.ApplicationInsights.RemoteDependency".into(),
            time: telemetry.timestamp.to_rfc3339_opts(SecondsFormat::Millis, true),
            i_key: Some(context.i_key),
            tags: Some(ContextTags::combine(context.tags, telemetry.tags).into()),
            data: Some(Base::Data(Data::RemoteDependencyData(RemoteDependencyData {
                name: telemetry.name,
                id: telemetry.id.map(|id| id.to_hyphenated().to_string()),
                result_code: telemetry.result_code,
                duration: telemetry.duration.to_string(),
                success: Some(telemetry.success),
                data: telemetry.data,
                target: Some(telemetry.target),
                type_: Some(telemetry.dependency_type),
                properties: Some(Properties::combine(context.properties, telemetry.properties).into()),
                measurements: Some(telemetry.measurements.into()),
                ..RemoteDependencyData::default()
            }))),
            ..Envelope::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use chrono::TimeZone;

    use super::*;

    #[test]
    fn it_overrides_properties_from_context() {
        time::set(Utc.ymd(2019, 1, 2).and_hms_milli(3, 4, 5, 800));

        let mut context =
            TelemetryContext::new("instrumentation".into(), ContextTags::default(), Properties::default());
        context.properties_mut().insert("test".into(), "ok".into());
        context.properties_mut().insert("no-write".into(), "fail".into());

        let mut telemetry = RemoteDependencyTelemetry::new(
            "GET https://example.com/main.html".into(),
            "HTTP".into(),
            StdDuration::from_secs(2),
            "example.com".into(),
            true,
        );
        telemetry.properties_mut().insert("no-write".into(), "ok".into());
        telemetry.measurements_mut().insert("latency".into(), 200.0);

        let envelop = Envelope::from((context, telemetry));

        let expected = Envelope {
            name: "Microsoft.ApplicationInsights.RemoteDependency".into(),
            time: "2019-01-02T03:04:05.800Z".into(),
            i_key: Some("instrumentation".into()),
            tags: Some(BTreeMap::default()),
            data: Some(Base::Data(Data::RemoteDependencyData(RemoteDependencyData {
                name: "GET https://example.com/main.html".into(),
                duration: "0.00:00:02.0000000".into(),
                success: Some(true),
                target: Some("example.com".into()),
                type_: Some("HTTP".into()),
                properties: Some({
                    let mut properties = BTreeMap::default();
                    properties.insert("test".into(), "ok".into());
                    properties.insert("no-write".into(), "ok".into());
                    properties
                }),
                measurements: Some({
                    let mut measurements = BTreeMap::default();
                    measurements.insert("latency".into(), 200.0);
                    measurements
                }),
                ..RemoteDependencyData::default()
            }))),
            ..Envelope::default()
        };

        assert_eq!(envelop, expected)
    }

    #[test]
    fn it_overrides_tags_from_context() {
        time::set(Utc.ymd(2019, 1, 2).and_hms_milli(3, 4, 5, 700));

        let mut context =
            TelemetryContext::new("instrumentation".into(), ContextTags::default(), Properties::default());
        context.tags_mut().insert("test".into(), "ok".into());
        context.tags_mut().insert("no-write".into(), "fail".into());

        let mut telemetry = RemoteDependencyTelemetry::new(
            "GET https://example.com/main.html".into(),
            "HTTP".into(),
            StdDuration::from_secs(2),
            "example.com".into(),
            true,
        );
        telemetry.tags_mut().insert("no-write".into(), "ok".into());

        let envelop = Envelope::from((context, telemetry));

        let expected = Envelope {
            name: "Microsoft.ApplicationInsights.RemoteDependency".into(),
            time: "2019-01-02T03:04:05.700Z".into(),
            i_key: Some("instrumentation".into()),
            tags: Some({
                let mut tags = BTreeMap::default();
                tags.insert("test".into(), "ok".into());
                tags.insert("no-write".into(), "ok".into());
                tags
            }),
            data: Some(Base::Data(Data::RemoteDependencyData(RemoteDependencyData {
                name: "GET https://example.com/main.html".into(),
                duration: "0.00:00:02.0000000".into(),
                success: Some(true),
                target: Some("example.com".into()),
                type_: Some("HTTP".into()),
                properties: Some(BTreeMap::default()),
                measurements: Some(BTreeMap::default()),
                ..RemoteDependencyData::default()
            }))),
            ..Envelope::default()
        };

        assert_eq!(envelop, expected)
    }
}