From 74500abb620c2a5e41f0ad3568fea0cf8f18dbf7 Mon Sep 17 00:00:00 2001 From: Tim Diekmann <21277928+TimDiekmann@users.noreply.github.com> Date: Fri, 19 Jan 2024 10:47:55 +0100 Subject: [PATCH] H-1928: Make sure all Rust spans are captured in Sentry (#3901) --- .env | 2 +- apps/hash-graph/libs/api/src/rest/status.rs | 3 ++ infra/terraform/hash/main.tf | 4 +++ libs/@local/tracing/src/lib.rs | 2 +- libs/@local/tracing/src/sentry.rs | 38 +++++++++++++++++++-- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/.env b/.env index 7da169130b6..8053192495c 100644 --- a/.env +++ b/.env @@ -49,7 +49,7 @@ HASH_TEMPORAL_VISIBILITY_PG_DATABASE=temporal_visibility HASH_GRAPH_PG_USER=graph HASH_GRAPH_PG_PASSWORD=graph HASH_GRAPH_PG_DATABASE=graph -HASH_GRAPH_LOG_LEVEL=trace,h2=info,tokio_util=debug,tower=info,tonic=debug,hyper=info,tokio_postgres=info,rustls=info +HASH_GRAPH_LOG_LEVEL=trace,h2=info,tokio_util=debug,tower=info,tonic=debug,hyper=info,tokio_postgres=info,rustls=info,tarpc=warn HASH_GRAPH_TYPE_FETCHER_HOST=localhost HASH_GRAPH_TYPE_FETCHER_PORT=4444 diff --git a/apps/hash-graph/libs/api/src/rest/status.rs b/apps/hash-graph/libs/api/src/rest/status.rs index 2e03ed4a20a..4ab941a6afb 100644 --- a/apps/hash-graph/libs/api/src/rest/status.rs +++ b/apps/hash-graph/libs/api/src/rest/status.rs @@ -30,6 +30,9 @@ where .copied() .or_else(|| report.request_value::().next()) .unwrap_or(StatusCode::Internal); + // TODO: Currently, this mostly duplicates the error printed below, when more information is + // added to the `Report` event consider commenting in this line again. + // hash_tracing::sentry::capture_report(&report); tracing::error!(error = ?report, tags.code = ?status_code.to_http_code()); status_to_response(Status::new( diff --git a/infra/terraform/hash/main.tf b/infra/terraform/hash/main.tf index 06def7861e1..2ca354289d2 100644 --- a/infra/terraform/hash/main.tf +++ b/infra/terraform/hash/main.tf @@ -233,6 +233,8 @@ module "application" { value = sensitive(data.vault_kv_secret_v2.secrets.data["graph_sentry_dsn"]) }, { name = "HASH_GRAPH_SENTRY_ENVIRONMENT", secret = false, value = "production" }, + { name = "HASH_GRAPH_SENTRY_EVENT_FILTER", secret = false, value = "debug" }, + { name = "HASH_GRAPH_SENTRY_SPAN_FILTER", secret = false, value = "trace" }, ]) graph_env_vars = concat(var.hash_graph_env_vars, [ { name = "HASH_GRAPH_PG_USER", secret = false, value = "graph" }, @@ -252,6 +254,8 @@ module "application" { value = sensitive(data.vault_kv_secret_v2.secrets.data["graph_sentry_dsn"]) }, { name = "HASH_GRAPH_SENTRY_ENVIRONMENT", secret = false, value = "production" }, + { name = "HASH_GRAPH_SENTRY_EVENT_FILTER", secret = false, value = "debug" }, + { name = "HASH_GRAPH_SENTRY_SPAN_FILTER", secret = false, value = "trace" }, ]) # The type fetcher uses the same image as the graph right now type_fetcher_image = module.graph_ecr diff --git a/libs/@local/tracing/src/lib.rs b/libs/@local/tracing/src/lib.rs index 4b134edd8d8..a118c7b6d0f 100644 --- a/libs/@local/tracing/src/lib.rs +++ b/libs/@local/tracing/src/lib.rs @@ -70,7 +70,7 @@ pub async fn init_tracing(config: TracingConfig) -> Result() -> SentryLayer +pub fn layer(config: &SentryConfig) -> SentryLayer where S: Subscriber + for<'a> LookupSpan<'a>, { - ::sentry::integrations::tracing::layer().enable_span_attributes() + let mut layer = ::sentry::integrations::tracing::layer(); + if config.sentry_enable_span_attributes { + layer = layer.enable_span_attributes(); + } + let span_filter = config.sentry_event_filter; + let event_filter = config.sentry_event_filter; + layer + .span_filter(move |metadata| *metadata.level() <= span_filter) + .event_filter(move |metadata| match *metadata.level() { + tracing::Level::ERROR => EventFilter::Exception, + level if level <= event_filter => EventFilter::Breadcrumb, + _ => EventFilter::Ignore, + }) } fn read_source(location: Location) -> (Vec, Option, Vec) {