Skip to content

Commit

Permalink
H-1928: Make sure all Rust spans are captured in Sentry (#3901)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann authored Jan 19, 2024
1 parent 81a66da commit 74500ab
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions apps/hash-graph/libs/api/src/rest/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ where
.copied()
.or_else(|| report.request_value::<StatusCode>().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(
Expand Down
4 changes: 4 additions & 0 deletions infra/terraform/hash/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libs/@local/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn init_tracing(config: TracingConfig) -> Result<impl Drop + 'static,
} else {
None
};
let sentry_layer = sentry::layer();
let sentry_layer = sentry::layer(&config.sentry);

tracing_subscriber::registry()
.with(filter)
Expand Down
38 changes: 35 additions & 3 deletions libs/@local/tracing/src/sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use clap::{builder::TypedValueParser, error::ErrorKind, Arg, Command, Error, Par
use error_stack::Report;
pub use sentry::release_name;
use sentry::{
integrations::tracing::SentryLayer,
integrations::tracing::{EventFilter, SentryLayer},
protocol::{Event, TemplateInfo},
types::Dsn,
ClientInitGuard, Hub, Level,
Expand Down Expand Up @@ -71,6 +71,7 @@ impl TypedValueParser for OptionalSentryDsnParser {
/// Arguments for configuring the logging setup
#[derive(Debug, Clone)]
#[cfg_attr(feature = "clap", derive(Parser))]
#[expect(clippy::struct_field_names, reason = "Used as clap arguments")]
pub struct SentryConfig {
// we need to qualify `Option` here, as otherwise `clap` tries to be too smart and only uses
// the `value_parser` on the internal `sentry::types::Dsn`, failing.
Expand All @@ -83,6 +84,25 @@ pub struct SentryConfig {
default_value_t = SentryEnvironment::default(),
))]
pub sentry_environment: SentryEnvironment,

/// Enable every parent span's attributes to be sent along with own event's attributes.
#[cfg_attr(
feature = "clap",
arg(long, env = "HASH_GRAPH_SENTRY_ENABLE_SPAN_ATTRIBUTES",)
)]
pub sentry_enable_span_attributes: bool,

#[cfg_attr(
feature = "clap",
arg(long, env = "HASH_GRAPH_SENTRY_SPAN_FILTER", default_value_t = tracing::Level::INFO)
)]
pub sentry_span_filter: tracing::Level,

#[cfg_attr(
feature = "clap",
arg(long, env = "HASH_GRAPH_SENTRY_EVENT_FILTER", default_value_t = tracing::Level::INFO)
)]
pub sentry_event_filter: tracing::Level,
}

pub fn init_sentry(
Expand Down Expand Up @@ -114,11 +134,23 @@ pub fn init_sentry(
}

#[must_use]
pub fn layer<S>() -> SentryLayer<S>
pub fn layer<S>(config: &SentryConfig) -> SentryLayer<S>
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<String>, Option<String>, Vec<String>) {
Expand Down

0 comments on commit 74500ab

Please sign in to comment.