Skip to content

Commit 879dbbb

Browse files
authored
ref(server): Introduce partial scoping type (#5291)
A tiny bit more type safety. Also in a future PR I plan to implement `FromRequestParts` for `PartialScoping`.
1 parent a782318 commit 879dbbb

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

relay-server/src/extractors/request_meta.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,40 @@ impl RequestMeta {
415415

416416
/// Returns scoping information from the request.
417417
///
418-
/// The scoping returned from this function is not complete since it lacks info from the Project
419-
/// state. To fetch full scoping information, invoke the `GetScoping` message on `Project`.
420-
pub fn get_partial_scoping(&self) -> Scoping {
421-
Scoping {
422-
organization_id: OrganizationId::new(0),
423-
project_id: self.project_id().unwrap_or_else(|| ProjectId::new(0)),
418+
/// The scoping returned from this function is not complete since it lacks info from the Project.
419+
pub fn get_partial_scoping(&self) -> PartialScoping {
420+
PartialScoping {
421+
organization_id: None,
422+
project_id: self.project_id(),
424423
project_key: self.public_key(),
424+
}
425+
}
426+
}
427+
428+
/// A partial [`Scoping`].
429+
///
430+
/// The scoping may not be complete and missing information compared to a full scoping.
431+
#[derive(Debug, Copy, Clone)]
432+
pub struct PartialScoping {
433+
/// The organization id.
434+
pub organization_id: Option<OrganizationId>,
435+
/// The project id.
436+
pub project_id: Option<ProjectId>,
437+
/// The project key or public key.
438+
pub project_key: ProjectKey,
439+
}
440+
441+
impl PartialScoping {
442+
/// Creates a [`Scoping`] from this partial scoping, unknown information is filled with defaults.
443+
///
444+
/// The scoping returned from this function is not complete since it lacks info from the Project state.
445+
pub fn into_scoping(self) -> Scoping {
446+
Scoping {
447+
organization_id: self
448+
.organization_id
449+
.unwrap_or_else(|| OrganizationId::new(0)),
450+
project_id: self.project_id.unwrap_or_else(|| ProjectId::new(0)),
451+
project_key: self.project_key,
425452
key_id: None,
426453
}
427454
}

relay-server/src/managed/envelope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl ManagedEnvelope {
168168
pub fn new(envelope: Box<Envelope>, outcome_aggregator: Addr<TrackOutcome>) -> Self {
169169
let meta = &envelope.meta();
170170
let summary = EnvelopeSummary::compute(envelope.as_ref());
171-
let scoping = meta.get_partial_scoping();
171+
let scoping = meta.get_partial_scoping().into_scoping();
172172

173173
Self {
174174
envelope,

relay-server/src/services/projects/project/info.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,22 @@ impl ProjectInfo {
228228
/// The processor must fetch the full scoping before attempting to rate limit with partial
229229
/// scoping.
230230
pub fn scope_request(&self, meta: &RequestMeta) -> Scoping {
231-
let mut scoping = meta.get_partial_scoping();
231+
let partial = meta.get_partial_scoping();
232232

233233
// The key configuration may be missing if the event has been queued for extended times and
234234
// project was refetched in between. In such a case, access to key quotas is not availabe,
235235
// but we can gracefully execute all other rate limiting.
236-
scoping.key_id = self
236+
let key_id = self
237237
.get_public_key_config()
238238
.and_then(|config| config.numeric_id);
239239

240240
// The original project identifier is part of the DSN. If the DSN was moved to another
241241
// project, the actual project identifier is different and can be obtained from project
242242
// states. This is only possible when the project state has been loaded.
243-
if let Some(project_id) = self.project_id {
244-
scoping.project_id = project_id;
245-
}
243+
let project_id = self
244+
.project_id
245+
.or(partial.project_id)
246+
.unwrap_or(ProjectId::new(0));
246247

247248
// This is a hack covering three cases:
248249
// 1. Relay has not fetched the project state. In this case we have no way of knowing
@@ -255,9 +256,17 @@ impl ProjectInfo {
255256
// 3. An organization id is available and can be matched against rate limits. In this
256257
// project, all organizations will match automatically, unless the organization id
257258
// has changed since the last fetch.
258-
scoping.organization_id = self.organization_id.unwrap_or(OrganizationId::new(0));
259-
260-
scoping
259+
let organization_id = self
260+
.organization_id
261+
.or(partial.organization_id)
262+
.unwrap_or(OrganizationId::new(0));
263+
264+
Scoping {
265+
organization_id,
266+
project_id,
267+
project_key: partial.project_key,
268+
key_id,
269+
}
261270
}
262271

263272
/// Returns quotas declared in this project state.

relay-server/src/utils/multipart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl UnconstrainedMultipart {
412412
|outcome, quantity| {
413413
outcome_aggregator.send(TrackOutcome {
414414
timestamp: request_meta.received_at(),
415-
scoping: request_meta.get_partial_scoping(),
415+
scoping: request_meta.get_partial_scoping().into_scoping(),
416416
outcome,
417417
event_id: None,
418418
remote_addr: request_meta.remote_addr(),

0 commit comments

Comments
 (0)