-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
editoast: extract all the STDCM log logic into a function #9947
Closed
woshilapin
wants to merge
2
commits into
hai/editoast-log-stdcm-requests
from
wsl/editoast-log-stdcm-requests-one-function
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
editoast/migrations/2024-11-21-145205_create_stdcm_logs/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE stdcm_logs; |
8 changes: 8 additions & 0 deletions
8
editoast/migrations/2024-11-21-145205_create_stdcm_logs/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE stdcm_logs ( | ||
id int8 PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, | ||
trace_id VARCHAR(32) UNIQUE NOT NULL, | ||
request jsonb NOT NULL, | ||
response jsonb NOT NULL, | ||
created timestamptz NOT NULL DEFAULT NOW(), | ||
user_id bigint REFERENCES authn_user ON DELETE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use chrono::DateTime; | ||
use chrono::Utc; | ||
use editoast_derive::Model; | ||
use editoast_models::DbConnection; | ||
use opentelemetry::trace::TraceContextExt; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use tracing_opentelemetry::OpenTelemetrySpanExt; | ||
use utoipa::ToSchema; | ||
|
||
use crate::core::stdcm::Request; | ||
use crate::core::stdcm::Response; | ||
use crate::models::prelude::*; | ||
use crate::views::Authentication; | ||
|
||
editoast_common::schemas! { | ||
StdcmLog, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, Model, ToSchema)] | ||
#[model(table = editoast_models::tables::stdcm_logs)] | ||
#[model(gen(ops = c))] | ||
pub struct StdcmLog { | ||
pub id: i64, | ||
pub trace_id: String, | ||
#[model(json)] | ||
pub request: Request, | ||
#[model(json)] | ||
pub response: Response, | ||
pub created: DateTime<Utc>, | ||
pub user_id: Option<i64>, | ||
} | ||
|
||
impl StdcmLog { | ||
// We just don't await the creation of the log entry since we want | ||
// the endpoint to return as soon as possible, and because failing | ||
// to persist a log entry is not a very important error here. | ||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this comment stay near the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. |
||
pub async fn log( | ||
auth: Authentication, | ||
mut conn: DbConnection, | ||
request: Request, | ||
response: Response, | ||
) { | ||
let user_id = auth.authorizer().map_or_else( | ||
|e| { | ||
tracing::error!("Authorization failed: {e}. Unable to retrieve user ID."); | ||
None | ||
}, | ||
|auth| Some(auth.user_id()), | ||
); | ||
let trace_id = tracing::Span::current() | ||
.context() | ||
.span() | ||
.span_context() | ||
.trace_id(); | ||
let stdcm_log_changeset = StdcmLog::changeset() | ||
.trace_id(trace_id.to_string()) | ||
.request(request) | ||
.response(response.clone()) | ||
.user_id(user_id); | ||
let _ = stdcm_log_changeset | ||
.create(&mut conn) | ||
.await | ||
.map_err(|e| tracing::error!("Failed during log operation: {e}")); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
views
import inmodels
:/Given how things are going, the authentication and authorization are likely to stay in
views
and in a dedicated crate. Themodels
"crate" won't probably know about that layer. Wdyt about taking anOption<i64>
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, so agreed. I really quickly prototyped this solution (maybe I should have opened a draft). This is one thing to improve for sure.