Skip to content
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

feat(#16369): add a udm events endpoint to the emulator for testing #2

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.62.0 as builder
FROM rust:1.82.0 as builder

WORKDIR /app
RUN apt update && apt install lld clang -y
Expand All @@ -7,7 +7,7 @@ COPY . .

RUN cargo build --release

FROM rust:1.62.0 as runtime
FROM rust:1.82.0 as runtime

WORKDIR /app

Expand Down
46 changes: 45 additions & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ pub struct UnstructuredLog {
ts_rfc3339: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UdmEvents {
pub customer_id: String,
pub events: Vec<UdmEvent>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UdmEvent {
metadata: UdmMetadata,
pub invalid: Option<bool>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct UdmMetadata {
log_type: String,
namespace: Option<String>,
ts_epoch_microseconds: Option<i64>,
ts_rfc3339: Option<String>,
}

impl From<UnstructuredLogs> for Vec<Log> {
fn from(logs: UnstructuredLogs) -> Self {
logs.entries
Expand All @@ -51,8 +71,32 @@ impl From<UnstructuredLogs> for Vec<Log> {
}
}

impl From<UdmEvents> for Vec<Log> {
fn from(logs: UdmEvents) -> Self {
logs.events.into_iter().map(|event| Log {
customer_id: logs.customer_id.clone(),
log_type: event.metadata.log_type.clone(),
log_text: String::new(),
namespace: event.metadata.namespace.clone(),
ts_rfc3339: event.metadata.ts_rfc3339.unwrap_or_else(|| {
event.metadata
.ts_epoch_microseconds
.map(|ms| Utc.timestamp_millis(ms))
.unwrap_or_else(Utc::now)
.to_rfc3339()
}),
}).collect()
}
}

/// Adds the logs to our (in memory) database.
pub fn add_unstructured_to_data(logs: UnstructuredLogs) {
let mut data = DATA.lock().unwrap();
data.append(&mut Vec::from(logs));
}

/// Adds the logs to our (in memory) database.
pub fn add_to_data(logs: UnstructuredLogs) {
pub fn add_udm_events_to_data(logs: UdmEvents) {
let mut data = DATA.lock().unwrap();
data.append(&mut Vec::from(logs));
}
23 changes: 22 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ async fn logs(log_type: Query<LogType>) -> Json<Vec<data::Log>> {
Json(logs)
}

/// Handler that posts a set UDM events.
/// To test unauthorised entries, if the `customer_id` of "INVALID" is passed
/// the handler will return a 401 UNAUTHORISED response.
/// if the invalid UDM field is passed
/// the handler will return 400 BAD_REQUEST response.
async fn create_udm_events(
Json(payload): Json<data::UdmEvents>,
_user: User,
) -> impl IntoResponse {
let contains_invalid_events = payload.events.iter().any(|event| event.invalid.unwrap_or_default());
if payload.customer_id == "INVALID" {
(StatusCode::UNAUTHORIZED, Json(false))
} else if contains_invalid_events {
(StatusCode::BAD_REQUEST, Json(false))
}else {
data::add_udm_events_to_data(payload);
(StatusCode::CREATED, Json(true))
}
}

/// Handler that posts a set of unstructured log entries.
/// To test invalid entries, if a `log_type` of "INVALID" is passed
/// the handler will return a 400 BAD_REQUEST response.
Expand All @@ -76,7 +96,7 @@ async fn create_unstructured(
if payload.log_type == "INVALID" {
(StatusCode::BAD_REQUEST, Json(false))
} else {
data::add_to_data(payload);
data::add_unstructured_to_data(payload);
(StatusCode::CREATED, Json(true))
}
}
Expand Down Expand Up @@ -166,6 +186,7 @@ async fn main() {
"/v2/unstructuredlogentries:batchCreate",
post(create_unstructured),
)
.route("v2/udmevents:batchCreate", post(create_udm_events))
// Query the posted logs.
.route("/logs", get(logs));

Expand Down