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

chore: more logging #250

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
114 changes: 61 additions & 53 deletions src/handlers/push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
},
serde::{Deserialize, Serialize},
std::sync::Arc,
tracing::instrument,
};

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -66,6 +67,8 @@ pub async fn handler(
let inner_packed = match res {
Ok((res, analytics_options_inner)) => (res.status().as_u16(), res, analytics_options_inner),
Err((error, analytics_option_inner)) => {
warn!("error handling push message: {error:?}");

#[cfg(feature = "analytics")]
let error_str = format!("{:?}", &error);
let res = error.into_response();
Expand Down Expand Up @@ -129,6 +132,7 @@ pub async fn handler(
Ok(response)
}

#[instrument(skip_all, fields(tenant_id, client_id = id, id = body.id))]
pub async fn handler_internal(
Path((tenant_id, id)): Path<(String, String)>,
StateExtractor(state): StateExtractor<Arc<AppState>>,
Expand Down Expand Up @@ -340,6 +344,7 @@ pub async fn handler_internal(
);

if tenant.suspended {
warn!("tenant suspended");
return Err((Error::TenantSuspended, analytics.clone()));
}

Expand All @@ -357,60 +362,63 @@ pub async fn handler_internal(

match provider.send_notification(client.token, body.payload).await {
Ok(()) => Ok(()),
Err(error) => match error {
Error::BadDeviceToken => {
state
.client_store
.delete_client(&tenant_id, &id)
.await
.map_err(|e| (Error::Store(e), analytics.clone()))?;
increment_counter!(state.metrics, client_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"client has been deleted due to a bad device token"
);
Err(Error::ClientDeleted)
}
Error::BadApnsCredentials => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid APNS Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
Error::BadFcmApiKey => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid FCM Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
Err(error) => {
warn!("error sending notification: {error:?}");
match error {
Error::BadDeviceToken => {
state
.client_store
.delete_client(&tenant_id, &id)
.await
.map_err(|e| (Error::Store(e), analytics.clone()))?;
increment_counter!(state.metrics, client_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"client has been deleted due to a bad device token"
);
Err(Error::ClientDeleted)
}
Error::BadApnsCredentials => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid APNS Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
Error::BadFcmApiKey => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid FCM Credentials")
.await
.map_err(|e| (e, analytics.clone()))?;
increment_counter!(state.metrics, tenant_suspensions);
warn!(
%request_id,
%tenant_id,
client_id = %id,
notification_id = %notification.id,
push_type = client.push_type.as_str(),
"tenant has been suspended due to invalid provider credentials"
);
Err(Error::TenantSuspended)
}
e => Err(e),
}
e => Err(e),
},
}
}
.map_err(|e| (e, analytics.clone()))?;

Expand Down
2 changes: 1 addition & 1 deletion src/providers/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ impl PushProvider for NoopProvider {
impl NoopProvider {
/// Insert empty notifications for a new token
fn bootstrap(&mut self, token: String) {
self.notifications.entry(token).or_insert_with(Vec::new);
self.notifications.entry(token).or_default();
}
}