Skip to content

Commit

Permalink
chore: add metrics for ai (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy authored Jan 20, 2025
1 parent 5d9efb4 commit e990bad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async fn stream_complete_text_handler(
) -> actix_web::Result<HttpResponse> {
let ai_model = ai_model_from_header(&req);
let params = payload.into_inner();
state.metrics.ai_metrics.record_total_completion_count(1);

match state
.ai_client
Expand Down Expand Up @@ -80,6 +81,7 @@ async fn summarize_row_handler(
);
}

state.metrics.ai_metrics.record_total_summary_row_count(1);
let ai_model = ai_model_from_header(&req);
let result = state.ai_client.summarize_row(&content, ai_model).await;
let resp = match result {
Expand All @@ -105,6 +107,7 @@ async fn translate_row_handler(
) -> actix_web::Result<Json<AppResponse<TranslateRowResponse>>> {
let params = payload.into_inner();
let ai_model = ai_model_from_header(&req);
state.metrics.ai_metrics.record_total_translate_row_count(1);
match state.ai_client.translate_row(params.data, ai_model).await {
Ok(resp) => Ok(AppResponse::Ok().with_data(resp).into()),
Err(err) => {
Expand Down
30 changes: 30 additions & 0 deletions src/biz/chat/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub struct AIMetrics {
total_stream_count: Counter,
failed_stream_count: Counter,
stream_image_count: Counter,
total_completion_count: Counter,
total_summary_row_count: Counter,
total_translate_row_count: Counter,
}

impl AIMetrics {
Expand All @@ -28,6 +31,21 @@ impl AIMetrics {
"Total count of image streams processed",
metrics.stream_image_count.clone(),
);
realtime_registry.register(
"total_completion_count",
"Total count of completions processed",
metrics.total_completion_count.clone(),
);
realtime_registry.register(
"total_summary_row_count",
"Total count of summary rows processed",
metrics.total_summary_row_count.clone(),
);
realtime_registry.register(
"total_translate_row_count",
"Total count of translation rows processed",
metrics.total_translate_row_count.clone(),
);

metrics
}
Expand All @@ -43,4 +61,16 @@ impl AIMetrics {
pub fn record_stream_image_count(&self, count: u64) {
self.stream_image_count.inc_by(count);
}

pub fn record_total_completion_count(&self, count: u64) {
self.total_completion_count.inc_by(count);
}

pub fn record_total_summary_row_count(&self, count: u64) {
self.total_summary_row_count.inc_by(count);
}

pub fn record_total_translate_row_count(&self, count: u64) {
self.total_translate_row_count.inc_by(count);
}
}

0 comments on commit e990bad

Please sign in to comment.