FPRProtoUtils.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/FPRProtoUtils.h"
  15. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  16. #import <CoreTelephony/CTCarrier.h>
  17. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  18. #endif
  19. #import <SystemConfiguration/SystemConfiguration.h>
  20. #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  21. #import "FirebasePerformance/Sources/FIRPerformance+Internal.h"
  22. #import "FirebasePerformance/Sources/FPRDataUtils.h"
  23. #import "FirebasePerformance/Sources/Public/FIRPerformance.h"
  24. #import "FirebasePerformance/Sources/Timer/FIRTrace+Internal.h"
  25. #import "FirebasePerformance/Sources/Timer/FIRTrace+Private.h"
  26. #import "FirebasePerformance/Sources/Gauges/CPU/FPRCPUGaugeData.h"
  27. #import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeData.h"
  28. #define BYTES_TO_KB(x) (x / 1024)
  29. static GPBStringInt64Dictionary *FPRGetProtoCounterForDictionary(
  30. NSDictionary<NSString *, NSNumber *> *dictionary);
  31. static FPRMSGNetworkRequestMetric_HttpMethod FPRHTTPMethodForString(NSString *methodString);
  32. static FPRMSGNetworkConnectionInfo_NetworkType FPRNetworkConnectionInfoNetworkType(void);
  33. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  34. static FPRMSGNetworkConnectionInfo_MobileSubtype FPRCellularNetworkType(void);
  35. #endif
  36. NSArray<FPRSessionDetails *> *FPRMakeFirstSessionVerbose(NSArray<FPRSessionDetails *> *sessions);
  37. #pragma mark - Public methods
  38. FPRMSGPerfMetric *FPRGetPerfMetricMessage(NSString *appID) {
  39. FPRMSGPerfMetric *perfMetricMessage = [FPRMSGPerfMetric message];
  40. perfMetricMessage.applicationInfo = FPRGetApplicationInfoMessage();
  41. perfMetricMessage.applicationInfo.googleAppId = appID;
  42. return perfMetricMessage;
  43. }
  44. FPRMSGApplicationInfo *FPRGetApplicationInfoMessage() {
  45. FPRMSGApplicationInfo *appInfoMessage = [FPRMSGApplicationInfo message];
  46. FPRMSGIosApplicationInfo *iosAppInfo = [FPRMSGIosApplicationInfo message];
  47. NSBundle *mainBundle = [NSBundle mainBundle];
  48. iosAppInfo.bundleShortVersion = [mainBundle infoDictionary][@"CFBundleShortVersionString"];
  49. iosAppInfo.sdkVersion = [NSString stringWithUTF8String:kFPRSDKVersion];
  50. iosAppInfo.networkConnectionInfo.networkType = FPRNetworkConnectionInfoNetworkType();
  51. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  52. CTTelephonyNetworkInfo *networkInfo = FPRNetworkInfo();
  53. CTCarrier *provider = networkInfo.subscriberCellularProvider;
  54. NSString *mccMnc = FPRValidatedMccMnc(provider.mobileCountryCode, provider.mobileNetworkCode);
  55. if (mccMnc) {
  56. iosAppInfo.mccMnc = mccMnc;
  57. }
  58. if (iosAppInfo.networkConnectionInfo.networkType ==
  59. FPRMSGNetworkConnectionInfo_NetworkType_Mobile) {
  60. iosAppInfo.networkConnectionInfo.mobileSubtype = FPRCellularNetworkType();
  61. }
  62. #endif
  63. appInfoMessage.iosAppInfo = iosAppInfo;
  64. appInfoMessage.customAttributes = [[FIRPerformance sharedInstance].attributes mutableCopy];
  65. return appInfoMessage;
  66. }
  67. FPRMSGTraceMetric *FPRGetTraceMetric(FIRTrace *trace) {
  68. if (trace == nil) {
  69. return nil;
  70. }
  71. FPRMSGTraceMetric *traceMetric = [FPRMSGTraceMetric message];
  72. traceMetric.name = trace.name;
  73. // Set if the trace is an internally created trace.
  74. traceMetric.isAuto = trace.isInternal;
  75. // Convert the trace duration from seconds to microseconds.
  76. traceMetric.durationUs = trace.totalTraceTimeInterval * USEC_PER_SEC;
  77. // Convert the start time from seconds to microseconds.
  78. traceMetric.clientStartTimeUs = trace.startTimeSinceEpoch * USEC_PER_SEC;
  79. traceMetric.counters = FPRGetProtoCounterForDictionary(trace.counters);
  80. NSMutableArray<FPRMSGTraceMetric *> *subtraces = [[NSMutableArray alloc] init];
  81. [trace.stages
  82. enumerateObjectsUsingBlock:^(FIRTrace *_Nonnull stage, NSUInteger idx, BOOL *_Nonnull stop) {
  83. [subtraces addObject:FPRGetTraceMetric(stage)];
  84. }];
  85. traceMetric.subtracesArray = subtraces;
  86. traceMetric.customAttributes = [trace.attributes mutableCopy];
  87. // Fillin session details
  88. traceMetric.perfSessionsArray = [[NSMutableArray<FPRMSGPerfSession *> alloc] init];
  89. NSArray<FPRSessionDetails *> *orderedSessions = FPRMakeFirstSessionVerbose(trace.sessions);
  90. [orderedSessions enumerateObjectsUsingBlock:^(FPRSessionDetails *_Nonnull session,
  91. NSUInteger index, BOOL *_Nonnull stop) {
  92. FPRMSGPerfSession *perfSession = [FPRMSGPerfSession message];
  93. perfSession.sessionId = session.sessionId;
  94. perfSession.sessionVerbosityArray = [GPBEnumArray array];
  95. if ((session.options & FPRSessionOptionsEvents) ||
  96. (session.options & FPRSessionOptionsGauges)) {
  97. [perfSession.sessionVerbosityArray addValue:FPRMSGSessionVerbosity_GaugesAndSystemEvents];
  98. }
  99. [traceMetric.perfSessionsArray addObject:perfSession];
  100. }];
  101. return traceMetric;
  102. }
  103. FPRMSGNetworkRequestMetric *FPRGetNetworkRequestMetric(FPRNetworkTrace *trace) {
  104. if (trace == nil) {
  105. return nil;
  106. }
  107. // If there is no valid status code, do not send the event to backend.
  108. if (!trace.hasValidResponseCode) {
  109. return nil;
  110. }
  111. FPRMSGNetworkRequestMetric *networkMetric = [FPRMSGNetworkRequestMetric message];
  112. networkMetric.URL = trace.trimmedURLString;
  113. networkMetric.HTTPMethod = FPRHTTPMethodForString(trace.URLRequest.HTTPMethod);
  114. // Convert the start time from seconds to microseconds.
  115. networkMetric.clientStartTimeUs = trace.startTimeSinceEpoch * USEC_PER_SEC;
  116. networkMetric.requestPayloadBytes = trace.requestSize;
  117. networkMetric.responsePayloadBytes = trace.responseSize;
  118. networkMetric.HTTPResponseCode = trace.responseCode;
  119. networkMetric.responseContentType = trace.responseContentType;
  120. if (trace.responseError) {
  121. networkMetric.networkClientErrorReason =
  122. FPRMSGNetworkRequestMetric_NetworkClientErrorReason_GenericClientError;
  123. }
  124. NSTimeInterval requestTimeUs =
  125. USEC_PER_SEC *
  126. [trace timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
  127. andState:FPRNetworkTraceCheckpointStateRequestCompleted];
  128. if (requestTimeUs > 0) {
  129. networkMetric.timeToRequestCompletedUs = requestTimeUs;
  130. }
  131. NSTimeInterval responseIntiationTimeUs =
  132. USEC_PER_SEC *
  133. [trace timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
  134. andState:FPRNetworkTraceCheckpointStateResponseReceived];
  135. if (responseIntiationTimeUs > 0) {
  136. networkMetric.timeToResponseInitiatedUs = responseIntiationTimeUs;
  137. }
  138. NSTimeInterval responseCompletedUs =
  139. USEC_PER_SEC *
  140. [trace timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
  141. andState:FPRNetworkTraceCheckpointStateResponseCompleted];
  142. if (responseCompletedUs > 0) {
  143. networkMetric.timeToResponseCompletedUs = responseCompletedUs;
  144. }
  145. networkMetric.customAttributes = [trace.attributes mutableCopy];
  146. // Fillin session details
  147. NSArray<FPRSessionDetails *> *orderedSessions = FPRMakeFirstSessionVerbose(trace.sessions);
  148. networkMetric.perfSessionsArray = [[NSMutableArray<FPRMSGPerfSession *> alloc] init];
  149. [orderedSessions enumerateObjectsUsingBlock:^(FPRSessionDetails *_Nonnull session,
  150. NSUInteger index, BOOL *_Nonnull stop) {
  151. FPRMSGPerfSession *perfSession = [FPRMSGPerfSession message];
  152. perfSession.sessionId = session.sessionId;
  153. perfSession.sessionVerbosityArray = [GPBEnumArray array];
  154. if ((session.options & FPRSessionOptionsEvents) ||
  155. (session.options & FPRSessionOptionsGauges)) {
  156. [perfSession.sessionVerbosityArray addValue:FPRMSGSessionVerbosity_GaugesAndSystemEvents];
  157. }
  158. [networkMetric.perfSessionsArray addObject:perfSession];
  159. }];
  160. return networkMetric;
  161. }
  162. FPRMSGGaugeMetric *FPRGetGaugeMetric(NSArray *gaugeData, NSString *sessionId) {
  163. if (gaugeData == nil || gaugeData.count == 0) {
  164. return nil;
  165. }
  166. if (sessionId == nil || sessionId.length == 0) {
  167. return nil;
  168. }
  169. FPRMSGGaugeMetric *gaugeMetric = [FPRMSGGaugeMetric message];
  170. gaugeMetric.sessionId = sessionId;
  171. NSMutableArray<FPRMSGCpuMetricReading *> *cpuReadings =
  172. [[NSMutableArray<FPRMSGCpuMetricReading *> alloc] init];
  173. NSMutableArray<FPRMSGIosMemoryReading *> *memoryReadings =
  174. [[NSMutableArray<FPRMSGIosMemoryReading *> alloc] init];
  175. [gaugeData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  176. if ([obj isKindOfClass:[FPRCPUGaugeData class]]) {
  177. FPRCPUGaugeData *gaugeData = (FPRCPUGaugeData *)obj;
  178. FPRMSGCpuMetricReading *cpuReading = [FPRMSGCpuMetricReading message];
  179. cpuReading.clientTimeUs = gaugeData.collectionTime.timeIntervalSince1970 * USEC_PER_SEC;
  180. cpuReading.systemTimeUs = gaugeData.systemTime;
  181. cpuReading.userTimeUs = gaugeData.userTime;
  182. [cpuReadings addObject:cpuReading];
  183. }
  184. if ([obj isKindOfClass:[FPRMemoryGaugeData class]]) {
  185. FPRMemoryGaugeData *gaugeData = (FPRMemoryGaugeData *)obj;
  186. FPRMSGIosMemoryReading *memoryReading = [FPRMSGIosMemoryReading message];
  187. memoryReading.clientTimeUs = gaugeData.collectionTime.timeIntervalSince1970 * USEC_PER_SEC;
  188. memoryReading.usedAppHeapMemoryKb = (int32_t)BYTES_TO_KB(gaugeData.heapUsed);
  189. memoryReading.freeAppHeapMemoryKb = (int32_t)BYTES_TO_KB(gaugeData.heapAvailable);
  190. [memoryReadings addObject:memoryReading];
  191. }
  192. }];
  193. gaugeMetric.cpuMetricReadingsArray = cpuReadings;
  194. gaugeMetric.iosMemoryReadingsArray = memoryReadings;
  195. return gaugeMetric;
  196. }
  197. FPRMSGApplicationProcessState FPRApplicationProcessState(FPRTraceState state) {
  198. FPRMSGApplicationProcessState processState =
  199. FPRMSGApplicationProcessState_ApplicationProcessStateUnknown;
  200. switch (state) {
  201. case FPRTraceStateForegroundOnly:
  202. processState = FPRMSGApplicationProcessState_Foreground;
  203. break;
  204. case FPRTraceStateBackgroundOnly:
  205. processState = FPRMSGApplicationProcessState_Background;
  206. break;
  207. case FPRTraceStateBackgroundAndForeground:
  208. processState = FPRMSGApplicationProcessState_ForegroundBackground;
  209. break;
  210. default:
  211. break;
  212. }
  213. return processState;
  214. }
  215. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  216. CTTelephonyNetworkInfo *FPRNetworkInfo() {
  217. static CTTelephonyNetworkInfo *networkInfo;
  218. static dispatch_once_t onceToken;
  219. dispatch_once(&onceToken, ^{
  220. networkInfo = [[CTTelephonyNetworkInfo alloc] init];
  221. });
  222. return networkInfo;
  223. }
  224. #endif
  225. #pragma mark - Proto creation utilities
  226. /** Converts a dictionary of <NSString *, NSNumber *> to a GPBStringInt64Dictionary proto object.
  227. * @return Reference to a GPBStringInt64Dictionary object.
  228. */
  229. static GPBStringInt64Dictionary *FPRGetProtoCounterForDictionary(
  230. NSDictionary<NSString *, NSNumber *> *dictionary) {
  231. GPBStringInt64Dictionary *counterDictionary = [[GPBStringInt64Dictionary alloc] init];
  232. [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSNumber *_Nonnull value,
  233. BOOL *_Nonnull stop) {
  234. [counterDictionary setInt64:[value longLongValue] forKey:key];
  235. }];
  236. return counterDictionary;
  237. }
  238. /** Converts the network method string to a value defined in the enum
  239. * FPRMSGNetworkRequestMetric_HttpMethod.
  240. * @return Enum value of the method string. If there is no mapping value defined for the method
  241. * string FPRMSGNetworkRequestMetric_HttpMethod_HTTPMethodUnknown is returned.
  242. */
  243. static FPRMSGNetworkRequestMetric_HttpMethod FPRHTTPMethodForString(NSString *methodString) {
  244. static NSDictionary<NSString *, NSNumber *> *HTTPToFPRNetworkTraceMethod;
  245. static dispatch_once_t onceToken = 0;
  246. dispatch_once(&onceToken, ^{
  247. HTTPToFPRNetworkTraceMethod = @{
  248. @"GET" : @(FPRMSGNetworkRequestMetric_HttpMethod_Get),
  249. @"POST" : @(FPRMSGNetworkRequestMetric_HttpMethod_Post),
  250. @"PUT" : @(FPRMSGNetworkRequestMetric_HttpMethod_Put),
  251. @"DELETE" : @(FPRMSGNetworkRequestMetric_HttpMethod_Delete),
  252. @"HEAD" : @(FPRMSGNetworkRequestMetric_HttpMethod_Head),
  253. @"PATCH" : @(FPRMSGNetworkRequestMetric_HttpMethod_Patch),
  254. @"OPTIONS" : @(FPRMSGNetworkRequestMetric_HttpMethod_Options),
  255. @"TRACE" : @(FPRMSGNetworkRequestMetric_HttpMethod_Trace),
  256. @"CONNECT" : @(FPRMSGNetworkRequestMetric_HttpMethod_Connect),
  257. };
  258. });
  259. NSNumber *HTTPMethod = HTTPToFPRNetworkTraceMethod[methodString];
  260. if (HTTPMethod == nil) {
  261. return FPRMSGNetworkRequestMetric_HttpMethod_HTTPMethodUnknown;
  262. }
  263. return HTTPMethod.intValue;
  264. }
  265. /** Get the current network connection type in NetworkConnectionInfo_NetworkType format.
  266. * @return Current network connection type.
  267. */
  268. static FPRMSGNetworkConnectionInfo_NetworkType FPRNetworkConnectionInfoNetworkType() {
  269. FPRMSGNetworkConnectionInfo_NetworkType networkType =
  270. FPRMSGNetworkConnectionInfo_NetworkType_None;
  271. static SCNetworkReachabilityRef reachabilityRef = 0;
  272. static dispatch_once_t onceToken;
  273. dispatch_once(&onceToken, ^{
  274. reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorSystemDefault, "google.com");
  275. });
  276. SCNetworkReachabilityFlags reachabilityFlags = 0;
  277. SCNetworkReachabilityGetFlags(reachabilityRef, &reachabilityFlags);
  278. // Parse the network flags to set the network type.
  279. if (reachabilityFlags & kSCNetworkReachabilityFlagsReachable) {
  280. if (reachabilityFlags & kSCNetworkReachabilityFlagsIsWWAN) {
  281. networkType = FPRMSGNetworkConnectionInfo_NetworkType_Mobile;
  282. } else {
  283. networkType = FPRMSGNetworkConnectionInfo_NetworkType_Wifi;
  284. }
  285. }
  286. return networkType;
  287. }
  288. #ifdef TARGET_HAS_MOBILE_CONNECTIVITY
  289. /** Get the current cellular network connection type in NetworkConnectionInfo_MobileSubtype format.
  290. * @return Current cellular network connection type.
  291. */
  292. static FPRMSGNetworkConnectionInfo_MobileSubtype FPRCellularNetworkType() {
  293. static NSDictionary<NSString *, NSNumber *> *cellularNetworkToMobileSubtype;
  294. static dispatch_once_t onceToken = 0;
  295. dispatch_once(&onceToken, ^{
  296. cellularNetworkToMobileSubtype = @{
  297. CTRadioAccessTechnologyGPRS : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Gprs),
  298. CTRadioAccessTechnologyEdge : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Edge),
  299. CTRadioAccessTechnologyWCDMA : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Cdma),
  300. CTRadioAccessTechnologyHSDPA : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Hsdpa),
  301. CTRadioAccessTechnologyHSUPA : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Hsupa),
  302. CTRadioAccessTechnologyCDMA1x : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Cdma),
  303. CTRadioAccessTechnologyCDMAEVDORev0 : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Evdo0),
  304. CTRadioAccessTechnologyCDMAEVDORevA : @(FPRMSGNetworkConnectionInfo_MobileSubtype_EvdoA),
  305. CTRadioAccessTechnologyCDMAEVDORevB : @(FPRMSGNetworkConnectionInfo_MobileSubtype_EvdoB),
  306. CTRadioAccessTechnologyeHRPD : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Ehrpd),
  307. CTRadioAccessTechnologyLTE : @(FPRMSGNetworkConnectionInfo_MobileSubtype_Lte)
  308. };
  309. });
  310. NSString *networkString = FPRNetworkInfo().currentRadioAccessTechnology;
  311. NSNumber *cellularNetworkType = cellularNetworkToMobileSubtype[networkString];
  312. return cellularNetworkType.intValue;
  313. }
  314. #endif
  315. /** Reorders the list of sessions to make sure the first session is verbose if at least one session
  316. * in the list is verbose.
  317. * @return Ordered list of sessions.
  318. */
  319. NSArray<FPRSessionDetails *> *FPRMakeFirstSessionVerbose(NSArray<FPRSessionDetails *> *sessions) {
  320. NSMutableArray<FPRSessionDetails *> *orderedSessions =
  321. [[NSMutableArray<FPRSessionDetails *> alloc] initWithArray:sessions];
  322. __block NSInteger firstVerboseSessionIndex = -1;
  323. [sessions enumerateObjectsUsingBlock:^(FPRSessionDetails *session, NSUInteger idx, BOOL *stop) {
  324. if ([session isVerbose]) {
  325. firstVerboseSessionIndex = idx;
  326. *stop = YES;
  327. }
  328. }];
  329. if (firstVerboseSessionIndex > 0) {
  330. FPRSessionDetails *verboseSession = orderedSessions[firstVerboseSessionIndex];
  331. [orderedSessions removeObjectAtIndex:firstVerboseSessionIndex];
  332. [orderedSessions insertObject:verboseSession atIndex:0];
  333. }
  334. return [orderedSessions copy];
  335. }