diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index 536a6bdf..7570e6b6 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -62,7 +62,10 @@ impl NewContext { .wrap_err_with(|| format!("Failed to write to file: {}", new_file_path.display()))?; } - track_request(); + let _detached_thread_handle = std::thread::Builder::new() + .spawn(mixpanel_tracking::track_usage) + .unwrap(); + execute_git_commands(project_dir)?; println!("New project is created at '{}'.\n", project_dir.display()); @@ -81,14 +84,6 @@ impl NewContext { } } -#[tracing::instrument(target = "tracing_instrument", name = "Sending a tracking request ...")] -fn track_request() { - tracing::Span::current(); - let _detached_thread_handle = std::thread::Builder::new() - .spawn(mixpanel_tracking::track_usage) - .unwrap(); -} - #[tracing::instrument( target = "tracing_instrument", name = "The process of executing", diff --git a/cargo-near/src/mixpanel_tracking.rs b/cargo-near/src/mixpanel_tracking.rs index 93302e64..b706b854 100644 --- a/cargo-near/src/mixpanel_tracking.rs +++ b/cargo-near/src/mixpanel_tracking.rs @@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine as _}; use reqwest::{header::HeaderMap, Client}; use serde::Serialize; use std::{env, str}; -use tracing::debug; +use tracing_indicatif::span_ext::IndicatifSpanExt; const SEND_TRACKING_REQUEST_ERROR: &str = "Can't send tracking usage event"; @@ -19,7 +19,14 @@ struct TrackingData { properties: MixpanelProperties, } +#[tracing::instrument( + target = "tracing_instrument", + name = "Sending a tracking request using Mixpanel via url" +)] pub(crate) fn track_usage() { + tracing::Span::current().pb_set_message("https://api.mixpanel.com/track ..."); + tracing::info!(target: "near_teach_me", "https://api.mixpanel.com/track ..."); + let properties = MixpanelProperties { token: "24177ef1ec09ffea5cb6f68909c66a61".to_string(), pkg_version: env!("CARGO_PKG_VERSION").to_string(), @@ -34,6 +41,26 @@ pub(crate) fn track_usage() { let client = Client::new(); + let request_payload = serde_json::json!({ + "base64_encoded_tracking_data": base64_encoded_data + }); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "I am making HTTP call to broadcast the tracking data, learn more https://mixpanel.com/" + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "HTTP GET https://api.mixpanel.com/track", + ); + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Body:\n{}", + near_cli_rs::common::indent_payload(&format!("{:#}", request_payload)) + ); + let mut headers = HeaderMap::new(); headers.insert("accept", "text/plain".parse().unwrap()); headers.insert("content-type", "application/json".parse().unwrap()); @@ -43,13 +70,27 @@ pub(crate) fn track_usage() { .block_on( client .get("https://api.mixpanel.com/track") - .query(&[("data", base64_encoded_data)]) - .headers(headers) + .query(&[("data", &base64_encoded_data)]) + .headers(headers.clone()) .send(), ) + .inspect(|response| { + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON Response:\n{}", + near_cli_rs::common::indent_payload(&format!("{:#?}", response)) + ); + }) .is_err() { - debug!(SEND_TRACKING_REQUEST_ERROR) + tracing::info!( + target: "near_teach_me", + parent: &tracing::Span::none(), + "JSON RPC Response:\n{}", + near_cli_rs::common::indent_payload(SEND_TRACKING_REQUEST_ERROR) + ); + tracing::debug!(SEND_TRACKING_REQUEST_ERROR) } }