Skip to content

Commit

Permalink
feat(tracing): Move all performance monitoring code behind a compile …
Browse files Browse the repository at this point in the history
…flag (getsentry#645)

This moves both the external and the internal parts of the 
API related to performance monitoring (/tracing) behind a 
compile flag. This is to make it easier for us to iterate on 
the API, and to potentially break it or leave it in a 
compilable but incomplete state. 

The flag is also enabled by default when building the 
example to speed up local testing during development.
  • Loading branch information
relaxolotl authored Jan 5, 2022
1 parent 7b168d8 commit 623ce2c
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 20 deletions.
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ set_target_properties(sentry PROPERTIES PUBLIC_HEADER "include/sentry.h")

if(DEFINED SENTRY_FOLDER)
set_target_properties(sentry PROPERTIES FOLDER ${SENTRY_FOLDER})
endif()
endif()

# check size type
include(CheckTypeSize)
Expand Down Expand Up @@ -457,7 +457,7 @@ if(SENTRY_BACKEND_CRASHPAD)
set_target_properties(crashpad_util PROPERTIES FOLDER ${SENTRY_FOLDER})
set_target_properties(crashpad_zlib PROPERTIES FOLDER ${SENTRY_FOLDER})
set_target_properties(mini_chromium PROPERTIES FOLDER ${SENTRY_FOLDER})
endif()
endif()

target_link_libraries(sentry PRIVATE
$<BUILD_INTERFACE:crashpad::client>
Expand Down Expand Up @@ -490,8 +490,8 @@ elseif(SENTRY_BACKEND_BREAKPAD)

if(DEFINED SENTRY_FOLDER)
set_target_properties(breakpad_client PROPERTIES FOLDER ${SENTRY_FOLDER})
endif()
endif()

if(NOT SENTRY_BUILD_SHARED_LIBS)
sentry_install(TARGETS breakpad_client EXPORT sentry
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
Expand Down Expand Up @@ -559,6 +559,8 @@ if(SENTRY_BUILD_EXAMPLES)
add_executable(sentry_example examples/example.c)
target_link_libraries(sentry_example PRIVATE sentry)

target_compile_definitions(sentry_example PRIVATE SENTRY_PERFORMANCE_MONITORING)

if(MSVC)
target_compile_options(sentry_example PRIVATE $<BUILD_INTERFACE:/wd5105>)
endif()
Expand All @@ -570,7 +572,7 @@ if(SENTRY_BUILD_EXAMPLES)

if(DEFINED SENTRY_FOLDER)
set_target_properties(sentry_example PROPERTIES FOLDER ${SENTRY_FOLDER})
endif()
endif()

add_test(NAME sentry_example COMMAND sentry_example)
endif()
4 changes: 4 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ main(int argc, char **argv)
options, sentry_transport_new(print_envelope));
}

#ifdef SENTRY_PERFORMANCE_MONITORING
if (has_arg(argc, argv, "capture-transaction")) {
sentry_options_set_traces_sample_rate(options, 1.0);
}
#endif

sentry_init(options);

Expand Down Expand Up @@ -212,6 +214,7 @@ main(int argc, char **argv)
sentry_capture_event(event);
}

#ifdef SENTRY_PERFORMANCE_MONITORING
if (has_arg(argc, argv, "capture-transaction")) {
sentry_value_t tx_ctx
= sentry_value_new_transaction_context("I'm a little teapot",
Expand All @@ -224,6 +227,7 @@ main(int argc, char **argv)
sentry_value_t tx = sentry_transaction_start(tx_ctx);
sentry_transaction_finish(tx);
}
#endif

// make sure everything flushes
sentry_close();
Expand Down
11 changes: 9 additions & 2 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,15 @@ SENTRY_API void sentry_envelope_free(sentry_envelope_t *envelope);
SENTRY_API sentry_value_t sentry_envelope_get_event(
const sentry_envelope_t *envelope);

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Given an Envelope, returns the embedded Transaction if there is one.
*
* This returns a borrowed value to the Transaction in the Envelope.
*/
SENTRY_EXPERIMENTAL_API sentry_value_t sentry_envelope_get_transaction(
const sentry_envelope_t *envelope);
#endif

/**
* Serializes the envelope.
Expand Down Expand Up @@ -1111,8 +1113,11 @@ SENTRY_API void sentry_user_consent_reset(void);
SENTRY_API sentry_user_consent_t sentry_user_consent_get(void);

/**
* Sends a sentry event. Returns a nil UUID if the event being passed in is a
* transaction; `sentry_transaction_finish` should be used to send transactions.
* Sends a sentry event.
*
* If SENTRY_PERFORMANCE_MONITORING is enabled, returns a nil UUID if the event
* being passed in is a transaction, and the transaction will not be sent nor
* consumed. `sentry_transaction_finish` should be used to send transactions.
*/
SENTRY_API sentry_uuid_t sentry_capture_event(sentry_value_t event);

Expand Down Expand Up @@ -1207,6 +1212,7 @@ SENTRY_API void sentry_start_session(void);
*/
SENTRY_API void sentry_end_session(void);

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Sets the maximum number of spans that can be attached to a
* transaction.
Expand Down Expand Up @@ -1311,6 +1317,7 @@ SENTRY_EXPERIMENTAL_API sentry_value_t sentry_transaction_start(
*/
SENTRY_EXPERIMENTAL_API sentry_uuid_t sentry_transaction_finish(
sentry_value_t transaction);
#endif

#ifdef __cplusplus
}
Expand Down
10 changes: 8 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ sentry_target_sources_cwd(sentry
sentry_symbolizer.h
sentry_sync.c
sentry_sync.h
sentry_tracing.c
sentry_tracing.h
sentry_transport.c
sentry_transport.h
sentry_utils.c
Expand Down Expand Up @@ -153,3 +151,11 @@ if(SENTRY_INTEGRATION_QT)
integrations/sentry_integration_qt.h
)
endif()

if(SENTRY_BUILD_EXAMPLES)
target_compile_definitions(sentry PRIVATE SENTRY_PERFORMANCE_MONITORING)
sentry_target_sources_cwd(sentry
sentry_tracing.c
sentry_tracing.h
)
endif()
22 changes: 21 additions & 1 deletion src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
#include "sentry_session.h"
#include "sentry_string.h"
#include "sentry_sync.h"
#include "sentry_tracing.h"
#include "sentry_transport.h"
#include "sentry_value.h"

#ifdef SENTRY_INTEGRATION_QT
# include "integrations/sentry_integration_qt.h"
#endif

#ifdef SENTRY_PERFORMANCE_MONITORING
# include "sentry_tracing.h"
#endif

static sentry_options_t *g_options = NULL;
static sentry_mutex_t g_options_lock = SENTRY__MUTEX_INIT;

Expand Down Expand Up @@ -354,21 +357,27 @@ event_is_considered_error(sentry_value_t event)
return false;
}

#ifdef SENTRY_PERFORMANCE_MONITORING
bool
sentry__event_is_transaction(sentry_value_t event)
{
sentry_value_t event_type = sentry_value_get_by_key(event, "type");
return sentry__string_eq("transaction", sentry_value_as_string(event_type));
}
#endif

sentry_uuid_t
sentry_capture_event(sentry_value_t event)
{
#ifdef SENTRY_PERFORMANCE_MONITORING
if (sentry__event_is_transaction(event)) {
return sentry_uuid_nil();
} else {
return sentry__capture_event(event);
}
#else
return sentry__capture_event(event);
#endif
}

sentry_uuid_t
Expand All @@ -381,11 +390,16 @@ sentry__capture_event(sentry_value_t event)
bool was_sent = false;
SENTRY_WITH_OPTIONS (options) {
was_captured = true;

#ifdef SENTRY_PERFORMANCE_MONITORING
if (sentry__event_is_transaction(event)) {
envelope = sentry__prepare_transaction(options, event, &event_id);
} else {
envelope = sentry__prepare_event(options, event, &event_id);
}
#else
envelope = sentry__prepare_event(options, event, &event_id);
#endif
if (envelope) {
if (options->session) {
sentry_options_t *mut_options = sentry__options_lock();
Expand Down Expand Up @@ -415,6 +429,7 @@ sentry__roll_dice(double probability)
|| ((double)rnd / (double)UINT64_MAX) <= probability;
}

#ifdef SENTRY_PERFORMANCE_MONITORING
bool
sentry__should_send_transaction(sentry_value_t tx_cxt)
{
Expand All @@ -431,6 +446,7 @@ sentry__should_send_transaction(sentry_value_t tx_cxt)
}
return send;
}
#endif

sentry_envelope_t *
sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
Expand Down Expand Up @@ -498,6 +514,7 @@ sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
return NULL;
}

#ifdef SENTRY_PERFORMANCE_MONITORING
sentry_envelope_t *
sentry__prepare_transaction(const sentry_options_t *options,
sentry_value_t transaction, sentry_uuid_t *event_id)
Expand Down Expand Up @@ -527,6 +544,7 @@ sentry__prepare_transaction(const sentry_options_t *options,
sentry_value_decref(transaction);
return NULL;
}
#endif

void
sentry_handle_exception(const sentry_ucontext_t *uctx)
Expand Down Expand Up @@ -710,6 +728,7 @@ sentry_set_level(sentry_level_t level)
}
}

#ifdef SENTRY_PERFORMANCE_MONITORING
sentry_value_t
sentry_transaction_start(sentry_value_t tx_cxt)
{
Expand Down Expand Up @@ -797,3 +816,4 @@ sentry_transaction_finish(sentry_value_t tx)
// scope
return sentry__capture_event(tx);
}
#endif
4 changes: 4 additions & 0 deletions src/sentry_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
*/
bool sentry__should_skip_upload(void);

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Given a well-formed event, returns whether an event is a transaction or not.
* Defaults to false, which will also be returned if the event is malformed.
*/
bool sentry__event_is_transaction(sentry_value_t event);
#endif

/**
* Convert the given event into an envelope. This assumes that the event
Expand All @@ -57,6 +59,7 @@ sentry_envelope_t *sentry__prepare_event(const sentry_options_t *options,
*/
sentry_uuid_t sentry__capture_event(sentry_value_t event);

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Convert the given transaction into an envelope. This assumes that the
* event being passed in is a transaction.
Expand All @@ -74,6 +77,7 @@ sentry_uuid_t sentry__capture_event(sentry_value_t event);
*/
sentry_envelope_t *sentry__prepare_transaction(const sentry_options_t *options,
sentry_value_t transaction, sentry_uuid_t *event_id);
#endif

/**
* This function will submit the `envelope` to the given `transport`, first
Expand Down
17 changes: 14 additions & 3 deletions src/sentry_envelope.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,23 @@ sentry_envelope_get_event(const sentry_envelope_t *envelope)
return sentry_value_new_null();
}
for (size_t i = 0; i < envelope->contents.items.item_count; i++) {

#ifdef SENTRY_PERFORMANCE_MONITORING
if (!sentry_value_is_null(envelope->contents.items.items[i].event)
&& !sentry__event_is_transaction(
envelope->contents.items.items[i].event)) {
return envelope->contents.items.items[i].event;
}
#else
if (!sentry_value_is_null(envelope->contents.items.items[i].event)) {
return envelope->contents.items.items[i].event;
}
#endif
}
return sentry_value_new_null();
}

#ifdef SENTRY_PERFORMANCE_MONITORING
sentry_value_t
sentry_envelope_get_transaction(const sentry_envelope_t *envelope)
{
Expand All @@ -224,6 +232,7 @@ sentry_envelope_get_transaction(const sentry_envelope_t *envelope)
}
return sentry_value_new_null();
}
#endif

sentry_envelope_item_t *
sentry__envelope_add_event(sentry_envelope_t *envelope, sentry_value_t event)
Expand Down Expand Up @@ -255,6 +264,7 @@ sentry__envelope_add_event(sentry_envelope_t *envelope, sentry_value_t event)
return item;
}

#ifdef SENTRY_PERFORMANCE_MONITORING
sentry_envelope_item_t *
sentry__envelope_add_transaction(
sentry_envelope_t *envelope, sentry_value_t transaction)
Expand Down Expand Up @@ -283,16 +293,17 @@ sentry__envelope_add_transaction(
sentry_value_incref(event_id);
sentry__envelope_set_header(envelope, "event_id", event_id);

#ifdef SENTRY_UNITTEST
# ifdef SENTRY_UNITTEST
sentry_value_t now = sentry_value_new_string("2021-12-16T05:53:59.343Z");
#else
# else
sentry_value_t now = sentry__value_new_string_owned(
sentry__msec_time_to_iso8601(sentry__msec_time()));
#endif
# endif
sentry__envelope_set_header(envelope, "sent_at", now);

return item;
}
#endif

sentry_envelope_item_t *
sentry__envelope_add_session(
Expand Down
2 changes: 2 additions & 0 deletions src/sentry_envelope.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope);
sentry_envelope_item_t *sentry__envelope_add_event(
sentry_envelope_t *envelope, sentry_value_t event);

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Add a transaction to this envelope.
*/
sentry_envelope_item_t *sentry__envelope_add_transaction(
sentry_envelope_t *envelope, sentry_value_t transaction);
#endif

/**
* Add a session to this envelope.
Expand Down
5 changes: 5 additions & 0 deletions src/sentry_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ sentry_options_new(void)
opts->refcount = 1;
opts->shutdown_timeout = SENTRY_DEFAULT_SHUTDOWN_TIMEOUT;

#ifdef SENTRY_PERFORMANCE_MONITORING
opts->traces_sample_rate = 0.0;
opts->max_spans = 0;
#endif

return opts;
}

Expand Down Expand Up @@ -375,6 +378,7 @@ sentry_options_set_database_pathw(sentry_options_t *opts, const wchar_t *path)
}
#endif

#ifdef SENTRY_PERFORMANCE_MONITORING
/**
* Sets the maximum number of spans that can be attached to a
* transaction.
Expand Down Expand Up @@ -421,3 +425,4 @@ sentry_options_get_traces_sample_rate(sentry_options_t *opts)
{
return opts->traces_sample_rate;
}
#endif
2 changes: 2 additions & 0 deletions src/sentry_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ typedef struct sentry_options_s {
sentry_event_function_t before_send_func;
void *before_send_data;

#ifdef SENTRY_PERFORMANCE_MONITORING
/* Experimentally exposed */
double traces_sample_rate;
size_t max_spans;
#endif

/* everything from here on down are options which are stored here but
not exposed through the options API */
Expand Down
Loading

0 comments on commit 623ce2c

Please sign in to comment.