Skip to content
This repository was archived by the owner on Apr 2, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
19 changes: 0 additions & 19 deletions core/01_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,6 @@
return ObjectFromEntries(op_resources());
}

function metrics() {
// TODO(mmastrac): we should replace this with a newer API
return {
opsDispatched: 0,
opsDispatchedSync: 0,
opsDispatchedAsync: 0,
opsDispatchedAsyncUnref: 0,
opsCompleted: 0,
opsCompletedSync: 0,
opsCompletedAsync: 0,
opsCompletedAsyncUnref: 0,
bytesSentControl: 0,
bytesSentData: 0,
bytesReceived: 0,
ops: {},
};
}

let reportExceptionCallback = (error) => {
op_dispatch_exception(error, false);
};
Expand Down Expand Up @@ -650,7 +632,6 @@
const core = ObjectAssign(globalThis.Deno.core, {
internalRidSymbol: Symbol("Deno.internal.rid"),
resources,
metrics,
eventLoopTick,
BadResource,
BadResourcePrototype,
Expand Down
2 changes: 0 additions & 2 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ pub use crate::ops_metrics::OpMetricsEvent;
pub use crate::ops_metrics::OpMetricsFactoryFn;
pub use crate::ops_metrics::OpMetricsFn;
pub use crate::ops_metrics::OpMetricsSource;
pub use crate::ops_metrics::OpMetricsSummary;
pub use crate::ops_metrics::OpMetricsSummaryTracker;
pub use crate::path::strip_unc_prefix;
pub use crate::runtime::stats;
pub use crate::runtime::CompiledWasmModuleStore;
Expand Down
76 changes: 0 additions & 76 deletions core/ops_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use crate::ops::OpCtx;
use crate::serde::Serialize;
use crate::OpDecl;
use crate::OpId;
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
use std::rc::Rc;

/// The type of op metrics event.
Expand Down Expand Up @@ -116,76 +113,3 @@ impl OpMetricsSummary {
self.ops_dispatched_async > self.ops_completed_async
}
}

#[derive(Default, Debug)]
pub struct OpMetricsSummaryTracker {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, core.metrics() was the only symbol that used this struct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is actually used by the deno test for leaking ops discovery. This should most likely stay. See runtime/worker.rs in Deno repo.

ops: RefCell<Vec<OpMetricsSummary>>,
}

impl OpMetricsSummaryTracker {
pub fn per_op(&self) -> Ref<'_, Vec<OpMetricsSummary>> {
self.ops.borrow()
}

pub fn aggregate(&self) -> OpMetricsSummary {
let mut sum = OpMetricsSummary::default();

for metrics in self.ops.borrow().iter() {
sum.ops_dispatched_sync += metrics.ops_dispatched_sync;
sum.ops_dispatched_fast += metrics.ops_dispatched_fast;
sum.ops_dispatched_async += metrics.ops_dispatched_async;
sum.ops_completed_async += metrics.ops_completed_async;
}

sum
}

#[inline]
fn metrics_mut(&self, id: OpId) -> RefMut<OpMetricsSummary> {
RefMut::map(self.ops.borrow_mut(), |ops| &mut ops[id as usize])
}

/// Returns a [`OpMetricsFn`] for this tracker.
fn op_metrics_fn(self: Rc<Self>) -> OpMetricsFn {
Rc::new(move |ctx, event, source| match event {
OpMetricsEvent::Dispatched => {
let mut m = self.metrics_mut(ctx.id);
if source == OpMetricsSource::Fast {
m.ops_dispatched_fast += 1;
}
if ctx.decl.is_async {
m.ops_dispatched_async += 1;
} else {
m.ops_dispatched_sync += 1;
}
}
OpMetricsEvent::Completed
| OpMetricsEvent::Error
| OpMetricsEvent::CompletedAsync
| OpMetricsEvent::ErrorAsync => {
if ctx.decl.is_async {
self.metrics_mut(ctx.id).ops_completed_async += 1;
}
}
})
}

/// Retrieves the metrics factory function for this tracker.
pub fn op_metrics_factory_fn(
self: Rc<Self>,
op_enabled: impl Fn(&OpDecl) -> bool + 'static,
) -> OpMetricsFactoryFn {
Box::new(move |_, total, op| {
let mut ops = self.ops.borrow_mut();
if ops.capacity() == 0 {
ops.reserve_exact(total);
}
ops.push(OpMetricsSummary::default());
if op_enabled(op) {
Some(self.clone().op_metrics_fn())
} else {
None
}
})
}
}
55 changes: 0 additions & 55 deletions core/runtime/tests/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,58 +685,3 @@ op_async_arg_error Dispatched
op_async_arg_error Error"#
);
}

#[tokio::test]
pub async fn test_op_metrics_summary_tracker() {
let tracker = Rc::new(OpMetricsSummaryTracker::default());
// We want to limit the tracker to just the ops we care about
let op_enabled = |op: &OpDecl| {
op.name.starts_with("op_async") || op.name.starts_with("op_sync")
};
let mut runtime = JsRuntime::new(RuntimeOptions {
extensions: vec![test_ext::init_ops()],
op_metrics_factory_fn: Some(
tracker.clone().op_metrics_factory_fn(op_enabled),
),
..Default::default()
});

let promise = runtime
.execute_script(
"filename.js",
r#"
const { op_sync, op_sync_error, op_async, op_async_error, op_async_yield, op_async_yield_error, op_async_deferred, op_async_lazy, op_async_impl_future_error, op_sync_arg_error, op_async_arg_error } = Deno.core.ops;
async function go() {
op_sync();
try { op_sync_error(); } catch {}
await op_async();
try { await op_async_error() } catch {}
await op_async_yield();
try { await op_async_yield_error() } catch {}
await op_async_deferred();
await op_async_lazy();
try { await op_async_impl_future_error() } catch {}
try { op_sync_arg_error() } catch {}
try { await op_async_arg_error() } catch {}
}

go()
"#,
)
.unwrap();
#[allow(deprecated)]
runtime
.resolve_value(promise)
.await
.expect("Failed to await promise");
drop(runtime);
assert_eq!(
tracker.aggregate(),
OpMetricsSummary {
ops_completed_async: 8,
ops_dispatched_async: 8,
ops_dispatched_sync: 3,
ops_dispatched_fast: 0,
}
);
}