-
Notifications
You must be signed in to change notification settings - Fork 25
feat: new version and tokio local key diags #187
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
# Copyright 2024 FastLabs Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
[package] | ||
name = "logforth-diagnostic-task-local" | ||
version = "0.3.0" | ||
|
||
description = "Task-local diagnostic for Logforth." | ||
keywords = ["logging", "log", "async", "task", "future"] | ||
|
||
categories.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
readme.workspace = true | ||
repository.workspace = true | ||
rust-version.workspace = true | ||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[dependencies] | ||
logforth-core = { workspace = true } | ||
pin-project = { workspace = true } | ||
|
||
[dev-dependencies] | ||
log = { workspace = true } | ||
|
||
[lints] | ||
workspace = true |
This file contains hidden or 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,139 @@ | ||
// Copyright 2024 FastLabs Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! A diagnostic that stores key-value pairs in a task-local map. | ||
//! | ||
//! # Examples | ||
//! | ||
//! ``` | ||
//! use logforth_core::Diagnostic; | ||
//! use logforth_core::kv::Visitor; | ||
//! use logforth_diagnostic_task_local::FutureExt; | ||
//! | ||
//! let fut = async { log::info!("Hello, world!") }; | ||
//! fut.with_task_local_context([("key".into(), "value".into())]); | ||
//! ``` | ||
|
||
use std::cell::RefCell; | ||
use std::pin::Pin; | ||
use std::task::Context; | ||
use std::task::Poll; | ||
|
||
use logforth_core::Diagnostic; | ||
use logforth_core::Error; | ||
use logforth_core::kv::Key; | ||
use logforth_core::kv::Value; | ||
use logforth_core::kv::Visitor; | ||
|
||
thread_local! { | ||
static TASK_LOCAL_MAP: RefCell<Vec<(String, String)>> = const { RefCell::new(Vec::new()) }; | ||
} | ||
|
||
/// A diagnostic that stores key-value pairs in a task-local context. | ||
/// | ||
/// See [module-level documentation](self) for usage examples. | ||
#[derive(Default, Debug, Clone, Copy)] | ||
#[non_exhaustive] | ||
pub struct TaskLocalDiagnostic {} | ||
|
||
impl Diagnostic for TaskLocalDiagnostic { | ||
fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> { | ||
TASK_LOCAL_MAP.with(|map| { | ||
let map = map.borrow(); | ||
for (key, value) in map.iter() { | ||
let key = Key::new_ref(key.as_str()); | ||
let value = Value::from(value.as_str()); | ||
visitor.visit(key, value)?; | ||
} | ||
Ok(()) | ||
}) | ||
} | ||
} | ||
|
||
/// An extension trait for futures to run them with a task-local context. | ||
/// | ||
/// See [module-level documentation](self) for usage examples. | ||
pub trait FutureExt: Future { | ||
/// Run a future with a task-local context. | ||
fn with_task_local_context( | ||
self, | ||
kvs: impl IntoIterator<Item = (String, String)>, | ||
) -> impl Future<Output = Self::Output> | ||
where | ||
Self: Sized, | ||
{ | ||
TaskLocalFuture { | ||
future: Some(self), | ||
context: kvs.into_iter().collect(), | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> FutureExt for F {} | ||
|
||
#[pin_project::pin_project] | ||
struct TaskLocalFuture<F> { | ||
#[pin] | ||
future: Option<F>, | ||
context: Vec<(String, String)>, | ||
} | ||
|
||
impl<F: Future> Future for TaskLocalFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
|
||
let mut fut = this.future; | ||
if let Some(future) = fut.as_mut().as_pin_mut() { | ||
struct Guard { | ||
n: usize, | ||
} | ||
|
||
impl Drop for Guard { | ||
fn drop(&mut self) { | ||
TASK_LOCAL_MAP.with(|map| { | ||
let mut map = map.borrow_mut(); | ||
for _ in 0..self.n { | ||
map.pop(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
TASK_LOCAL_MAP.with(|map| { | ||
let mut map = map.borrow_mut(); | ||
for (key, value) in this.context.iter() { | ||
map.push((key.clone(), value.clone())); | ||
} | ||
}); | ||
Comment on lines
+115
to
+120
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. This may not be quite efficient. But generally kvs are small and we can anyway improve later. |
||
|
||
let n = this.context.len(); | ||
let guard = Guard { n }; | ||
|
||
let result = match future.poll(cx) { | ||
Poll::Ready(output) => { | ||
fut.set(None); | ||
Poll::Ready(output) | ||
} | ||
Poll::Pending => Poll::Pending, | ||
}; | ||
|
||
drop(guard); | ||
return result; | ||
} | ||
|
||
unreachable!("TaskLocalFuture polled after completion"); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
I'd prefer a shorter name, but not have a good idea now.