From cde599702cf2ece58c0d6f2c0f6693e5c34db504 Mon Sep 17 00:00:00 2001 From: JHeil <52839010+JHeilCoveo@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:41:41 +0200 Subject: [PATCH] feat(sentry): Add tags to sentry configuration (#3944) We have many regions for a given environment, when an issue arise it is impossible for us to know in which region the issue occurred as the only tag included in the sentry issue is the environment one. This adds configurable tags in sentry configuration. Each event sent by Sentry relay will have those tags appended. Fix #3842 --- CHANGELOG.md | 6 ++++++ relay-log/src/setup.rs | 24 ++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae37e45ea..93b80f284a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +**Features:** + +- Add a config option to add default tags to all Relay Sentry events. ([#3944](https://github.com/getsentry/relay/pull/3944)) + ## 24.9.0 **Bug Fixes**: diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index 8eaa96ac73..1d77a4960b 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -1,8 +1,10 @@ use std::borrow::Cow; +use std::collections::BTreeMap; use std::env; use std::fmt::{self, Display}; use std::path::PathBuf; use std::str::FromStr; +use std::sync::Arc; use relay_common::impl_str_serde; use sentry::types::Dsn; @@ -171,6 +173,9 @@ pub struct SentryConfig { /// Sets the environment for this service. pub environment: Option>, + /// Add defaults tags to the events emitted by Relay + pub default_tags: Option>, + /// Internal. Enables crash handling and sets the absolute path to where minidumps should be /// cached on disk. The path is created if it doesn't exist. Path must be UTF-8. pub _crash_db: Option, @@ -191,6 +196,7 @@ impl Default for SentryConfig { .ok(), enabled: false, environment: None, + default_tags: None, _crash_db: None, } } @@ -281,7 +287,7 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { .init(); if let Some(dsn) = sentry.enabled_dsn() { - let guard = sentry::init(sentry::ClientOptions { + let mut options = sentry::ClientOptions { dsn: Some(dsn).cloned(), in_app_include: vec!["relay"], release: Some(RELEASE.into()), @@ -289,7 +295,21 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { environment: sentry.environment.clone(), traces_sample_rate: config.traces_sample_rate, ..Default::default() - }); + }; + + // If `default_tags` is set in Sentry configuration install the `before_send` hook + // in order to inject said tags into each event + if let Some(default_tags) = sentry.default_tags.clone() { + // Install hook + options.before_send = Some(Arc::new(move |mut event| { + // Extend `event.tags` with `default_tags` without replacing tags already present + let previous_event_tags = std::mem::replace(&mut event.tags, default_tags.clone()); + event.tags.extend(previous_event_tags); + Some(event) + })); + } + + let guard = sentry::init(options); // Keep the client initialized. The client is flushed manually in `main`. std::mem::forget(guard);