-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Can currently set the `telemetry` flag. - Telemetry status is stored in `MULTIRUST_HOME/telemetry` - Adds routing for telemetry for rustc calls and target add - Provides basic command proxying for telemetry
- Loading branch information
Showing
12 changed files
with
298 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use std; | ||
use std::env; | ||
use std::ffi::OsStr; | ||
use std::io::{self, Write}; | ||
use std::path::PathBuf; | ||
use std::process::{self, Command, Output}; | ||
use std::time::Instant; | ||
|
||
use Cfg; | ||
use errors::*; | ||
use multirust_utils; | ||
use telemetry; | ||
use telemetry::TelemetryEvent; | ||
|
||
|
||
pub fn run_command_for_dir<S: AsRef<OsStr>>(cmd: Command, | ||
args: &[S], | ||
cfg: &Cfg) -> Result<()> { | ||
let arg0 = env::args().next().map(|a| PathBuf::from(a)); | ||
let arg0 = arg0.as_ref() | ||
.and_then(|a| a.file_name()) | ||
.and_then(|a| a.to_str()); | ||
let arg0 = try!(arg0.ok_or(Error::NoExeName)); | ||
if (arg0 == "rustc" || arg0 == "rustc.exe") && cfg.telemetry_enabled() { | ||
return telemetry_rustc(cmd, &args, &cfg); | ||
} | ||
|
||
run_command_for_dir_without_telemetry(cmd, &args) | ||
} | ||
|
||
fn telemetry_rustc<S: AsRef<OsStr>>(cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> { | ||
let now = Instant::now(); | ||
|
||
let output = bare_run_command_for_dir(cmd, &args); | ||
|
||
let duration = now.elapsed(); | ||
|
||
let ms = (duration.as_secs() as u64 * 1000) + (duration.subsec_nanos() as u64 / 1000 / 1000); | ||
|
||
match output { | ||
Ok(out) => { | ||
let exit_code = out.status.code().unwrap_or(1); | ||
|
||
let errors = match out.status.success() { | ||
true => None, | ||
_ => Some(String::from_utf8_lossy(&out.stderr).to_string()), | ||
}; | ||
|
||
let _ = io::stdout().write(&out.stdout); | ||
let _ = io::stdout().flush(); | ||
let _ = io::stderr().write(&out.stderr); | ||
let _ = io::stderr().flush(); | ||
|
||
let te = TelemetryEvent::RustcRun { duration_ms: ms, | ||
exit_code: exit_code, | ||
errors: errors }; | ||
telemetry::log_telemetry(te, cfg); | ||
process::exit(exit_code); | ||
}, | ||
Err(e) => { | ||
let exit_code = e.raw_os_error().unwrap_or(1); | ||
let te = TelemetryEvent::RustcRun { duration_ms: ms, | ||
exit_code: exit_code, | ||
errors: Some(format!("{}", e)) }; | ||
telemetry::log_telemetry(te, cfg); | ||
Err(multirust_utils::Error::RunningCommand { | ||
name: args[0].as_ref().to_owned(), | ||
error: multirust_utils::raw::CommandError::Io(e), | ||
}.into()) | ||
}, | ||
} | ||
} | ||
|
||
fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(cmd: Command, args: &[S]) -> Result<()> { | ||
let output = bare_run_command_for_dir(cmd, &args); | ||
|
||
match output { | ||
Ok(out) => { | ||
let _ = io::stdout().write(&out.stdout); | ||
let _ = io::stdout().flush(); | ||
let _ = io::stderr().write(&out.stderr); | ||
let _ = io::stderr().flush(); | ||
|
||
let status = out.status; | ||
// Ensure correct exit code is returned | ||
let code = status.code().unwrap_or(1); | ||
process::exit(code); | ||
} | ||
Err(e) => { | ||
Err(multirust_utils::Error::RunningCommand { | ||
name: args[0].as_ref().to_owned(), | ||
error: multirust_utils::raw::CommandError::Io(e), | ||
}.into()) | ||
} | ||
} | ||
} | ||
|
||
fn bare_run_command_for_dir<S: AsRef<OsStr>>(mut cmd: Command, args: &[S]) -> std::result::Result<Output, std::io::Error> { | ||
cmd.args(&args[1..]); | ||
|
||
// FIXME rust-lang/rust#32254. It's not clear to me | ||
// when and why this is needed. | ||
cmd.stdin(process::Stdio::inherit()); | ||
|
||
cmd.output() | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use time; | ||
use config::Cfg; | ||
use multirust_utils::utils; | ||
use rustc_serialize::json; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum TelemetryMode { | ||
On, | ||
Off, | ||
} | ||
|
||
#[derive(RustcDecodable, RustcEncodable, Debug)] | ||
pub enum TelemetryEvent { | ||
RustcRun { duration_ms: u64, exit_code: i32, errors: Option<String> }, | ||
ToolchainUpdate { toolchain: String, success: bool } , | ||
TargetAdd { toolchain: String, target: String, success: bool }, | ||
} | ||
|
||
#[derive(RustcDecodable, RustcEncodable, Debug)] | ||
struct LogMessage { | ||
log_time_s: i64, | ||
event: TelemetryEvent, | ||
} | ||
|
||
pub fn log_telemetry(event: TelemetryEvent, cfg: &Cfg) { | ||
let ln = LogMessage { log_time_s: time::get_time().sec, event: event }; | ||
|
||
let json = json::encode(&ln).unwrap(); | ||
|
||
let now = time::now_utc(); | ||
let filename = format!("telemetry-{}-{:02}-{:02}", now.tm_year + 1900, now.tm_mon, now.tm_mday); | ||
|
||
let _ = utils::append_file("telemetry", &cfg.multirust_dir.join(&filename), &json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.