From a9719b75d24d23f17b7e825df6c92fe84f37988f Mon Sep 17 00:00:00 2001 From: JHeil Date: Fri, 23 Aug 2024 11:53:47 +0200 Subject: [PATCH 1/5] Add tags to sentry configuration --- relay-log/src/setup.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index 16070b0e8c4..8a1ecaf051e 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -1,8 +1,10 @@ use std::borrow::Cow; +use std::collections::HashMap; 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; @@ -174,6 +176,9 @@ pub struct SentryConfig { /// Sets the environment for this service. pub environment: Option>, + /// Add custom tags to the events produced by Relay + pub 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, @@ -194,6 +199,7 @@ impl Default for SentryConfig { .ok(), enabled: false, environment: None, + tags: None, _crash_db: None, } } @@ -289,7 +295,7 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { logs_subscriber.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()), @@ -297,7 +303,23 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { environment: sentry.environment.clone(), traces_sample_rate: config.traces_sample_rate, ..Default::default() - }); + }; + + // If `tags` is set in Sentry configuration install the before_send hook + // in order to inject said tags into each event + if let Some(tags) = sentry.tags.as_ref() { + // We need an object that will outlive `sentry` lifetime + let tags = tags.clone(); + // Install hook + options.before_send = Some(Arc::new(move |mut event| { + event + .tags + .extend(tags.iter().map(|(k, v)| (k.to_owned(), v.to_owned()))); + Some(event) + })); + } + + let guard = sentry::init(options); // Keep the client initialized. The client is flushed manually in `main`. std::mem::forget(guard); From 1a66ff27ade864338eeed787106938b8f0230dd2 Mon Sep 17 00:00:00 2001 From: JHeil Date: Thu, 29 Aug 2024 10:59:51 +0200 Subject: [PATCH 2/5] Extend event.tags with default_tags and not the other way around --- relay-log/src/setup.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index 8a1ecaf051e..fc8aebef5f8 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::env; use std::fmt::{self, Display}; use std::path::PathBuf; @@ -176,8 +176,8 @@ pub struct SentryConfig { /// Sets the environment for this service. pub environment: Option>, - /// Add custom tags to the events produced by Relay - pub tags: Option>, + /// Add defaults tags to the tags' events produced 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. @@ -199,7 +199,7 @@ impl Default for SentryConfig { .ok(), enabled: false, environment: None, - tags: None, + default_tags: None, _crash_db: None, } } @@ -305,16 +305,14 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { ..Default::default() }; - // If `tags` is set in Sentry configuration install the before_send hook + // 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(tags) = sentry.tags.as_ref() { - // We need an object that will outlive `sentry` lifetime - let tags = tags.clone(); + if let Some(default_tags) = sentry.default_tags.clone() { // Install hook options.before_send = Some(Arc::new(move |mut event| { - event - .tags - .extend(tags.iter().map(|(k, v)| (k.to_owned(), v.to_owned()))); + // Don't override `event.tags` with `default_tags` + let previous_event_tags = std::mem::replace(&mut event.tags, default_tags.clone()); + event.tags.extend(previous_event_tags.into_iter()); Some(event) })); } From 219339ae51ef3a1a9cd653ef234ecff5656b1b8c Mon Sep 17 00:00:00 2001 From: JHeil Date: Thu, 29 Aug 2024 11:05:09 +0200 Subject: [PATCH 3/5] Improve comments --- relay-log/src/setup.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index fc8aebef5f8..ffe2643b499 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -176,7 +176,7 @@ pub struct SentryConfig { /// Sets the environment for this service. pub environment: Option>, - /// Add defaults tags to the tags' events produced by Relay + /// 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 @@ -310,7 +310,7 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { if let Some(default_tags) = sentry.default_tags.clone() { // Install hook options.before_send = Some(Arc::new(move |mut event| { - // Don't override `event.tags` with `default_tags` + // 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.into_iter()); Some(event) From 7c1746e33ecf9d418f273e643964f8b28c4c941f Mon Sep 17 00:00:00 2001 From: JHeil Date: Mon, 2 Sep 2024 15:39:20 +0200 Subject: [PATCH 4/5] Remove extraneous into_iter() --- relay-log/src/setup.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index ffe2643b499..75df6b7d981 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -312,7 +312,7 @@ pub fn init(config: &LogConfig, sentry: &SentryConfig) { 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.into_iter()); + event.tags.extend(previous_event_tags); Some(event) })); } From 8d4e575f261f3011389822483a8b13f3d488bf95 Mon Sep 17 00:00:00 2001 From: David Herberth Date: Wed, 18 Sep 2024 11:28:37 +0200 Subject: [PATCH 5/5] add changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ae37e45eaa..93b80f284a6 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**: