Skip to content

Commit

Permalink
Merge branch 'master' of github.com:breadrock1/doc-searcher
Browse files Browse the repository at this point in the history
  • Loading branch information
breadrock1 committed Oct 27, 2024
2 parents f3b63a5 + cb0c890 commit d85906a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 14 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ maintenance = { status = "actively-developed" }
[features]
enable-cacher = ["dep:redis"]
enable-semantic = []
enable-prometheus = ["dep:actix-web-prom"]
default = []

[dependencies]
Expand Down Expand Up @@ -38,6 +39,11 @@ tracing-actix-web = "^0.7"
version = "^0.4"
features = ["rustc-serialize", "serde"]

[dependencies.actix-web-prom]
optional = true
version = "^0.9"
features = ["process"]

[dependencies.redis]
optional = true
version = "0.27.3"
Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ services:
REDIS_PASSWORD: ${REDIS_ROOT_PASSWORD}
REDIS_USER_PASSWORD: ${REDIS_CLIENT_PASSWORD}

prometheus:
image: prom/prometheus:v2.36.2
restart: on-failure
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
links:
- doc-searcher
ports:
- '9090:9090'
volumes:
- './prometheus/:/etc/prometheus/'
- 'prom-vol-1:/prometheus'

doc-searcher:
image: doc-searcher:latest
command: /app/doc-searcher-init && /app/doc-searcher-run
Expand Down Expand Up @@ -64,3 +80,5 @@ volumes:
driver: local
cacher-vol-1:
driver: local
prom-vol-1:
driver: local
18 changes: 18 additions & 0 deletions prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
global:
scrape_interval: 15s
evaluation_interval: 15s

external_labels:
monitor: 'doc-searcher'

scrape_configs:
- job_name: 'prometheus'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']

- job_name: 'doc-searcher'
metrics_path: '/metrics'
scrape_interval: 15s
static_configs:
- targets: ['doc-searcher:2892']
14 changes: 13 additions & 1 deletion src/bin/doc-searcher-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use doc_search::cacher;
#[cfg(feature = "enable-semantic")]
use doc_search::embeddings;

#[cfg(feature = "enable-prometheus")]
use doc_search::metrics::prometheus;

#[actix_web::main]
async fn main() -> Result<(), anyhow::Error> {
let s_config = config::ServiceConfig::new()?;
Expand All @@ -25,14 +28,17 @@ async fn main() -> Result<(), anyhow::Error> {
let logger_config = s_config.logger();
logger::init_logger(logger_config)?;

let search_service = elastic::ElasticClient::connect(s_config.elastic())?;
#[cfg(feature = "enable-prometheus")]
let prometheus = prometheus::init_prometheus()?;

#[cfg(feature = "enable-semantic")]
let embed_service = embeddings::native::EmbeddingsClient::connect(s_config.embeddings())?;

#[cfg(feature = "enable-cacher")]
let cacher_service = cacher::redis::RedisClient::connect(s_config.cacher())?;

let search_service = elastic::ElasticClient::connect(s_config.elastic())?;

HttpServer::new(move || {
let cors = cors::build_cors(&cors_config.clone());
let logger = Logger::default();
Expand All @@ -56,17 +62,23 @@ async fn main() -> Result<(), anyhow::Error> {
#[cfg(feature = "enable-cacher")]
let cacher_search_cxt: cacher::redis::SemanticParamsCached =
Box::new(cacher_service.clone());

#[cfg(feature = "enable-cacher")]
let cacher_fulltext_cxt: cacher::redis::FullTextParamsCached =
Box::new(cacher_service.clone());

#[cfg(feature = "enable-cacher")]
let cacher_paginate_cxt: cacher::redis::PaginatedCached = Box::new(cacher_service.clone());

#[cfg(feature = "enable-cacher")]
let app = app
.app_data(web::Data::new(cacher_search_cxt))
.app_data(web::Data::new(cacher_fulltext_cxt))
.app_data(web::Data::new(cacher_paginate_cxt));

#[cfg(feature = "enable-prometheus")]
let app = app.wrap(prometheus.clone());

app.wrap(logger)
.wrap(cors)
.service(build_metrics_scope())
Expand Down
9 changes: 2 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ use derive_builder::Builder;
use getset::{CopyGetters, Getters};
use serde_derive::Deserialize;

#[derive(Builder, Clone, Deserialize, CopyGetters, Getters)]
#[derive(Builder, Clone, Deserialize, Getters)]
#[getset(get = "pub")]
pub struct ServiceConfig {
#[getset(get = "pub")]
logger: LoggerConfig,
#[getset(get = "pub")]
server: ServerConfig,
#[getset(get = "pub")]
cors: CorsConfig,
#[getset(get = "pub")]
elastic: ElasticConfig,
#[getset(get = "pub")]
cacher: CacherConfig,
#[getset(get = "pub")]
embeddings: EmbeddingsConfig,
}

Expand Down
20 changes: 15 additions & 5 deletions src/metrics/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use crate::errors::{ErrorResponse, JsonResponse, Successful};

use actix_web::web::Json;
use actix_web::{get, web, Scope};
use actix_web::{get, web, HttpResponse, Scope};

pub fn build_scope() -> Scope {
web::scope("/metrics").service(metrics)
let scope = web::scope("/metrics").service(hello);

#[cfg(feature = "enable-prometheus")]
let scope = scope.service(metrics);

scope
}

#[utoipa::path(
get,
path = "/metrics/",
path = "/metrics/hello",
tag = "Metrics",
responses(
(
Expand All @@ -26,7 +31,12 @@ pub fn build_scope() -> Scope {
),
),
)]
#[get("/")]
async fn metrics() -> JsonResponse<Successful> {
#[get("/hello")]
async fn hello() -> JsonResponse<Successful> {
Ok(Json(Successful::new(200, "Ok")))
}

#[get("/metrics")]
async fn metrics() -> HttpResponse {
HttpResponse::Ok().finish()
}
3 changes: 3 additions & 0 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod endpoints;

#[cfg(feature = "enable-prometheus")]
pub mod prometheus;
14 changes: 14 additions & 0 deletions src/metrics/prometheus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use actix_web_prom::{PrometheusMetrics, PrometheusMetricsBuilder};
use std::collections::HashMap;

pub fn init_prometheus() -> Result<PrometheusMetrics, anyhow::Error> {
let mut labels = HashMap::new();
labels.insert("label1".to_string(), "value1".to_string());
let prometheus = PrometheusMetricsBuilder::new("metrics")
.endpoint("/metrics")
.const_labels(labels)
.build()
.map_err(|err| anyhow::Error::msg(err.to_string()))?;

Ok(prometheus)
}
2 changes: 1 addition & 1 deletion src/swagger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use utoipa_swagger_ui::SwaggerUi;
description = "There is API endpoints of DocSearch project based on Rust and Elasticsearch technologies."
),
paths(
metrics,
hello,
get_folder,
get_folders,
create_folder,
Expand Down

0 comments on commit d85906a

Please sign in to comment.