Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tracing): Allow users to bind a transaction to the global scope #632

Merged
merged 15 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1325,22 +1325,28 @@ SENTRY_EXPERIMENTAL_API sentry_value_t sentry_transaction_start(
* otherwise.
*
* Always takes ownership of `transaction`, regardless of whether the operation
* was successful or not.
* was successful or not. If `sentry_set_span` was invoked with `transaction`,
* this will remove the
*/
SENTRY_EXPERIMENTAL_API sentry_uuid_t sentry_transaction_finish(
sentry_value_t transaction);
#endif

/**
* Sets the Span (actually Transaction) so any Events sent while the Transaction
* is active will be associated with the Transaction. Returns a value which
* should then be passed into `sentry_transaction_finish` to finish and render
* the Transaction inactive.
* is active will be associated with the Transaction.
*
* If the Transaction being passed in is unsampled, it will still be associated
* with any new Events. This will lead to some Events pointing to orphan or
* missing traces in sentry, see
* https://docs.sentry.io/product/sentry-basics/tracing/trace-view/#orphan-traces-and-broken-subtraces
*
* This takes ownership of the Transaction.
* This increases the number of references pointing to the transaction.
* Invoke `sentry_transaction_finish` to remove the Span set by this function as
* well as its reference by passing in the same Transaction as the one passed
* into this function.
*/
SENTRY_EXPERIMENTAL_API sentry_value_t sentry_set_span(
sentry_value_t transaction);
SENTRY_EXPERIMENTAL_API void sentry_set_span(sentry_value_t transaction);
#endif

#ifdef __cplusplus
}
Expand Down
40 changes: 21 additions & 19 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ sentry__prepare_transaction(const sentry_options_t *options,
sentry_envelope_t *envelope = NULL;

SENTRY_WITH_SCOPE (scope) {
SENTRY_TRACE("merging scope into event");
SENTRY_TRACE("merging scope into transaction");
// Don't include debugging info
sentry_scope_mode_t mode = SENTRY_SCOPE_ALL & ~SENTRY_SCOPE_MODULES
& ~SENTRY_SCOPE_STACKTRACES;
Expand All @@ -540,6 +540,7 @@ sentry__prepare_transaction(const sentry_options_t *options,
return envelope;

fail:
SENTRY_WARN("dropping transaction");
sentry_envelope_free(envelope);
sentry_value_decref(transaction);
return NULL;
Expand Down Expand Up @@ -756,7 +757,7 @@ sentry_transaction_start(sentry_value_t tx_cxt)
sentry_value_set_by_key(
tx, "trace_id", sentry_value_get_by_key_owned(tx_cxt, "trace_id"));
sentry_value_set_by_key(
tx, "span_id", sentry_value_get_by_key_owned(tx_cxt, "trace_id"));
tx, "span_id", sentry_value_get_by_key_owned(tx_cxt, "span_id"));
sentry_value_set_by_key(tx, "transaction",
sentry_value_get_by_key_owned(tx_cxt, "transaction"));
sentry_value_set_by_key(
Expand All @@ -772,20 +773,26 @@ sentry_transaction_start(sentry_value_t tx_cxt)
sentry_uuid_t
sentry_transaction_finish(sentry_value_t tx)
{
if (sentry_value_get_type(tx) == SENTRY_VALUE_TYPE_STRING) {
sentry_value_decref(tx);
tx = sentry_value_new_null();
// Minimize the amount of time spent locking the scope, and do checking
// on the transaction when this is not holding onto the lock.
SENTRY_WITH_SCOPE (scope) {
tx = sentry__value_clone(scope->span);
}
sentry__scope_remove_span();
}
if (sentry_value_is_null(tx)) {
SENTRY_DEBUG("no transaction available to finish");
goto fail;
}

SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) {
const char *tx_id
= sentry_value_as_string(sentry_value_get_by_key(tx, "trace_id"));
const char *scope_tx_id = sentry_value_as_string(
sentry_value_get_by_key(scope->span, "trace_id"));
if (sentry__string_eq(tx_id, scope_tx_id)) {
sentry_value_decref(scope->span);
scope->span = sentry_value_new_null();
}
}
// immediately flush the scope so the transaction doesn't get attached to
// any new events
SENTRY_WITH_SCOPE_MUT (scope) {
(void)scope;
}
// The sampling decision should already be made for transactions
// during their construction. No need to recalculate here. See
// `sentry__should_skip_transaction`.
Expand Down Expand Up @@ -832,18 +839,13 @@ sentry_transaction_finish(sentry_value_t tx)
return sentry_uuid_nil();
}

sentry_value_t
void
sentry_set_span(sentry_value_t transaction)
{
sentry_uuid_t nil_uuid = sentry_uuid_nil();
sentry_value_t sentinel = sentry__value_new_internal_uuid(&nil_uuid);
SENTRY_WITH_SCOPE_MUT (scope) {
sentry_value_decref(sentinel);
sentinel = sentry__value_clone(
sentry_value_get_by_key(transaction, "span_id"));
sentry_value_decref(scope->span);
sentry_value_incref(transaction);
scope->span = transaction;
}
return sentinel;
}
#endif
14 changes: 3 additions & 11 deletions src/sentry_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ sentry__symbolize_stacktrace(sentry_value_t stacktrace)
}
}

#ifdef SENTRY_UNITTEST
#ifdef SENTRY_PERFORMANCE_MONITORING
# ifdef SENTRY_UNITTEST
sentry_value_t
sentry__scope_get_span()
{
Expand All @@ -246,16 +247,7 @@ sentry__scope_get_span()
}
return sentry_value_new_null();
}
#endif

void
sentry__scope_remove_span()
{
SENTRY_WITH_SCOPE_MUT (scope) {
sentry_value_decref(scope->span);
scope->span = sentry_value_new_null();
}
}
# endif
#endif

void
Expand Down
11 changes: 2 additions & 9 deletions src/sentry_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ void sentry__scope_apply_to_event(const sentry_scope_t *scope,
const sentry_options_t *options, sentry_value_t event,
sentry_scope_mode_t mode);

/**
* Removes the current span (actually transaction) on the scope. If the
* transaction has not yet finished, this does not finish the transaction
* nor does it send it to sentry; The transaction will be discarded.
*
* Invoke this at your own discretion.
*/
void sentry__scope_remove_span();

/**
* These are convenience macros to automatically lock/unlock a scope inside a
* code block.
Expand All @@ -103,7 +94,9 @@ void sentry__scope_remove_span();

#endif

#ifdef SENTRY_PERFORMANCE_MONITORING
// this is only used in unit tests
#ifdef SENTRY_UNITTEST
sentry_value_t sentry__scope_get_span();
#endif
#endif
6 changes: 3 additions & 3 deletions tests/unit/test_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ SENTRY_TEST(multiple_transactions)

sentry_value_t tx_cxt = sentry_value_new_transaction_context("wow!", NULL);
sentry_value_t tx = sentry_transaction_start(tx_cxt);
tx = sentry_set_span(tx);
sentry_set_span(tx);

sentry_value_t scope_tx = sentry__scope_get_span();
CHECK_STRING_PROPERTY(scope_tx, "transaction", "wow!");
Expand All @@ -298,11 +298,11 @@ SENTRY_TEST(multiple_transactions)
// one
tx_cxt = sentry_value_new_transaction_context("whoa!", NULL);
tx = sentry_transaction_start(tx_cxt);
tx = sentry_set_span(tx);
sentry_set_span(tx);
sentry_value_decref(tx);
tx_cxt = sentry_value_new_transaction_context("wowee!", NULL);
tx = sentry_transaction_start(tx_cxt);
tx = sentry_set_span(tx);
sentry_set_span(tx);
scope_tx = sentry__scope_get_span();
CHECK_STRING_PROPERTY(scope_tx, "transaction", "wowee!");
event_id = sentry_transaction_finish(tx);
Expand Down