-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): Defer some transaction validation and allow creation o…
…f internal spans (#633)
- Loading branch information
1 parent
418588e
commit 7b168d8
Showing
5 changed files
with
134 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,12 @@ | |
#include "sentry_tracing.h" | ||
#include "sentry_uuid.h" | ||
|
||
#define IS_NULL(Src, Field) \ | ||
sentry_value_is_null(sentry_value_get_by_key(Src, Field)) | ||
#define CHECK_STRING_PROPERTY(Src, Field, Expected) \ | ||
TEST_CHECK_STRING_EQUAL( \ | ||
sentry_value_as_string(sentry_value_get_by_key(Src, Field)), Expected) | ||
|
||
SENTRY_TEST(basic_tracing_context) | ||
{ | ||
sentry_value_t span = sentry_value_new_object(); | ||
|
@@ -38,46 +44,31 @@ SENTRY_TEST(basic_transaction) | |
{ | ||
sentry_value_t tx_cxt = sentry_value_new_transaction_context(NULL, NULL); | ||
TEST_CHECK(!sentry_value_is_null(tx_cxt)); | ||
const char *tx_name | ||
= sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "name")); | ||
TEST_CHECK_STRING_EQUAL(tx_name, "<unlabeled transaction>"); | ||
const char *tx_op | ||
= sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "op")); | ||
TEST_CHECK_STRING_EQUAL(tx_op, ""); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "trace_id"))); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "span_id"))); | ||
CHECK_STRING_PROPERTY(tx_cxt, "transaction", ""); | ||
CHECK_STRING_PROPERTY(tx_cxt, "op", ""); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "trace_id")); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "span_id")); | ||
|
||
sentry_value_decref(tx_cxt); | ||
tx_cxt = sentry_value_new_transaction_context("", ""); | ||
TEST_CHECK(!sentry_value_is_null(tx_cxt)); | ||
tx_name = sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "name")); | ||
TEST_CHECK_STRING_EQUAL(tx_name, "<unlabeled transaction>"); | ||
TEST_CHECK_STRING_EQUAL(tx_op, ""); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "trace_id"))); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "span_id"))); | ||
CHECK_STRING_PROPERTY(tx_cxt, "transaction", ""); | ||
CHECK_STRING_PROPERTY(tx_cxt, "op", ""); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "trace_id")); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "span_id")); | ||
|
||
sentry_value_decref(tx_cxt); | ||
tx_cxt = sentry_value_new_transaction_context("honk.beep", "beepbeep"); | ||
tx_name = sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "name")); | ||
TEST_CHECK_STRING_EQUAL(tx_name, "honk.beep"); | ||
tx_op = sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "op")); | ||
TEST_CHECK_STRING_EQUAL(tx_op, "beepbeep"); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "trace_id"))); | ||
TEST_CHECK( | ||
!sentry_value_is_null(sentry_value_get_by_key(tx_cxt, "span_id"))); | ||
CHECK_STRING_PROPERTY(tx_cxt, "transaction", "honk.beep"); | ||
CHECK_STRING_PROPERTY(tx_cxt, "op", "beepbeep"); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "trace_id")); | ||
TEST_CHECK(!IS_NULL(tx_cxt, "span_id")); | ||
|
||
sentry_transaction_context_set_name(tx_cxt, ""); | ||
tx_name = sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "name")); | ||
TEST_CHECK_STRING_EQUAL(tx_name, "<unlabeled transaction>"); | ||
CHECK_STRING_PROPERTY(tx_cxt, "transaction", ""); | ||
|
||
sentry_transaction_context_set_operation(tx_cxt, ""); | ||
tx_op = sentry_value_as_string(sentry_value_get_by_key(tx_cxt, "op")); | ||
TEST_CHECK_STRING_EQUAL(tx_op, ""); | ||
CHECK_STRING_PROPERTY(tx_cxt, "op", ""); | ||
|
||
sentry_transaction_context_set_sampled(tx_cxt, 1); | ||
TEST_CHECK( | ||
|
@@ -86,24 +77,65 @@ SENTRY_TEST(basic_transaction) | |
sentry_value_decref(tx_cxt); | ||
} | ||
|
||
static void | ||
check_backfilled_name(sentry_envelope_t *envelope, void *data) | ||
{ | ||
uint64_t *called = data; | ||
*called += 1; | ||
|
||
sentry_value_t tx = sentry_envelope_get_transaction(envelope); | ||
TEST_CHECK(!sentry_value_is_null(tx)); | ||
CHECK_STRING_PROPERTY(tx, "transaction", "<unlabeled transaction>"); | ||
|
||
sentry_envelope_free(envelope); | ||
} | ||
|
||
SENTRY_TEST(transaction_name_backfill_on_finish) | ||
{ | ||
uint64_t called = 0; | ||
|
||
sentry_options_t *options = sentry_options_new(); | ||
sentry_options_set_dsn(options, "https://[email protected]/42"); | ||
|
||
sentry_transport_t *transport = sentry_transport_new(check_backfilled_name); | ||
sentry_transport_set_state(transport, &called); | ||
sentry_options_set_transport(options, transport); | ||
|
||
sentry_options_set_traces_sample_rate(options, 1.0); | ||
sentry_init(options); | ||
|
||
sentry_value_t tx_cxt = sentry_value_new_transaction_context(NULL, NULL); | ||
sentry_value_t tx = sentry_transaction_start(tx_cxt); | ||
sentry_uuid_t event_id = sentry_transaction_finish(tx); | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
|
||
tx_cxt = sentry_value_new_transaction_context("", ""); | ||
tx = sentry_transaction_start(tx_cxt); | ||
event_id = sentry_transaction_finish(tx); | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
|
||
sentry_close(); | ||
TEST_CHECK_INT_EQUAL(called, 2); | ||
} | ||
|
||
static void | ||
send_transaction_envelope_test_basic(sentry_envelope_t *envelope, void *data) | ||
{ | ||
uint64_t *called = data; | ||
*called += 1; | ||
|
||
sentry_value_t transaction = sentry_envelope_get_transaction(envelope); | ||
TEST_CHECK(!sentry_value_is_null(transaction)); | ||
const char *event_id = sentry_value_as_string( | ||
sentry_value_get_by_key(transaction, "event_id")); | ||
sentry_value_t tx = sentry_envelope_get_transaction(envelope); | ||
TEST_CHECK(!sentry_value_is_null(tx)); | ||
const char *event_id | ||
= sentry_value_as_string(sentry_value_get_by_key(tx, "event_id")); | ||
TEST_CHECK_STRING_EQUAL(event_id, "4c035723-8638-4c3a-923f-2ab9d08b4018"); | ||
|
||
if (*called == 1) { | ||
const char *type = sentry_value_as_string( | ||
sentry_value_get_by_key(transaction, "type")); | ||
const char *type | ||
= sentry_value_as_string(sentry_value_get_by_key(tx, "type")); | ||
TEST_CHECK_STRING_EQUAL(type, "transaction"); | ||
const char *name = sentry_value_as_string( | ||
sentry_value_get_by_key(transaction, "transaction")); | ||
sentry_value_get_by_key(tx, "transaction")); | ||
TEST_CHECK_STRING_EQUAL(name, "honk"); | ||
} | ||
|
||
|
@@ -126,25 +158,25 @@ SENTRY_TEST(basic_function_transport_transaction) | |
sentry_options_set_require_user_consent(options, true); | ||
sentry_init(options); | ||
|
||
sentry_value_t transaction = sentry_value_new_transaction_context( | ||
sentry_value_t tx_cxt = sentry_value_new_transaction_context( | ||
"How could you", "Don't capture this."); | ||
transaction = sentry_transaction_start(transaction); | ||
sentry_uuid_t event_id = sentry_transaction_finish(transaction); | ||
sentry_value_t tx = sentry_transaction_start(tx_cxt); | ||
sentry_uuid_t event_id = sentry_transaction_finish(tx); | ||
// TODO: `sentry_capture_event` acts as if the event was sent if user | ||
// consent was not given | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
sentry_user_consent_give(); | ||
|
||
transaction = sentry_value_new_transaction_context("honk", "beep"); | ||
transaction = sentry_transaction_start(transaction); | ||
event_id = sentry_transaction_finish(transaction); | ||
tx_cxt = sentry_value_new_transaction_context("honk", "beep"); | ||
tx = sentry_transaction_start(tx_cxt); | ||
event_id = sentry_transaction_finish(tx); | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
|
||
sentry_user_consent_revoke(); | ||
transaction = sentry_value_new_transaction_context( | ||
tx_cxt = sentry_value_new_transaction_context( | ||
"How could you again", "Don't capture this either."); | ||
transaction = sentry_transaction_start(transaction); | ||
event_id = sentry_transaction_finish(transaction); | ||
tx = sentry_transaction_start(tx_cxt); | ||
event_id = sentry_transaction_finish(tx); | ||
// TODO: `sentry_capture_event` acts as if the event was sent if user | ||
// consent was not given | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
|
@@ -171,10 +203,10 @@ SENTRY_TEST(transport_sampling_transactions) | |
|
||
uint64_t sent_transactions = 0; | ||
for (int i = 0; i < 100; i++) { | ||
sentry_value_t transaction | ||
sentry_value_t tx_cxt | ||
= sentry_value_new_transaction_context("honk", "beep"); | ||
transaction = sentry_transaction_start(transaction); | ||
sentry_uuid_t event_id = sentry_transaction_finish(transaction); | ||
sentry_value_t tx = sentry_transaction_start(tx_cxt); | ||
sentry_uuid_t event_id = sentry_transaction_finish(tx); | ||
if (!sentry_uuid_is_nil(&event_id)) { | ||
sent_transactions += 1; | ||
} | ||
|
@@ -214,14 +246,17 @@ SENTRY_TEST(transactions_skip_before_send) | |
sentry_options_set_before_send(options, before_send, &called_beforesend); | ||
sentry_init(options); | ||
|
||
sentry_value_t transaction | ||
sentry_value_t tx_cxt | ||
= sentry_value_new_transaction_context("honk", "beep"); | ||
transaction = sentry_transaction_start(transaction); | ||
sentry_uuid_t event_id = sentry_transaction_finish(transaction); | ||
sentry_value_t tx = sentry_transaction_start(tx_cxt); | ||
sentry_uuid_t event_id = sentry_transaction_finish(tx); | ||
TEST_CHECK(!sentry_uuid_is_nil(&event_id)); | ||
|
||
sentry_close(); | ||
|
||
TEST_CHECK_INT_EQUAL(called_transport, 1); | ||
TEST_CHECK_INT_EQUAL(called_beforesend, 0); | ||
} | ||
|
||
#undef IS_NULL | ||
#undef CHECK_STRING_PROPERTY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters