Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sentry): Add tags to sentry configuration #3944

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**:
Expand Down
24 changes: 22 additions & 2 deletions relay-log/src/setup.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -171,6 +173,9 @@ pub struct SentryConfig {
/// Sets the environment for this service.
pub environment: Option<Cow<'static, str>>,

/// Add defaults tags to the events emitted by Relay
pub default_tags: Option<BTreeMap<String, String>>,

/// 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<PathBuf>,
Expand All @@ -191,6 +196,7 @@ impl Default for SentryConfig {
.ok(),
enabled: false,
environment: None,
default_tags: None,
_crash_db: None,
}
}
Expand Down Expand Up @@ -281,15 +287,29 @@ 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()),
attach_stacktrace: config.enable_backtraces,
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);
Expand Down
Loading