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

Commit

Permalink
Creating CustomMetrics for Rust CustomEvents (#3188)
Browse files Browse the repository at this point in the history
* Adding initial metric.

* Syntax.

* syntax.

* Trying something else.

* Playing around with new metric function:
:

* Trying new format

* Fixing arguments.

* Importing metrics

* Reverting to events

* Removing

* Adding.

* Changing to int.

* Changing back to float.

* Adding metric lines for all events.

* trying to set.

* Fixing.

* Adding copy.

* Was this a problem..

* Adding different.

* Solution for all.

* Another.

* removing

* Resolving.
  • Loading branch information
nharper285 authored Jun 21, 2023
1 parent fda68ac commit 7716a81
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
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

0 comments on commit 7716a81

Please sign in to comment.