Skip to content

Commit 72c8d84

Browse files
authored
feat: Add Sampling Decision to Trace Envelope Header (#3286)
Adds sampled flag to all trace envelope headers
1 parent 97797b4 commit 72c8d84

File tree

12 files changed

+73
-21
lines changed

12 files changed

+73
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
### Features
2121

22-
- Rename "http.method" to "http.request.method" for network Spans #3268
22+
- Rename "http.method" to "http.request.method" for network Spans (#3268)
23+
- Add Sampling Decision to Trace Envelope Header (#3286)
2324

2425
## 8.11.0
2526

Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 52;
6+
objectVersion = 54;
77
objects = {
88

99
/* Begin PBXBuildFile section */

Sources/Sentry/Public/SentryDefines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ static DEPRECATED_MSG_ATTRIBUTE(
118118

119119
static NSUInteger const defaultMaxBreadcrumbs = 100;
120120

121+
static NSString *_Nonnull const kSentryTrueString = @"true";
122+
static NSString *_Nonnull const kSentryFalseString = @"false";
123+
121124
/**
122125
* Transaction name source.
123126
*/

Sources/Sentry/SentryBaggage.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
1717
transaction:(nullable NSString *)transaction
1818
userSegment:(nullable NSString *)userSegment
1919
sampleRate:(nullable NSString *)sampleRate
20+
sampled:(nullable NSString *)sampled
2021
{
2122

2223
if (self = [super init]) {
@@ -27,6 +28,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
2728
_transaction = transaction;
2829
_userSegment = userSegment;
2930
_sampleRate = sampleRate;
31+
_sampled = sampled;
3032
}
3133

3234
return self;
@@ -65,6 +67,10 @@ - (NSString *)toHTTPHeaderWithOriginalBaggage:(NSDictionary *_Nullable)originalB
6567
[information setValue:_sampleRate forKey:@"sentry-sample_rate"];
6668
}
6769

70+
if (_sampled != nil) {
71+
[information setValue:_sampled forKey:@"sentry-sampled"];
72+
}
73+
6874
return [SentrySerialization baggageEncodedDictionary:information];
6975
}
7076

Sources/Sentry/SentryPropagationContext.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ - (SentryTraceContext *)traceContext
3838
environment:options.environment
3939
transaction:nil
4040
userSegment:scope.userObject.segment
41-
sampleRate:nil];
41+
sampleRate:nil
42+
sampled:nil];
4243
}
4344

4445
- (NSDictionary<NSString *, NSString *> *)traceContextForEvent

Sources/Sentry/SentryTraceContext.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#import "SentryTraceContext.h"
22
#import "SentryBaggage.h"
3+
#import "SentryDefines.h"
34
#import "SentryDsn.h"
45
#import "SentryId.h"
56
#import "SentryLog.h"
67
#import "SentryOptions+Private.h"
8+
#import "SentrySampleDecision.h"
79
#import "SentryScope+Private.h"
810
#import "SentrySerialization.h"
911
#import "SentryTracer.h"
@@ -21,6 +23,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
2123
transaction:(nullable NSString *)transaction
2224
userSegment:(nullable NSString *)userSegment
2325
sampleRate:(nullable NSString *)sampleRate
26+
sampled:(nullable NSString *)sampled
2427
{
2528
if (self = [super init]) {
2629
_traceId = traceId;
@@ -30,6 +33,7 @@ - (instancetype)initWithTraceId:(SentryId *)traceId
3033
_transaction = transaction;
3134
_userSegment = userSegment;
3235
_sampleRate = sampleRate;
36+
_sampled = sampled;
3337
}
3438
return self;
3539
}
@@ -63,13 +67,20 @@ - (nullable instancetype)initWithTracer:(SentryTracer *)tracer
6367
[NSString stringWithFormat:@"%@", [(SentryTransactionContext *)tracer sampleRate]];
6468
}
6569

70+
NSString *sampled = nil;
71+
if (tracer.sampled != kSentrySampleDecisionUndecided) {
72+
sampled
73+
= tracer.sampled == kSentrySampleDecisionYes ? kSentryTrueString : kSentryFalseString;
74+
}
75+
6676
return [self initWithTraceId:tracer.traceId
6777
publicKey:options.parsedDsn.url.user
6878
releaseName:options.releaseName
6979
environment:options.environment
7080
transaction:tracer.transactionContext.name
7181
userSegment:userSegment
72-
sampleRate:sampleRate];
82+
sampleRate:sampleRate
83+
sampled:sampled];
7384
}
7485

7586
- (nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)dictionary
@@ -94,7 +105,8 @@ - (nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)dictionary
94105
environment:dictionary[@"environment"]
95106
transaction:dictionary[@"transaction"]
96107
userSegment:userSegment
97-
sampleRate:dictionary[@"sample_rate"]];
108+
sampleRate:dictionary[@"sample_rate"]
109+
sampled:dictionary[@"sampled"]];
98110
}
99111

100112
- (SentryBaggage *)toBaggage
@@ -105,7 +117,8 @@ - (SentryBaggage *)toBaggage
105117
environment:_environment
106118
transaction:_transaction
107119
userSegment:_userSegment
108-
sampleRate:_sampleRate];
120+
sampleRate:_sampleRate
121+
sampled:_sampled];
109122
return result;
110123
}
111124

@@ -134,6 +147,10 @@ - (SentryBaggage *)toBaggage
134147
[result setValue:_sampleRate forKey:@"sample_rate"];
135148
}
136149

150+
if (_sampled != nil) {
151+
[result setValue:_sampleRate forKey:@"sampled"];
152+
}
153+
137154
return result;
138155
}
139156

Sources/Sentry/include/SentryBaggage.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ static NSString *const SENTRY_BAGGAGE_HEADER = @"baggage";
4949
*/
5050
@property (nullable, nonatomic, readonly) NSString *sampleRate;
5151

52+
/**
53+
* Value indicating whether the trace was sampled.
54+
*/
55+
@property (nullable, nonatomic, strong) NSString *sampled;
56+
5257
- (instancetype)initWithTraceId:(SentryId *)traceId
5358
publicKey:(NSString *)publicKey
5459
releaseName:(nullable NSString *)releaseName
5560
environment:(nullable NSString *)environment
5661
transaction:(nullable NSString *)transaction
5762
userSegment:(nullable NSString *)userSegment
58-
sampleRate:(nullable NSString *)sampleRate;
63+
sampleRate:(nullable NSString *)sampleRate
64+
sampled:(nullable NSString *)sampled;
5965

6066
- (NSString *)toHTTPHeader;
6167
- (NSString *)toHTTPHeaderWithOriginalBaggage:(NSDictionary *_Nullable)originalBaggage;

Sources/Sentry/include/SentryTraceContext.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ NS_ASSUME_NONNULL_BEGIN
5050
/**
5151
* Sample rate used for this trace.
5252
*/
53-
@property (nullable, nonatomic, strong) NSString *sampleRate;
53+
@property (nullable, nonatomic, readonly) NSString *sampleRate;
54+
55+
/**
56+
* Value indicating whether the trace was sampled.
57+
*/
58+
@property (nullable, nonatomic, readonly) NSString *sampled;
5459

5560
/**
5661
* Initializes a SentryTraceContext with given properties.
@@ -61,7 +66,8 @@ NS_ASSUME_NONNULL_BEGIN
6166
environment:(nullable NSString *)environment
6267
transaction:(nullable NSString *)transaction
6368
userSegment:(nullable NSString *)userSegment
64-
sampleRate:(nullable NSString *)sampleRate;
69+
sampleRate:(nullable NSString *)sampleRate
70+
sampled:(nullable NSString *)sampled;
6571

6672
/**
6773
* Initializes a SentryTraceContext with data from scope and options.

Tests/SentryTests/Helper/SentrySerializationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class SentrySerializationTests: XCTestCase {
44

55
private class Fixture {
66
static var invalidData = "hi".data(using: .utf8)!
7-
static var traceContext = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: "some segment", sampleRate: "0.25")
7+
static var traceContext = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: "some segment", sampleRate: "0.25", sampled: "true")
88
}
99

1010
func testSerializationFailsWithInvalidJSONObject() {
@@ -123,7 +123,7 @@ class SentrySerializationTests: XCTestCase {
123123
}
124124

125125
func testSentryEnvelopeSerializer_TraceStateWithoutUser() {
126-
let trace = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: nil, sampleRate: nil)
126+
let trace = SentryTraceContext(trace: SentryId(), publicKey: "PUBLIC_KEY", releaseName: "RELEASE_NAME", environment: "TEST", transaction: "transaction", userSegment: nil, sampleRate: nil, sampled: nil)
127127

128128
let envelopeHeader = SentryEnvelopeHeader(id: nil, traceContext: trace)
129129
let envelope = SentryEnvelope(header: envelopeHeader, singleItem: createItemWithEmptyAttachment())

Tests/SentryTests/Protocol/SentryEnvelopeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class SentryEnvelopeTests: XCTestCase {
158158

159159
func testInitSentryEnvelopeHeader_SetIdAndTraceState() {
160160
let eventId = SentryId()
161-
let traceContext = SentryTraceContext(trace: SentryId(), publicKey: "publicKey", releaseName: "releaseName", environment: "environment", transaction: "transaction", userSegment: nil, sampleRate: nil)
161+
let traceContext = SentryTraceContext(trace: SentryId(), publicKey: "publicKey", releaseName: "releaseName", environment: "environment", transaction: "transaction", userSegment: nil, sampleRate: nil, sampled: nil)
162162

163163
let envelopeHeader = SentryEnvelopeHeader(id: eventId, traceContext: traceContext)
164164
XCTAssertEqual(eventId, envelopeHeader.eventId)

0 commit comments

Comments
 (0)