|
29 | 29 | import com.google.firebase.perf.util.Rate;
|
30 | 30 | import com.google.firebase.perf.util.Timer;
|
31 | 31 | import com.google.firebase.perf.util.Utils;
|
32 |
| -import com.google.firebase.perf.v1.NetworkRequestMetric; |
33 | 32 | import com.google.firebase.perf.v1.PerfMetric;
|
34 | 33 | import com.google.firebase.perf.v1.PerfSession;
|
35 | 34 | import com.google.firebase.perf.v1.SessionVerbosity;
|
36 |
| -import com.google.firebase.perf.v1.TraceMetric; |
37 | 35 | import java.util.List;
|
38 | 36 | import java.util.Random;
|
39 | 37 |
|
@@ -107,46 +105,47 @@ private boolean isDeviceAllowedToSendNetworkEvents() {
|
107 | 105 | }
|
108 | 106 |
|
109 | 107 | /**
|
110 |
| - * Check if we should log the {@link PerfMetric} to transport. |
111 |
| - * |
112 |
| - * <p>Cases in which we don't log a {@link PerfMetric} to transport: |
113 |
| - * |
114 |
| - * <ul> |
115 |
| - * <li>It is a {@link TraceMetric}, the {@link PerfSession} is not verbose and trace metrics are |
116 |
| - * sampled. |
117 |
| - * <li>It is a {@link NetworkRequestMetric}, the {@link PerfSession} is not verbose and network |
118 |
| - * requests are sampled. |
119 |
| - * <li>The number of metrics being sent exceeds what the rate limiter allows. |
120 |
| - * </ul> |
| 108 | + * Check if the {@link PerfMetric} should be rate limited. |
121 | 109 | *
|
122 | 110 | * @param metric {@link PerfMetric} object.
|
123 |
| - * @return true if allowed, false if not allowed. |
| 111 | + * @return true if event is rated limited, false if event is not rate limited. |
124 | 112 | */
|
125 |
| - boolean check(PerfMetric metric) { |
126 |
| - if (metric.hasTraceMetric() |
127 |
| - && !isDeviceAllowedToSendTraces() |
128 |
| - && !hasVerboseSessions(metric.getTraceMetric().getPerfSessionsList())) { |
| 113 | + boolean isEventRateLimited(PerfMetric metric) { |
| 114 | + if (!isRateLimitApplicable(metric)) { |
| 115 | + // Apply rate limiting on this metric. |
129 | 116 | return false;
|
130 | 117 | }
|
131 | 118 |
|
132 |
| - if (metric.hasNetworkRequestMetric() |
133 |
| - && !isDeviceAllowedToSendNetworkEvents() |
134 |
| - && !hasVerboseSessions(metric.getNetworkRequestMetric().getPerfSessionsList())) { |
135 |
| - return false; |
| 119 | + if (metric.hasNetworkRequestMetric()) { |
| 120 | + return !networkLimiter.check(metric); |
| 121 | + } else if (metric.hasTraceMetric()) { |
| 122 | + return !traceLimiter.check(metric); |
| 123 | + } else { |
| 124 | + // Should not reach here |
| 125 | + return true; |
136 | 126 | }
|
| 127 | + } |
137 | 128 |
|
138 |
| - if (!isRateLimited(metric)) { |
139 |
| - // Do not apply rate limiting on this metric. |
140 |
| - return true; |
| 129 | + /** |
| 130 | + * Check if the {@link PerfMetric} should be sampled. A {@link PerfMetric} is considered sampled |
| 131 | + * if the device isn't allowed to send the event type and it is not part of a verbose session. |
| 132 | + * |
| 133 | + * @param metric {@link PerfMetric} object. |
| 134 | + * @return true if allowed, false if not allowed. |
| 135 | + */ |
| 136 | + boolean isEventSampled(PerfMetric metric) { |
| 137 | + if (metric.hasTraceMetric() |
| 138 | + && !(isDeviceAllowedToSendTraces() |
| 139 | + || hasVerboseSessions(metric.getTraceMetric().getPerfSessionsList()))) { |
| 140 | + return false; |
141 | 141 | }
|
142 | 142 |
|
143 |
| - if (metric.hasNetworkRequestMetric()) { |
144 |
| - return networkLimiter.check(metric); |
145 |
| - } else if (metric.hasTraceMetric()) { |
146 |
| - return traceLimiter.check(metric); |
147 |
| - } else { |
| 143 | + if (metric.hasNetworkRequestMetric() |
| 144 | + && !(isDeviceAllowedToSendNetworkEvents() |
| 145 | + || hasVerboseSessions(metric.getNetworkRequestMetric().getPerfSessionsList()))) { |
148 | 146 | return false;
|
149 | 147 | }
|
| 148 | + return true; |
150 | 149 | }
|
151 | 150 |
|
152 | 151 | /**
|
@@ -174,7 +173,7 @@ private boolean hasVerboseSessions(List<PerfSession> perfSessions) {
|
174 | 173 | * @param metric {@link PerfMetric} object.
|
175 | 174 | * @return true if applying rate limiting. false if not.
|
176 | 175 | */
|
177 |
| - boolean isRateLimited(@NonNull PerfMetric metric) { |
| 176 | + boolean isRateLimitApplicable(@NonNull PerfMetric metric) { |
178 | 177 | if (metric.hasTraceMetric()
|
179 | 178 | && (metric
|
180 | 179 | .getTraceMetric()
|
|
0 commit comments