Skip to content

Commit

Permalink
feat: make edge log bad request information from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Oct 24, 2023
1 parent 4248164 commit d3cdd44
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
10 changes: 5 additions & 5 deletions server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;
use serde_json::json;
use tracing::debug;

use crate::types::EdgeToken;
use crate::types::{EdgeToken, UnleashBadRequest};

pub const TRUST_PROXY_PARSE_ERROR: &str =
"needs to be a valid ip address (ipv4 or ipv6) or a valid cidr (ipv4 or ipv6)";
Expand Down Expand Up @@ -98,7 +98,7 @@ pub enum EdgeError {
FeatureNotFound(String),
PersistenceError(String),
EdgeMetricsError,
EdgeMetricsRequestError(StatusCode),
EdgeMetricsRequestError(StatusCode, Option<UnleashBadRequest>),
EdgeTokenError,
EdgeTokenParseError,
InvalidBackupFile(String, String),
Expand Down Expand Up @@ -158,8 +158,8 @@ impl Display for EdgeError {
EdgeError::InvalidServerUrl(msg) => write!(f, "Failed to parse server url: [{msg}]"),
EdgeError::EdgeTokenError => write!(f, "Edge token error"),
EdgeError::EdgeTokenParseError => write!(f, "Failed to parse token response"),
EdgeError::EdgeMetricsRequestError(status_code) => {
write!(f, "Failed to post metrics with status code: {status_code}")
EdgeError::EdgeMetricsRequestError(status_code, message) => {
write!(f, "Failed to post metrics with status code: {status_code} and response {message:?}")
}
EdgeError::AuthorizationPending => {
write!(f, "No validation for token has happened yet")
Expand Down Expand Up @@ -226,7 +226,7 @@ impl ResponseError for EdgeError {
EdgeError::FrontendNotYetHydrated(_) => StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
EdgeError::ContextParseError => StatusCode::BAD_REQUEST,
EdgeError::ServiceAccountTokenNotEnabled => StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
EdgeError::EdgeMetricsRequestError(status_code) => *status_code,
EdgeError::EdgeMetricsRequestError(status_code, _) => *status_code,
EdgeError::HealthCheckError(_) => StatusCode::INTERNAL_SERVER_ERROR,
EdgeError::ReadyCheckError(_) => StatusCode::INTERNAL_SERVER_ERROR,
EdgeError::ClientHydrationFailed(_) => StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
6 changes: 2 additions & 4 deletions server/src/http/background_send_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn send_metrics_task(
if !batch.applications.is_empty() || !batch.metrics.is_empty() {
if let Err(edge_error) = unleash_client.send_batch_metrics(batch.clone()).await {
match edge_error {
EdgeError::EdgeMetricsRequestError(status_code) => {
EdgeError::EdgeMetricsRequestError(status_code, message) => {
METRICS_UPSTREAM_HTTP_ERRORS
.with_label_values(&[status_code.as_str()])
.inc();
Expand All @@ -46,9 +46,7 @@ pub async fn send_metrics_task(
size_of_batch(&batch)
),
StatusCode::BAD_REQUEST => {
error!(
"We couldn't format metrics properly. Will drop what we had"
);
error!("Unleash said [{message:?}]. Dropping our data to avoid consuming to much memory");
}
_ => {
warn!("Failed to send metrics. Status code was {status_code}. Will reinsert metrics for next attempt");
Expand Down
9 changes: 8 additions & 1 deletion server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use unleash_types::client_features::ClientFeatures;
use unleash_types::client_metrics::ClientApplication;

use crate::cli::ClientIdentity;
use crate::error::EdgeError::EdgeMetricsRequestError;
use crate::error::{CertificateError, FeatureError};
use crate::metrics::client_metrics::MetricsBatch;
use crate::tls::build_upstream_certificate;
Expand Down Expand Up @@ -425,7 +426,13 @@ impl UnleashClient {
if result.status().is_success() {
Ok(())
} else {
Err(EdgeError::EdgeMetricsRequestError(result.status()))
match result.status() {
StatusCode::BAD_REQUEST => Err(EdgeError::EdgeMetricsRequestError(
result.status(),
result.json().await.ok(),
)),
_ => Err(EdgeMetricsRequestError(result.status(), None)),
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ pub struct TokenRefresh {
pub last_check: Option<DateTime<Utc>>,
}

#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct UnleashValidationDetail {
pub path: Option<String>,
pub description: Option<String>,
pub message: Option<String>,
}

#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct UnleashBadRequest {
pub id: Option<String>,
pub name: Option<String>,
pub message: Option<String>,
pub details: Option<Vec<UnleashValidationDetail>>,
}

impl TokenRefresh {
pub fn new(token: EdgeToken, etag: Option<EntityTag>) -> Self {
Self {
Expand Down

0 comments on commit d3cdd44

Please sign in to comment.