Skip to content

Commit

Permalink
feat: add sample_rand / fix sample_rate in baggage (#4751)
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime authored Jan 31, 2025
1 parent 70c49ca commit 675ef9c
Show file tree
Hide file tree
Showing 30 changed files with 2,080 additions and 96 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

## Improvements

- Add `sample_rand` to baggage (#4751)

## Fixes

- Fix missing `sample_rate` in baggage (#4751)

## 8.44.0

### Fixes
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ test:
run-test-server:
cd ./test-server && swift build
cd ./test-server && swift run &
.PHONY: run-test-server

run-test-server-sync:
cd ./test-server && swift build
cd ./test-server && swift run

.PHONY: run-test-server run-test-server-sync

test-alamofire:
./scripts/test-alamofire.sh
Expand Down
22 changes: 13 additions & 9 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
A8AFFCD42907E0CA00967CD7 /* SentryRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8AFFCD32907E0CA00967CD7 /* SentryRequestTests.swift */; };
A8F17B2E2901765900990B25 /* SentryRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F17B2D2901765900990B25 /* SentryRequest.m */; };
A8F17B342902870300990B25 /* SentryHttpStatusCodeRange.m in Sources */ = {isa = PBXBuildFile; fileRef = A8F17B332902870300990B25 /* SentryHttpStatusCodeRange.m */; };
D42E48572D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D42E48562D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift */; };
D48724DB2D352597005DE483 /* SentryTraceOrigin.swift in Sources */ = {isa = PBXBuildFile; fileRef = D48724DA2D352591005DE483 /* SentryTraceOrigin.swift */; };
D48724DD2D354939005DE483 /* SentrySpanOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = D48724DC2D354934005DE483 /* SentrySpanOperation.swift */; };
D48724E02D3549CA005DE483 /* SentrySpanOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D48724DF2D3549C6005DE483 /* SentrySpanOperationTests.swift */; };
Expand Down Expand Up @@ -894,7 +895,7 @@
D8853C842833EABC00700D64 /* SentryANRTrackerV1.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BCFA71427D0BAB7008C662C /* SentryANRTrackerV1.h */; };
D88817D826D7149100BF2251 /* SentryTraceContext.m in Sources */ = {isa = PBXBuildFile; fileRef = D88817D626D7149100BF2251 /* SentryTraceContext.m */; };
D88817DA26D72AB800BF2251 /* SentryTraceContext.h in Headers */ = {isa = PBXBuildFile; fileRef = D88817D926D72AB800BF2251 /* SentryTraceContext.h */; settings = {ATTRIBUTES = (Public, ); }; };
D88817DD26D72BA500BF2251 /* SentryTraceStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88817DB26D72B7B00BF2251 /* SentryTraceStateTests.swift */; };
D88817DD26D72BA500BF2251 /* SentryTraceContextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88817DB26D72B7B00BF2251 /* SentryTraceContextTests.swift */; };
D88B30A92D48D8C3008DE513 /* SentryMaskingPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88B30A82D48D88E008DE513 /* SentryMaskingPreviewView.swift */; };
D8918B222849FA6D00701F9A /* SentrySDKIntegrationTestsBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8918B212849FA6D00701F9A /* SentrySDKIntegrationTestsBase.swift */; };
D8A3649C2C91AA3300AC569B /* SentryReplayApi.m in Sources */ = {isa = PBXBuildFile; fileRef = D8A3649B2C91AA3300AC569B /* SentryReplayApi.m */; };
Expand Down Expand Up @@ -1897,6 +1898,7 @@
A8AFFCD32907E0CA00967CD7 /* SentryRequestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryRequestTests.swift; sourceTree = "<group>"; };
A8F17B2D2901765900990B25 /* SentryRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryRequest.m; sourceTree = "<group>"; };
A8F17B332902870300990B25 /* SentryHttpStatusCodeRange.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryHttpStatusCodeRange.m; sourceTree = "<group>"; };
D42E48562D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBuildAppStartSpansTests.swift; sourceTree = "<group>"; };
D41909922D48FFF6002B83D0 /* SentryNSDictionarySanitize+Tests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryNSDictionarySanitize+Tests.h"; sourceTree = "<group>"; };
D41909942D490006002B83D0 /* SentryNSDictionarySanitize+Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentryNSDictionarySanitize+Tests.m"; sourceTree = "<group>"; };
D42E48582D48FC8F00D251BC /* SentryNSDictionarySanitizeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSDictionarySanitizeTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2013,7 +2015,7 @@
D885266327739D01001269FC /* SentryFileIOTrackingIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryFileIOTrackingIntegrationTests.swift; sourceTree = "<group>"; };
D88817D626D7149100BF2251 /* SentryTraceContext.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTraceContext.m; sourceTree = "<group>"; };
D88817D926D72AB800BF2251 /* SentryTraceContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryTraceContext.h; path = Public/SentryTraceContext.h; sourceTree = "<group>"; };
D88817DB26D72B7B00BF2251 /* SentryTraceStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTraceStateTests.swift; sourceTree = "<group>"; };
D88817DB26D72B7B00BF2251 /* SentryTraceContextTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTraceContextTests.swift; sourceTree = "<group>"; };
D88B30A82D48D88E008DE513 /* SentryMaskingPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMaskingPreviewView.swift; sourceTree = "<group>"; };
D88D25E92B8E0BAC0073C3D5 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = "<group>"; };
D8918B212849FA6D00701F9A /* SentrySDKIntegrationTestsBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySDKIntegrationTestsBase.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3016,17 +3018,18 @@
7B6C5ED4264E62B60010D138 /* Transaction */ = {
isa = PBXGroup;
children = (
7B6C5ED5264E62CA0010D138 /* SentryTransactionTests.swift */,
D880E3A628573E87008A90DB /* SentryBaggageTests.swift */,
D42E48562D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift */,
7BE912AE272166DD00E49E62 /* SentryNoOpSpanTests.swift */,
8EC4CF4F25C3A0070093DEE9 /* SentrySpanContextTests.swift */,
8E70B0FC25CB72BE002B3155 /* SentrySpanTests.swift */,
D88817DB26D72B7B00BF2251 /* SentryTraceStateTests.swift */,
7BE912AE272166DD00E49E62 /* SentryNoOpSpanTests.swift */,
D880E3A628573E87008A90DB /* SentryBaggageTests.swift */,
8EAC7FF7265C8910005B44E5 /* SentryTracerTests.swift */,
D88817DB26D72B7B00BF2251 /* SentryTraceContextTests.swift */,
7BBEB16026AEE5EF00C06C03 /* SentryTracer+Test.h */,
8EAC7FF7265C8910005B44E5 /* SentryTracerTests.swift */,
62950F0F29E7FE0100A42624 /* SentryTransactionContextTests.swift */,
7B6C5ED5264E62CA0010D138 /* SentryTransactionTests.swift */,
D8137D52272B53070082656C /* TestSentrySpan.h */,
D8137D53272B53070082656C /* TestSentrySpan.m */,
62950F0F29E7FE0100A42624 /* SentryTransactionContextTests.swift */,
);
path = Transaction;
sourceTree = "<group>";
Expand Down Expand Up @@ -5146,7 +5149,7 @@
62CFD9AD2C99770B00834E1B /* SentryInvalidJSONString.m in Sources */,
62375FB92B47F9F000CC55F1 /* SentryDependencyContainerTests.swift in Sources */,
7BC6EC08255C36DE0059822A /* SentryStacktraceTests.swift in Sources */,
D88817DD26D72BA500BF2251 /* SentryTraceStateTests.swift in Sources */,
D88817DD26D72BA500BF2251 /* SentryTraceContextTests.swift in Sources */,
7B26BBFB24C0A66D00A79CCC /* SentrySdkInfoNilTests.m in Sources */,
D4E3F35D2D4A864600F79E2B /* SentryNSDictionarySanitizeTests.swift in Sources */,
7B984A9F28E572AF001F4BEE /* CrashReport.swift in Sources */,
Expand Down Expand Up @@ -5245,6 +5248,7 @@
849AC40029E0C1FF00889C16 /* SentryFormatterTests.swift in Sources */,
7BDDE3CC2966BD4700EB9177 /* SentryMXManagerTests.swift in Sources */,
7BC6EC0C255C3DF80059822A /* SentryThreadTests.swift in Sources */,
D42E48572D48DF1600D251BC /* SentryBuildAppStartSpansTests.swift in Sources */,
D884A20527C80F6300074664 /* SentryCoreDataTrackerTest.swift in Sources */,
8E70B10125CB8695002B3155 /* SentrySpanIdTests.swift in Sources */,
84EB21962BF01CEA00EDDA28 /* SentryCrashInstallationTests.swift in Sources */,
Expand Down
4 changes: 3 additions & 1 deletion SentryTestUtils/SentryLaunchProfiling+Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ typedef struct {
} SentryLaunchProfileConfig;

SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate;
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyTracesSampleRand;
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate;
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRand;
SENTRY_EXTERN NSString *const kSentryLaunchProfileConfigKeyContinuousProfiling;

SENTRY_EXTERN SentryTracer *_Nullable sentry_launchTracer;
Expand All @@ -39,7 +41,7 @@ BOOL sentry_willProfileNextLaunch(SentryOptions *options);
*/
void _sentry_nondeduplicated_startLaunchProfile(void);

SentryTransactionContext *sentry_context(NSNumber *tracesRate);
SentryTransactionContext *sentry_context(NSNumber *tracesRate, NSNumber *tracesRand);

NS_ASSUME_NONNULL_END

Expand Down
36 changes: 29 additions & 7 deletions Sources/Sentry/Profiling/SentryLaunchProfiling.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

BOOL isProfilingAppLaunch;
NSString *const kSentryLaunchProfileConfigKeyTracesSampleRate = @"traces";
NSString *const kSentryLaunchProfileConfigKeyTracesSampleRand = @"traces.sample_rand";
NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRate = @"profiles";
NSString *const kSentryLaunchProfileConfigKeyProfilesSampleRand = @"profiles.sample_rand";
NSString *const kSentryLaunchProfileConfigKeyContinuousProfiling = @"continuous-profiling";
static SentryTracer *_Nullable launchTracer;

Expand All @@ -34,12 +36,13 @@
SentryTracer *_Nullable sentry_launchTracer;

SentryTracerConfiguration *
sentry_config(NSNumber *profilesRate)
sentry_config(NSNumber *profilesRate, NSNumber *profilesRand)
{
SentryTracerConfiguration *config = [SentryTracerConfiguration defaultConfiguration];
config.profilesSamplerDecision =
[[SentrySamplerDecision alloc] initWithDecision:kSentrySampleDecisionYes
forSampleRate:profilesRate];
forSampleRate:profilesRate
withSampleRand:profilesRand];
return config;
}

Expand Down Expand Up @@ -92,15 +95,16 @@
}

SentryTransactionContext *
sentry_context(NSNumber *tracesRate)
sentry_context(NSNumber *tracesRate, NSNumber *tracesRand)
{
SentryTransactionContext *context =
[[SentryTransactionContext alloc] initWithName:@"launch"
nameSource:kSentryTransactionNameSourceCustom
operation:SentrySpanOperation.appLifecycle
origin:SentryTraceOrigin.autoAppStartProfile
sampled:kSentrySampleDecisionYes];
context.sampleRate = tracesRate;
sampled:kSentrySampleDecisionYes
sampleRate:tracesRate
sampleRand:tracesRand];
return context;
}

Expand Down Expand Up @@ -143,19 +147,33 @@
return;
}

NSNumber *profilesRand = launchConfig[kSentryLaunchProfileConfigKeyProfilesSampleRand];
if (profilesRate == nil) {
SENTRY_LOG_DEBUG(@"Received a nil configured launch profile sample rand, will not "
@"start trace profiler for launch.");
return;
}

NSNumber *tracesRate = launchConfig[kSentryLaunchProfileConfigKeyTracesSampleRate];
if (tracesRate == nil) {
SENTRY_LOG_DEBUG(@"Received a nil configured launch trace sample rate, will not start "
@"trace profiler for launch.");
return;
}

NSNumber *tracesRand = launchConfig[kSentryLaunchProfileConfigKeyTracesSampleRand];
if (tracesRate == nil) {
SENTRY_LOG_DEBUG(@"Received a nil configured launch trace sample rand, will not start "
@"trace profiler for launch.");
return;
}

SENTRY_LOG_INFO(@"Starting app launch trace profile at %llu.", getAbsoluteTime());
sentry_isTracingAppLaunch = YES;
sentry_launchTracer =
[[SentryTracer alloc] initWithTransactionContext:sentry_context(tracesRate)
[[SentryTracer alloc] initWithTransactionContext:sentry_context(tracesRate, tracesRand)
hub:nil
configuration:sentry_config(profilesRate)];
configuration:sentry_config(profilesRate, profilesRand)];
}

# pragma mark - Public
Expand All @@ -179,8 +197,12 @@
} else {
configDict[kSentryLaunchProfileConfigKeyTracesSampleRate]
= config.tracesDecision.sampleRate;
configDict[kSentryLaunchProfileConfigKeyTracesSampleRand]
= config.tracesDecision.sampleRand;
configDict[kSentryLaunchProfileConfigKeyProfilesSampleRate]
= config.profilesDecision.sampleRate;
configDict[kSentryLaunchProfileConfigKeyProfilesSampleRand]
= config.profilesDecision.sampleRand;
}
writeAppLaunchProfilingConfigFile(configDict);
}];
Expand Down
18 changes: 18 additions & 0 deletions Sources/Sentry/Public/SentryBaggage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ NS_SWIFT_NAME(Baggage)
*/
@property (nullable, nonatomic, readonly) NSString *userSegment;

/**
* The random value used to determine if the trace is sampled.
*
* A float (`0.1234` notation) in the range of `[0, 1)` (including 0.0, excluding 1.0).
*/
@property (nullable, nonatomic, readonly) NSString *sampleRand;

/**
* The sample rate.
*/
Expand All @@ -67,6 +74,17 @@ NS_SWIFT_NAME(Baggage)
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId;

- (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate
sampleRand:(nullable NSString *)sampleRand
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId;

- (NSString *)toHTTPHeaderWithOriginalBaggage:(NSDictionary *_Nullable)originalBaggage;

@end
Expand Down
21 changes: 20 additions & 1 deletion Sources/Sentry/Public/SentryTraceContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ NS_SWIFT_NAME(TraceContext)
@property (nullable, nonatomic, readonly) NSString *userSegment;

/**
* Sample rate used for this trace.
* Serialized sample rate used for this trace.
*/
@property (nullable, nonatomic, readonly) NSString *sampleRate;

/**
* Serialized random value used to determine if the trace is sampled.
*/
@property (nullable, nonatomic, readonly) NSString *sampleRand;

/**
* Value indicating whether the trace was sampled.
*/
Expand All @@ -75,6 +80,20 @@ NS_SWIFT_NAME(TraceContext)
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId;

/**
* Initializes a SentryTraceContext with given properties.
*/
- (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
userSegment:(nullable NSString *)userSegment
sampleRate:(nullable NSString *)sampleRate
sampleRand:(nullable NSString *)sampleRand
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId;

/**
* Initializes a SentryTraceContext with data from scope and options.
*/
Expand Down
55 changes: 51 additions & 4 deletions Sources/Sentry/Public/SentryTransactionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,30 @@ SENTRY_NO_INIT
@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) SentryTransactionNameSource nameSource;

/**
* Rate of sampling
*/
@property (nonatomic, strong, nullable) NSNumber *sampleRate;

/**
* Random value used to determine if the span is sampled.
*/
@property (nonatomic, strong, nullable) NSNumber *sampleRand;

/**
* Parent sampled
*/
@property (nonatomic) SentrySampleDecision parentSampled;

/**
* Sample rate used for this transaction
* Parent sample rate used for this transaction
*/
@property (nonatomic, strong, nullable) NSNumber *sampleRate;
@property (nonatomic, strong, nullable) NSNumber *parentSampleRate;

/**
* Parent random value used to determine if the trace is sampled.
*/
@property (nonatomic, strong, nullable) NSNumber *parentSampleRand;

/**
* If app launch profiling is enabled via @c SentryOptions.enableAppLaunchProfiling and
Expand All @@ -54,7 +69,37 @@ SENTRY_NO_INIT
*/
- (instancetype)initWithName:(NSString *)name
operation:(NSString *)operation
sampled:(SentrySampleDecision)sampled;
sampled:(SentrySampleDecision)sampled
DEPRECATED_MSG_ATTRIBUTE("Use initWithName:operation:sampled:sampleRate:sampleRand instead");

/**
* @param name Transaction name
* @param operation The operation this span is measuring.
* @param sampled Determines whether the trace should be sampled.
*/
- (instancetype)initWithName:(NSString *)name
operation:(NSString *)operation
sampled:(SentrySampleDecision)sampled
sampleRate:(nullable NSNumber *)sampleRate
sampleRand:(nullable NSNumber *)sampleRand;

/**
* @param name Transaction name
* @param operation The operation this span is measuring.
* @param traceId Trace Id
* @param spanId Span Id
* @param parentSpanId Parent span id
* @param parentSampled Whether the parent is sampled
*/
- (instancetype)initWithName:(NSString *)name
operation:(NSString *)operation
traceId:(SentryId *)traceId
spanId:(SentrySpanId *)spanId
parentSpanId:(nullable SentrySpanId *)parentSpanId
parentSampled:(SentrySampleDecision)parentSampled
DEPRECATED_MSG_ATTRIBUTE("Use "
"initWithName:operation:traceId:spanId:parentSpanId:parentSampled:"
"parentSampleRate:parentSampleRand instead");

/**
* @param name Transaction name
Expand All @@ -69,7 +114,9 @@ SENTRY_NO_INIT
traceId:(SentryId *)traceId
spanId:(SentrySpanId *)spanId
parentSpanId:(nullable SentrySpanId *)parentSpanId
parentSampled:(SentrySampleDecision)parentSampled;
parentSampled:(SentrySampleDecision)parentSampled
parentSampleRate:(nullable NSNumber *)parentSampleRate
parentSampleRand:(nullable NSNumber *)parentSampleRand;

@end

Expand Down
Loading

0 comments on commit 675ef9c

Please sign in to comment.