refactor: split metrics into sync and async functions
@@ -140,29 +140,26 @@ lazy_static! {
///
/// This function needs to run in the context of a Tokio runtime.
pub fn send_metrics(event: MetricEvent) {
- let do_not_track = var("PACT_DO_NOT_TRACK")
- .or_else(|_| var("pact_do_not_track"))
- .map(|v| v == "true")
- .unwrap_or(false);
-
- if do_not_track {
- debug!("'PACT_DO_NOT_TRACK' environment variable is set, will not send metrics");
- } else {
match tokio::runtime::Handle::try_current() {
Ok(handle) => {
- let mut guard = WARNING_LOGGED.lock().unwrap();
- let warning_logged = (*guard).get_mut();
- if *warning_logged == false {
- warn!(
- "\n\nPlease note:\n\
- We are tracking events anonymously to gather important usage statistics like Pact version \
- and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment \
- variable to 'true'.\n\n"
- );
- *warning_logged = true;
+ handle.spawn(async move {
+ send_metrics_async(event).await
+ });
+ },
+ Err(err) => {
+ debug!("Could not get the tokio runtime, will not send metrics - {}", err)
+ }
+ }
}
- handle.spawn(async move {
+/// This sends anonymous metrics to a Google Analytics account. It is used to track usage of
+/// Pact library and operating system versions. This can be disabled by setting the
+/// `pact_do_not_track` environment variable to `true`.
+pub async fn send_metrics_async(event: MetricEvent) {
+ if do_not_track() {
+ debug!("'PACT_DO_NOT_TRACK' environment variable is set, will not send metrics");
+ } else {
+ log_warning();
let ci_context = if CIS.iter()
.any(|n| var(n).map(|val| !val.is_empty()).unwrap_or(false)) {
"CI"
@@ -200,13 +197,28 @@ pub fn send_metrics(event: MetricEvent) {
if let Err(err) = result {
debug!("Failed to post event - {}", err);
}
- });
- },
- Err(err) => {
- debug!("Could not get the tokio runtime, will not send metrics - {}", err)
}
}
+
+fn log_warning() {
+ let mut guard = WARNING_LOGGED.lock().unwrap();
+ let warning_logged = (*guard).get_mut();
+ if *warning_logged == false {
+ warn!(
+ "\n\nPlease note:\n\
+ We are tracking events anonymously to gather important usage statistics like Pact version \
+ and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment \
+ variable to 'true'.\n\n"
+ );
+ *warning_logged = true;
+ }
}
+
+fn do_not_track() -> bool {
+ var("PACT_DO_NOT_TRACK")
+ .or_else(|_| var("pact_do_not_track"))
+ .map(|v| v == "true")
+ .unwrap_or(false)
}
/// Calculates a one-way hash of the hostname where the event occurred
