Skip to content

Commit

Permalink
Add bad_queries_total metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Dec 3, 2024
1 parent cee8c8d commit f1d9360
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
21 changes: 13 additions & 8 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ pub async fn inline_handler(bot: Bot, q: InlineQuery, usr_client: UserService<Us
senders::send_locations_inline(bot, q.id, lang_code, locations).await
}

fn is_query_correct(query: &str) -> bool {
let allowed = query.is_empty().not() && (
QUERY_REGEX.is_match(query) ||
COORDS_REGEXP.is_match(query) ||
*QUERY_CHECK_MODE != QueryCheckMode::Regex
);
if !allowed {
log::info!("Invalid query: {}", query);
metrics::INLINE_COUNTER.inc_bad_query();
}
allowed
}

async fn rate_limit_exceeded(q: &InlineQuery) -> bool {
let forbidden = !INLINE_REQUESTS_LIMITER.is_req_allowed(q).await;
if forbidden {
Expand All @@ -108,14 +121,6 @@ pub async fn inline_chosen_handler(_: Bot, _: ChosenInlineResult) -> HandlerResu
Ok(())
}

fn is_query_correct(query: &str) -> bool {
query.is_empty().not() && (
QUERY_REGEX.is_match(query) ||
COORDS_REGEXP.is_match(query) ||
*QUERY_CHECK_MODE != QueryCheckMode::Regex
)
}

#[derive(From)]
enum AnswerMessage {
Text(String),
Expand Down
4 changes: 2 additions & 2 deletions src/loc/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl GoogleLocFinder {
self.inc_resp_counter(&resp);

let json = resp.json::<serde_json::Value>().await?;
log::info!("response from Google Maps Geocoding API: {json}");
log::info!("Response from Google Maps Geocoding API: {json}");

let results = iter_over_array(&json["results"])
.filter_map(map_resp_geo)
Expand All @@ -132,7 +132,7 @@ impl GoogleLocFinder {
self.inc_resp_counter(&resp);

let json = resp.json::<serde_json::Value>().await?;
log::info!("response from Google Maps Text Search API: {json}");
log::info!("Response from Google Maps Text Search API: {json}");

let results: Vec<Location> = iter_over_array(&json["places"])
.filter_map(map_resp_place)
Expand Down
2 changes: 1 addition & 1 deletion src/loc/osm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl LocFinder for OpenStreetMapLocFinder {
self.inc_resp_counter(&resp);

let json = resp.json::<serde_json::Value>().await?;
log::info!("response from Open Street Map Nominatim API: {json}");
log::info!("Response from Open Street Map Nominatim API: {json}");

let results = json.as_array().unwrap().iter()
.filter_map(map_resp)
Expand Down
4 changes: 2 additions & 2 deletions src/loc/yandex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl YandexLocFinder {
self.inc_resp_counter(&resp);

let json = resp.json::<serde_json::Value>().await?;
log::info!("response from Yandex Maps Geocoder: {json}");
log::info!("Response from Yandex Maps Geocoder: {json}");

let empty: Vec<serde_json::Value> = Vec::new();
let result = json["response"]["GeoObjectCollection"]["featureMember"]
Expand All @@ -118,7 +118,7 @@ impl YandexLocFinder {
self.inc_resp_counter(&resp);

let json = resp.json::<serde_json::Value>().await?;
log::info!("response from Yandex Maps Places API: {json}");
log::info!("Response from Yandex Maps Places API: {json}");

let empty: Vec<serde_json::Value> = Vec::new();
let result = json["features"]
Expand Down
10 changes: 9 additions & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use prometheus::{Encoder, Opts, TextEncoder};
/// Register additional metrics of our own structs by using this registry instance.
pub static REGISTRY: Lazy<Registry> = Lazy::new(|| Registry(prometheus::Registry::new()));

// Export special preconstructed counters for Teloxide's handlers.
// Export special pre-constructed counters for Teloxide's handlers.
pub static INLINE_COUNTER: Lazy<InlineCounters> = Lazy::new(|| {
let opts = Opts::new("inline_usage_total", "count of inline queries processed by the bot or rejected by the rate limiter");
let bad_query_opts = Opts::new("bad_queries_total", "count of bad queries");
InlineCounters {
allowed: Counter::new("inline (allowed)", opts.clone().const_label("limiter", "allowed")),
forbidden: Counter::new("inline (forbidden)", opts.const_label("limiter", "forbidden")),
bad_query: Counter::new("inline (bad query)", bad_query_opts),
}
});
pub static INLINE_CHOSEN_COUNTER: Lazy<Counter> = Lazy::new(|| {
Expand Down Expand Up @@ -44,6 +46,7 @@ pub fn init() -> axum::Router {
let prometheus = REGISTRY
.register(&INLINE_COUNTER.allowed)
.register(&INLINE_COUNTER.forbidden)
.register(&INLINE_COUNTER.bad_query)
.register(&INLINE_CHOSEN_COUNTER)
.register(&MESSAGE_COUNTER)
.register(&CMD_START_COUNTER)
Expand Down Expand Up @@ -74,6 +77,7 @@ pub struct Counter {
pub struct InlineCounters {
allowed: Counter,
forbidden: Counter,
bad_query: Counter,
}
pub struct ComplexCommandCounters {
invoked: Counter,
Expand Down Expand Up @@ -132,4 +136,8 @@ impl InlineCounters {
pub fn inc_forbidden(&self) {
self.forbidden.inc()
}

pub fn inc_bad_query(&self) {
self.bad_query.inc()
}
}

0 comments on commit f1d9360

Please sign in to comment.