Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo committed Aug 27, 2024
2 parents 8d1af52 + cce218f commit c61aab2
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

- Add `EnvelopeStore` trait and implement `DiskUsage` for tracking disk usage. ([#3925](https://github.com/getsentry/relay/pull/3925))

**Features**:

- Add configuration option to specify the instance type of Relay. ([#3938](https://github.com/getsentry/relay/pull/3938))
- Update definitions for user agent parsing. ([#3951](https://github.com/getsentry/relay/pull/3951))

## 24.8.0

**Bug Fixes**:
Expand Down
54 changes: 54 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ trait ConfigObject: DeserializeOwned + Serialize {
pub struct OverridableConfig {
/// The operation mode of this relay.
pub mode: Option<String>,
/// The instance type of this relay.
pub instance: Option<String>,
/// The log level of this relay.
pub log_level: Option<String>,
/// The upstream relay or sentry instance.
Expand Down Expand Up @@ -352,6 +354,44 @@ impl fmt::Display for RelayMode {
}
}

/// The instance type of Relay.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum RelayInstance {
/// This Relay is run as a default instance.
Default,

/// This Relay is run as a canary instance where experiments can be run.
Canary,
}

impl RelayInstance {
/// Returns `true` if the [`RelayInstance`] is of type [`RelayInstance::Canary`].
pub fn is_canary(&self) -> bool {
matches!(self, RelayInstance::Canary)
}
}

impl fmt::Display for RelayInstance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RelayInstance::Default => write!(f, "default"),
RelayInstance::Canary => write!(f, "canary"),
}
}
}

impl FromStr for RelayInstance {
type Err = fmt::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"canary" => Ok(RelayInstance::Canary),
_ => Ok(RelayInstance::Default),
}
}
}

/// Error returned when parsing an invalid [`RelayMode`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseRelayModeError;
Expand Down Expand Up @@ -436,6 +476,8 @@ impl Default for ReadinessCondition {
pub struct Relay {
/// The operation mode of this relay.
pub mode: RelayMode,
/// The instance type of this relay.
pub instance: RelayInstance,
/// The upstream relay or sentry instance.
pub upstream: UpstreamDescriptor<'static>,
/// The host the relay should bind to (network interface).
Expand Down Expand Up @@ -463,6 +505,7 @@ impl Default for Relay {
fn default() -> Self {
Relay {
mode: RelayMode::Managed,
instance: RelayInstance::Default,
upstream: "https://sentry.io/".parse().unwrap(),
host: default_host(),
port: 3000,
Expand Down Expand Up @@ -1617,6 +1660,12 @@ impl Config {
.with_context(|| ConfigError::field("mode"))?;
}

if let Some(deployment) = overrides.instance {
relay.instance = deployment
.parse::<RelayInstance>()
.with_context(|| ConfigError::field("deployment"))?;
}

if let Some(log_level) = overrides.log_level {
self.values.logging.level = log_level.parse()?;
}
Expand Down Expand Up @@ -1830,6 +1879,11 @@ impl Config {
self.values.relay.mode
}

/// Returns the instance type of relay.
pub fn relay_instance(&self) -> RelayInstance {
self.values.relay.instance
}

/// Returns the upstream target as descriptor.
pub fn upstream_descriptor(&self) -> &UpstreamDescriptor<'_> {
&self.values.relay.upstream
Expand Down
16 changes: 8 additions & 8 deletions relay-event-schema/src/protocol/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ pub struct User {
#[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")]
pub name: Annotated<String>,

/// The user string representation as handled in Sentry.
///
/// This field is computed by concatenating the name of specific fields of the `User`
/// struct with their value. For example, if `id` is set, `sentry_user` will be equal to
/// `"id:id-of-the-user".
#[metastructure(pii = "true", skip_serialization = "empty")]
pub sentry_user: Annotated<String>,

/// Approximate geographical location of the end user or device.
#[metastructure(skip_serialization = "empty")]
pub geo: Annotated<Geo>,
Expand All @@ -76,14 +84,6 @@ pub struct User {
#[metastructure(skip_serialization = "empty")]
pub segment: Annotated<String>,

/// The user string representation as handled in Sentry.
///
/// This field is computed by concatenating the name of specific fields of the `User`
/// struct with their value. For example, if `id` is set, `sentry_user` will be equal to
/// `"id:id-of-the-user".
#[metastructure(skip_serialization = "empty")]
pub sentry_user: Annotated<String>,

/// Additional arbitrary fields, as stored in the database (and sometimes as sent by clients).
/// All data from `self.other` should end up here after store normalization.
#[metastructure(pii = "true", skip_serialization = "empty")]
Expand Down
27 changes: 27 additions & 0 deletions relay-pii/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,33 @@ mod tests {
assert_debug_snapshot!(&data);
}

#[test]
fn test_sentry_user() {
let mut data = Event::from_value(
json!({
"user": {
"ip_address": "73.133.27.120",
"sentry_user": "ip:73.133.27.120",
},
})
.into(),
);

let scrubbing_config = DataScrubbingConfig {
scrub_data: true,
scrub_ip_addresses: true,
scrub_defaults: true,
..Default::default()
};

let pii_config = to_pii_config(&scrubbing_config).unwrap();
let mut pii_processor = PiiProcessor::new(pii_config.compiled());

process_value(&mut data, &mut pii_processor, ProcessingState::root()).unwrap();

assert_debug_snapshot!(&data);
}

#[test]
fn test_basic_stripping() {
let config = serde_json::from_str::<PiiConfig>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ Event {
},
),
name: ~,
sentry_user: ~,
geo: ~,
segment: ~,
sentry_user: ~,
data: ~,
other: {},
},
Expand Down
112 changes: 112 additions & 0 deletions relay-pii/src/snapshots/relay_pii__processor__tests__sentry_user.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
source: relay-pii/src/processor.rs
expression: "&data"
---
Event {
id: ~,
level: ~,
version: ~,
ty: ~,
fingerprint: ~,
culprit: ~,
transaction: ~,
transaction_info: ~,
time_spent: ~,
logentry: ~,
logger: ~,
modules: ~,
platform: ~,
timestamp: ~,
start_timestamp: ~,
received: ~,
server_name: ~,
release: ~,
dist: ~,
environment: ~,
site: ~,
user: User {
id: ~,
email: ~,
ip_address: Meta {
remarks: [
Remark {
ty: Substituted,
rule_id: "@ip:replace",
range: Some(
(
0,
4,
),
),
},
Remark {
ty: Removed,
rule_id: "@anything:remove",
range: None,
},
],
errors: [],
original_length: Some(
13,
),
original_value: None,
},
username: ~,
name: ~,
sentry_user: Annotated(
"ip:[ip]",
Meta {
remarks: [
Remark {
ty: Substituted,
rule_id: "@ip:replace",
range: Some(
(
3,
7,
),
),
},
],
errors: [],
original_length: Some(
16,
),
original_value: None,
},
),
geo: ~,
segment: ~,
data: ~,
other: {},
},
request: ~,
contexts: ~,
breadcrumbs: ~,
exceptions: ~,
stacktrace: ~,
template: ~,
threads: ~,
tags: ~,
extra: ~,
debug_meta: ~,
client_sdk: ~,
ingest_path: ~,
errors: ~,
key_id: ~,
project: ~,
grouping_config: ~,
checksum: ~,
csp: ~,
hpkp: ~,
expectct: ~,
expectstaple: ~,
spans: ~,
measurements: ~,
breakdowns: ~,
scraping_attempts: ~,
_metrics: ~,
_metrics_summary: ~,
_dsc: ~,
other: {},
}
16 changes: 16 additions & 0 deletions relay-server/src/services/processor/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::error::Error;
use std::net::IpAddr;

use bytes::Bytes;
use relay_base_schema::project::ProjectId;
use relay_dynamic_config::{Feature, GlobalConfig, ProjectConfig};
use relay_event_normalization::replay::{self, ReplayError};
use relay_event_normalization::RawUserAgentInfo;
Expand All @@ -28,6 +29,8 @@ pub fn process(
let replays_disabled = state.should_filter(Feature::SessionReplay);
let scrubbing_enabled = project_state.has_feature(Feature::SessionReplayRecordingScrubbing);
let replay_video_disabled = project_state.has_feature(Feature::SessionReplayVideoDisabled);
let project_id = project_state.project_id;
let organization_id = project_state.organization_id;

let meta = state.envelope().meta().clone();
let client_addr = meta.client_addr();
Expand Down Expand Up @@ -79,6 +82,8 @@ pub fn process(
global_config,
client_addr,
user_agent,
project_id,
organization_id,
)?;
item.set_payload(ContentType::Json, replay_event);
}
Expand All @@ -101,6 +106,8 @@ pub fn process(
user_agent,
scrubbing_enabled,
&mut scrubber,
project_id,
organization_id,
)?;
item.set_payload(ContentType::OctetStream, replay_video);
}
Expand All @@ -113,13 +120,16 @@ pub fn process(

// Replay Event Processing.

#[allow(clippy::too_many_arguments)]
fn handle_replay_event_item(
payload: Bytes,
event_id: &Option<EventId>,
config: &ProjectConfig,
global_config: &GlobalConfig,
client_ip: Option<IpAddr>,
user_agent: &RawUserAgentInfo<&str>,
project_id: Option<ProjectId>,
organization_id: Option<u64>,
) -> Result<Bytes, ProcessingError> {
let filter_settings = &config.filter_settings;

Expand Down Expand Up @@ -151,6 +161,8 @@ fn handle_replay_event_item(
relay_log::warn!(
error = &error as &dyn Error,
?event_id,
project_id = project_id.map(|v| v.value()),
organization_id = organization_id,
"invalid replay event"
);
Err(match error {
Expand Down Expand Up @@ -259,6 +271,8 @@ fn handle_replay_video_item(
user_agent: &RawUserAgentInfo<&str>,
scrubbing_enabled: bool,
scrubber: &mut RecordingScrubber,
project_id: Option<ProjectId>,
organization_id: Option<u64>,
) -> Result<Bytes, ProcessingError> {
let ReplayVideoEvent {
replay_event,
Expand All @@ -282,6 +296,8 @@ fn handle_replay_video_item(
global_config,
client_ip,
user_agent,
project_id,
organization_id,
)?;

// Process as a replay-recording envelope item.
Expand Down
2 changes: 2 additions & 0 deletions relay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub fn extract_config_args(matches: &ArgMatches) -> OverridableConfig {
secret_key: matches.get_one("secret_key").cloned(),
outcome_source: matches.get_one("source_id").cloned(),
shutdown_timeout: matches.get_one("shutdown_timeout").cloned(),
instance: matches.get_one("instance").cloned(),
}
}

Expand All @@ -117,6 +118,7 @@ pub fn extract_config_env_vars() -> OverridableConfig {
secret_key: env::var("RELAY_SECRET_KEY").ok(),
outcome_source: None, //already extracted in params
shutdown_timeout: env::var("SHUTDOWN_TIMEOUT").ok(),
instance: env::var("RELAY_INSTANCE").ok(),
}
}

Expand Down
Loading

0 comments on commit c61aab2

Please sign in to comment.