Skip to content

Commit

Permalink
Add new metrics for OpenStreetMap
Browse files Browse the repository at this point in the history
- open_street_map_api_requests_total
- open_street_map_api_responses_total
  • Loading branch information
Leonid Kozarin committed Aug 19, 2023
1 parent 57b3935 commit f31e29c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ strum = "0.24.1"
strum_macros = "0.24.3"
rust-i18n = "2.1.0"
async-trait = "0.1.68"
http-cache = { version = "0.11.0", default-features = false, features = ["manager-moka"] }
http-cache-reqwest = { version = "0.9.0", default-features = false, features = ["manager-moka"] }
reqwest-middleware = "0.2.2"
anyhow = "1.0.71"
1 change: 1 addition & 0 deletions src/loc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl SearchChain {
}

/// Reserved for Yandex Maps or 2GIS providers which may be used for RU locale in the future
#[allow(dead_code)]
pub fn for_lang_code(mut self, lc: &str, mut finders: Vec<DynLocFinder>) -> Self {
self.regional_finders
.entry(lc.to_string())
Expand Down
39 changes: 33 additions & 6 deletions src/loc/osm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use async_trait::async_trait;
use reqwest::header::{ACCEPT_LANGUAGE, USER_AGENT};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use http_cache::{HitOrMiss, XCACHELOOKUP};
use http_cache_reqwest::{Cache, CacheMode, MokaManager, HttpCache, CacheOptions};
use prometheus::Opts;
use super::{LocFinder, LocResult, Location};
use crate::metrics;

pub struct OpenStreetMapLocFinder {
client: ClientWithMiddleware
client: ClientWithMiddleware,

api_req_counter: prometheus::Counter,
cached_resp_counter: prometheus::Counter,
fetched_resp_counter: prometheus::Counter
}

impl OpenStreetMapLocFinder {
Expand All @@ -20,25 +27,45 @@ impl OpenStreetMapLocFinder {
}))
.build();

OpenStreetMapLocFinder { client }
let api_req_opts = Opts::new("open_street_map_api_requests_total", "count of requests to the OpenStreetMap API");

let resp_opts = Opts::new("open_street_map_api_responses_total", "count of responses from the OpenStreetMap API split by the source");
let from_cache_opts = resp_opts.clone().const_label("source", "cache");
let from_remote_opts = resp_opts.const_label("source", "remote");

OpenStreetMapLocFinder {
client,

api_req_counter: metrics::REGISTRY.register_counter("OpenStreetMap API requests", api_req_opts),
cached_resp_counter: metrics::REGISTRY.register_counter("OpenStreetMap API requests", from_cache_opts),
fetched_resp_counter: metrics::REGISTRY.register_counter("OpenStreetMap API requests", from_remote_opts),
}
}
}

#[async_trait]
impl LocFinder for OpenStreetMapLocFinder {
async fn find(&self, query: &str, lang_code: &str) -> LocResult {
self.api_req_counter.inc();

let url = format!("https://nominatim.openstreetmap.org/search?q={query}&format=json");
let resp = self.client.get(url)
.header(USER_AGENT, "kozalosev/LocPlaceBot")
.header(ACCEPT_LANGUAGE, lang_code)
.send()
.await?
.json::<serde_json::Value>()
.await?;

log::info!("response from Open Street Map Nominatim API: {}", resp);
let resp_counter = resp.headers()
.get(XCACHELOOKUP)
.filter(|x| x.to_str().unwrap_or("") == HitOrMiss::HIT.to_string())
.map(|_| &self.cached_resp_counter)
.unwrap_or(&self.fetched_resp_counter);
resp_counter.inc();

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

let results = resp.as_array().unwrap().iter()
let results = json.as_array().unwrap().iter()
.filter_map(map_resp)
.collect();
Ok(results)
Expand Down

0 comments on commit f31e29c

Please sign in to comment.