Skip to content

Commit 19495a8

Browse files
authored
Merge f8e2fe3 into c75d62a
2 parents c75d62a + f8e2fe3 commit 19495a8

File tree

4 files changed

+103
-36
lines changed

4 files changed

+103
-36
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[workspace]
2+
resolver = "2"
23
members = [
34
"sentry",
45
"sentry-actix",

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ check-default-features:
6868

6969
check-no-default-features:
7070
@echo 'NO DEFAULT FEATURES'
71-
@cd sentry && RUSTFLAGS=-Dwarnings cargo check --no-default-features
71+
@cd sentry-core && RUSTFLAGS=-Dwarnings cargo check --no-default-features
7272
.PHONY: check-no-default-features
7373

7474
check-panic:

sentry-core/src/performance.rs

+99-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use std::sync::Arc;
22
use std::sync::Mutex;
33

4-
use crate::{protocol, Client, Hub};
4+
use crate::{protocol, Hub};
55

6+
#[cfg(feature = "client")]
7+
use crate::Client;
8+
9+
#[cfg(feature = "client")]
610
const MAX_SPANS: usize = 1_000;
711

812
// global API:
@@ -14,8 +18,15 @@ const MAX_SPANS: usize = 1_000;
1418
/// The transaction itself also represents the root span in the span hierarchy.
1519
/// Child spans can be started with the [`Transaction::start_child`] method.
1620
pub fn start_transaction(ctx: TransactionContext) -> Transaction {
17-
let client = Hub::with_active(|hub| hub.client());
18-
Transaction::new(client, ctx)
21+
#[cfg(feature = "client")]
22+
{
23+
let client = Hub::with_active(|hub| hub.client());
24+
Transaction::new(client, ctx)
25+
}
26+
#[cfg(not(feature = "client"))]
27+
{
28+
Transaction::new_noop(ctx)
29+
}
1930
}
2031

2132
// Hub API:
@@ -25,7 +36,14 @@ impl Hub {
2536
///
2637
/// See the global [`start_transaction`] for more documentation.
2738
pub fn start_transaction(&self, ctx: TransactionContext) -> Transaction {
28-
Transaction::new(self.client(), ctx)
39+
#[cfg(feature = "client")]
40+
{
41+
Transaction::new(self.client(), ctx)
42+
}
43+
#[cfg(not(feature = "client"))]
44+
{
45+
Transaction::new_noop(ctx)
46+
}
2947
}
3048
}
3149

@@ -37,6 +55,7 @@ impl Hub {
3755
/// Transaction, and also the connection point for distributed tracing.
3856
#[derive(Debug)]
3957
pub struct TransactionContext {
58+
#[cfg_attr(not(feature = "client"), allow(dead_code))]
4059
name: String,
4160
op: String,
4261
trace_id: protocol::TraceId,
@@ -203,6 +222,7 @@ impl TransactionOrSpan {
203222
}
204223
}
205224

225+
#[cfg(feature = "client")]
206226
pub(crate) fn apply_to_event(&self, event: &mut protocol::Event<'_>) {
207227
if event.contexts.contains_key("trace") {
208228
return;
@@ -238,6 +258,7 @@ impl TransactionOrSpan {
238258

239259
#[derive(Debug)]
240260
struct TransactionInner {
261+
#[cfg(feature = "client")]
241262
client: Option<Arc<Client>>,
242263
sampled: bool,
243264
context: protocol::TraceContext,
@@ -257,6 +278,7 @@ pub struct Transaction {
257278
}
258279

259280
impl Transaction {
281+
#[cfg(feature = "client")]
260282
fn new(mut client: Option<Arc<Client>>, ctx: TransactionContext) -> Self {
261283
let context = protocol::TraceContext {
262284
trace_id: ctx.trace_id,
@@ -294,6 +316,25 @@ impl Transaction {
294316
}
295317
}
296318

319+
#[cfg(not(feature = "client"))]
320+
fn new_noop(ctx: TransactionContext) -> Self {
321+
let context = protocol::TraceContext {
322+
trace_id: ctx.trace_id,
323+
parent_span_id: ctx.parent_span_id,
324+
op: Some(ctx.op),
325+
..Default::default()
326+
};
327+
let sampled = ctx.sampled.unwrap_or(false);
328+
329+
Self {
330+
inner: Arc::new(Mutex::new(TransactionInner {
331+
sampled,
332+
context,
333+
transaction: None,
334+
})),
335+
}
336+
}
337+
297338
/// Set some extra information to be sent with this Transaction.
298339
pub fn set_data(&self, key: &str, value: protocol::Value) {
299340
let mut inner = self.inner.lock().unwrap();
@@ -332,26 +373,28 @@ impl Transaction {
332373
/// This records the end timestamp and sends the transaction together with
333374
/// all finished child spans to Sentry.
334375
pub fn finish(self) {
335-
let mut inner = self.inner.lock().unwrap();
336-
if let Some(mut transaction) = inner.transaction.take() {
337-
if let Some(client) = inner.client.take() {
338-
transaction.finish();
339-
transaction
340-
.contexts
341-
.insert("trace".into(), inner.context.clone().into());
342-
343-
// TODO: apply the scope to the transaction, whatever that means
344-
let opts = client.options();
345-
transaction.release = opts.release.clone();
346-
transaction.environment = opts.environment.clone();
347-
transaction.sdk = Some(std::borrow::Cow::Owned(client.sdk_info.clone()));
348-
349-
let mut envelope = protocol::Envelope::new();
350-
envelope.add_item(transaction);
351-
352-
client.send_envelope(envelope)
376+
with_client_impl! {{
377+
let mut inner = self.inner.lock().unwrap();
378+
if let Some(mut transaction) = inner.transaction.take() {
379+
if let Some(client) = inner.client.take() {
380+
transaction.finish();
381+
transaction
382+
.contexts
383+
.insert("trace".into(), inner.context.clone().into());
384+
385+
// TODO: apply the scope to the transaction, whatever that means
386+
let opts = client.options();
387+
transaction.release = opts.release.clone();
388+
transaction.environment = opts.environment.clone();
389+
transaction.sdk = Some(std::borrow::Cow::Owned(client.sdk_info.clone()));
390+
391+
let mut envelope = protocol::Envelope::new();
392+
envelope.add_item(transaction);
393+
394+
client.send_envelope(envelope)
395+
}
353396
}
354-
}
397+
}}
355398
}
356399

357400
/// Starts a new child Span with the given `op` and `description`.
@@ -425,18 +468,20 @@ impl Span {
425468
/// This will record the end timestamp and add the span to the transaction
426469
/// in which it was started.
427470
pub fn finish(self) {
428-
let mut span = self.span.lock().unwrap();
429-
if span.timestamp.is_some() {
430-
// the span was already finished
431-
return;
432-
}
433-
span.finish();
434-
let mut inner = self.transaction.lock().unwrap();
435-
if let Some(transaction) = inner.transaction.as_mut() {
436-
if transaction.spans.len() <= MAX_SPANS {
437-
transaction.spans.push(span.clone());
471+
with_client_impl! {{
472+
let mut span = self.span.lock().unwrap();
473+
if span.timestamp.is_some() {
474+
// the span was already finished
475+
return;
438476
}
439-
}
477+
span.finish();
478+
let mut inner = self.transaction.lock().unwrap();
479+
if let Some(transaction) = inner.transaction.as_mut() {
480+
if transaction.spans.len() <= MAX_SPANS {
481+
transaction.spans.push(span.clone());
482+
}
483+
}
484+
}}
440485
}
441486

442487
/// Starts a new child Span with the given `op` and `description`.
@@ -510,11 +555,12 @@ impl std::fmt::Display for SentryTrace {
510555

511556
#[cfg(test)]
512557
mod tests {
558+
use std::str::FromStr;
559+
513560
use super::*;
514561

515562
#[test]
516563
fn parses_sentry_trace() {
517-
use std::str::FromStr;
518564
let trace_id = protocol::TraceId::from_str("09e04486820349518ac7b5d2adbf6ba5").unwrap();
519565
let parent_trace_id = protocol::SpanId::from_str("9cf635fa5b870b3a").unwrap();
520566

@@ -528,4 +574,22 @@ mod tests {
528574
let parsed = parse_sentry_trace(&format!("{}", trace));
529575
assert_eq!(parsed, Some(trace));
530576
}
577+
578+
#[test]
579+
fn disabled_forwards_trace_id() {
580+
let headers = [(
581+
"SenTrY-TRAce",
582+
"09e04486820349518ac7b5d2adbf6ba5-9cf635fa5b870b3a-1",
583+
)];
584+
let ctx = TransactionContext::continue_from_headers("noop", "noop", headers);
585+
let trx = start_transaction(ctx);
586+
587+
let span = trx.start_child("noop", "noop");
588+
589+
let header = span.iter_headers().next().unwrap().1;
590+
let parsed = parse_sentry_trace(&header).unwrap();
591+
592+
assert_eq!(&parsed.0.to_string(), "09e04486820349518ac7b5d2adbf6ba5");
593+
assert_eq!(parsed.2, Some(true));
594+
}
531595
}

sentry-core/src/scope/noop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22

33
use crate::protocol::{Context, Event, Level, User, Value};
4+
use crate::TransactionOrSpan;
45

56
/// A minimal API scope guard.
67
///
@@ -111,6 +112,7 @@ impl Scope {
111112

112113
/// Set the given [`TransactionOrSpan`] as the active span for this scope.
113114
pub fn set_span(&mut self, span: Option<TransactionOrSpan>) {
115+
let _ = span;
114116
minimal_unreachable!();
115117
}
116118

0 commit comments

Comments
 (0)