Skip to content

Commit a6bc0a2

Browse files
authored
feat(profiles): Support profiler_id in context (#3714)
1 parent 9ec42ed commit a6bc0a2

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**Internal**:
1212

1313
- Aggregate metrics before rate limiting. ([#3746](https://github.com/getsentry/relay/pull/3746))
14+
- Accept profiler_id in the profile context. ([#3714](https://github.com/getsentry/relay/pull/3714))
1415

1516
## 24.6.0
1617

relay-event-schema/src/protocol/contexts/profile.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use crate::protocol::EventId;
1010
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
1111
pub struct ProfileContext {
1212
/// The profile ID.
13-
#[metastructure(required = "true")]
1413
pub profile_id: Annotated<EventId>,
14+
15+
/// The profiler ID.
16+
pub profiler_id: Annotated<EventId>,
1517
}
1618

1719
impl super::DefaultContext for ProfileContext {
@@ -60,6 +62,7 @@ mod tests {
6062
profile_id: Annotated::new(EventId(
6163
"4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
6264
)),
65+
..ProfileContext::default()
6366
})));
6467

6568
assert_eq!(context, Annotated::from_json(json).unwrap());
@@ -76,6 +79,20 @@ mod tests {
7679
profile_id: Annotated::new(EventId(
7780
"4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
7881
)),
82+
..ProfileContext::default()
83+
})));
84+
85+
assert_eq!(context, Annotated::from_json(json).unwrap());
86+
}
87+
88+
#[test]
89+
fn context_with_profiler_id() {
90+
let json = r#"{"profiler_id": "4C79F60C11214EB38604F4AE0781BFB2", "type": "profile"}"#;
91+
let context = Annotated::new(Context::Profile(Box::new(ProfileContext {
92+
profiler_id: Annotated::new(EventId(
93+
"4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
94+
)),
95+
..ProfileContext::default()
7996
})));
8097

8198
assert_eq!(context, Annotated::from_json(json).unwrap());

relay-event-schema/src/protocol/event.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ mod tests {
11561156
profile_id: Annotated::new(EventId(uuid!(
11571157
"abadcade-feed-dead-beef-8addadfeedaa"
11581158
))),
1159+
..ProfileContext::default()
11591160
});
11601161
contexts
11611162
}),

relay-server/src/services/processor/profile.rs

+89-1
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@ pub fn transfer_id(
7676
let contexts = event.contexts.get_or_insert_with(Contexts::new);
7777
contexts.add(ProfileContext {
7878
profile_id: Annotated::new(profile_id),
79+
..ProfileContext::default()
7980
});
8081
}
8182
None => {
8283
if let Some(contexts) = event.contexts.value_mut() {
83-
contexts.remove::<ProfileContext>();
84+
if let Some(profile_context) = contexts.get_mut::<ProfileContext>() {
85+
profile_context.profile_id = Annotated::empty();
86+
}
8487
}
8588
}
8689
}
@@ -239,6 +242,7 @@ mod tests {
239242
profile_id: EventId(
240243
012d836b-15bb-49d7-bbf9-9e64295d995b,
241244
),
245+
profiler_id: ~,
242246
}
243247
"###);
244248
}
@@ -293,4 +297,88 @@ mod tests {
293297
let envelope_response = processor.process(message).unwrap();
294298
assert!(envelope_response.envelope.is_none());
295299
}
300+
301+
#[tokio::test]
302+
async fn test_profile_id_removed_profiler_id_kept() {
303+
// Setup
304+
let processor = create_test_processor(Default::default());
305+
let event_id = EventId::new();
306+
let dsn = "https://e12d836b15bb49d7bbf99e64295d995b:@sentry.io/42"
307+
.parse()
308+
.unwrap();
309+
let request_meta = RequestMeta::new(dsn);
310+
let mut envelope = Envelope::from_request(Some(event_id), request_meta);
311+
312+
// Add a valid transaction item.
313+
envelope.add_item({
314+
let mut item = Item::new(ItemType::Transaction);
315+
316+
item.set_payload(
317+
ContentType::Json,
318+
r#"{
319+
"type": "transaction",
320+
"transaction": "/foo/",
321+
"timestamp": 946684810.0,
322+
"start_timestamp": 946684800.0,
323+
"contexts": {
324+
"trace": {
325+
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
326+
"span_id": "fa90fdead5f74053",
327+
"op": "http.server",
328+
"type": "trace"
329+
},
330+
"profile": {
331+
"profile_id": "4c79f60c11214eb38604f4ae0781bfb2",
332+
"profiler_id": "4c79f60c11214eb38604f4ae0781bfb2",
333+
"type": "profile"
334+
}
335+
},
336+
"transaction_info": {
337+
"source": "url"
338+
}
339+
}"#,
340+
);
341+
item
342+
});
343+
344+
let mut project_state = ProjectState::allowed();
345+
project_state.config.features.0.insert(Feature::Profiling);
346+
347+
let mut envelopes = ProcessingGroup::split_envelope(*envelope);
348+
assert_eq!(envelopes.len(), 1);
349+
350+
let (group, envelope) = envelopes.pop().unwrap();
351+
let envelope = ManagedEnvelope::standalone(envelope, Addr::dummy(), Addr::dummy(), group);
352+
353+
let message = ProcessEnvelope {
354+
envelope,
355+
project_state: Arc::new(project_state),
356+
sampling_project_state: None,
357+
reservoir_counters: ReservoirCounters::default(),
358+
};
359+
360+
let envelope_response = processor.process(message).unwrap();
361+
let ctx = envelope_response.envelope.unwrap();
362+
let new_envelope = ctx.envelope();
363+
364+
// Get the re-serialized context.
365+
let item = new_envelope
366+
.get_item_by(|item| item.ty() == &ItemType::Transaction)
367+
.unwrap();
368+
let transaction = Annotated::<Event>::from_json_bytes(&item.payload()).unwrap();
369+
let context = transaction
370+
.value()
371+
.unwrap()
372+
.context::<ProfileContext>()
373+
.unwrap();
374+
375+
assert_debug_snapshot!(context, @r###"
376+
ProfileContext {
377+
profile_id: ~,
378+
profiler_id: EventId(
379+
4c79f60c-1121-4eb3-8604-f4ae0781bfb2,
380+
),
381+
}
382+
"###);
383+
}
296384
}

relay-server/tests/snapshots/test_fixtures__event_schema.snap

+13-3
Original file line numberDiff line numberDiff line change
@@ -2988,12 +2988,22 @@ expression: "relay_event_schema::protocol::event_json_schema()"
29882988
"anyOf": [
29892989
{
29902990
"type": "object",
2991-
"required": [
2992-
"profile_id"
2993-
],
29942991
"properties": {
29952992
"profile_id": {
29962993
"description": " The profile ID.",
2994+
"default": null,
2995+
"anyOf": [
2996+
{
2997+
"$ref": "#/definitions/EventId"
2998+
},
2999+
{
3000+
"type": "null"
3001+
}
3002+
]
3003+
},
3004+
"profiler_id": {
3005+
"description": " The profiler ID.",
3006+
"default": null,
29973007
"anyOf": [
29983008
{
29993009
"$ref": "#/definitions/EventId"

0 commit comments

Comments
 (0)