FPRGDTLogSampler.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/FPRGDTLogSampler.h"
  15. #import "GoogleDataTransport/GDTCORLibrary/Internal/GoogleDataTransportInternal.h"
  16. #import "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
  17. #import "FirebasePerformance/Sources/Common/FPRDiagnostics.h"
  18. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  19. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  20. #import "FirebasePerformance/Sources/Loggers/FPRGDTEvent.h"
  21. #import "FirebasePerformance/ProtoSupport/PerfMetric.pbobjc.h"
  22. @class FPRGDTEvent;
  23. @interface FPRGDTLogSampler ()
  24. /** Configuration flags that are used for sampling. */
  25. @property(nonatomic, readonly) FPRConfigurations *flags;
  26. /** Vendor identifier used as the random seed for sampling. */
  27. @property(nonatomic, readonly) double samplingBucketId;
  28. @end
  29. @implementation FPRGDTLogSampler
  30. - (instancetype)init {
  31. double randomNumberBetween0And1 = ((double)arc4random() / UINT_MAX);
  32. return [self initWithFlags:[FPRConfigurations sharedInstance]
  33. samplingThreshold:randomNumberBetween0And1];
  34. }
  35. - (instancetype)initWithFlags:(FPRConfigurations *)flags samplingThreshold:(double)bucket {
  36. self = [super init];
  37. if (self) {
  38. _flags = flags;
  39. _samplingBucketId = bucket;
  40. if (bucket > 1 || bucket < 0.0) {
  41. _samplingBucketId = 1.0;
  42. }
  43. }
  44. return self;
  45. }
  46. /**
  47. * Samples PerfMetric Events based on sampling logic, event that should be
  48. * dropped will return nil in this transformer.
  49. *
  50. * @param event The event to be evaluated by sampling logic.
  51. * @return A transformed event, or nil if the transformation dropped the event.
  52. */
  53. - (GDTCOREvent *)transform:(GDTCOREvent *)event {
  54. // An event is sampled means that the event is dropped.
  55. // If the current active session is verbose, do not sample any event.
  56. if (![event.dataObject isKindOfClass:[FPRGDTEvent class]]) {
  57. return event;
  58. }
  59. FPRGDTEvent *gdtEvent = (FPRGDTEvent *)event.dataObject;
  60. FPRMSGPerfMetric *perfMetric = gdtEvent.metric;
  61. // If it is a gaugeEvent, do not sample.
  62. if (perfMetric.hasGaugeMetric) {
  63. return event;
  64. }
  65. // If the traceMetric contains a verbose session, do not sample.
  66. if (perfMetric.hasTraceMetric) {
  67. FPRMSGTraceMetric *traceMetric = perfMetric.traceMetric;
  68. // Sessions are ordered so that the first session is the most verbose one.
  69. if (traceMetric.perfSessionsArray.count > 0) {
  70. FPRMSGPerfSession *firstSession = traceMetric.perfSessionsArray[0];
  71. if (firstSession.sessionVerbosityArray.count > 0) {
  72. FPRMSGSessionVerbosity firstVerbosity =
  73. (FPRMSGSessionVerbosity)[firstSession.sessionVerbosityArray valueAtIndex:0];
  74. if (firstVerbosity == FPRMSGSessionVerbosity_GaugesAndSystemEvents) {
  75. return event;
  76. }
  77. }
  78. }
  79. }
  80. // If the networkMetric contains a verbose session, do not sample.
  81. if (perfMetric.hasNetworkRequestMetric) {
  82. FPRMSGNetworkRequestMetric *networkMetric = perfMetric.networkRequestMetric;
  83. // Sessions are ordered so that the first session is the most verbose one.
  84. if (networkMetric.perfSessionsArray.count > 0) {
  85. FPRMSGPerfSession *firstSession = networkMetric.perfSessionsArray[0];
  86. if (firstSession.sessionVerbosityArray.count > 0) {
  87. FPRMSGSessionVerbosity firstVerbosity =
  88. (FPRMSGSessionVerbosity)[firstSession.sessionVerbosityArray valueAtIndex:0];
  89. if (firstVerbosity == FPRMSGSessionVerbosity_GaugesAndSystemEvents) {
  90. return event;
  91. }
  92. }
  93. }
  94. }
  95. if ([self shouldDropEvent:perfMetric]) {
  96. return nil;
  97. }
  98. return event;
  99. }
  100. /**
  101. * Determines if the log should be dropped based on sampling configuration from remote
  102. * configuration.
  103. *
  104. * @param event The event on which the decision would be made.
  105. * @return Boolean value of YES if the log should be dropped/sampled out. Otherwise, NO.
  106. */
  107. - (BOOL)shouldDropEvent:(FPRMSGPerfMetric *)event {
  108. // Find the correct sampling rate and make the decision to drop or log the event.
  109. float samplingRate = [self.flags logTraceSamplingRate];
  110. if (event.hasNetworkRequestMetric) {
  111. samplingRate = [self.flags logNetworkSamplingRate];
  112. }
  113. return self.samplingBucketId >= samplingRate;
  114. }
  115. @end