appinsights 0.2.3

Application Insights SDK for Rust
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! A blocking TelemetryClient API.
//!
//! The blocking `TelemetryClient` will block the current thread to execute, instead
//! of returning futures that need to be executed on a runtime.
//!
//! Conversely, the functionality in `appinsights::blocking` must *not* be executed
//! within an runtime, or it will panic when attempting to block. If
//! calling directly from an function, consider using an async
//! [`appinsights::TelemetryClient`][crate::TelemetryClient] instead.
//!
//! ```rust
//! use appinsights::blocking::TelemetryClient;
//!
//! // configure telemetry client with default settings
//! let client = TelemetryClient::new("<instrumentation key>".to_string());
//!
//! // send event telemetry to the Application Insights server
//! client.track_event("Application started");
//!
//! // stop the client
//! // NOT it will **block** the current thread until
//! client.close_channel();
//! ```

use std::{fmt::Display, time::Duration};

use http::{Method, Uri};
use log::debug;
use tokio::sync::mpsc;

use crate::{
    channel::{InMemoryChannel, TelemetryChannel},
    contracts::Envelope,
    telemetry::{
        AvailabilityTelemetry, EventTelemetry, MetricTelemetry, RemoteDependencyTelemetry, RequestTelemetry,
        SeverityLevel, Telemetry, TraceTelemetry,
    },
    TelemetryConfig, TelemetryContext,
};

/// A blocking version of Application Insights telemetry client. It provides an interface to track telemetry items.
pub struct TelemetryClient {
    inner: ChannelHandle,
}

impl TelemetryClient {
    /// Creates a new telemetry client that submits telemetry with specified instrumentation key.
    pub fn new(i_key: String) -> Self {
        Self::from_config(TelemetryConfig::new(i_key))
    }

    /// Creates a new telemetry client configured with specified configuration.
    pub fn from_config(config: TelemetryConfig) -> Self {
        Self::create(config, |config| InMemoryChannel::new(config))
    }

    pub(crate) fn create<C, F>(config: TelemetryConfig, channel: F) -> Self
    where
        C: TelemetryChannel,
        F: FnOnce(&TelemetryConfig) -> C + Send + 'static,
    {
        let inner = ChannelHandle::new(config, channel);
        Self { inner }
    }

    /// Determines whether this client is enabled and will accept telemetry.
    pub fn is_enabled(&self) -> bool {
        self.inner.is_enabled()
    }

    /// Enables or disables telemetry client. When disabled, telemetry is silently swallowed by the client. Defaults to enabled.
    pub fn enabled(&mut self, enabled: bool) {
        self.inner.enabled(enabled);
    }

    /// Returns an immutable reference to a collection of tag data to attach to the telemetry item.
    pub fn context(&self) -> &TelemetryContext {
        &self.inner.context
    }

    /// Returns a mutable reference to a collection of tag data to attach to the telemetry item.
    pub fn context_mut(&mut self) -> &mut TelemetryContext {
        &mut self.inner.context
    }

    /// Logs a user action with the specified name.
    pub fn track_event(&self, name: impl Into<String>) {
        let event = EventTelemetry::new(name);
        self.track(event)
    }

    /// Logs a trace message with a specified severity level.
    pub fn track_trace(&self, message: impl Into<String>, severity: SeverityLevel) {
        let event = TraceTelemetry::new(message, severity);
        self.track(event)
    }

    /// Logs a numeric value that is not specified with a specific event.
    /// Typically used to send regular reports of performance indicators.
    pub fn track_metric(&self, name: impl Into<String>, value: f64) {
        let event = MetricTelemetry::new(name, value);
        self.track(event)
    }

    /// Logs a HTTP request with the specified method, URL, duration and response code.
    pub fn track_request(&self, method: Method, uri: Uri, duration: Duration, response_code: impl Into<String>) {
        let event = RequestTelemetry::new(method, uri, duration, response_code);
        self.track(event)
    }

    /// Logs a dependency with the specified name, type, target, and success status.
    pub fn track_remote_dependency(
        &self,
        name: impl Into<String>,
        dependency_type: impl Into<String>,
        target: impl Into<String>,
        success: bool,
    ) {
        let event = RemoteDependencyTelemetry::new(name, dependency_type, Default::default(), target, success);
        self.track(event)
    }

    /// Logs an availability test result with the specified test name, duration, and success status.
    pub fn track_availability(&self, name: impl Into<String>, duration: Duration, success: bool) {
        let event = AvailabilityTelemetry::new(name, duration, success);
        self.track(event)
    }

    /// Submits a specific telemetry event.
    pub fn track<E>(&self, event: E)
    where
        E: Telemetry,
        (TelemetryContext, E): Into<Envelope>,
    {
        self.inner.track(event);
    }

    /// Forces all pending telemetry items to be submitted. The current thread will not be blocked.
    pub fn flush_channel(&self) {
        self.inner.flush();
    }

    /// Flushes and tears down the submission flow and closes internal channels.
    /// It blocks the current thread until all pending telemetry items have been submitted and it is safe to
    /// shutdown without losing telemetry.
    /// This method consumes the value of client so it makes impossible to use a client with close
    /// channel.
    ///
    /// # Examples
    ///
    /// ```rust, no_run
    /// # use appinsights::TelemetryClient;
    /// # let client = TelemetryClient::new("<instrumentation key>".to_string());
    /// // send heartbeats while application is running
    /// let running = true;
    /// while running {
    ///     client.track_event("app is running");
    /// }
    ///
    /// // wait until pending telemetry is sent at most once and tear down submission flow
    /// client.close_channel();
    ///
    /// // unable to sent any telemetry after client closes its channel
    /// // client.track_event("app is stopped".to_string());
    /// ```
    pub fn close_channel(self) {
        self.inner.close();
    }

    /// Tears down the submission flow and closes internal channels.
    /// Any telemetry waiting to be sent is discarded. This is a more abrupt version of [`close_channel`](#method.close_channel).
    /// This method consumes the value of client so it makes impossible to use a client with close
    /// channel.
    ///
    /// # Examples
    ///
    /// ```rust, no_run
    /// # use appinsights::TelemetryClient;
    /// # let client = TelemetryClient::new("<instrumentation key>".to_string());
    /// // send heartbeats while application is running
    /// let running = true;
    /// while running {
    ///     client.track_event("app is running");
    /// }
    ///
    /// // wait until pending telemetry is sent at most once and tear down submission flow
    /// // or just drop(client)
    /// client.terminate();
    ///
    /// // unable to sent any telemetry after client closes its channel
    /// // client.track_event("app is stopped".to_string());
    /// ```
    pub fn terminate(self) {}
}

struct ChannelHandle {
    enabled: bool,
    context: TelemetryContext,
    inner: InnerChannelHandle,
}

impl ChannelHandle {
    fn new<C, F>(config: TelemetryConfig, channel: F) -> Self
    where
        C: TelemetryChannel,
        F: FnOnce(&TelemetryConfig) -> C + Send + 'static,
    {
        let context = TelemetryContext::from_config(&config);

        let (tx, mut rx) = mpsc::unbounded_channel::<(ClientCommand, OneshotResponse)>();

        let handle = std::thread::Builder::new()
            .name("appinsights-internal-sync-runtime".into())
            .spawn(move || {
                let rt = tokio::runtime::Builder::new_current_thread()
                    .enable_all()
                    .build()
                    .expect("tokio runtime");

                let f = async move {
                    let mut channel = channel(&config);

                    while let Some((command, req_tx)) = rx.recv().await {
                        match command {
                            ClientCommand::Envelope(envelop) => channel.send(envelop),
                            ClientCommand::Flush => channel.flush(),
                            ClientCommand::Stop => channel.close().await,
                            ClientCommand::Terminate => channel.terminate().await,
                        }
                        let _ = req_tx.send(());
                    }
                };
                rt.block_on(f);
            })
            .expect("failed to create a thread");

        let inner = InnerChannelHandle {
            tx: Some(tx),
            thread: Some(handle),
        };

        ChannelHandle {
            inner,
            enabled: true,
            context,
        }
    }

    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    pub fn enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    fn track<E>(&self, event: E)
    where
        E: Telemetry,
        (TelemetryContext, E): Into<Envelope>,
    {
        if self.is_enabled() {
            let envelop = (self.context.clone(), event).into();
            let command = ClientCommand::Envelope(envelop);

            let (tx, mut rx) = mpsc::channel(1);

            self.inner
                .tx
                .as_ref()
                .expect("sync thread exited early")
                .send((command, tx))
                .expect("sync thread panicked");

            let _ = rx.blocking_recv();
        }
    }

    fn flush(&self) {
        self.inner.flush();
    }

    fn close(mut self) {
        self.inner.shutdown(ClientCommand::Stop)
    }
}

type OneshotResponse = mpsc::Sender<()>;

type ThreadSender = mpsc::UnboundedSender<(ClientCommand, OneshotResponse)>;

struct InnerChannelHandle {
    tx: Option<ThreadSender>,
    thread: Option<std::thread::JoinHandle<()>>,
}

impl InnerChannelHandle {
    fn flush(&self) {
        if let Some(sender) = &self.tx {
            send_command(sender, ClientCommand::Flush);
        }
    }

    fn shutdown(&mut self, command: ClientCommand) {
        if let Some(sender) = self.tx.take() {
            send_command(&sender, command);
        }

        self.thread.take().map(|h| h.join());
    }
}

impl Drop for InnerChannelHandle {
    fn drop(&mut self) {
        self.shutdown(ClientCommand::Terminate)
    }
}

fn send_command(sender: &ThreadSender, command: ClientCommand) {
    debug!("Sending {} command to channel", command);
    let (tx, mut rx) = mpsc::channel(1);
    sender.send((command, tx)).expect("sync thread panicked?");

    let _ = rx.blocking_recv();
}

#[derive(Debug, Clone)]
enum ClientCommand {
    Envelope(Envelope),
    Flush,
    Stop,
    Terminate,
}

impl Display for ClientCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            ClientCommand::Envelope(_) => "event",
            ClientCommand::Flush => "flush",
            ClientCommand::Stop => "stop",
            ClientCommand::Terminate => "terminate",
        };

        write!(f, "{}", message)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crossbeam_queue::SegQueue;
    use matches::assert_matches;

    use super::*;
    use crate::client::tests::{TestChannel, TestTelemetry};

    #[test]
    fn it_enabled_by_default() {
        let client = TelemetryClient::new("key".into());
        assert!(client.is_enabled())
    }

    #[test]
    fn it_disables_telemetry() {
        let mut client = TelemetryClient::new("key".into());

        client.enabled(false);

        assert!(!client.is_enabled())
    }

    #[test]
    fn it_submits_telemetry() {
        let events = Arc::new(SegQueue::default());
        let client = create_client(events.clone());

        client.track(TestTelemetry {});

        assert_eq!(events.len(), 1)
    }

    #[test]
    fn it_swallows_telemetry_when_disabled() {
        let events = Arc::new(SegQueue::default());
        let mut client = create_client(events.clone());
        client.enabled(false);

        client.track(TestTelemetry {});

        assert!(events.is_empty())
    }

    #[test]
    fn it_creates_client_with_default_tags() {
        let client = TelemetryClient::new("instrumentation".into());

        let tags = client.context().tags();
        assert_matches!(tags.internal().sdk_version(), Some(version) if version.starts_with("rust"));
        assert_matches!(tags.device().os_version(), Some(_))
    }

    #[test]
    fn it_does_not_fail_with_tokio() {
        let client = TelemetryClient::new("instrumentation".into());
        assert!(client.is_enabled())
    }

    fn create_client(events: Arc<SegQueue<Envelope>>) -> TelemetryClient {
        let config = TelemetryConfig::new("instrumentation".into());
        TelemetryClient::create(config, |_| TestChannel::new(events))
    }
}

#[cfg(test)]
mod integration_tests;