Skip to content

Commit

Permalink
fix(core): 🐞 fix prometheus exporter
Browse files Browse the repository at this point in the history
the prometheus CounterVec definition is inconstent with the implementation

Close #111
  • Loading branch information
Forsworns committed May 28, 2023
1 parent 8629645 commit 520b6b6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
24 changes: 12 additions & 12 deletions sentinel-core/src/core/base/slot_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ pub trait BaseSlot: Any + AsAny + Sync + Send {
/// StatPrepareSlot is responsible for some preparation before statistic
/// For example: init structure and so on
pub trait StatPrepareSlot: BaseSlot {
/// prepare fntion do some initialization
/// prepare function do some initialization
/// Such as: init statistic structure、node and etc
/// The result of preparing would store in EntryContext
/// All StatPrepareSlots execute in sequence
/// prepare fntion should not throw panic.
/// prepare function should not throw panic.
fn prepare(&self, _ctx: &mut EntryContext) {}
}

/// RuleCheckSlot is rule based checking strategy
/// All checking rule must implement this interface.
pub trait RuleCheckSlot: BaseSlot {
// check fntion do some validation
// check function do some validation
// It can break off the slot pipeline
// Each TokenResult will return check result
// The upper logic will control pipeline according to SlotResult.
Expand All @@ -42,17 +42,17 @@ pub trait RuleCheckSlot: BaseSlot {
/// StatSlot is responsible for counting all custom biz metrics.
/// StatSlot would not handle any panic, and pass up all panic to slot chain
pub trait StatSlot: BaseSlot {
/// OnEntryPass fntion will be invoked when StatPrepareSlots and RuleCheckSlots execute pass
/// OnEntryPass function will be invoked when StatPrepareSlots and RuleCheckSlots execute pass
/// StatSlots will do some statistic logic, such as QPS、log、etc
fn on_entry_pass(&self, _ctx: &EntryContext) {}
/// on_entry_blocked fntion will be invoked when StatPrepareSlots and RuleCheckSlots fail to execute
/// on_entry_blocked function will be invoked when StatPrepareSlots and RuleCheckSlots fail to execute
/// It may be inbound flow control or outbound cir
/// StatSlots will do some statistic logic, such as QPS、log、etc
/// blockError introduce the block detail
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: Option<BlockError>) {}
/// on_completed fntion will be invoked when chain exits.
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: BlockError) {}
/// on_completed function will be invoked when chain exits.
/// The semantics of on_completed is the entry passed and completed
/// Note: blocked entry will not call this fntion
/// Note: blocked entry will not call this function
fn on_completed(&self, _ctx: &mut EntryContext) {}
}

Expand Down Expand Up @@ -142,9 +142,9 @@ impl SlotChain {
// indicate the result of rule based checking slot.
if ctx.result().is_pass() {
s.on_entry_pass(&*ctx) // Rc/Arc clone
} else {
// The block error should not be nil.
s.on_entry_blocked(&*ctx, ctx.result().block_err()) // Rc/Arc clone
} else if ctx.result().is_blocked() {
// The block error should not be none.
s.on_entry_blocked(&*ctx, ctx.result().block_err().unwrap()) // Rc/Arc clone
}
}
ctx.result().clone()
Expand Down Expand Up @@ -297,7 +297,7 @@ mod test {
impl BaseSlot for StatSlot {}
impl StatSlot for StatSlot {
fn on_entry_pass(&self, ctx: &EntryContext);
fn on_entry_blocked(&self, ctx: &EntryContext, block_error: Option<BlockError>);
fn on_entry_blocked(&self, ctx: &EntryContext, block_error: BlockError);
fn on_completed(&self, ctx: &mut EntryContext);
}
}
Expand Down
2 changes: 1 addition & 1 deletion sentinel-core/src/core/circuitbreaker/stat_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl BaseSlot for MetricStatSlot {
impl StatSlot for MetricStatSlot {
fn on_entry_pass(&self, _ctx: &EntryContext) {}

fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: Option<BlockError>) {}
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: BlockError) {}

fn on_completed(&self, ctx: &mut EntryContext) {
let res = ctx.resource().name();
Expand Down
2 changes: 1 addition & 1 deletion sentinel-core/src/core/flow/standalone_stat_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl StatSlot for StandaloneStatSlot {
}
}

fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: Option<BlockError>) {}
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: BlockError) {}

fn on_completed(&self, _ctx: &mut EntryContext) {}
}
2 changes: 1 addition & 1 deletion sentinel-core/src/core/hotspot/concurrency_stat_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl StatSlot for ConcurrencyStatSlot {
}
}

fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: Option<BlockError>) {}
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: BlockError) {}

fn on_completed(&self, ctx: &mut EntryContext) {
let res = ctx.resource().name();
Expand Down
2 changes: 1 addition & 1 deletion sentinel-core/src/core/log/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl StatSlot for Slot {
fn on_entry_pass(&self, _ctx: &EntryContext) {}

// todo: write sentinel-block.log here
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: Option<BlockError>) {}
fn on_entry_blocked(&self, _ctx: &EntryContext, _block_error: BlockError) {}

fn on_completed(&self, _ctx: &mut EntryContext) {}
}
23 changes: 16 additions & 7 deletions sentinel-core/src/core/stat/stat_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ impl StatSlot for ResourceNodeStatSlot {
}
}
#[cfg(feature = "exporter")]
crate::exporter::add_handled_counter(input.batch_count(), res.name(), TokenResult::Pass);
crate::exporter::add_handled_counter(
input.batch_count(),
res.name(),
TokenResult::Pass,
None,
);
}

#[allow(unused_variables)]
fn on_entry_blocked(&self, ctx: &EntryContext, block_error: Option<BlockError>) {
fn on_entry_blocked(&self, ctx: &EntryContext, block_error: BlockError) {
let res = ctx.resource();
let input = ctx.input();
if let Some(stat_node) = ctx.stat_node().clone() {
Expand All @@ -70,11 +75,15 @@ impl StatSlot for ResourceNodeStatSlot {
}
}
#[cfg(feature = "exporter")]
crate::exporter::add_handled_counter(
input.batch_count(),
res.name(),
TokenResult::Blocked(block_error.unwrap()),
);
{
let tp = block_error.block_type();
crate::exporter::add_handled_counter(
input.batch_count(),
res.name(),
TokenResult::Blocked(block_error),
Some(tp),
);
}
}

fn on_completed(&self, ctx: &mut EntryContext) {
Expand Down
13 changes: 11 additions & 2 deletions sentinel-core/src/exporter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{base::TokenResult, config};
use crate::{
base::{BlockType, TokenResult},
config,
};
///! exporter the process protected by Sentinel
use lazy_static::lazy_static;
use prometheus_exporter::{
Expand Down Expand Up @@ -96,14 +99,20 @@ pub fn add_state_change_counter(resourse: &str, from: &str, to: &str) {
.inc_by(1.0);
}

pub fn add_handled_counter(batch_count: u32, resource: &str, result: TokenResult) {
pub fn add_handled_counter(
batch_count: u32,
resource: &str,
result: TokenResult,
block_type: Option<BlockType>,
) {
HANDLED_COUNTER
.with_label_values(&[
&HOST_NAME,
&PROCESS_NAME,
&PID_STRING,
resource,
&result.to_string(),
&block_type.map_or(String::new(), |v| v.to_string()),
])
.inc_by(batch_count as f64);
}
Expand Down

0 comments on commit 520b6b6

Please sign in to comment.