Skip to content

Commit

Permalink
feat(tracing): Support basic sampling of transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxolotl committed Dec 11, 2021
1 parent a031aae commit 1982965
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,45 @@ sentry_capture_event(sentry_value_t event)
return was_captured ? event_id : sentry_uuid_nil();
}

bool
sentry__make_sampling_decision(double sample_rate)
{
uint64_t rnd;
return sample_rate < 1.0 && !sentry__getrandom(&rnd, sizeof(rnd))
&& ((double)rnd / (double)UINT64_MAX) > sample_rate;
}

bool
sentry__should_sample_transaction(sentry_value_t tx_cxt)
{
sentry_value_t context_setting = sentry_value_get_by_key(tx_cxt, "sampled");
if (!sentry_value_is_null(context_setting)) {
return sentry_value_is_true(context_setting);
}

bool sample = false;
SENTRY_WITH_OPTIONS (options) {
sample = sentry__make_sampling_decision(options->traces_sample_rate);
// TODO: run through traces sampler function if rate is unavailable
}
return sample;
}

bool
sentry__should_skip_event(const sentry_options_t *options, sentry_value_t event)
{
sentry_value_t event_type = sentry_value_get_by_key(event, "type");
// Not a transaction
if (sentry_value_is_null(event_type)) {
return sentry__make_sampling_decision(options->sample_rate);
} else {
// The sampling decision should already be made for transactions
// during their construction. No need to recalculate here.
// See `sentry__should_sample_transaction`.
return !sentry_value_is_true(sentry_value_get_by_key(event, "sampled"));
}
}

sentry_envelope_t *
sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
sentry_uuid_t *event_id)
Expand All @@ -395,9 +434,7 @@ sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
sentry__record_errors_on_current_session(1);
}

uint64_t rnd;
if (options->sample_rate < 1.0 && !sentry__getrandom(&rnd, sizeof(rnd))
&& ((double)rnd / (double)UINT64_MAX) > options->sample_rate) {
if (sentry__should_skip_event(options, event)) {
SENTRY_DEBUG("throwing away event due to sample rate");
goto fail;
}
Expand Down

0 comments on commit 1982965

Please sign in to comment.