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: add traces_sampler #1108

Merged
merged 35 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
60dc201
add traces_sampler option
JoshuaMoelans Dec 20, 2024
dd25b29
update CHANGELOG.md
JoshuaMoelans Dec 20, 2024
9dc332f
introduced `sentry_traces_sampler_function` typedef
JoshuaMoelans Dec 20, 2024
26ff58a
added sampling context
JoshuaMoelans Dec 23, 2024
cc77b52
format
JoshuaMoelans Dec 23, 2024
7344f8a
eof newlines
JoshuaMoelans Dec 24, 2024
78f01c9
feat: pass sampling_context into sampling decision
JoshuaMoelans Jan 3, 2025
49c60eb
remove unnecessary commented include
JoshuaMoelans Jan 3, 2025
2fe7646
add memory freeing/incref-decref
JoshuaMoelans Jan 3, 2025
e08bc8e
change custom sampling context type to sentry_value_t
JoshuaMoelans Jan 7, 2025
2be1836
add decref
JoshuaMoelans Jan 7, 2025
76ca4d2
removed unneeded creation function
JoshuaMoelans Jan 7, 2025
c35fa07
format
JoshuaMoelans Jan 7, 2025
1279675
set codeql runner to ubuntu-22.04
JoshuaMoelans Jan 7, 2025
4e4aa0a
take ownership of custom_sampling_ctx
JoshuaMoelans Jan 9, 2025
372e887
add parent sampling decision
JoshuaMoelans Jan 9, 2025
17177ec
add traces_sampler and parent sampling decision tests
JoshuaMoelans Jan 9, 2025
d43df63
decref
JoshuaMoelans Jan 9, 2025
87efced
change callback arg from struct to parameter list
JoshuaMoelans Jan 9, 2025
f2fe073
format
JoshuaMoelans Jan 9, 2025
a9e2057
mark unused parameters
JoshuaMoelans Jan 10, 2025
a79245c
format
JoshuaMoelans Jan 10, 2025
d172a80
cleanup test + docs
JoshuaMoelans Jan 10, 2025
77e4b23
Merge branch 'master' into joshua/feat/tracesSampler_support
supervacuus Jan 10, 2025
39d9aa3
apply PR feedback
JoshuaMoelans Jan 13, 2025
bfaefe8
format
JoshuaMoelans Jan 13, 2025
e159e12
cleanup example
JoshuaMoelans Jan 13, 2025
a9d7221
add getters for transaction_ctx name + operation
JoshuaMoelans Jan 13, 2025
4610f33
remove (void) on transaction_ctx in example.c
JoshuaMoelans Jan 13, 2025
370b162
add transaction_ctx getter tests
JoshuaMoelans Jan 13, 2025
eb3f9c5
dont expose sampling_context_s in sentry.h
JoshuaMoelans Jan 14, 2025
2936acc
change bool to int
JoshuaMoelans Jan 14, 2025
2175251
cleanup example.c
JoshuaMoelans Jan 14, 2025
72baf78
refactor tx_cxt to tx_ctx convention
JoshuaMoelans Jan 14, 2025
e1d191e
change sampled_int init
JoshuaMoelans Jan 14, 2025
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
**Features**:

- Add option to set debug log level. ([#1107](https://github.com/getsentry/sentry-native/pull/1107))

- Add `traces_sampler` ([#1108](https://github.com/getsentry/sentry-native/pull/1108))
## 0.7.17
supervacuus marked this conversation as resolved.
Show resolved Hide resolved

**Features**:
Expand Down
12 changes: 12 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,18 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_traces_sample_rate(
SENTRY_EXPERIMENTAL_API double sentry_options_get_traces_sample_rate(
sentry_options_t *opts);

struct sentry_sampling_context_s;
typedef struct sentry_sampling_context_s sentry_sampling_context_t;
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
typedef double (*sentry_traces_sampler_function)(
sentry_sampling_context_t *sampling_ctx);

/**
* Sets the traces sampler callback. Should be a function that returns a double
* and (TODO takes in a samplingContext object)
*/
SENTRY_EXPERIMENTAL_API void sentry_options_set_traces_sampler(
sentry_options_t *opts, sentry_traces_sampler_function callback);

#ifdef SENTRY_PLATFORM_LINUX

/**
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ sentry_target_sources_cwd(sentry
sentry_random.h
sentry_ratelimiter.c
sentry_ratelimiter.h
sentry_sampling_context.c
sentry_sampling_context.h
sentry_scope.c
sentry_scope.h
sentry_session.c
Expand Down
14 changes: 11 additions & 3 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,15 @@ sentry__should_send_transaction(sentry_value_t tx_cxt)

bool send = false;
SENTRY_WITH_OPTIONS (options) {
send = sentry__roll_dice(options->traces_sample_rate);
// TODO(tracing): Run through traces sampler function if rate is
// unavailable.
if (options->traces_sampler) {
// TODO use samplingContext instead of only tx_cxt
double result
= ((double (*)(void *))options->traces_sampler)(&tx_cxt);
send = sentry__roll_dice(result);
} else {
// TODO if there is a parent sampling decision, use it
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
send = sentry__roll_dice(options->traces_sample_rate);
}
}
return send;
}
Expand Down Expand Up @@ -875,6 +881,8 @@ sentry_transaction_start_ts(sentry_transaction_context_t *opaque_tx_cxt,
sentry_value_remove_by_key(tx, "timestamp");

sentry__value_merge_objects(tx, tx_cxt);
// TODO construct a sampling context based on tx_cxt and
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
// sampling_ctx, pass it into should_send_transaction

bool should_sample = sentry__should_send_transaction(tx_cxt);
sentry_value_set_by_key(
Expand Down
7 changes: 7 additions & 0 deletions src/sentry_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,13 @@ sentry_options_get_traces_sample_rate(sentry_options_t *opts)
return opts->traces_sample_rate;
}

void
sentry_options_set_traces_sampler(
sentry_options_t *opts, sentry_traces_sampler_function callback)
{
opts->traces_sampler = callback;
}

void
sentry_options_set_backend(sentry_options_t *opts, sentry_backend_t *backend)
{
Expand Down
1 change: 1 addition & 0 deletions src/sentry_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef struct sentry_options_s {

/* Experimentally exposed */
double traces_sample_rate;
sentry_traces_sampler_function traces_sampler;
size_t max_spans;

/* everything from here on down are options which are stored here but
Expand Down
28 changes: 28 additions & 0 deletions src/sentry_sampling_context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// sentry_sampling_context.c
#include "sentry_sampling_context.h"
#include <sentry_alloc.h>
#include <stdlib.h>

sentry_sampling_context_t *
sentry_sampling_context_new(sentry_transaction_context_t *transaction_context)
{
sentry_sampling_context_t *context = SENTRY_MAKE(sentry_sampling_context_t);
if (context) {
context->transaction_context = transaction_context; // todo incref?
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
context->custom_sampling_context = NULL;
}
return context;
}

sentry_sampling_context_t *
sentry_sampling_context_new_with_custom(
sentry_transaction_context_t *transaction_context,
void *custom_sampling_context)
{
sentry_sampling_context_t *context
= sentry_sampling_context_new(transaction_context);
if (context) {
context->custom_sampling_context = custom_sampling_context;
}
return context;
}
19 changes: 19 additions & 0 deletions src/sentry_sampling_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// sentry_sampling_context.h
#ifndef SENTRY_SAMPLING_CONTEXT_H_INCLUDED
#define SENTRY_SAMPLING_CONTEXT_H_INCLUDED

#include "sentry_tracing.h"

typedef struct sentry_sampling_context_s {
sentry_transaction_context_t *transaction_context;
void *custom_sampling_context; // TODO what type should this be?
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
} sentry_sampling_context_t;

// TODO add refcounting for this; users might want to reuse this
JoshuaMoelans marked this conversation as resolved.
Show resolved Hide resolved
sentry_sampling_context_t *sentry_sampling_context_new(
sentry_transaction_context_t *transaction_context);
sentry_sampling_context_t *sentry_sampling_context_new_with_custom(
sentry_transaction_context_t *transaction_context,
void *custom_sampling_context);

#endif // SENTRY_SAMPLING_CONTEXT_H_INCLUDED
Loading