@@ -52,7 +52,6 @@ public final class NetworkRequestMetricBuilder extends AppStateUpdateHandler
52
52
implements SessionAwareObject {
53
53
54
54
private static final AndroidLogger logger = AndroidLogger .getInstance ();
55
-
56
55
private static final char HIGHEST_CONTROL_CHAR = '\u001f' ;
57
56
private static final char HIGHEST_ASCII_CHAR = '\u007f' ;
58
57
@@ -61,16 +60,14 @@ public final class NetworkRequestMetricBuilder extends AppStateUpdateHandler
61
60
private final GaugeManager gaugeManager ;
62
61
private final TransportManager transportManager ;
63
62
64
- private final NetworkRequestMetric .Builder networkMetricBuilder =
65
- NetworkRequestMetric . newBuilder ( );
63
+ private final NetworkRequestMetric .Builder builder = NetworkRequestMetric . newBuilder ();
64
+ private final WeakReference < SessionAwareObject > weakReference = new WeakReference <>( this );
66
65
67
66
@ Nullable private String userAgent ;
68
67
69
68
private boolean isReportSent ;
70
69
private boolean isManualNetworkRequestMetric ;
71
70
72
- private final WeakReference <SessionAwareObject > weakReference = new WeakReference <>(this );
73
-
74
71
/** @hide */
75
72
@ Override
76
73
/** @hide */
@@ -134,15 +131,15 @@ public NetworkRequestMetricBuilder setUrl(@Nullable String url) {
134
131
url = Utils .stripSensitiveInfo (url );
135
132
136
133
// Limits the URL length to 2000 chars.
137
- networkMetricBuilder .setUrl (Utils .truncateURL (url , Constants .MAX_URL_LENGTH ));
134
+ builder .setUrl (Utils .truncateURL (url , Constants .MAX_URL_LENGTH ));
138
135
}
139
136
140
137
return this ;
141
138
}
142
139
143
140
/** Gets the url for the current {@link NetworkRequestMetric}. */
144
141
public String getUrl () {
145
- return networkMetricBuilder .getUrl ();
142
+ return builder .getUrl ();
146
143
}
147
144
148
145
/** Sets the user-agent for the current network request. */
@@ -196,31 +193,31 @@ public NetworkRequestMetricBuilder setHttpMethod(@Nullable String method) {
196
193
httpMethod = HttpMethod .HTTP_METHOD_UNKNOWN ;
197
194
break ;
198
195
}
199
- networkMetricBuilder .setHttpMethod (httpMethod );
196
+ builder .setHttpMethod (httpMethod );
200
197
}
201
198
return this ;
202
199
}
203
200
204
201
/** Sets the httpResponseCode for the current {@link NetworkRequestMetric}. */
205
202
public NetworkRequestMetricBuilder setHttpResponseCode (int code ) {
206
- networkMetricBuilder .setHttpResponseCode (code );
203
+ builder .setHttpResponseCode (code );
207
204
return this ;
208
205
}
209
206
210
207
/** Answers whether the builder has an HttpResponseCode. */
211
208
public boolean hasHttpResponseCode () {
212
- return networkMetricBuilder .hasHttpResponseCode ();
209
+ return builder .hasHttpResponseCode ();
213
210
}
214
211
215
212
/** Sets the requestPayloadBytes for the current {@link NetworkRequestMetric}. */
216
213
public NetworkRequestMetricBuilder setRequestPayloadBytes (long bytes ) {
217
- networkMetricBuilder .setRequestPayloadBytes (bytes );
214
+ builder .setRequestPayloadBytes (bytes );
218
215
return this ;
219
216
}
220
217
221
218
/** Sets the customAttributes for the current {@link NetworkRequestMetric}. */
222
219
public NetworkRequestMetricBuilder setCustomAttributes (Map <String , String > attributes ) {
223
- networkMetricBuilder .clearCustomAttributes ().putAllCustomAttributes (attributes );
220
+ builder .clearCustomAttributes ().putAllCustomAttributes (attributes );
224
221
return this ;
225
222
}
226
223
@@ -238,7 +235,7 @@ public NetworkRequestMetricBuilder setRequestStartTimeMicros(long time) {
238
235
PerfSession perfSession = sessionManager .perfSession ();
239
236
SessionManager .getInstance ().registerForSessionUpdates (weakReference );
240
237
241
- networkMetricBuilder .setClientStartTimeUs (time );
238
+ builder .setClientStartTimeUs (time );
242
239
updateSession (perfSession );
243
240
244
241
if (perfSession .isGaugeAndEventCollectionEnabled ()) {
@@ -250,19 +247,19 @@ public NetworkRequestMetricBuilder setRequestStartTimeMicros(long time) {
250
247
251
248
/** Sets the timeToRequestCompletedUs for the current {@link NetworkRequestMetric}. */
252
249
public NetworkRequestMetricBuilder setTimeToRequestCompletedMicros (long time ) {
253
- networkMetricBuilder .setTimeToRequestCompletedUs (time );
250
+ builder .setTimeToRequestCompletedUs (time );
254
251
return this ;
255
252
}
256
253
257
254
/** Sets the timeToResponseInitiatedUs for the current {@link NetworkRequestMetric}. */
258
255
public NetworkRequestMetricBuilder setTimeToResponseInitiatedMicros (long time ) {
259
- networkMetricBuilder .setTimeToResponseInitiatedUs (time );
256
+ builder .setTimeToResponseInitiatedUs (time );
260
257
return this ;
261
258
}
262
259
263
260
/** Gets the timeToResponseInitiatedUs for the current {@link NetworkRequestMetric}. */
264
261
public long getTimeToResponseInitiatedMicros () {
265
- return networkMetricBuilder .getTimeToResponseInitiatedUs ();
262
+ return builder .getTimeToResponseInitiatedUs ();
266
263
}
267
264
268
265
/**
@@ -275,7 +272,7 @@ public long getTimeToResponseInitiatedMicros() {
275
272
* @see PerfSession#isGaugeAndEventCollectionEnabled()
276
273
*/
277
274
public NetworkRequestMetricBuilder setTimeToResponseCompletedMicros (long time ) {
278
- networkMetricBuilder .setTimeToResponseCompletedUs (time );
275
+ builder .setTimeToResponseCompletedUs (time );
279
276
280
277
if (SessionManager .getInstance ().perfSession ().isGaugeAndEventCollectionEnabled ()) {
281
278
gaugeManager .collectGaugeMetricOnce (SessionManager .getInstance ().perfSession ().getTimer ());
@@ -286,19 +283,19 @@ public NetworkRequestMetricBuilder setTimeToResponseCompletedMicros(long time) {
286
283
287
284
/** Sets the responsePayloadBytes for the current {@link NetworkRequestMetric}. */
288
285
public NetworkRequestMetricBuilder setResponsePayloadBytes (long bytes ) {
289
- networkMetricBuilder .setResponsePayloadBytes (bytes );
286
+ builder .setResponsePayloadBytes (bytes );
290
287
return this ;
291
288
}
292
289
293
290
/** Sets the responseContentType for the current {@link NetworkRequestMetric}. */
294
291
public NetworkRequestMetricBuilder setResponseContentType (@ Nullable String contentType ) {
295
292
if (contentType == null ) {
296
- networkMetricBuilder .clearResponseContentType ();
293
+ builder .clearResponseContentType ();
297
294
return this ;
298
295
}
299
296
300
297
if (isValidContentType (contentType )) {
301
- networkMetricBuilder .setResponseContentType (contentType );
298
+ builder .setResponseContentType (contentType );
302
299
} else {
303
300
logger .info ("The content type of the response is not a valid content-type:" + contentType );
304
301
}
@@ -310,7 +307,7 @@ public NetworkRequestMetricBuilder setResponseContentType(@Nullable String conte
310
307
* the current {@link NetworkRequestMetric}.
311
308
*/
312
309
public NetworkRequestMetricBuilder setNetworkClientErrorReason () {
313
- networkMetricBuilder .setNetworkClientErrorReason (NetworkClientErrorReason .GENERIC_CLIENT_ERROR );
310
+ builder .setNetworkClientErrorReason (NetworkClientErrorReason .GENERIC_CLIENT_ERROR );
314
311
return this ;
315
312
}
316
313
@@ -322,10 +319,10 @@ public NetworkRequestMetric build() {
322
319
com .google .firebase .perf .v1 .PerfSession [] perfSessions =
323
320
PerfSession .buildAndSort (getSessions ());
324
321
if (perfSessions != null ) {
325
- networkMetricBuilder .addAllPerfSessions (Arrays .asList (perfSessions ));
322
+ builder .addAllPerfSessions (Arrays .asList (perfSessions ));
326
323
}
327
324
328
- NetworkRequestMetric metric = networkMetricBuilder .build ();
325
+ NetworkRequestMetric metric = builder .build ();
329
326
330
327
if (!isAllowedUserAgent (userAgent )) {
331
328
logger .debug ("Dropping network request from a 'User-Agent' that is not allowed" );
@@ -349,12 +346,12 @@ public NetworkRequestMetric build() {
349
346
350
347
/** Checks if the network request has stopped. */
351
348
private boolean isStopped () {
352
- return networkMetricBuilder .hasTimeToResponseCompletedUs ();
349
+ return builder .hasTimeToResponseCompletedUs ();
353
350
}
354
351
355
352
/** Checks if the network request has started. */
356
353
private boolean hasStarted () {
357
- return networkMetricBuilder .hasClientStartTimeUs ();
354
+ return builder .hasClientStartTimeUs ();
358
355
}
359
356
360
357
@ VisibleForTesting
@@ -384,7 +381,7 @@ void setReportSent() {
384
381
385
382
@ VisibleForTesting
386
383
void clearBuilderFields () {
387
- networkMetricBuilder .clear ();
384
+ builder .clear ();
388
385
}
389
386
390
387
/**
0 commit comments