Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2073,3 +2073,23 @@ webhooks_patch_1: |-
.unwrap();
webhooks_delete_1: |-
client.delete_webhook("WEBHOOK_UUID").await.unwrap();
customize_log_levels_1: |-
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
let new_log_level = NewLogLevel { target:"info".to_string() };
client.customize_log_levels(new_log_level).await.unwrap();
open_log_stream_1: |-
use futures::StreamExt;
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
let logs_config = LogStreamRequest {
target: "info".to_string(),
mode: LogMode::Human
};
let byte_stream = client.open_log_stream(logs_config).await.unwrap();
byte_s.for_each(|chunk| async {
if let Ok(chunk) = chunk {
println!("{}", String::from_utf8_lossy(&chunk));
}
}).await;
Comment on lines +2081 to +2092
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix type name and variable typo so the sample compiles.
GetLogs doesn’t exist in the new API surface and byte_s is undefined.

🔧 Proposed fix
-  let logs_config = GetLogs {
+  let logs_config = LogStreamRequest {
     target: "info".to_string(),
     mode: LogMode::Human
   };
   let byte_stream = client.open_log_stream(logs_config).await.unwrap();
-  byte_s.for_each(|chunk| async {
+  byte_stream.for_each(|chunk| async {
       if let Ok(chunk) = chunk {
         println!("{}", String::from_utf8_lossy(&chunk));
       }
   }).await;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use futures::StreamExt;
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
let logs_config = GetLogs {
target: "info".to_string(),
mode: LogMode::Human
};
let byte_stream = client.open_log_stream(logs_config).await.unwrap();
byte_s.for_each(|chunk| async {
if let Ok(chunk) = chunk {
println!("{}", String::from_utf8_lossy(&chunk));
}
}).await;
use futures::StreamExt;
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
let logs_config = LogStreamRequest {
target: "info".to_string(),
mode: LogMode::Human
};
let byte_stream = client.open_log_stream(logs_config).await.unwrap();
byte_stream.for_each(|chunk| async {
if let Ok(chunk) = chunk {
println!("{}", String::from_utf8_lossy(&chunk));
}
}).await;
🤖 Prompt for AI Agents
In @.code-samples.meilisearch.yaml around lines 2081 - 2092, Replace the invalid
type and variable names so the sample compiles: swap the old GetLogs type for
the current API name (e.g., GetLogsConfig or GetLogsOptions as exported by the
crate) and construct logs_config with that type, and fix the typo by using the
byte_stream variable returned from client.open_log_stream(...) (change
byte_s.for_each to byte_stream.for_each) while keeping the same closure logic;
references: GetLogs, logs_config, open_log_stream, byte_stream, byte_s.

interrupt_log_stream_1: |-
let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
client.interrupt_log_stream().await.unwrap();
57 changes: 57 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_json::{json, Value};
use std::{collections::HashMap, time::Duration};
use time::OffsetDateTime;

use crate::logs::NewLogLevel;
use crate::{
errors::*,
indexes::*,
Expand Down Expand Up @@ -1444,6 +1445,62 @@ impl<Http: HttpClient> Client<Http> {
.await
}

/// Customize logging levels for the default logging system.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, logs::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// let new_log_level = NewLogLevel { target:"info".to_string() };
/// client.customize_log_levels(new_log_level).await.unwrap();
///# });
/// ```
pub async fn customize_log_levels(&self, log_target: NewLogLevel) -> Result<(), Error> {
self.http_client
.request::<(), NewLogLevel, ()>(
&format!("{}/logs/stderr", self.host),
Method::Post {
query: (),
body: log_target,
},
204,
)
.await
}

/// Interrupt a log stream.
///
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, logs::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// client.interrupt_log_stream().await.unwrap();
///# });
/// ```
pub async fn interrupt_log_stream(&self) -> Result<(), Error> {
self.http_client
.request::<(), (), ()>(
&format!("{}/logs/stream", self.host),
Method::Delete { query: () },
204,
)
.await
}

fn sleep_backend(&self) -> SleepBackend {
SleepBackend::infer(self.http_client.is_tokio())
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,13 @@ pub mod features;
pub mod indexes;
/// Module containing the [`Key`](key::Key) struct.
pub mod key;
/// Module for logs related operations
pub mod logs;
/// Module for Network configuration API (sharding/remotes).
pub mod network;
pub mod request;
#[cfg(feature = "reqwest")]
pub mod reqwest;
/// Module related to search queries and results.
pub mod search;
/// Module containing [`Settings`](settings::Settings).
Expand All @@ -271,9 +275,6 @@ mod utils;
/// Module to manage webhooks.
pub mod webhooks;

#[cfg(feature = "reqwest")]
pub mod reqwest;

#[cfg(feature = "reqwest")]
pub type DefaultHttpClient = reqwest::ReqwestClient;

Expand Down
85 changes: 85 additions & 0 deletions src/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::client::Client;
use crate::errors::Error;
use bytes::Bytes;
use futures_core::Stream;
use serde::Serialize;

#[derive(Serialize)]
pub struct NewLogLevel {
pub target: String,
}

#[derive(Serialize)]
pub struct LogStreamRequest {
pub target: String,
pub mode: LogMode,
}

#[derive(Debug, Default, Clone, Copy, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum LogMode {
/// Output the logs in a human-readable form
#[default]
Human,
/// Output the logs in JSON format
Json,
/// Output the logs in Firefox profiler format for visualization
Profile,
}

impl Client<crate::reqwest::ReqwestClient> {
/// Opens a continuous stream of logs for focused debugging sessions.
/// The stream will continue to run indefinitely until you
/// [interrupt](Client::interrupt_log_stream) it.
///
/// The function returns a [`Stream`] that you can iterate over with
/// the [`StreamExt`] trait to process the logs.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, logs::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// let logs_config = LogStreamRequest {
/// target: "info".to_string(),
/// mode: LogMode::Human
/// };
/// let byte_stream = client.open_log_stream(logs_config).await.unwrap();
///# });
/// ```
pub async fn open_log_stream(
&self,
get_logs: LogStreamRequest,
) -> Result<impl Stream<Item = Result<Bytes, reqwest::Error>>, Error> {
let res = self
.http_client
.inner()
.post(format!("{}/logs/stream", self.host))
.header("Content-Type", "application/json")
.body(serde_json::to_string(&get_logs)?)
.send()
.await?;
res.error_for_status_ref()?;
Ok(res.bytes_stream())
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

#[cfg(test)]
mod tests {
use super::*;
use meilisearch_test_macro::meilisearch_test;
#[meilisearch_test]
async fn test_open_log_stream(client: Client) {
let logs_config = LogStreamRequest {
mode: LogMode::Human,
target: "info".to_string(),
};
assert!(client.open_log_stream(logs_config).await.is_ok());
assert!(client.interrupt_log_stream().await.is_ok());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}