Skip to content

Commit 63765bd

Browse files
jpnurmiclaude
andauthored
feat(options): read sample rates from env vars (#1540)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8058bb3 commit 63765bd

6 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
**Features**:
6+
7+
- Support `SENTRY_SAMPLE_RATE` and `SENTRY_TRACES_SAMPLE_RATE` environment variables. ([#1540](https://github.com/getsentry/sentry-native/pull/1540))
8+
59
**Fixes**:
610

711
- Fix use-after-free on allocation failure when merging scope tags, extra, and contexts into a captured event. ([#1539](https://github.com/getsentry/sentry-native/pull/1539))

src/sentry_options.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sentry_string.h"
99
#include "sentry_sync.h"
1010
#include "sentry_transport.h"
11+
#include "sentry_utils.h"
1112
#include <stdlib.h>
1213

1314
sentry_options_t *
@@ -69,10 +70,12 @@ sentry_options_new(void)
6970
#endif
7071
opts->backend = sentry__backend_new();
7172
opts->transport = sentry__transport_new_default();
72-
opts->sample_rate = 1.0;
7373
opts->refcount = 1;
7474
opts->shutdown_timeout = SENTRY_DEFAULT_SHUTDOWN_TIMEOUT;
75-
opts->traces_sample_rate = 0.0;
75+
sentry_options_set_sample_rate(
76+
opts, sentry__getenv_double("SENTRY_SAMPLE_RATE", 1.0));
77+
sentry_options_set_traces_sample_rate(
78+
opts, sentry__getenv_double("SENTRY_TRACES_SAMPLE_RATE", 0.0));
7679
opts->max_spans = SENTRY_SPANS_MAX;
7780
opts->handler_strategy = SENTRY_HANDLER_STRATEGY_DEFAULT;
7881

src/sentry_utils.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "sentry_random.h"
1212

1313
#include <locale.h>
14+
#include <math.h>
1415
#include <stdarg.h>
1516
#include <stdio.h>
1617
#include <stdlib.h>
@@ -563,6 +564,24 @@ sentry__strtod_c(const char *ptr, char **endptr)
563564
#endif
564565
}
565566

567+
double
568+
sentry__getenv_double(const char *name, double fallback)
569+
{
570+
const char *str = getenv(name);
571+
if (!str) {
572+
return fallback;
573+
}
574+
char *end = NULL;
575+
double val = sentry__strtod_c(str, &end);
576+
if (end == str || !isfinite(val)) {
577+
return fallback;
578+
}
579+
if (end[strspn(end, " \t\r\n")] != '\0') {
580+
return fallback;
581+
}
582+
return val;
583+
}
584+
566585
int
567586
sentry__snprintf_c(char *buf, size_t buf_size, const char *fmt, ...)
568587
{

src/sentry_utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ uint64_t sentry__iso8601_to_usec(const char *iso);
215215
*/
216216
double sentry__strtod_c(const char *ptr, char **endptr);
217217

218+
/**
219+
* Reads a double from the environment variable `name`.
220+
* Returns `fallback` if the variable is unset, empty, or not a finite number.
221+
* Leading and trailing whitespace is tolerated.
222+
*/
223+
double sentry__getenv_double(const char *name, double fallback);
224+
218225
/**
219226
* Locale independent (or rather, using "C" locale) `snprintf`.
220227
*/

tests/unit/test_utils.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
#include "sentry_testsupport.h"
33
#include "sentry_utils.h"
44
#include "sentry_value.h"
5+
#include <stdlib.h>
56

67
#ifdef SENTRY_PLATFORM_UNIX
78
# include "sentry_unix_pageallocator.h"
89
#endif
910

11+
#ifdef SENTRY_PLATFORM_WINDOWS
12+
# define setenv(k, v, o) _putenv_s(k, v)
13+
# define unsetenv(k) _putenv_s(k, "")
14+
#endif
15+
1016
SENTRY_TEST(iso_time)
1117
{
1218
uint64_t usec;
@@ -421,3 +427,36 @@ SENTRY_TEST(dsn_auth_header_invalid_dsn)
421427
sentry_free(auth_header);
422428
sentry__dsn_decref(dsn);
423429
}
430+
431+
SENTRY_TEST(getenv_double)
432+
{
433+
setenv("SENTRY_TEST_DOUBLE", "", 1);
434+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
435+
436+
setenv("SENTRY_TEST_DOUBLE", "0.5", 1);
437+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 99.0) == 0.5);
438+
439+
setenv("SENTRY_TEST_DOUBLE", "-3.14", 1);
440+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 99.0) == -3.14);
441+
442+
setenv("SENTRY_TEST_DOUBLE", "0", 1);
443+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 99.0) == 0.0);
444+
445+
setenv("SENTRY_TEST_DOUBLE", " 1.0 \t", 1);
446+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 99.0) == 1.0);
447+
448+
setenv("SENTRY_TEST_DOUBLE", "not_a_number", 1);
449+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
450+
451+
setenv("SENTRY_TEST_DOUBLE", "NaN", 1);
452+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
453+
454+
setenv("SENTRY_TEST_DOUBLE", "inf", 1);
455+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
456+
457+
setenv("SENTRY_TEST_DOUBLE", "-inf", 1);
458+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
459+
460+
unsetenv("SENTRY_TEST_DOUBLE");
461+
TEST_CHECK(sentry__getenv_double("SENTRY_TEST_DOUBLE", 42.0) == 42.0);
462+
}

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ XX(feedback_with_null_hint)
103103
XX(feedback_without_hint)
104104
XX(formatted_log_messages)
105105
XX(fuzz_json)
106+
XX(getenv_double)
106107
XX(init_failure)
107108
XX(internal_uuid_api)
108109
XX(invalid_dsn)

0 commit comments

Comments
 (0)