Skip to content

Commit 280abd8

Browse files
committed
feat(types): add accessors for envelope headers
1 parent 5cdb44c commit 280abd8

File tree

2 files changed

+171
-5
lines changed

2 files changed

+171
-5
lines changed

sentry-types/src/protocol/envelope.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum EnvelopeError {
4242

4343
/// The supported [Sentry Envelope Headers](https://develop.sentry.dev/sdk/data-model/envelopes/#headers).
4444
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq)]
45-
struct EnvelopeHeaders {
45+
pub struct EnvelopeHeaders {
4646
#[serde(default, skip_serializing_if = "Option::is_none")]
4747
event_id: Option<Uuid>,
4848
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -59,6 +59,64 @@ struct EnvelopeHeaders {
5959
trace: Option<DynamicSamplingContext>,
6060
}
6161

62+
impl EnvelopeHeaders {
63+
/// Creates empty Envelope headers.
64+
pub fn new() -> EnvelopeHeaders {
65+
Default::default()
66+
}
67+
68+
/// Returns the Event ID.
69+
pub fn event_id(&self) -> Option<&Uuid> {
70+
self.event_id.as_ref()
71+
}
72+
73+
/// Sets the Event ID.
74+
pub fn set_event_id(&mut self, event_id: Option<Uuid>) {
75+
self.event_id = event_id;
76+
}
77+
78+
/// Returns the DSN.
79+
pub fn dsn(&self) -> Option<&Dsn> {
80+
self.dsn.as_ref()
81+
}
82+
83+
/// Sets the DSN.
84+
pub fn set_dsn(&mut self, dsn: Option<Dsn>) {
85+
self.dsn = dsn;
86+
}
87+
88+
/// Returns the SDK information.
89+
pub fn sdk(&self) -> Option<&ClientSdkInfo> {
90+
self.sdk.as_ref()
91+
}
92+
93+
/// Sets the SDK information.
94+
pub fn set_sdk(&mut self, sdk: Option<ClientSdkInfo>) {
95+
self.sdk = sdk;
96+
}
97+
98+
/// Returns the time this envelope was sent at.
99+
pub fn sent_at(&self) -> Option<&SystemTime> {
100+
self.sent_at.as_ref()
101+
}
102+
103+
/// Sets the time this envelope was sent at.
104+
/// This timestamp should be generated as close as possible to the transmision of the event.
105+
pub fn set_sent_at(&mut self, sent_at: Option<SystemTime>) {
106+
self.sent_at = sent_at;
107+
}
108+
109+
/// Returns the Dynamic Sampling Context.
110+
pub fn trace(&self) -> Option<&DynamicSamplingContext> {
111+
self.trace.as_ref()
112+
}
113+
114+
/// Sets the Dynamic Sampling Context.
115+
pub fn set_trace(&mut self, trace: Option<DynamicSamplingContext>) {
116+
self.trace = trace;
117+
}
118+
}
119+
62120
/// An Envelope Item Type.
63121
#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
64122
#[non_exhaustive]
@@ -334,6 +392,16 @@ impl Envelope {
334392
EnvelopeItemIter { inner }
335393
}
336394

395+
/// Returns the Envelope headers.
396+
pub fn headers(&self) -> &EnvelopeHeaders {
397+
&self.headers
398+
}
399+
400+
/// Sets the Envelope headers.
401+
pub fn set_headers(&mut self, headers: EnvelopeHeaders) {
402+
self.headers = headers
403+
}
404+
337405
/// Returns the Envelopes Uuid, if any.
338406
pub fn uuid(&self) -> Option<&Uuid> {
339407
self.headers.event_id.as_ref()

sentry-types/src/protocol/v7.rs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,7 @@ impl<'de> Deserialize<'de> for LogAttribute {
23392339

23402340
/// An ID that identifies an organization in the Sentry backend.
23412341
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
2342-
struct OrganizationId(u64);
2342+
pub struct OrganizationId(u64);
23432343

23442344
impl From<u64> for OrganizationId {
23452345
fn from(value: u64) -> Self {
@@ -2363,7 +2363,7 @@ impl std::fmt::Display for OrganizationId {
23632363

23642364
/// A random number generated at the start of a trace by the head of trace SDK.
23652365
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
2366-
struct SampleRand(f64);
2366+
pub struct SampleRand(f64);
23672367

23682368
impl From<f64> for SampleRand {
23692369
fn from(value: f64) -> Self {
@@ -2392,8 +2392,8 @@ impl std::fmt::Display for SampleRand {
23922392
/// This feature allows users to specify target sample rates for each project via the frontend instead of requiring an application redeployment.
23932393
/// The backend needs additional information from the SDK to support these features, contained in
23942394
/// the Dynamic Sampling Context.
2395-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2396-
pub(crate) struct DynamicSamplingContext {
2395+
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
2396+
pub struct DynamicSamplingContext {
23972397
// Strictly required fields
23982398
// Still typed as optional, as when deserializing an envelope created by an older SDK they might still be missing
23992399
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -2432,3 +2432,101 @@ pub(crate) struct DynamicSamplingContext {
24322432
)]
24332433
org_id: Option<OrganizationId>,
24342434
}
2435+
2436+
impl DynamicSamplingContext {
2437+
/// Creates an empty Dynamic Sampling Context.
2438+
pub fn new() -> Self {
2439+
Default::default()
2440+
}
2441+
2442+
/// Gets the trace ID.
2443+
pub fn trace_id(&self) -> Option<&TraceId> {
2444+
self.trace_id.as_ref()
2445+
}
2446+
2447+
/// Sets the trace ID.
2448+
pub fn set_trace_id(&mut self, trace_id: Option<TraceId>) {
2449+
self.trace_id = trace_id;
2450+
}
2451+
2452+
/// Gets the DSN public key.
2453+
pub fn public_key(&self) -> Option<&String> {
2454+
self.public_key.as_ref()
2455+
}
2456+
2457+
/// Sets the DSN public key.
2458+
pub fn set_public_key(&mut self, public_key: Option<String>) {
2459+
self.public_key = public_key;
2460+
}
2461+
2462+
/// Gets the sample rate.
2463+
pub fn sample_rate(&self) -> Option<&f32> {
2464+
self.sample_rate.as_ref()
2465+
}
2466+
2467+
/// Sets the sample rate.
2468+
pub fn set_sample_rate(&mut self, sample_rate: Option<f32>) {
2469+
self.sample_rate = sample_rate;
2470+
}
2471+
2472+
/// Gets the sample random value generated by the head of trace SDK.
2473+
pub fn sample_rand(&self) -> Option<&SampleRand> {
2474+
self.sample_rand.as_ref()
2475+
}
2476+
2477+
/// Sets the sample random value generated by the head of trace SDK.
2478+
pub fn set_sample_rand(&mut self, sample_rand: Option<SampleRand>) {
2479+
self.sample_rand = sample_rand;
2480+
}
2481+
2482+
/// Gets the sampled flag, true if and only if the trace was sampled. This is set by the head
2483+
/// of trace SDK.
2484+
pub fn sampled(&self) -> Option<&bool> {
2485+
self.sampled.as_ref()
2486+
}
2487+
2488+
/// Sets the sampled flag.
2489+
pub fn set_sampled(&mut self, sampled: Option<bool>) {
2490+
self.sampled = sampled;
2491+
}
2492+
2493+
/// Gets the release.
2494+
pub fn release(&self) -> Option<&String> {
2495+
self.release.as_ref()
2496+
}
2497+
2498+
/// Sets the release.
2499+
pub fn set_release(&mut self, release: Option<String>) {
2500+
self.release = release;
2501+
}
2502+
2503+
/// Gets the environment.
2504+
pub fn environment(&self) -> Option<&String> {
2505+
self.environment.as_ref()
2506+
}
2507+
2508+
/// Sets the environment.
2509+
pub fn set_environment(&mut self, environment: Option<String>) {
2510+
self.environment = environment;
2511+
}
2512+
2513+
/// Gets the transaction.
2514+
pub fn transaction(&self) -> Option<&String> {
2515+
self.transaction.as_ref()
2516+
}
2517+
2518+
/// Sets the transaction.
2519+
pub fn set_transaction(&mut self, transaction: Option<String>) {
2520+
self.transaction = transaction;
2521+
}
2522+
2523+
/// Gets the organization ID.
2524+
pub fn org_id(&self) -> Option<&OrganizationId> {
2525+
self.org_id.as_ref()
2526+
}
2527+
2528+
/// Sets the organization ID.
2529+
pub fn set_org_id(&mut self, org_id: Option<OrganizationId>) {
2530+
self.org_id = org_id;
2531+
}
2532+
}

0 commit comments

Comments
 (0)