-
-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathSentryTraceContext.m
More file actions
234 lines (207 loc) · 8.48 KB
/
SentryTraceContext.m
File metadata and controls
234 lines (207 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#import "SentryTraceContext.h"
#import "SentryBaggage.h"
#import "SentryDefines.h"
#import "SentryInternalDefines.h"
#import "SentryLogC.h"
#import "SentrySampleDecision.h"
#import "SentryScope+Private.h"
#import "SentryScope+PrivateSwift.h"
#import "SentrySerialization.h"
#import "SentrySwift.h"
#import "SentryTracer.h"
#import "SentryTransactionContext.h"
#import "SentryUser.h"
NS_ASSUME_NONNULL_BEGIN
@implementation SentryTraceContext
- (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
sampleRate:(nullable NSString *)sampleRate
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId
{
return [self initWithTraceId:traceId
publicKey:publicKey
releaseName:releaseName
environment:environment
transaction:transaction
sampleRate:sampleRate
sampleRand:nil
sampled:sampled
replayId:replayId
orgId:nil];
}
- (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
sampleRate:(nullable NSString *)sampleRate
sampleRand:(nullable NSString *)sampleRand
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId
{
return [self initWithTraceId:traceId
publicKey:publicKey
releaseName:releaseName
environment:environment
transaction:transaction
sampleRate:sampleRate
sampleRand:sampleRand
sampled:sampled
replayId:replayId
orgId:nil];
}
- (instancetype)initWithTraceId:(SentryId *)traceId
publicKey:(NSString *)publicKey
releaseName:(nullable NSString *)releaseName
environment:(nullable NSString *)environment
transaction:(nullable NSString *)transaction
sampleRate:(nullable NSString *)sampleRate
sampleRand:(nullable NSString *)sampleRand
sampled:(nullable NSString *)sampled
replayId:(nullable NSString *)replayId
orgId:(nullable NSString *)orgId
{
if (self = [super init]) {
_traceId = traceId;
_publicKey = publicKey;
_environment = environment;
_releaseName = releaseName;
_transaction = transaction;
_sampleRand = sampleRand;
_sampleRate = sampleRate;
_sampled = sampled;
_replayId = replayId;
_orgId = orgId;
}
return self;
}
- (nullable instancetype)initWithScope:(SentryScope *)scope options:(SentryOptions *)options
{
SentryTracer *tracer = [SentryTracer getTracer:scope.span];
if (tracer == nil) {
return nil;
}
return [self initWithTracer:tracer scope:scope options:options];
}
- (nullable instancetype)initWithTracer:(SentryTracer *)tracer
scope:(nullable SentryScope *)scope
options:(SentryOptions *)options
{
if (tracer.traceId == nil || options.parsedDsn == nil) {
return nil;
}
NSString *serializedSampleRand = nil;
NSNumber *sampleRand = [tracer.transactionContext sampleRand];
if (sampleRand != nil) {
serializedSampleRand = [NSString stringWithFormat:@"%f", sampleRand.doubleValue];
}
NSString *serializedSampleRate = nil;
NSNumber *sampleRate = [tracer.transactionContext sampleRate];
if (sampleRate != nil) {
serializedSampleRate = [NSString stringWithFormat:@"%f", sampleRate.doubleValue];
}
NSString *sampled = nil;
if (tracer.sampled != kSentrySampleDecisionUndecided) {
sampled
= tracer.sampled == kSentrySampleDecisionYes ? kSentryTrueString : kSentryFalseString;
}
return [self initWithTraceId:tracer.traceId
publicKey:SENTRY_UNWRAP_NULLABLE(NSString, options.parsedDsn.url.user)
releaseName:options.releaseName
environment:options.environment
transaction:tracer.transactionContext.name
sampleRate:serializedSampleRate
sampleRand:serializedSampleRand
sampled:sampled
replayId:scope.replayId
orgId:options.effectiveOrgId];
}
- (instancetype)initWithTraceId:(SentryId *)traceId
options:(SentryOptions *)options
replayId:(nullable NSString *)replayId;
{
return [[SentryTraceContext alloc]
initWithTraceId:traceId
publicKey:SENTRY_UNWRAP_NULLABLE(NSString, options.parsedDsn.url.user)
releaseName:options.releaseName
environment:options.environment
transaction:nil
sampleRate:nil
sampleRand:nil
sampled:nil
replayId:replayId
orgId:options.effectiveOrgId];
}
- (nullable instancetype)initWithDict:(NSDictionary<NSString *, id> *)dictionary
{
NSString *_Nullable rawTraceId = dictionary[@"trace_id"];
if (rawTraceId == nil || ![rawTraceId isKindOfClass:[NSString class]]) {
SENTRY_LOG_ERROR(@"Invalid trace_id: %@", rawTraceId);
return nil;
}
SentryId *traceId =
[[SentryId alloc] initWithUUIDString:SENTRY_UNWRAP_NULLABLE(NSString, rawTraceId)];
NSString *publicKey = dictionary[@"public_key"];
if (traceId == nil || publicKey == nil)
return nil;
return [self initWithTraceId:traceId
publicKey:publicKey
releaseName:dictionary[@"release"]
environment:dictionary[@"environment"]
transaction:dictionary[@"transaction"]
sampleRate:dictionary[@"sample_rate"]
sampleRand:dictionary[@"sample_rand"]
sampled:dictionary[@"sampled"]
replayId:dictionary[@"replay_id"]
orgId:dictionary[@"org_id"]];
}
- (SentryBaggage *)toBaggage
{
SentryBaggage *result = [[SentryBaggage alloc] initWithTraceId:_traceId
publicKey:_publicKey
releaseName:_releaseName
environment:_environment
transaction:_transaction
sampleRate:_sampleRate
sampleRand:_sampleRand
sampled:_sampled
replayId:_replayId
orgId:_orgId];
return result;
}
- (NSDictionary<NSString *, id> *)serialize
{
NSMutableDictionary *result =
@{ @"trace_id" : _traceId.sentryIdString, @"public_key" : _publicKey }.mutableCopy;
if (_releaseName != nil) {
[result setValue:_releaseName forKey:@"release"];
}
if (_environment != nil) {
[result setValue:_environment forKey:@"environment"];
}
if (_transaction != nil) {
[result setValue:_transaction forKey:@"transaction"];
}
if (_sampleRand != nil) {
[result setValue:_sampleRand forKey:@"sample_rand"];
}
if (_sampleRate != nil) {
[result setValue:_sampleRate forKey:@"sample_rate"];
}
if (_sampled != nil) {
[result setValue:_sampled forKey:@"sampled"];
}
if (_replayId != nil) {
[result setValue:_replayId forKey:@"replay_id"];
}
if (_orgId != nil) {
[result setValue:_orgId forKey:@"org_id"];
}
return result;
}
@end
NS_ASSUME_NONNULL_END