Skip to content

Commit 0491b5e

Browse files
Add spacetime_worker_reducer_returned_errors_total metric (#3613)
# Description of Changes Currently, we have a metric for reducer panics called `spacetime_worker_wasm_instance_errors_total`. This commit adds a metric for tracking errors returned from the module, like for example an Err result in Rust, or throwing a SenderError in TypeScript This will be needed for the web database overview dashboard, but it should be useful for tracking sender errors in general. # Expected complexity level and risk 1 # Testing - [x] I've tested the change locally verifying the counter increments when an instance returns an error --------- Co-authored-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
1 parent 827bb9b commit 0491b5e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use spacetimedb_lib::buffer::DecodeError;
3737
use spacetimedb_lib::db::raw_def::v9::Lifecycle;
3838
use spacetimedb_lib::de::DeserializeSeed;
3939
use spacetimedb_lib::identity::AuthCtx;
40-
use spacetimedb_lib::{bsatn, ConnectionId, RawModuleDef, Timestamp};
40+
use spacetimedb_lib::{bsatn, ConnectionId, Hash, RawModuleDef, Timestamp};
4141
use spacetimedb_primitives::{ProcedureId, TableId, ViewFnPtr, ViewId};
4242
use spacetimedb_schema::auto_migrate::{MigratePlan, MigrationPolicy, MigrationPolicyError};
4343
use spacetimedb_schema::def::{ModuleDef, ViewDef};
@@ -745,7 +745,13 @@ impl InstanceCommon {
745745
self.handle_outer_error(&result.stats.energy, reducer_name)
746746
}
747747
Err(ExecutionError::User(err)) => {
748-
log_reducer_error(inst.replica_ctx(), timestamp, reducer_name, &err);
748+
log_reducer_error(
749+
inst.replica_ctx(),
750+
timestamp,
751+
reducer_name,
752+
&err,
753+
&self.info.module_hash,
754+
);
749755
EventStatus::Failed(err.into())
750756
}
751757
// We haven't actually committed yet - `commit_and_broadcast_event` will commit
@@ -763,7 +769,13 @@ impl InstanceCommon {
763769
Ok(()) => EventStatus::Committed(DatabaseUpdate::default()),
764770
Err(err) => {
765771
let err = err.to_string();
766-
log_reducer_error(inst.replica_ctx(), timestamp, reducer_name, &err);
772+
log_reducer_error(
773+
inst.replica_ctx(),
774+
timestamp,
775+
reducer_name,
776+
&err,
777+
&self.info.module_hash,
778+
);
767779
EventStatus::Failed(err)
768780
}
769781
}
@@ -1161,9 +1173,20 @@ fn maybe_log_long_running_function(reducer_name: &str, total_duration: Duration)
11611173
}
11621174

11631175
/// Logs an error `message` for `reducer` at `timestamp` into `replica_ctx`.
1164-
fn log_reducer_error(replica_ctx: &ReplicaContext, timestamp: Timestamp, reducer: &str, message: &str) {
1176+
fn log_reducer_error(
1177+
replica_ctx: &ReplicaContext,
1178+
timestamp: Timestamp,
1179+
reducer: &str,
1180+
message: &str,
1181+
module_hash: &Hash,
1182+
) {
11651183
use database_logger::Record;
11661184

1185+
WORKER_METRICS
1186+
.sender_errors
1187+
.with_label_values(&replica_ctx.database_identity, module_hash, reducer)
1188+
.inc();
1189+
11671190
log::info!("reducer returned error: {message}");
11681191

11691192
let record = Record {

crates/core/src/worker_metrics/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ metrics_group!(
246246
#[labels(database_identity: Identity, module_hash: Hash, reducer_symbol: str)]
247247
pub wasm_instance_errors: IntCounterVec,
248248

249+
#[name = spacetime_worker_sender_errors_total]
250+
#[help = "The number of sender errors returned from reducers."]
251+
#[labels(database_identity: Identity, module_hash: Hash, reducer_symbol: str)]
252+
pub sender_errors: IntCounterVec,
253+
249254
#[name = spacetime_worker_wasm_memory_bytes]
250255
#[help = "The number of bytes of linear memory allocated by the database's WASM module instance"]
251256
#[labels(database_identity: Identity)]

0 commit comments

Comments
 (0)