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