Skip to content

Commit 8d9b972

Browse files
committed
feat(types): add accessors for envelope headers
1 parent ccd1de8 commit 8d9b972

2 files changed

Lines changed: 171 additions & 5 deletions

File tree

sentry-types/src/protocol/envelope.rs

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

4747
/// The supported [Sentry Envelope Headers](https://develop.sentry.dev/sdk/data-model/envelopes/#headers).
4848
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq)]
49-
struct EnvelopeHeaders {
49+
pub struct EnvelopeHeaders {
5050
#[serde(default, skip_serializing_if = "Option::is_none")]
5151
event_id: Option<Uuid>,
5252
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -63,6 +63,64 @@ struct EnvelopeHeaders {
6363
trace: Option<DynamicSamplingContext>,
6464
}
6565

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

399+
/// Returns the Envelope headers.
400+
pub fn headers(&self) -> &EnvelopeHeaders {
401+
&self.headers
402+
}
403+
404+
/// Sets the Envelope headers.
405+
pub fn set_headers(&mut self, headers: EnvelopeHeaders) {
406+
self.headers = headers
407+
}
408+
341409
/// Returns the Envelopes Uuid, if any.
342410
pub fn uuid(&self) -> Option<&Uuid> {
343411
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)