Skip to content

Commit

Permalink
auth-server: record metrics for output net of gas and fees
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Jan 21, 2025
1 parent ef6fe5b commit 12c0998
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
37 changes: 32 additions & 5 deletions auth/auth-server/src/telemetry/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ use crate::{
ASSET_METRIC_TAG, BASE_ASSET_METRIC_TAG, EXTERNAL_MATCH_BASE_VOLUME,
EXTERNAL_MATCH_FILL_RATIO, EXTERNAL_MATCH_QUOTE_VOLUME, EXTERNAL_MATCH_SETTLED_BASE_VOLUME,
EXTERNAL_MATCH_SETTLED_QUOTE_VOLUME, EXTERNAL_ORDER_BASE_VOLUME,
EXTERNAL_ORDER_QUOTE_VOLUME, NUM_EXTERNAL_MATCH_REQUESTS, OUR_OUTPUT_NET_OF_FEE_TAG,
OUR_OUTPUT_NET_OF_GAS_TAG, OUR_PRICE_TAG, QUOTE_OUTPUT_NET_OF_FEE_DIFF_BPS_METRIC,
EXTERNAL_ORDER_QUOTE_VOLUME, NUM_EXTERNAL_MATCH_REQUESTS, OUR_NET_OUTPUT_TAG,
OUR_OUTPUT_NET_OF_FEE_TAG, OUR_OUTPUT_NET_OF_GAS_TAG, OUR_PRICE_TAG,
QUOTE_NET_OUTPUT_DIFF_BPS_METRIC, QUOTE_OUTPUT_NET_OF_FEE_DIFF_BPS_METRIC,
QUOTE_OUTPUT_NET_OF_GAS_DIFF_BPS_METRIC, QUOTE_PRICE_DIFF_BPS_METRIC,
SETTLEMENT_STATUS_TAG, SOURCE_NAME_TAG, SOURCE_OUTPUT_NET_OF_FEE_TAG,
SOURCE_OUTPUT_NET_OF_GAS_TAG, SOURCE_PRICE_TAG,
SETTLEMENT_STATUS_TAG, SOURCE_NAME_TAG, SOURCE_NET_OUTPUT_TAG,
SOURCE_OUTPUT_NET_OF_FEE_TAG, SOURCE_OUTPUT_NET_OF_GAS_TAG, SOURCE_PRICE_TAG,
},
};

Expand Down Expand Up @@ -318,7 +319,7 @@ fn extract_nullifier_from_match_bundle(
// --- Quote Comparison --- //

/// Record a single quote comparison metric with all data as tags
pub(crate) fn record_comparison(
pub(crate) fn record_quote_price_comparison(
comparison: &QuoteComparison,
side: OrderSide,
extra_labels: &[(String, String)],
Expand Down Expand Up @@ -390,3 +391,29 @@ pub(crate) fn record_output_value_net_of_fee_comparison(

metrics::gauge!(QUOTE_OUTPUT_NET_OF_FEE_DIFF_BPS_METRIC, labels.as_slice()).set(fee_diff_bps);
}

/// Record a quote comparison net of gas and fee
pub(crate) fn record_net_output_value_comparison(
comparison: &QuoteComparison,
side: OrderSide,
extra_labels: &[(String, String)],
) {
let usdc_per_gas = comparison.usdc_per_gas;
let net_output_diff_bps = comparison.net_output_value_diff_bps(usdc_per_gas, side);

let our_net_output = comparison.our_quote.output_net_of_gas_and_fee(side, usdc_per_gas);
let source_net_output = comparison.source_quote.output_net_of_gas_and_fee(side, usdc_per_gas);

let side_label = if side == OrderSide::Sell { "sell" } else { "buy" };
let base_token = Token::from_addr(&comparison.our_quote.base_mint);
let mut labels = vec![
(SIDE_TAG.to_string(), side_label.to_string()),
(SOURCE_NAME_TAG.to_string(), comparison.source_quote.name.to_string()),
(OUR_NET_OUTPUT_TAG.to_string(), our_net_output.to_string()),
(SOURCE_NET_OUTPUT_TAG.to_string(), source_net_output.to_string()),
];
labels.extend(extra_labels.iter().cloned());
labels = extend_labels_with_base_asset(&base_token.get_addr(), labels);

metrics::gauge!(QUOTE_NET_OUTPUT_DIFF_BPS_METRIC, labels.as_slice()).set(net_output_diff_bps);
}
7 changes: 7 additions & 0 deletions auth/auth-server/src/telemetry/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub const QUOTE_OUTPUT_NET_OF_GAS_DIFF_BPS_METRIC: &str = "quote.output_net_of_g
/// Metric describing the difference in output value net of fee between our
/// quote and the source quote, in basis points
pub const QUOTE_OUTPUT_NET_OF_FEE_DIFF_BPS_METRIC: &str = "quote.output_net_of_fee_diff_bps";
/// Metric describing the difference in output value net of gas and fee between
/// our quote and the source quote, in basis points
pub const QUOTE_NET_OUTPUT_DIFF_BPS_METRIC: &str = "quote.net_output_diff_bps";

// ---------------
// | METRIC TAGS |
Expand Down Expand Up @@ -73,3 +76,7 @@ pub const SOURCE_OUTPUT_NET_OF_GAS_TAG: &str = "source_output_net_of_gas";
pub const OUR_OUTPUT_NET_OF_FEE_TAG: &str = "our_output_net_of_fee";
/// Metric tag for the comparison source's output net of fee
pub const SOURCE_OUTPUT_NET_OF_FEE_TAG: &str = "source_output_net_of_fee";
/// Metric tag for our output net of gas and fee
pub const OUR_NET_OUTPUT_TAG: &str = "our_net_output";
/// Metric tag for the comparison source's output net of gas and fee
pub const SOURCE_NET_OUTPUT_TAG: &str = "source_net_output";
9 changes: 6 additions & 3 deletions auth/auth-server/src/telemetry/quote_comparison/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use renegade_common::types::token::Token;

use renegade_api::http::external_match::AtomicMatchApiBundle;

use crate::telemetry::helpers::record_output_value_net_of_gas_comparison;
use crate::{
error::AuthServerError,
telemetry::{
helpers::{record_comparison, record_output_value_net_of_fee_comparison},
helpers::{
record_net_output_value_comparison, record_output_value_net_of_fee_comparison,
record_output_value_net_of_gas_comparison, record_quote_price_comparison,
},
sources::{QuoteResponse, QuoteSource},
},
};
Expand Down Expand Up @@ -80,9 +82,10 @@ impl QuoteComparisonHandler {
let usdc_per_gas = self.get_usdc_per_gas().await?;
let comparison = QuoteComparison { our_quote, source_quote: &quote, usdc_per_gas };

record_comparison(&comparison, side, &labels);
record_quote_price_comparison(&comparison, side, &labels);
record_output_value_net_of_gas_comparison(&comparison, side, &labels);
record_output_value_net_of_fee_comparison(&comparison, side, &labels);
record_net_output_value_comparison(&comparison, side, &labels);
Ok(())
}
}
Expand Down
15 changes: 15 additions & 0 deletions auth/auth-server/src/telemetry/quote_comparison/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ impl<'a> QuoteComparison<'a> {

calc_bps_diff(our_output_value_net_of_fee, source_output_value_net_of_fee)
}

/// Calculate the net output value difference in basis points (bps).
/// Positive bps indicates our quote is better.
pub fn net_output_value_diff_bps(&self, usdc_per_gas: f64, side: OrderSide) -> i32 {
let our_output_value_net_of_gas =
self.our_quote.output_net_of_gas_and_fee(side, usdc_per_gas);
let source_output_value_net_of_gas =
self.source_quote.output_net_of_gas_and_fee(side, usdc_per_gas);

let diff_ratio = (our_output_value_net_of_gas - source_output_value_net_of_gas)
/ source_output_value_net_of_gas;
let bps_diff = diff_ratio * DECIMAL_TO_BPS;

bps_diff as i32
}
}
8 changes: 7 additions & 1 deletion auth/auth-server/src/telemetry/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ impl QuoteResponse {
self.deduct_fees(amount, &token)
}

/// Helper to apply gas cost deduction based on order side
/// Calculates output value with gas and fees deducted
pub fn output_net_of_gas_and_fee(&self, side: OrderSide, usdc_per_gas: f64) -> f64 {
let value = self.output_net_of_fee(side);
self.deduct_gas(value, side, usdc_per_gas)
}

/// Deducts gas costs from the given value based on the order side.
fn deduct_gas(&self, value: f64, side: OrderSide, usdc_per_gas: f64) -> f64 {
let gas_cost_usdc = self.gas_cost(usdc_per_gas);
match side {
Expand Down

0 comments on commit 12c0998

Please sign in to comment.