FPRGDTCCLogger.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Loggers/FPRGDTCCLogger.h"
  15. #import "FirebasePerformance/Sources/Loggers/FPRGDTCCLogger_Private.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  17. #import "FirebasePerformance/Sources/Loggers/FPRGDTEvent.h"
  18. #import "FirebasePerformance/Sources/Loggers/FPRGDTLogSampler.h"
  19. #import "FirebasePerformance/Sources/Loggers/FPRGDTRateLimiter.h"
  20. #import <GoogleDataTransport/GoogleDataTransport.h>
  21. #import "FirebasePerformance/ProtoSupport/PerfMetric.pbobjc.h"
  22. @implementation FPRGDTCCLogger
  23. - (instancetype)initWithLogSource:(NSInteger)logSource {
  24. if (self = [super init]) {
  25. _logSource = logSource;
  26. _queue = dispatch_queue_create("com.google.FPRGDTCCLogger", DISPATCH_QUEUE_SERIAL);
  27. _configurations = [FPRConfigurations sharedInstance];
  28. FPRGDTLogSampler *logSampler = [[FPRGDTLogSampler alloc] init];
  29. FPRGDTRateLimiter *rateLimiter = [[FPRGDTRateLimiter alloc] init];
  30. _gdtcctTransport = [[GDTCORTransport alloc] initWithMappingID:@(logSource).stringValue
  31. transformers:@[ logSampler, rateLimiter ]
  32. target:kGDTCORTargetCCT];
  33. _gdtfllTransport = [[GDTCORTransport alloc] initWithMappingID:@(logSource).stringValue
  34. transformers:@[ logSampler, rateLimiter ]
  35. target:kGDTCORTargetFLL];
  36. _isSimulator = NO;
  37. // If app is running on simulator, environment variable SIMULATOR_UDID exists.
  38. // Otherwise, SIMULATOR_UDID is not provided when app is running on real device.
  39. // For development, developers can dispatch performance events immediately if
  40. // they are running app on simulator, so it can expedite development process.
  41. if ([[[NSProcessInfo processInfo] environment] objectForKey:@"SIMULATOR_UDID"]) {
  42. _isSimulator = YES;
  43. }
  44. _instanceSeed = -1.0; // -1.0 means instanceSeed has not been computed.
  45. }
  46. return self;
  47. }
  48. - (void)logEvent:(FPRMSGPerfMetric *)event {
  49. GDTCORTransport *eventTransporter = self.gdtcctTransport;
  50. if ([self shouldSendEventToFll:event]) {
  51. eventTransporter = self.gdtfllTransport;
  52. }
  53. dispatch_async(self.queue, ^{
  54. GDTCOREvent *gdtEvent = [eventTransporter eventForTransport];
  55. if (self.isSimulator) {
  56. gdtEvent.qosTier = GDTCOREventQoSFast;
  57. } else {
  58. gdtEvent.qosTier = GDTCOREventQosDefault;
  59. }
  60. gdtEvent.dataObject = [FPRGDTEvent gdtEventForPerfMetric:event];
  61. [eventTransporter sendDataEvent:gdtEvent];
  62. });
  63. }
  64. - (BOOL)shouldSendEventToFll:(FPRMSGPerfMetric *)event {
  65. return ([self.configurations fllTransportPercentage] >= [self instanceSeedForEvent:event]);
  66. }
  67. - (float)instanceSeedForEvent:(FPRMSGPerfMetric *)event {
  68. if (_instanceSeed == -1.0) {
  69. // Seed is a float value with range [1 - 100].
  70. _instanceSeed = (float)([event.applicationInfo.appInstanceId hash] % 100 + 1);
  71. }
  72. return _instanceSeed;
  73. }
  74. @end