Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Creating CustomMetrics for Rust CustomEvents #3188

Merged
merged 33 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
22a57a3
Adding initial metric.
nharper285 Jun 13, 2023
f7edf64
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 13, 2023
e8a0770
Syntax.
nharper285 Jun 13, 2023
5b00931
Merge branch 'user/noharper/rust-metrics' of https://github.com/nharp…
nharper285 Jun 13, 2023
780db08
syntax.
nharper285 Jun 13, 2023
b4020f4
Trying something else.
nharper285 Jun 13, 2023
fd3ca48
Playing around with new metric function:
nharper285 Jun 13, 2023
dd80ab6
Trying new format
nharper285 Jun 15, 2023
8be6915
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 15, 2023
65189fd
Fixing arguments.
nharper285 Jun 15, 2023
9732ce0
Merge branch 'user/noharper/rust-metrics' of https://github.com/nharp…
nharper285 Jun 15, 2023
f977d12
Importing metrics
nharper285 Jun 15, 2023
758bff3
Reverting to events
nharper285 Jun 15, 2023
7ff201c
Removing
nharper285 Jun 15, 2023
013d3f7
Adding.
nharper285 Jun 15, 2023
c65a786
Changing to int.
nharper285 Jun 15, 2023
781f1c5
Changing back to float.
nharper285 Jun 15, 2023
dca5e97
Adding metric lines for all events.
nharper285 Jun 16, 2023
b57c4e1
trying to set.
nharper285 Jun 16, 2023
cf2ed19
Fixing.
nharper285 Jun 16, 2023
1e7d8da
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 16, 2023
3fb5682
Adding copy.
nharper285 Jun 16, 2023
58baf0d
Merge branch 'user/noharper/rust-metrics' of https://github.com/nharp…
nharper285 Jun 16, 2023
c152084
Was this a problem..
nharper285 Jun 16, 2023
f0cbea6
Adding different.
nharper285 Jun 16, 2023
5a46e64
Solution for all.
nharper285 Jun 16, 2023
af0e342
Another.
nharper285 Jun 16, 2023
6e707f8
removing
nharper285 Jun 16, 2023
a4c9996
Resolving.
nharper285 Jun 19, 2023
5aa54a9
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 19, 2023
91c5500
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 20, 2023
f545624
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 20, 2023
49be939
Merge branch 'main' into user/noharper/rust-metrics
nharper285 Jun 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/agent/onefuzz-task/src/tasks/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,15 @@ impl Config {
match self {
Config::GenericGenerator(c) => {
event!(task_start; EventData::Type = event_type, EventData::ToolName = c.generator_exe.clone());
metric!(task_start; 1.0; EventData::Type = event_type, EventData::ToolName = c.generator_exe.clone());
}
Config::GenericAnalysis(c) => {
event!(task_start; EventData::Type = event_type, EventData::ToolName = c.analyzer_exe.clone());
metric!(task_start; 1.0; EventData::Type = event_type, EventData::ToolName = c.analyzer_exe.clone());
}
_ => {
event!(task_start; EventData::Type = event_type);
metric!(task_start; 1.0; EventData::Type = event_type);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/agent/onefuzz-task/src/tasks/coverage/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ impl<'a> TaskContext<'a> {
if entry.file_type().await?.is_file() {
if let Err(e) = self.record_input(&entry.path()).await {
event!(coverage_failed; EventData::Path = entry.path().display().to_string());
metric!(coverage_failed; 1.0; EventData::Path = entry.path().display().to_string());
warn!(
"ignoring error recording coverage for input: {}, error: {}",
entry.path().display(),
Expand Down Expand Up @@ -407,6 +408,7 @@ impl<'a> TaskContext<'a> {

let s = CoverageStats::new(&self.coverage);
event!(coverage_data; Covered = s.covered, Features = s.features, Rate = s.rate);
metric!(coverage_data; 1.0; Covered = s.covered, Features = s.features, Rate = s.rate);

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions src/agent/onefuzz-task/src/tasks/fuzz/libfuzzer/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ impl TotalStats {
EventData::Count = self.count,
EventData::ExecsSecond = self.execs_sec
);
metric!(
runtime_stats;
1.0;
EventData::Count = self.count,
EventData::ExecsSecond = self.execs_sec
);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/agent/onefuzz-task/src/tasks/report/crash_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ impl RegressionReport {
};

if upload_or_save_local(&self, &name, regression_reports).await? {
event!(event; EventData::Path = name);
event!(event; EventData::Path = name.clone());
metric!(event; 1.0; EventData::Path = name.clone());
}
Ok(())
}
Expand Down Expand Up @@ -155,14 +156,16 @@ impl CrashTestResult {
if let Some(unique_reports) = unique_reports {
let name = report.unique_blob_name();
if upload_or_save_local(&report, &name, unique_reports).await? {
event!(new_unique_report; EventData::Path = name);
event!(new_unique_report; EventData::Path = report.unique_blob_name());
metric!(new_unique_report; 1.0; EventData::Path = report.unique_blob_name());
}
}

if let Some(reports) = reports {
let name = report.blob_name();
if upload_or_save_local(&report, &name, reports).await? {
event!(new_report; EventData::Path = name);
event!(new_report; EventData::Path = report.blob_name());
metric!(new_report; 1.0; EventData::Path = report.blob_name());
}
}
}
Expand All @@ -171,7 +174,8 @@ impl CrashTestResult {
if let Some(no_repro) = no_repro {
let name = report.blob_name();
if upload_or_save_local(&report, &name, no_repro).await? {
event!(new_unable_to_reproduce; EventData::Path = name);
event!(new_unable_to_reproduce; EventData::Path = report.blob_name());
metric!(new_unable_to_reproduce; 1.0; EventData::Path = report.blob_name());
}
}
}
Expand Down
56 changes: 48 additions & 8 deletions src/agent/onefuzz-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,33 @@ pub fn track_event(event: &Event, properties: &[EventData]) {
try_broadcast_event(chrono::Utc::now(), event, properties);
}

pub fn track_metric(metric: &Event, value: f64, properties: &[EventData]) {
use appinsights::telemetry::Telemetry;

if let Some(client) = client(ClientType::Instance) {
let mut mtr = appinsights::telemetry::MetricTelemetry::new(metric.as_str(), value);
let props = mtr.properties_mut();
for property in properties {
let (name, val) = property.as_values();
props.insert(name.to_string(), val);
}
client.track(mtr);
}

if let Some(client) = client(ClientType::Microsoft) {
let mut mtr = appinsights::telemetry::MetricTelemetry::new(metric.as_str(), value);
let props = mtr.properties_mut();

for property in properties {
if property.can_share_with_microsoft() {
let (name, val) = property.as_values();
props.insert(name.to_string(), val);
}
}
client.track(mtr);
}
}

pub fn to_log_level(level: &appinsights::telemetry::SeverityLevel) -> log::Level {
match level {
Verbose => log::Level::Debug,
Expand Down Expand Up @@ -551,6 +578,27 @@ macro_rules! event {
}};
}

#[macro_export]
macro_rules! log_metrics {
($name: expr; $value: expr; $metrics: expr) => {{
onefuzz_telemetry::track_metric(&$name, $value, &$metrics);
}};
}

#[macro_export]
macro_rules! metric {
($name: expr ; $value: expr ; $($k: path = $v: expr),*) => {{
let mut metrics = Vec::new();

$({
metrics.push($k(From::from($v)));

})*;

log_metrics!($name; $value; metrics);
}};
}

#[macro_export]
macro_rules! log {
($level: expr, $($arg: tt)+) => {{
Expand Down Expand Up @@ -597,11 +645,3 @@ macro_rules! critical {
onefuzz_telemetry::log!(onefuzz_telemetry::Critical, $($arg)+);
}}
}

#[macro_export]
macro_rules! metric {
($name: expr, $value: expr) => {{
let client = onefuzz_telemetry::client(onefuzz_telemetry::ClientType::Instance);
client.track_metric($name.into(), $value);
}};
}
20 changes: 12 additions & 8 deletions src/agent/onefuzz/src/syncdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,21 @@ impl SyncedDir {
let file_name = item
.file_name()
.ok_or_else(|| anyhow!("invalid file path"))?;
let file_name_str = file_name.to_string_lossy();
let file_name_event_str = file_name.to_string_lossy();
let file_name_str_metric_str = file_name.to_string_lossy();

// explicitly ignore azcopy temporary files
// https://github.com/Azure/azure-storage-azcopy/blob/main/ste/xfer-remoteToLocal-file.go#L35
if file_name_str.starts_with(".azDownload-") {
if file_name_event_str.starts_with(".azDownload-") {
continue;
}

if ignore_dotfiles && file_name_str.starts_with('.') {
if ignore_dotfiles && file_name_event_str.starts_with('.') {
continue;
}

event!(event.clone(); EventData::Path = file_name_str);
event!(event.clone(); EventData::Path = file_name_event_str);
metric!(event.clone(); 1.0; EventData::Path = file_name_str_metric_str);
let destination = path.join(file_name);
if let Err(err) = fs::copy(&item, &destination).await {
let error_message = format!(
Expand All @@ -288,19 +290,21 @@ impl SyncedDir {
let file_name = item
.file_name()
.ok_or_else(|| anyhow!("invalid file path"))?;
let file_name_str = file_name.to_string_lossy();
let file_name_event_str = file_name.to_string_lossy();
let file_name_str_metric_str = file_name.to_string_lossy();

// explicitly ignore azcopy temporary files
// https://github.com/Azure/azure-storage-azcopy/blob/main/ste/xfer-remoteToLocal-file.go#L35
if file_name_str.starts_with(".azDownload-") {
if file_name_event_str.starts_with(".azDownload-") {
continue;
}

if ignore_dotfiles && file_name_str.starts_with('.') {
if ignore_dotfiles && file_name_event_str.starts_with('.') {
continue;
}

event!(event.clone(); EventData::Path = file_name_str);
event!(event.clone(); EventData::Path = file_name_event_str);
metric!(event.clone(); 1.0; EventData::Path = file_name_str_metric_str);
if let Err(err) = uploader.upload(item.clone()).await {
let error_message = format!(
"Couldn't upload file. path:{} dir:{} err:{:?}",
Expand Down