Starting impl Sentry and tracing #3
+5
-1
@@ -11,7 +11,7 @@ use serde_json::Value;
|
|||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use subtle::ConstantTimeEq;
|
use subtle::ConstantTimeEq;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::info;
|
use tracing::{info, instrument};
|
||||||
|
|
||||||
use crate::consts::{GITEA_EVENT_TYPE_HEADER_NAME, GITEA_SIG_HEADER_NAME, MAX_WEBHOOK_BODY_SIZE};
|
use crate::consts::{GITEA_EVENT_TYPE_HEADER_NAME, GITEA_SIG_HEADER_NAME, MAX_WEBHOOK_BODY_SIZE};
|
||||||
use crate::errors::AppError;
|
use crate::errors::AppError;
|
||||||
@@ -40,10 +40,13 @@ async fn root() -> &'static str {
|
|||||||
"Hi, i'm Herald :)"
|
"Hi, i'm Herald :)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(app_state), fields(webhook_type), err)]
|
||||||
async fn webhook(
|
async fn webhook(
|
||||||
State(app_state): State<AppState>,
|
State(app_state): State<AppState>,
|
||||||
WebhookExtract(wb): WebhookExtract,
|
WebhookExtract(wb): WebhookExtract,
|
||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
|
tracing::Span::current().record("webhook_type", tracing::field::debug(&wb));
|
||||||
|
|
||||||
app_state
|
app_state
|
||||||
.bot_tx
|
.bot_tx
|
||||||
.send(wb)
|
.send(wb)
|
||||||
@@ -62,6 +65,7 @@ where
|
|||||||
{
|
{
|
||||||
type Rejection = AppError;
|
type Rejection = AppError;
|
||||||
|
|
||||||
|
#[instrument(skip(req, state), err)]
|
||||||
async fn from_request(req: axum::extract::Request, state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request(req: axum::extract::Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||||
let app_state = AppState::from_ref(state);
|
let app_state = AppState::from_ref(state);
|
||||||
let headers = req.headers();
|
let headers = req.headers();
|
||||||
|
|||||||
+7
-5
@@ -1,12 +1,11 @@
|
|||||||
use serde::Deserialize;
|
|
||||||
use std::time::Duration;
|
|
||||||
use tracing::{error, info};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
env::EnvConfig,
|
env::EnvConfig,
|
||||||
gitea::{GiteaAPI, WebhookType},
|
gitea::{GiteaAPI, WebhookType},
|
||||||
open_router::OpenRouterClient,
|
open_router::OpenRouterClient,
|
||||||
};
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tracing::{error, info, instrument};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct ReviewResult {
|
pub struct ReviewResult {
|
||||||
@@ -59,10 +58,13 @@ impl Bot {
|
|||||||
self.exec(wb).await;
|
self.exec(wb).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info!("Bot shutting down, channel closed");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self))]
|
||||||
pub async fn exec(&self, webhook: WebhookType) {
|
pub async fn exec(&self, webhook: WebhookType) {
|
||||||
|
tracing::Span::current().record("webhook_type", tracing::field::debug(&webhook));
|
||||||
let exec_result = match webhook {
|
let exec_result = match webhook {
|
||||||
WebhookType::Review(review_payload) => crate::bot_actions::review::exec_review(
|
WebhookType::Review(review_payload) => crate::bot_actions::review::exec_review(
|
||||||
&self.gitea_api,
|
&self.gitea_api,
|
||||||
@@ -77,7 +79,7 @@ impl Bot {
|
|||||||
match exec_result {
|
match exec_result {
|
||||||
Ok(_) => info!("Task completed"),
|
Ok(_) => info!("Task completed"),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("{}", err);
|
error!(%err, "Task error");
|
||||||
sentry_anyhow::capture_anyhow(&err);
|
sentry_anyhow::capture_anyhow(&err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
use futures_util::stream::TryStreamExt;
|
use futures_util::stream::TryStreamExt;
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tokio_util::io::StreamReader;
|
use tokio_util::io::StreamReader;
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
bot::ReviewResult,
|
bot::ReviewResult,
|
||||||
consts::{BOT_PROCESS_MSG, MAX_DIFF_SIZE, REVIEW_PROMPT},
|
consts::{BOT_PROCESS_MSG, MAX_DIFF_SIZE, REVIEW_PROMPT},
|
||||||
errors::AppError,
|
|
||||||
gitea::{GiteaAPI, ReviewPayload},
|
gitea::{GiteaAPI, ReviewPayload},
|
||||||
open_router::OpenRouterClient,
|
open_router::OpenRouterClient,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[instrument(skip(gitea_api, open_router_client, http_client, review_payload), err)]
|
||||||
pub async fn exec_review(
|
pub async fn exec_review(
|
||||||
gitea_api: &GiteaAPI,
|
gitea_api: &GiteaAPI,
|
||||||
open_router_client: &OpenRouterClient,
|
open_router_client: &OpenRouterClient,
|
||||||
@@ -17,6 +18,13 @@ pub async fn exec_review(
|
|||||||
model: &str,
|
model: &str,
|
||||||
review_payload: ReviewPayload,
|
review_payload: ReviewPayload,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
|
tracing::info!(
|
||||||
|
repo = %review_payload.repository.full_name,
|
||||||
|
pr = review_payload.pull_request.number,
|
||||||
|
action = %review_payload.action,
|
||||||
|
"Starting review"
|
||||||
|
);
|
||||||
|
|
||||||
let new_comment = gitea_api
|
let new_comment = gitea_api
|
||||||
.comment(
|
.comment(
|
||||||
&BOT_PROCESS_MSG.replace("{model}", model),
|
&BOT_PROCESS_MSG.replace("{model}", model),
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
@@ -55,7 +56,7 @@ impl IntoResponse for AppError {
|
|||||||
"WebHook sig header is invalid".to_string(),
|
"WebHook sig header is invalid".to_string(),
|
||||||
),
|
),
|
||||||
AppError::Other(err) => {
|
AppError::Other(err) => {
|
||||||
sentry_anyhow::capture_anyhow(&err);
|
error!(%err, "Internal server error");
|
||||||
(
|
(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
"Internal server error".to_string(),
|
"Internal server error".to_string(),
|
||||||
|
|||||||
+6
-4
@@ -2,11 +2,9 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
use crate::{
|
use crate::{bot::ReviewResult, errors::AppError};
|
||||||
bot::{ReviewItem, ReviewResult},
|
|
||||||
errors::AppError,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct GiteaAPI {
|
pub struct GiteaAPI {
|
||||||
base_url: String,
|
base_url: String,
|
||||||
@@ -30,6 +28,7 @@ impl GiteaAPI {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), err)]
|
||||||
pub async fn comment(
|
pub async fn comment(
|
||||||
&self,
|
&self,
|
||||||
body: &str,
|
body: &str,
|
||||||
@@ -57,6 +56,7 @@ impl GiteaAPI {
|
|||||||
res.json::<Comment>().await.map_err(anyhow::Error::from)
|
res.json::<Comment>().await.map_err(anyhow::Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), err)]
|
||||||
pub async fn edit_comment(
|
pub async fn edit_comment(
|
||||||
&self,
|
&self,
|
||||||
body: &str,
|
body: &str,
|
||||||
@@ -84,6 +84,7 @@ impl GiteaAPI {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), err)]
|
||||||
pub async fn delete_comment(&self, full_name: &str, comment_id: u64) -> anyhow::Result<()> {
|
pub async fn delete_comment(&self, full_name: &str, comment_id: u64) -> anyhow::Result<()> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/api/v1/repos/{}/issues/comments/{}",
|
"{}/api/v1/repos/{}/issues/comments/{}",
|
||||||
@@ -102,6 +103,7 @@ impl GiteaAPI {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, review_result), err)]
|
||||||
pub async fn post_pull_request_review(
|
pub async fn post_pull_request_review(
|
||||||
&self,
|
&self,
|
||||||
review_result: &ReviewResult,
|
review_result: &ReviewResult,
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
async fn run() -> anyhow::Result<()> {
|
async fn run() -> anyhow::Result<()> {
|
||||||
let config = env::load_config()?;
|
let config = env::load_config()?;
|
||||||
|
|
||||||
|
info!(
|
||||||
|
port = config.http_port,
|
||||||
|
model = %config.open_router_model,
|
||||||
|
gitea_url = %config.gitea_url,
|
||||||
|
bot_name = %config.bot_name,
|
||||||
|
"Starting Herald"
|
||||||
|
);
|
||||||
|
|
||||||
let bot = Bot::new(config.clone())?;
|
let bot = Bot::new(config.clone())?;
|
||||||
|
|
||||||
let (tx, rx) = tokio::sync::mpsc::channel::<WebhookType>(1);
|
let (tx, rx) = tokio::sync::mpsc::channel::<WebhookType>(1);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use openrouter_rs::{Message, api::chat::ChatCompletionRequest};
|
use openrouter_rs::{Message, api::chat::ChatCompletionRequest};
|
||||||
|
use tracing::instrument;
|
||||||
|
|
||||||
pub struct ChatResult {
|
pub struct ChatResult {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
@@ -27,6 +28,7 @@ impl OpenRouterClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), err)]
|
||||||
pub async fn chat(&self, msg: &str) -> anyhow::Result<ChatResult> {
|
pub async fn chat(&self, msg: &str) -> anyhow::Result<ChatResult> {
|
||||||
let request = ChatCompletionRequest::builder()
|
let request = ChatCompletionRequest::builder()
|
||||||
.model(&self.model)
|
.model(&self.model)
|
||||||
|
|||||||
Reference in New Issue
Block a user