From 0be48b6904872ee7cd398ca7c588054a8a96a17e Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Tue, 12 Jul 2022 17:55:47 +0200 Subject: [PATCH] ref: Mark options.sdkInfo as deprecated (#1960) options.sdkInfo is deprecated; SentryMeta is now the only source of truth. The values can be overridden by hybrid SDKs via PrivateSentrySDKOnly. --- CHANGELOG.md | 1 + Sentry.xcodeproj/project.pbxproj | 4 ++ Sources/Sentry/PrivateSentrySDKOnly.m | 7 ++++ Sources/Sentry/Public/PrivateSentrySDKOnly.h | 5 +++ Sources/Sentry/Public/SentryOptions.h | 4 +- Sources/Sentry/SentryClient.m | 8 ++-- Sources/Sentry/SentryEnvelope.m | 13 +------ Sources/Sentry/SentryMeta.m | 14 ++++++- Sources/Sentry/SentryOptions.m | 14 +++++-- Sources/Sentry/SentryTransportAdapter.m | 16 +++----- Sources/Sentry/include/SentryMeta.h | 4 +- Tests/SentryTests/SentryMetaTests.swift | 14 +++++++ Tests/SentryTests/SentryOptionsTest.m | 41 +++++++++++++++++++- Tests/SentryTests/SentryTests.m | 4 +- 14 files changed, 112 insertions(+), 37 deletions(-) create mode 100644 Tests/SentryTests/SentryMetaTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index f3258b9f93d..6ff8ae4cd39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Properly sanitize the event context and SDK information (#1943) - Don't send error 429 as `network_error` (#1957) +- Deprecate not needed option `sdkInfo` (#1960) ## 7.20.0 diff --git a/Sentry.xcodeproj/project.pbxproj b/Sentry.xcodeproj/project.pbxproj index f6ce2efcddb..8cdad541437 100644 --- a/Sentry.xcodeproj/project.pbxproj +++ b/Sentry.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 03F84D3727DD4191008FE43F /* SentrySamplingProfiler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D3027DD4191008FE43F /* SentrySamplingProfiler.cpp */; }; 03F84D3827DD4191008FE43F /* SentryBacktrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 03F84D3127DD4191008FE43F /* SentryBacktrace.cpp */; }; 03F9D37C2819A65C00602916 /* SentryProfilerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 03F9D37B2819A65C00602916 /* SentryProfilerTests.mm */; }; + 0A1C3592287D7107007D01E3 /* SentryMetaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */; }; 15360CCF2432777500112302 /* SentrySessionTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 15360CCE2432777400112302 /* SentrySessionTracker.m */; }; 15360CD2243277A000112302 /* SentrySessionTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 15360CD12432779F00112302 /* SentrySessionTracker.h */; }; 15360CD62432832400112302 /* SentryAutoSessionTrackingIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = 15360CD52432832400112302 /* SentryAutoSessionTrackingIntegration.m */; }; @@ -704,6 +705,7 @@ 03F84D3027DD4191008FE43F /* SentrySamplingProfiler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentrySamplingProfiler.cpp; path = Sources/Sentry/SentrySamplingProfiler.cpp; sourceTree = SOURCE_ROOT; }; 03F84D3127DD4191008FE43F /* SentryBacktrace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SentryBacktrace.cpp; path = Sources/Sentry/SentryBacktrace.cpp; sourceTree = SOURCE_ROOT; }; 03F9D37B2819A65C00602916 /* SentryProfilerTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryProfilerTests.mm; sourceTree = ""; }; + 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryMetaTests.swift; sourceTree = ""; }; 15360CCE2432777400112302 /* SentrySessionTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentrySessionTracker.m; sourceTree = ""; }; 15360CD12432779F00112302 /* SentrySessionTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentrySessionTracker.h; path = include/SentrySessionTracker.h; sourceTree = ""; }; 15360CD52432832400112302 /* SentryAutoSessionTrackingIntegration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryAutoSessionTrackingIntegration.m; sourceTree = ""; }; @@ -1759,6 +1761,7 @@ 630C01931EC3402C00C52CEF /* SentryKSCrashReportConverterTests.m */, 63B819131EC352A7002FDF4C /* SentryInterfacesTests.m */, 63EED6C22237989300E02400 /* SentryOptionsTest.m */, + 0A1C3591287D7107007D01E3 /* SentryMetaTests.swift */, 632331F52404FFA8008D91D6 /* SentryScopeTests.m */, 7B569DFE2590EEF600B653FC /* SentryScope+Equality.h */, 7B569DFF2590EEF600B653FC /* SentryScope+Equality.m */, @@ -3381,6 +3384,7 @@ 7BBD18992449DE9D00427C76 /* TestRateLimits.swift in Sources */, 8E4A038625F76A7600000D77 /* TypeMapping.swift in Sources */, 7B04A9AB24EA5F8D00E710B1 /* SentryUserTests.swift in Sources */, + 0A1C3592287D7107007D01E3 /* SentryMetaTests.swift in Sources */, 7BA61CCF247EB59500C130A8 /* SentryCrashUUIDConversionTests.swift in Sources */, 7BBD188D2448453600427C76 /* SentryHttpDateParserTests.swift in Sources */, 7BBD18BB24530D2600427C76 /* SentryFileManagerTests.swift in Sources */, diff --git a/Sources/Sentry/PrivateSentrySDKOnly.m b/Sources/Sentry/PrivateSentrySDKOnly.m index 63a86d153b5..5c58e87f326 100644 --- a/Sources/Sentry/PrivateSentrySDKOnly.m +++ b/Sources/Sentry/PrivateSentrySDKOnly.m @@ -1,6 +1,7 @@ #import "PrivateSentrySDKOnly.h" #import "SentryDebugImageProvider.h" #import "SentryInstallation.h" +#import "SentryMeta.h" #import "SentrySDK+Private.h" #import "SentrySerialization.h" #import @@ -66,6 +67,12 @@ + (void)setAppStartMeasurementHybridSDKMode:(BOOL)appStartMeasurementHybridSDKMo _appStartMeasurementHybridSDKMode = appStartMeasurementHybridSDKMode; } ++ (void)setSdkName:(NSString *)sdkName andVersionString:(NSString *)versionString +{ + SentryMeta.sdkName = sdkName; + SentryMeta.versionString = versionString; +} + #if SENTRY_HAS_UIKIT + (BOOL)framesTrackingMeasurementHybridSDKMode diff --git a/Sources/Sentry/Public/PrivateSentrySDKOnly.h b/Sources/Sentry/Public/PrivateSentrySDKOnly.h index a8e01712f43..f82869ecdb6 100644 --- a/Sources/Sentry/Public/PrivateSentrySDKOnly.h +++ b/Sources/Sentry/Public/PrivateSentrySDKOnly.h @@ -41,6 +41,11 @@ typedef void (^SentryOnAppStartMeasurementAvailable)( */ + (NSArray *)getDebugImages; +/** + * Override SDK information. + */ ++ (void)setSdkName:(NSString *)sdkName andVersionString:(NSString *)versionString; + @property (class, nullable, nonatomic, copy) SentryOnAppStartMeasurementAvailable onAppStartMeasurementAvailable; diff --git a/Sources/Sentry/Public/SentryOptions.h b/Sources/Sentry/Public/SentryOptions.h index db775eb3f4f..9a0168248e9 100644 --- a/Sources/Sentry/Public/SentryOptions.h +++ b/Sources/Sentry/Public/SentryOptions.h @@ -153,8 +153,10 @@ NS_SWIFT_NAME(Options) /** * Describes the Sentry SDK and its configuration used to capture and transmit an event. + * This is reserved for internal use, and will be removed in a future version of the SDK. */ -@property (nonatomic, readonly, strong) SentrySdkInfo *sdkInfo; +@property (nonatomic, readonly, strong) SentrySdkInfo *sdkInfo DEPRECATED_MSG_ATTRIBUTE( + "This property will be removed in a future version of the SDK"); /** * The maximum size for each attachment in bytes. Default is 20 MiB / 20 * 1024 * 1024 bytes. diff --git a/Sources/Sentry/SentryClient.m b/Sources/Sentry/SentryClient.m index 7bba78b429a..a43164aab0d 100644 --- a/Sources/Sentry/SentryClient.m +++ b/Sources/Sentry/SentryClient.m @@ -363,8 +363,8 @@ - (void)captureSession:(SentrySession *)session } SentryEnvelopeItem *item = [[SentryEnvelopeItem alloc] initWithSession:session]; - SentryEnvelopeHeader *envelopeHeader = - [[SentryEnvelopeHeader alloc] initWithId:nil sdkInfo:self.options.sdkInfo traceContext:nil]; + SentryEnvelopeHeader *envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:nil + traceContext:nil]; SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader singleItem:item]; [self captureEnvelope:envelope]; @@ -591,8 +591,8 @@ - (void)setSdk:(SentryEvent *)event } event.sdk = @{ - @"name" : self.options.sdkInfo.name, - @"version" : self.options.sdkInfo.version, + @"name" : SentryMeta.sdkName, + @"version" : SentryMeta.versionString, @"integrations" : integrations }; } diff --git a/Sources/Sentry/SentryEnvelope.m b/Sources/Sentry/SentryEnvelope.m index e09b7d98fe7..93149553dcd 100644 --- a/Sources/Sentry/SentryEnvelope.m +++ b/Sources/Sentry/SentryEnvelope.m @@ -20,26 +20,16 @@ @implementation SentryEnvelopeHeader // id can be null if no event in the envelope or attachment related to event - (instancetype)initWithId:(SentryId *_Nullable)eventId { - SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName - andVersion:SentryMeta.versionString]; - self = [self initWithId:eventId andSdkInfo:sdkInfo]; - + self = [self initWithId:eventId traceContext:nil]; return self; } -- (instancetype)initWithId:(SentryId *_Nullable)eventId andSdkInfo:(SentrySdkInfo *_Nullable)sdkInfo -{ - return [self initWithId:eventId sdkInfo:sdkInfo traceContext:nil]; -} - - (instancetype)initWithId:(nullable SentryId *)eventId traceContext:(nullable SentryTraceContext *)traceContext { SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName andVersion:SentryMeta.versionString]; - self = [self initWithId:eventId sdkInfo:sdkInfo traceContext:traceContext]; - return self; } @@ -47,7 +37,6 @@ - (instancetype)initWithId:(nullable SentryId *)eventId sdkInfo:(nullable SentrySdkInfo *)sdkInfo traceContext:(nullable SentryTraceContext *)traceContext { - if (self = [super init]) { _eventId = eventId; _sdkInfo = sdkInfo; diff --git a/Sources/Sentry/SentryMeta.m b/Sources/Sentry/SentryMeta.m index 454cb727f79..409b70e59ec 100644 --- a/Sources/Sentry/SentryMeta.m +++ b/Sources/Sentry/SentryMeta.m @@ -5,17 +5,27 @@ @implementation SentryMeta // Don't remove the static keyword. If you do the compiler adds the constant name to the global // symbol table and it might clash with other constants. When keeping the static keyword the // compiler replaces all occurrences with the value. -static NSString *const versionString = @"7.20.0"; -static NSString *const sdkName = @"sentry.cocoa"; +static NSString *versionString = @"7.20.0"; +static NSString *sdkName = @"sentry.cocoa"; + (NSString *)versionString { return versionString; } ++ (void)setVersionString:(NSString *)value +{ + versionString = value; +} + + (NSString *)sdkName { return sdkName; } ++ (void)setSdkName:(NSString *)value +{ + sdkName = value; +} + @end diff --git a/Sources/Sentry/SentryOptions.m b/Sources/Sentry/SentryOptions.m index 81f705da60f..b728c17b3bb 100644 --- a/Sources/Sentry/SentryOptions.m +++ b/Sources/Sentry/SentryOptions.m @@ -95,8 +95,6 @@ - (instancetype)init } _inAppExcludes = [NSArray new]; - _sdkInfo = [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName - andVersion:SentryMeta.versionString]; // Set default release name if (nil != infoDict) { @@ -303,8 +301,12 @@ - (BOOL)validateOptions:(NSDictionary *)options // SentrySdkInfo already expects a dictionary with {"sdk": {"name": ..., "value": ...}} // so we're passing the whole options object. + // Note: we should remove this code once the hybrid SDKs move over to the new + // PrivateSentrySDKOnly setter functions. if ([options[@"sdk"] isKindOfClass:[NSDictionary class]]) { - _sdkInfo = [[SentrySdkInfo alloc] initWithDict:options orDefaults:_sdkInfo]; + SentrySdkInfo *sdkInfo = [[SentrySdkInfo alloc] initWithDict:options]; + SentryMeta.versionString = sdkInfo.version; + SentryMeta.sdkName = sdkInfo.name; } if (nil != error && nil != *error) { @@ -314,6 +316,12 @@ - (BOOL)validateOptions:(NSDictionary *)options } } +- (SentrySdkInfo *)sdkInfo +{ + return [[SentrySdkInfo alloc] initWithName:SentryMeta.sdkName + andVersion:SentryMeta.versionString]; +} + - (void)setBool:(id)value block:(void (^)(BOOL))block { // Entries in the dictionary can be NSNull. Especially, on React-Native, this can happen. diff --git a/Sources/Sentry/SentryTransportAdapter.m b/Sources/Sentry/SentryTransportAdapter.m index 7ba8956083d..5e6bdfa3f1c 100644 --- a/Sources/Sentry/SentryTransportAdapter.m +++ b/Sources/Sentry/SentryTransportAdapter.m @@ -58,10 +58,8 @@ - (void)sendEvent:(SentryEvent *)event attachments:attachments]; [items addObjectsFromArray:additionalEnvelopeItems]; - SentryEnvelopeHeader *envelopeHeader = - [[SentryEnvelopeHeader alloc] initWithId:event.eventId - sdkInfo:self.options.sdkInfo - traceContext:traceContext]; + SentryEnvelopeHeader *envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:event.eventId + traceContext:traceContext]; SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader items:items]; [self sendEnvelope:envelope]; @@ -76,10 +74,8 @@ - (void)sendEvent:(SentryEvent *)event attachments:attachments]; [items addObject:[[SentryEnvelopeItem alloc] initWithSession:session]]; - SentryEnvelopeHeader *envelopeHeader = - [[SentryEnvelopeHeader alloc] initWithId:event.eventId - sdkInfo:self.options.sdkInfo - traceContext:traceContext]; + SentryEnvelopeHeader *envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:event.eventId + traceContext:traceContext]; SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader items:items]; @@ -90,9 +86,7 @@ - (void)sendUserFeedback:(SentryUserFeedback *)userFeedback { SentryEnvelopeItem *item = [[SentryEnvelopeItem alloc] initWithUserFeedback:userFeedback]; SentryEnvelopeHeader *envelopeHeader = - [[SentryEnvelopeHeader alloc] initWithId:userFeedback.eventId - sdkInfo:self.options.sdkInfo - traceContext:nil]; + [[SentryEnvelopeHeader alloc] initWithId:userFeedback.eventId traceContext:nil]; SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader singleItem:item]; [self sendEnvelope:envelope]; diff --git a/Sources/Sentry/include/SentryMeta.h b/Sources/Sentry/include/SentryMeta.h index 21e66eeb242..3ee7fd9efc4 100644 --- a/Sources/Sentry/include/SentryMeta.h +++ b/Sources/Sentry/include/SentryMeta.h @@ -7,12 +7,12 @@ NS_ASSUME_NONNULL_BEGIN /** * Return a version string e.g: 1.2.3 (3) */ -@property (nonatomic, class, readonly, copy) NSString *versionString; +@property (nonatomic, class, copy) NSString *versionString; /** * Return a string sentry-cocoa */ -@property (nonatomic, class, readonly, copy) NSString *sdkName; +@property (nonatomic, class, copy) NSString *sdkName; @end diff --git a/Tests/SentryTests/SentryMetaTests.swift b/Tests/SentryTests/SentryMetaTests.swift new file mode 100644 index 00000000000..6150f240060 --- /dev/null +++ b/Tests/SentryTests/SentryMetaTests.swift @@ -0,0 +1,14 @@ +import Sentry +import XCTest + +class SentryMetaTest: XCTestCase { + func testChangeVersion() { + SentryMeta.versionString = "0.0.1" + XCTAssertEqual(SentryMeta.versionString, "0.0.1") + } + + func testChangeName() { + SentryMeta.sdkName = "test" + XCTAssertEqual(SentryMeta.sdkName, "test") + } +} diff --git a/Tests/SentryTests/SentryOptionsTest.m b/Tests/SentryTests/SentryOptionsTest.m index 465c1651ace..9ad4b9f427c 100644 --- a/Tests/SentryTests/SentryOptionsTest.m +++ b/Tests/SentryTests/SentryOptionsTest.m @@ -545,8 +545,12 @@ - (void)assertDefaultValues:(SentryOptions *)options #if SENTRY_TARGET_PROFILING_SUPPORTED XCTAssertEqual(NO, options.enableProfiling); #endif + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); +#pragma clang diagnostic pop } - (void)testSetValidDsn @@ -585,10 +589,12 @@ - (void)testSetInvalidValidDsn - (void)testSdkInfo { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" SentryOptions *options = [[SentryOptions alloc] init]; - XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); +#pragma clang diagnostic pop } - (void)testSetCustomSdkInfo @@ -601,8 +607,16 @@ - (void)testSetCustomSdkInfo didFailWithError:&error]; XCTAssertNil(error); + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" XCTAssertEqual(dict[@"name"], options.sdkInfo.name); XCTAssertEqual(dict[@"version"], options.sdkInfo.version); +#pragma clang diagnostic pop + + NSDictionary *info = [[NSBundle bundleForClass:[SentryClient class]] infoDictionary]; + NSString *version = [NSString stringWithFormat:@"%@", info[@"CFBundleShortVersionString"]]; + SentryMeta.versionString = version; } - (void)testSetCustomSdkName @@ -615,8 +629,12 @@ - (void)testSetCustomSdkName didFailWithError:&error]; XCTAssertNil(error); + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" XCTAssertEqual(dict[@"name"], options.sdkInfo.name); XCTAssertEqual(SentryMeta.versionString, options.sdkInfo.version); // default version +#pragma clang diagnostic pop } - (void)testSetCustomSdkVersion @@ -629,8 +647,16 @@ - (void)testSetCustomSdkVersion didFailWithError:&error]; XCTAssertNil(error); + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" XCTAssertEqual(SentryMeta.sdkName, options.sdkInfo.name); // default name XCTAssertEqual(dict[@"version"], options.sdkInfo.version); +#pragma clang diagnostic pop + + NSDictionary *info = [[NSBundle bundleForClass:[SentryClient class]] infoDictionary]; + NSString *version = [NSString stringWithFormat:@"%@", info[@"CFBundleShortVersionString"]]; + SentryMeta.versionString = version; } - (void)testMaxAttachmentSize @@ -912,6 +938,19 @@ - (void)testUrlSessionDelegate XCTAssertNotNil(options.urlSessionDelegate); } +- (void)testSdkInfoChanges +{ + SentryOptions *options = [self getValidOptions:@{}]; + SentryMeta.sdkName = @"new name"; + SentryMeta.versionString = @"0.0.6"; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + XCTAssertEqual(options.sdkInfo.name, SentryMeta.sdkName); + XCTAssertEqual(options.sdkInfo.version, SentryMeta.versionString); +#pragma clang diagnostic pop +} + - (void)assertArrayEquals:(NSArray *)expected actual:(NSArray *)actual { XCTAssertEqualObjects([expected sortedArrayUsingSelector:@selector(compare:)], diff --git a/Tests/SentryTests/SentryTests.m b/Tests/SentryTests/SentryTests.m index 762ff42d339..2776ca7d8be 100644 --- a/Tests/SentryTests/SentryTests.m +++ b/Tests/SentryTests/SentryTests.m @@ -33,7 +33,9 @@ - (void)testVersion // (code was loaded inside an app for example) // in this case, we don't care about asserting our hard coded value matches // since this will be the app version instead of our SDK version. - XCTAssert([version isEqualToString:SentryMeta.versionString]); + XCTAssert([version isEqualToString:SentryMeta.versionString], + @"Version of bundle:%@ not equal to version of SentryMeta:%@", version, + SentryMeta.versionString); } }