-
Notifications
You must be signed in to change notification settings - Fork 4
feat: unconditionally send off a final report on LocalReporter drop #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
d4b6593
4d59b7d
d6a2538
aa68241
6000da8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
|
|
||
| use async_trait::async_trait; | ||
| use chrono::SecondsFormat; | ||
| use std::path::PathBuf; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::time::SystemTime; | ||
| use thiserror::Error; | ||
|
|
||
|
|
@@ -61,18 +61,22 @@ impl LocalReporter { | |
| } | ||
| } | ||
|
|
||
| fn jfr_file_name() -> String { | ||
| let time: chrono::DateTime<chrono::Utc> = SystemTime::now().into(); | ||
| let time = time | ||
| .to_rfc3339_opts(SecondsFormat::Secs, true) | ||
| .replace(":", "-"); | ||
| format!("{time}.jfr") | ||
| } | ||
|
|
||
| /// Writes the jfr file to disk. | ||
| async fn report_profiling_data( | ||
| &self, | ||
| jfr: Vec<u8>, | ||
| _metadata_obj: &ReportMetadata<'_>, | ||
| ) -> Result<(), std::io::Error> { | ||
| let time: chrono::DateTime<chrono::Utc> = SystemTime::now().into(); | ||
| let time = time | ||
| .to_rfc3339_opts(SecondsFormat::Secs, true) | ||
| .replace(":", "-"); | ||
| tracing::debug!("reporting {time}.jfr"); | ||
| let file_name = format!("{time}.jfr"); | ||
| let file_name = Self::jfr_file_name(); | ||
| tracing::debug!("reporting {file_name}"); | ||
| tokio::fs::write(self.directory.join(file_name), jfr).await?; | ||
| Ok(()) | ||
| } | ||
|
|
@@ -89,6 +93,18 @@ impl Reporter for LocalReporter { | |
| .await | ||
| .map_err(|e| Box::new(LocalReporterError::IoError(e)) as _) | ||
| } | ||
|
|
||
| fn report_blocking( | ||
| &self, | ||
| jfr_path: &Path, | ||
| _metadata: &ReportMetadata, | ||
| ) -> Result<(), Box<dyn std::error::Error + Send>> { | ||
| let file_name = Self::jfr_file_name(); | ||
| tracing::debug!("reporting {file_name} (blocking)"); | ||
| std::fs::copy(jfr_path, self.directory.join(file_name)) | ||
| .map(|_| ()) | ||
| .map_err(|e| Box::new(LocalReporterError::IoError(e)) as _) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -120,4 +136,25 @@ mod test { | |
| .unwrap(); | ||
| assert_eq!(tokio::fs::read(jfr_file.path()).await.unwrap(), b"JFR"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_local_reporter_reports_on_drop() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let src = dir.path().join("input.jfr"); | ||
| std::fs::write(&src, b"JFR-DROP").unwrap(); | ||
| let out_dir = tempfile::tempdir().unwrap(); | ||
| let reporter = LocalReporter::new(out_dir.path()); | ||
| reporter.report_blocking(&src, &DUMMY_METADATA).unwrap(); | ||
| let jfr_file = std::fs::read_dir(out_dir.path()) | ||
| .unwrap() | ||
| .flat_map(|f| f.ok()) | ||
| .filter(|f| { | ||
| Path::new(&f.file_name()) | ||
| .extension() | ||
| .is_some_and(|e| e == "jfr") | ||
| }) | ||
| .next() | ||
| .unwrap(); | ||
| assert_eq!(std::fs::read(jfr_file.path()).unwrap(), b"JFR-DROP"); | ||
|
Comment on lines
+150
to
+165
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this test testing? It does not seem to actually test that dropping the local reporter does something. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| //! 3. [multi::MultiReporter], which allows combining multiple reporters. | ||
|
|
||
| use std::fmt; | ||
| use std::path::Path; | ||
|
|
||
| use async_trait::async_trait; | ||
|
|
||
|
|
@@ -38,4 +39,22 @@ pub trait Reporter: fmt::Debug { | |
| jfr: Vec<u8>, | ||
| metadata: &ReportMetadata, | ||
| ) -> Result<(), Box<dyn std::error::Error + Send>>; | ||
|
|
||
| /// Synchronously report profiling data. Called during drop when the | ||
| /// async runtime is shutting down and async reporting is not possible. | ||
| /// | ||
| /// The default implementation does nothing. Reporters that can perform | ||
| /// synchronous I/O (like [`local::LocalReporter`]) should override this. | ||
| #[doc(hidden)] | ||
| fn report_blocking( | ||
| &self, | ||
| _jfr_path: &Path, | ||
| _metadata: &ReportMetadata, | ||
| ) -> Result<(), Box<dyn std::error::Error + Send>> { | ||
| tracing::info!( | ||
| "reporter does not support synchronous reporting, last sample will be lost. \ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that this message is only reached if a sample is actually lost
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this say |
||
| Add a call to `RunningProfiler::stop` to wait for the upload to finish." | ||
| ); | ||
| Ok(()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since that is doc hidden do we still want to say this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should clarify that this only applies to the local reporter. I think "The local reporter will flush its contents on drop. For other reporters, you must call ..."