FPRGDTLogSampler.m 4.6 KB

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