Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: fixed analytics #421

Merged
merged 11 commits into from
Dec 24, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next release

- fix: instrumentation code
- feat: fetch eth/strk price and sync strk gas price
- feat(block_production): continue pending block on restart
- feat(mempool): mempool transaction saving on db
Expand Down
20 changes: 19 additions & 1 deletion crates/client/analytics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ::time::UtcOffset;
use opentelemetry::metrics::{Counter, Gauge, Histogram, Meter};
use opentelemetry::metrics::{Counter, Gauge, Histogram, Meter, UpDownCounter};
use opentelemetry::trace::TracerProvider;
use opentelemetry::{global, KeyValue};
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
Expand Down Expand Up @@ -198,6 +198,24 @@ pub fn register_counter_metric_instrument<T: CounterType<T> + Display>(
T::register_counter(crate_meter, instrument_name, desc, unit)
}

pub trait UpDownCounterType<T> {
Copy link
Member

Choose a reason for hiding this comment

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

would be great to document those functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NA

fn register_counter(meter: &Meter, name: String, description: String, unit: String) -> UpDownCounter<T>;
Copy link
Member

Choose a reason for hiding this comment

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

I would rename that register_up_down_counter to align with other declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NA

}

impl UpDownCounterType<f64> for f64 {
fn register_counter(meter: &Meter, name: String, description: String, unit: String) -> UpDownCounter<f64> {
meter.f64_up_down_counter(name).with_description(description).with_unit(unit).init()
}
}
pub fn register_up_down_counter_metric_instrument<T: UpDownCounterType<T> + Display>(
crate_meter: &Meter,
instrument_name: String,
desc: String,
unit: String,
) -> UpDownCounter<T> {
T::register_counter(crate_meter, instrument_name, desc, unit)
}

pub trait HistogramType<T> {
fn register_histogram(meter: &Meter, name: String, description: String, unit: String) -> Histogram<T>;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/client/mempool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Mempool {
.expect("Poisoned lock")
.insert_tx(MempoolTransaction { tx, arrived_at, converted_class }, force)?;

self.metrics.accepted_transaction_counter.add(1, &[]);
self.metrics.accepted_transaction_counter.add(1.0, &[]);
Copy link
Member

Choose a reason for hiding this comment

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

why do we need a decimal counter here? cant we stick with an integer?

Copy link
Member

Choose a reason for hiding this comment

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

also why an upDownCounter if we never decrement? AM I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, initially we were going store the flow of increase and decrease of transactions in the mempool, but now we deprecated that thought, removed the up_down_counter logic completely

}

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/client/mempool/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use mc_analytics::register_counter_metric_instrument;
use opentelemetry::metrics::Counter;
use mc_analytics::register_up_down_counter_metric_instrument;
use opentelemetry::metrics::UpDownCounter;
use opentelemetry::{global, KeyValue};

pub struct MempoolMetrics {
pub accepted_transaction_counter: Counter<u64>,
pub accepted_transaction_counter: UpDownCounter<f64>,
}

impl MempoolMetrics {
Expand All @@ -17,7 +17,7 @@ impl MempoolMetrics {
Some(common_scope_attributes.clone()),
);

let accepted_transaction_counter = register_counter_metric_instrument(
let accepted_transaction_counter = register_up_down_counter_metric_instrument(
&mempool_meter,
"accepted_transaction_count".to_string(),
"A counter to show accepted transactions in the mempool".to_string(),
Expand Down
10 changes: 5 additions & 5 deletions crates/node/src/service/rpc/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ impl RpcMetrics {
}

pub(crate) fn ws_disconnect(&self, now: Instant) {
let micros = now.elapsed().as_secs();
let millis = now.elapsed().as_millis();

if let Some(counter) = self.ws_sessions_closed.as_ref() {
counter.add(1, &[]);
}
self.ws_sessions_time.record(micros as f64, &[]);
self.ws_sessions_time.record(millis as f64, &[]);
}

pub(crate) fn on_call(&self, req: &Request, transport_label: &'static str) {
Expand All @@ -113,15 +113,15 @@ impl RpcMetrics {
tracing::trace!(target: "rpc_metrics", "[{transport_label}] on_response started_at={:?}", now);
tracing::trace!(target: "rpc_metrics::extra", "[{transport_label}] result={}", rp.as_result());

let micros = now.elapsed().as_micros();
let millis = now.elapsed().as_millis();
Copy link
Member

Choose a reason for hiding this comment

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

my comment was about doing

        let time = now.elapsed();
        tracing::debug!(
            target: "rpc_metrics",
            "[{transport_label}] {} call took {:?}",
            req.method_name(),
            time,
        );

        self.calls_time.record(time.to_millis() as f64, &[KeyValue::new("method", req.method_name().to_string())]);

instead, using the Debug impl of Duration, not the Debug impl of u64

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, but don't you think this increases possibility of the two readings going out of sync,
let's say due to xyz reasons a RPC call took 1 sec, the tracing might record it as 1 sec but the calls_time metric will record it as 1000 milliseconds.
Whereas in the previous implementation both would have recorded 1000 milliseconds

WDYT ?

tracing::debug!(
target: "rpc_metrics",
"[{transport_label}] {} call took {} μs",
Copy link
Member

Choose a reason for hiding this comment

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

here you should remove μs seconds to mili

Copy link
Contributor Author

Choose a reason for hiding this comment

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

valid, done!

req.method_name(),
micros,
millis,
);

self.calls_time.record(micros as f64, &[KeyValue::new("method", req.method_name().to_string())]);
self.calls_time.record(millis as f64, &[KeyValue::new("method", req.method_name().to_string())]);

self.calls_finished.add(
1,
Expand Down
Loading