-
Notifications
You must be signed in to change notification settings - Fork 10
Replay aware logger #48
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
Show all changes
3 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 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,37 @@ | ||
use restate_sdk::prelude::*; | ||
use std::time::Duration; | ||
use tracing::info; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer}; | ||
|
||
#[restate_sdk::service] | ||
trait Greeter { | ||
async fn greet(name: String) -> Result<String, HandlerError>; | ||
} | ||
|
||
struct GreeterImpl; | ||
|
||
impl Greeter for GreeterImpl { | ||
async fn greet(&self, ctx: Context<'_>, name: String) -> Result<String, HandlerError> { | ||
info!("Before sleep"); | ||
ctx.sleep(Duration::from_secs(61)).await?; // More than suspension timeout to trigger replay | ||
info!("After sleep"); | ||
Ok(format!("Greetings {name}")) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "info,restate_sdk=debug".into()); | ||
let replay_filter = restate_sdk::filter::ReplayAwareFilter; | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::fmt::layer() | ||
.with_filter(env_filter) | ||
.with_filter(replay_filter), | ||
) | ||
.init(); | ||
HttpServer::new(Endpoint::builder().bind(GreeterImpl.serve()).build()) | ||
.listen_and_serve("0.0.0.0:9080".parse().unwrap()) | ||
.await; | ||
} |
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,90 @@ | ||
//! Replay aware tracing filter. | ||
|
||
use std::fmt::Debug; | ||
use tracing::{ | ||
field::{Field, Visit}, | ||
span::{Attributes, Record}, | ||
Event, Id, Metadata, Subscriber, | ||
}; | ||
use tracing_subscriber::{ | ||
layer::{Context, Filter}, | ||
registry::LookupSpan, | ||
Layer, | ||
}; | ||
|
||
#[derive(Debug)] | ||
struct ReplayField(bool); | ||
|
||
struct ReplayFieldVisitor(bool); | ||
|
||
impl Visit for ReplayFieldVisitor { | ||
fn record_bool(&mut self, field: &Field, value: bool) { | ||
if field.name().eq("restate.sdk.is_replaying") { | ||
self.0 = value; | ||
} | ||
} | ||
|
||
fn record_debug(&mut self, _field: &Field, _value: &dyn Debug) {} | ||
} | ||
|
||
/// Replay aware tracing filter. | ||
/// | ||
/// Use this filter to skip tracing events in the service while replaying: | ||
/// | ||
/// ```rust,no_run | ||
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer}; | ||
/// tracing_subscriber::registry() | ||
/// .with( | ||
/// tracing_subscriber::fmt::layer() | ||
/// // Default Env filter to read RUST_LOG | ||
/// .with_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
/// // Replay aware filter | ||
/// .with_filter(restate_sdk::filter::ReplayAwareFilter) | ||
/// ) | ||
/// .init(); | ||
/// ``` | ||
pub struct ReplayAwareFilter; | ||
|
||
impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Filter<S> for ReplayAwareFilter { | ||
fn enabled(&self, _meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool { | ||
true | ||
} | ||
|
||
fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, S>) -> bool { | ||
if let Some(scope) = cx.event_scope(event) { | ||
let iterator = scope.from_root(); | ||
for span in iterator { | ||
if span.name() == "restate_sdk_endpoint_handle" { | ||
if let Some(replay) = span.extensions().get::<ReplayField>() { | ||
return !replay.0; | ||
} | ||
} | ||
} | ||
} | ||
true | ||
} | ||
|
||
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { | ||
if let Some(span) = ctx.span(id) { | ||
if span.name() == "restate_sdk_endpoint_handle" { | ||
let mut visitor = ReplayFieldVisitor(false); | ||
attrs.record(&mut visitor); | ||
let mut extensions = span.extensions_mut(); | ||
extensions.replace::<ReplayField>(ReplayField(visitor.0)); | ||
} | ||
} | ||
} | ||
|
||
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { | ||
if let Some(span) = ctx.span(id) { | ||
if span.name() == "restate_sdk_endpoint_handle" { | ||
let mut visitor = ReplayFieldVisitor(false); | ||
values.record(&mut visitor); | ||
let mut extensions = span.extensions_mut(); | ||
extensions.replace::<ReplayField>(ReplayField(visitor.0)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<S: Subscriber> Layer<S> for ReplayAwareFilter {} |
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.
Uh oh!
There was an error while loading. Please reload this page.