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
//! An [Azure Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview) exporter implementation for [OpenTelemetry Rust](https://github.com/open-telemetry/opentelemetry-rust).
//!
//! **Disclaimer**: This is not an official Microsoft product.
//!
//! # Usage
//!
//! Configure the exporter:
//!
//! ```rust,no_run
//! use opentelemetry::{global, sdk};
//!
//! fn init_tracer() {
//!     let instrumentation_key = "...".to_string();
//!     let exporter = opentelemetry_application_insights::Exporter::new(instrumentation_key);
//!     let provider = sdk::Provider::builder()
//!         .with_simple_exporter(exporter)
//!         .build();
//!     global::set_provider(provider);
//! }
//! ```
//!
//! Then follow the documentation of [opentelemetry](https://github.com/open-telemetry/opentelemetry-rust) to create spans and events.
//!
//! # Attribute mapping
//!
//! OpenTelemetry and Application Insights are using different terminology. This crate tries it's best to map OpenTelemetry fields to their correct Application Insights pendant.
//!
//! - [OpenTelemetry specification: Span](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#span)
//! - [Application Insights data model](https://docs.microsoft.com/en-us/azure/azure-monitor/app/data-model)
//!
//! The OpenTelemetry SpanKind determines the Application Insights telemetry type:
//!
//! | OpenTelemetry SpanKind           | Application Insights telemetry type |
//! | -------------------------------- | ----------------------------------- |
//! | `CLIENT`, `PRODUCER`, `INTERNAL` | Dependency                          |
//! | `SERVER`, `CONSUMER`             | Request                             |
//!
//! The Span's list of Events are converted to Trace telemetry.
//!
//! The Span's status determines the Success field of a Dependency or Request. Success is `true` if the status is `OK`; otherwise `false`.
//!
//! For `INTERNAL` Spans the Dependency Type is always `"InProc"` and Success is `true`.
//!
//! The following of the Span's attributes map to special fields in Application Insights (the mapping tries to follow [OpenTelemetry semantic conventions](https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions)).
//!
//! | OpenTelemetry attribute key              | Application Insights field     |
//! | ---------------------------------------- | ------------------------------ |
//! | `enduser.id`                             | Context: Authenticated user id |
//! | `http.url`                               | Dependency Data                |
//! | `db.statement`                           | Dependency Data                |
//! | `http.host`                              | Dependency Target              |
//! | `net.peer.name`                          | Dependency Target              |
//! | `db.instance`                            | Dependency Target              |
//! | `http.status_code`                       | Dependency Result code         |
//! | `db.type`                                | Dependency Type                |
//! | `messaging.system`                       | Dependency Type                |
//! | `"HTTP"` if any `http.` attribute exists | Dependency Type                |
//! | `"DB"` if any `db.` attribute exists     | Dependency Type                |
//! | `http.url`                               | Request Url                    |
//! | `http.target`                            | Request Url                    |
//! | `http.status_code`                       | Request Response code          |
//!
//! All other attributes are be directly converted to custom properties.
//!
//! For Requests the attributes `http.method` and `http.route` override the Name.
#![doc(html_root_url = "https://docs.rs/opentelemetry-application-insights/0.2.0")]
#![deny(missing_docs, unreachable_pub, missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]

mod convert;
mod models;
mod tags;
mod uploader;

use convert::{
    attrs_to_properties, duration_to_string, evictedhashmap_to_hashmap, span_id_to_string,
    time_to_string,
};
use models::{Data, Envelope, MessageData, RemoteDependencyData, RequestData};
use opentelemetry::api::{SpanKind, StatusCode};
use opentelemetry::exporter::trace;
use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;
use std::time::Duration;
use tags::{get_common_tags, get_tags_for_event, get_tags_for_span, merge_tags};

/// Application Insights span exporter
#[derive(Debug)]
pub struct Exporter {
    instrumentation_key: String,
    common_tags: BTreeMap<String, String>,
    sample_rate: f64,
    request_ignored_properties: HashSet<&'static str>,
    dependency_ignored_properties: HashSet<&'static str>,
}

impl Exporter {
    /// Create a new exporter.
    pub fn new(instrumentation_key: String) -> Self {
        let common_tags = get_common_tags();
        let request_ignored_properties: HashSet<&'static str> = [
            "enduser.id",
            "http.method",
            "http.route",
            "http.status_code",
            "http.url",
            "http.target",
        ]
        .iter()
        .cloned()
        .collect();
        let dependency_ignored_properties: HashSet<&'static str> = [
            "enduser.id",
            "http.status_code",
            "http.url",
            "db.statement",
            "http.host",
            "net.peer.name",
            "db.instance",
            "db.type",
            "messaging.system",
        ]
        .iter()
        .cloned()
        .collect();
        Self {
            instrumentation_key,
            common_tags,
            sample_rate: 100.0,
            request_ignored_properties,
            dependency_ignored_properties,
        }
    }

    /// Add an application version to all telemetry items.
    ///
    /// ```
    /// let exporter = opentelemetry_application_insights::Exporter::new("...".into())
    ///     .with_application_version(std::env!("CARGO_PKG_VERSION").into());
    /// ```
    pub fn with_application_version(mut self, ver: String) -> Self {
        self.common_tags.insert("ai.application.ver".into(), ver);
        self
    }

    /// Set sample rate, which is passed through to Application Insights. It should be a value
    /// between 0 and 1 and match the rate given to the sampler.
    ///
    /// Default: 1.0
    ///
    /// ```
    /// # use opentelemetry::{global, sdk};
    /// let sample_rate = 0.3;
    /// let exporter = opentelemetry_application_insights::Exporter::new("...".into())
    ///     .with_sample_rate(sample_rate);
    /// let provider = sdk::Provider::builder()
    ///     .with_simple_exporter(exporter)
    ///     .with_config(sdk::Config {
    ///         default_sampler: Box::new(sdk::Sampler::Probability(sample_rate)),
    ///         ..Default::default()
    ///     })
    ///     .build();
    /// ```
    pub fn with_sample_rate(mut self, sample_rate: f64) -> Self {
        // Application Insights expects the sample rate as a percentage.
        self.sample_rate = sample_rate * 100.0;
        self
    }

    fn create_envelopes(&self, span: Arc<trace::SpanData>) -> Vec<Envelope> {
        let mut result = Vec::with_capacity(1 + span.message_events.len());
        result.push(match span.span_kind {
            SpanKind::Server | SpanKind::Consumer => {
                let mut data = RequestData {
                    ver: 2,
                    id: span_id_to_string(span.span_context.span_id()),
                    name: Some(span.name.clone()).filter(|x| !x.is_empty()),
                    duration: duration_to_string(
                        span.end_time
                            .duration_since(span.start_time)
                            .unwrap_or(Duration::from_secs(0)),
                    ),
                    response_code: (span.status_code.clone() as i32).to_string(),
                    success: span.status_code == StatusCode::OK,
                    source: None,
                    url: None,
                    properties: None,
                };
                let mut attrs = evictedhashmap_to_hashmap(&span.attributes);
                for (k, v) in span.resource.iter() {
                    attrs.insert(k.as_str(), v);
                }
                let tags = merge_tags(self.common_tags.clone(), get_tags_for_span(&span, &attrs));
                if let Some(method) = attrs.get("http.method") {
                    if let Some(route) = attrs.get("http.route") {
                        data.name = Some(format!(
                            "{} {}",
                            String::from(*method),
                            String::from(*route)
                        ));
                    } else {
                        data.name = Some(String::from(*method));
                    }
                }
                if let Some(status_code) = attrs.get("http.status_code") {
                    data.response_code = String::from(*status_code);
                }
                if let Some(url) = attrs.get("http.url") {
                    data.url = Some(String::from(*url));
                } else if let Some(target) = attrs.get("http.target") {
                    data.url = Some(String::from(*target));
                }
                data.properties = attrs_to_properties(attrs, &self.request_ignored_properties);
                Envelope {
                    name: "Microsoft.ApplicationInsights.Request".into(),
                    time: time_to_string(span.start_time),
                    sample_rate: Some(self.sample_rate),
                    i_key: Some(self.instrumentation_key.clone()),
                    tags: Some(tags),
                    data: Some(Data::Request(data)),
                }
            }
            SpanKind::Client | SpanKind::Producer | SpanKind::Internal => {
                let mut data = RemoteDependencyData {
                    ver: 2,
                    id: Some(span_id_to_string(span.span_context.span_id())),
                    name: span.name.clone(),
                    duration: duration_to_string(
                        span.end_time
                            .duration_since(span.start_time)
                            .unwrap_or(Duration::from_secs(0)),
                    ),
                    result_code: Some((span.status_code.clone() as i32).to_string()),
                    success: Some(span.status_code == StatusCode::OK),
                    data: None,
                    target: None,
                    type_: None,
                    properties: None,
                };
                let mut attrs = evictedhashmap_to_hashmap(&span.attributes);
                for (k, v) in span.resource.iter() {
                    attrs.insert(k.as_str(), v);
                }
                let tags = merge_tags(self.common_tags.clone(), get_tags_for_span(&span, &attrs));
                if let Some(status_code) = attrs.get("http.status_code") {
                    data.result_code = Some(String::from(*status_code));
                }
                if let Some(url) = attrs.get("http.url") {
                    data.data = Some(String::from(*url));
                } else if let Some(statement) = attrs.get("db.statement") {
                    data.data = Some(String::from(*statement));
                }
                if let Some(host) = attrs.get("http.host") {
                    data.target = Some(String::from(*host));
                } else if let Some(peer_name) = attrs.get("net.peer.name") {
                    data.target = Some(String::from(*peer_name));
                } else if let Some(db_instance) = attrs.get("db.instance") {
                    data.target = Some(String::from(*db_instance));
                }
                if span.span_kind == SpanKind::Internal {
                    data.type_ = Some("InProc".into());
                    data.success = Some(true);
                } else if let Some(db_type) = attrs.get("db.type") {
                    data.type_ = Some(String::from(*db_type));
                } else if let Some(messaging_system) = attrs.get("messaging.system") {
                    data.type_ = Some(String::from(*messaging_system));
                } else if attrs.keys().any(|x| x.starts_with("http.")) {
                    data.type_ = Some("HTTP".into());
                } else if attrs.keys().any(|x| x.starts_with("db.")) {
                    data.type_ = Some("DB".into());
                }
                data.properties = attrs_to_properties(attrs, &self.dependency_ignored_properties);
                Envelope {
                    name: "Microsoft.ApplicationInsights.RemoteDependency".into(),
                    time: time_to_string(span.start_time),
                    sample_rate: Some(self.sample_rate),
                    i_key: Some(self.instrumentation_key.clone()),
                    tags: Some(tags),
                    data: Some(Data::RemoteDependency(data)),
                }
            }
        });

        for event in span.message_events.iter() {
            let data = MessageData {
                ver: 2,
                message: if event.name.is_empty() {
                    "<no message>".into()
                } else {
                    event.name.clone()
                },
                properties: Some(
                    event
                        .attributes
                        .iter()
                        .map(|kv| (kv.key.as_str().to_string(), String::from(&kv.value)))
                        .chain(
                            span.resource
                                .iter()
                                .map(|(k, v)| (k.as_str().to_string(), v.into())),
                        )
                        .collect(),
                )
                .filter(|x: &BTreeMap<String, String>| !x.is_empty()),
            };
            result.push(Envelope {
                name: "Microsoft.ApplicationInsights.Message".into(),
                time: time_to_string(event.timestamp),
                sample_rate: Some(self.sample_rate),
                i_key: Some(self.instrumentation_key.clone()),
                tags: Some(merge_tags(
                    self.common_tags.clone(),
                    get_tags_for_event(&span),
                )),
                data: Some(Data::Message(data)),
            });
        }

        result
    }
}

impl trace::SpanExporter for Exporter {
    /// Export spans to Application Insights
    fn export(&self, batch: Vec<Arc<trace::SpanData>>) -> trace::ExportResult {
        let envelopes = batch
            .into_iter()
            .flat_map(|span| self.create_envelopes(span))
            .collect();
        uploader::send(envelopes).into()
    }

    fn shutdown(&self) {}
}

impl From<uploader::Response> for trace::ExportResult {
    fn from(response: uploader::Response) -> trace::ExportResult {
        match response {
            uploader::Response::Success => Self::Success,
            uploader::Response::Retry => Self::FailedRetryable,
            uploader::Response::NoRetry => Self::FailedNotRetryable,
        }
    }
}