Skip to content

Commit

Permalink
Merge pull request #4344 from systeminit/feat/loop-over-telemetry-levels
Browse files Browse the repository at this point in the history
feat/loop over telemetry levels
  • Loading branch information
johnrwatson authored Aug 15, 2024
2 parents 8e05c3f + af733c8 commit f315715
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 63 deletions.
16 changes: 2 additions & 14 deletions lib/telemetry-application-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ struct TelemetrySignalHandlerTask {
client: ApplicationTelemetryClient,
shutdown_token: CancellationToken,
sig_usr1: unix::Signal,
sig_usr2: unix::Signal,
}

impl TelemetrySignalHandlerTask {
Expand All @@ -559,13 +558,11 @@ impl TelemetrySignalHandlerTask {
shutdown_token: CancellationToken,
) -> io::Result<Self> {
let sig_usr1 = unix::signal(SignalKind::user_defined1())?;
let sig_usr2 = unix::signal(SignalKind::user_defined2())?;

Ok(Self {
client,
shutdown_token,
sig_usr1,
sig_usr2,
})
}

Expand All @@ -577,20 +574,11 @@ impl TelemetrySignalHandlerTask {
break;
}
Some(_) = self.sig_usr1.recv() => {
if let Err(err) = self.client.increase_verbosity().await {
if let Err(err) = self.client.modify_verbosity().await {
warn!(
task = Self::NAME,
error = ?err,
"error while trying to increase verbosity",
);
}
}
Some(_) = self.sig_usr2.recv() => {
if let Err(err) = self.client.decrease_verbosity().await {
warn!(
task = Self::NAME,
error = ?err,
"error while trying to decrease verbosity",
"error while trying to modify verbosity",
);
}
}
Expand Down
64 changes: 15 additions & 49 deletions lib/telemetry-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ impl SpanExt for tracing::Span {
#[async_trait]
pub trait TelemetryClient: Clone + Send + Sync + 'static {
async fn set_verbosity(&mut self, updated: Verbosity) -> Result<(), ClientError>;
async fn increase_verbosity(&mut self) -> Result<(), ClientError>;
async fn decrease_verbosity(&mut self) -> Result<(), ClientError>;
async fn modify_verbosity(&mut self) -> Result<(), ClientError>;
async fn set_custom_tracing(
&mut self,
directives: impl Into<String> + Send + 'async_trait,
Expand Down Expand Up @@ -214,10 +213,10 @@ impl ApplicationTelemetryClient {
Ok(())
}

pub async fn increase_verbosity_and_wait(&mut self) -> Result<(), ClientError> {
pub async fn modify_verbosity_and_wait(&mut self) -> Result<(), ClientError> {
let (tx, rx) = oneshot::channel();

self.increase_verbosity_inner(Some(tx)).await?;
self.modify_verbosity_inner(Some(tx)).await?;

if let Err(err) = rx.await {
warn!(
Expand All @@ -229,21 +228,6 @@ impl ApplicationTelemetryClient {
Ok(())
}

pub async fn decrease_verbosity_and_wait(&mut self) -> Result<(), ClientError> {
let (tx, rx) = oneshot::channel();

self.decrease_verbosity_inner(Some(tx)).await?;

if let Err(err) = rx.await {
warn!(
error = ?err,
"sender already closed while waiting on verbosity decrease change",
);
}

Ok(())
}

pub async fn set_custom_tracing_and_wait(
&mut self,
directives: impl Into<String> + Send,
Expand Down Expand Up @@ -292,31 +276,17 @@ impl ApplicationTelemetryClient {
Ok(())
}

async fn increase_verbosity_inner(
&mut self,
wait: Option<oneshot::Sender<()>>,
) -> Result<(), ClientError> {
let guard = self.tracing_level.lock().await;

match guard.deref() {
TracingLevel::Verbosity { verbosity, .. } => {
let updated = verbosity.increase();
drop(guard);
self.set_verbosity_inner(updated, wait).await
}
TracingLevel::Custom(_) => Err(ClientError::CustomHasNoVerbosity),
}
}

async fn decrease_verbosity_inner(
async fn modify_verbosity_inner(
&mut self,
wait: Option<oneshot::Sender<()>>,
) -> Result<(), ClientError> {
let guard = self.tracing_level.lock().await;

match guard.deref() {
TracingLevel::Verbosity { verbosity, .. } => {
let updated = verbosity.decrease();
let updated = match verbosity.is_max() {
true => Verbosity::InfoAll,
false => verbosity.increase(),
};
drop(guard);
self.set_verbosity_inner(updated, wait).await
}
Expand Down Expand Up @@ -349,12 +319,8 @@ impl TelemetryClient for ApplicationTelemetryClient {
self.set_verbosity_inner(updated, None).await
}

async fn increase_verbosity(&mut self) -> Result<(), ClientError> {
self.increase_verbosity_inner(None).await
}

async fn decrease_verbosity(&mut self) -> Result<(), ClientError> {
self.decrease_verbosity_inner(None).await
async fn modify_verbosity(&mut self) -> Result<(), ClientError> {
self.modify_verbosity_inner(None).await
}

async fn set_custom_tracing(
Expand Down Expand Up @@ -385,11 +351,7 @@ impl TelemetryClient for NoopClient {
Ok(())
}

async fn increase_verbosity(&mut self) -> Result<(), ClientError> {
Ok(())
}

async fn decrease_verbosity(&mut self) -> Result<(), ClientError> {
async fn modify_verbosity(&mut self) -> Result<(), ClientError> {
Ok(())
}

Expand Down Expand Up @@ -495,6 +457,10 @@ impl Verbosity {
!matches!(self, Self::InfoAll)
}

fn is_max(&self) -> bool {
matches!(self, Self::TraceAll)
}

#[inline]
fn as_u8(self) -> u8 {
self.into()
Expand Down

0 comments on commit f315715

Please sign in to comment.