HttpMetric

public class HttpMetric


Metric used to collect data for network requests/responses. A new object must be used for every request/response. This class is not thread safe.

Summary

Constants

static final int

Maximum allowed length of the Key of the Trace attribute

static final int

Maximum allowed length of the Value of the Trace attribute

static final int

Maximum allowed number of attributes allowed in a trace.

static final int

Maximum allowed length of the name of the Trace

Public methods

@Nullable String

Returns the value of an attribute.

@NonNull Map<StringString>

Returns the map of all the attributes added to this HttpMetric.

void
putAttribute(@NonNull String attribute, @NonNull String value)

Sets a String value for the specified attribute.

void

Removes an already added attribute from the HttpMetric.

void
setHttpResponseCode(int responseCode)

Sets the httpResponse code of the request

void

Sets the size of the request payload

void

Content type of the response such as text/html, application/json, etc...

void

Sets the size of the response payload

void

Marks the start time of the request

void

Marks the end time of the response and queues the network request metric on the device for transmission.

Extension functions

final void
PerformanceKt.trace(
    @NonNull HttpMetric receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull HttpMetricUnit> block
)

Measures the time it takes to run the block wrapped by calls to start and stop using HttpMetric.

final void
PerformanceKt.trace(
    @NonNull HttpMetric receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull HttpMetricUnit> block
)

This method is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

Constants

MAX_ATTRIBUTE_KEY_LENGTH

public static final int MAX_ATTRIBUTE_KEY_LENGTH = 40

Maximum allowed length of the Key of the Trace attribute

MAX_ATTRIBUTE_VALUE_LENGTH

public static final int MAX_ATTRIBUTE_VALUE_LENGTH = 100

Maximum allowed length of the Value of the Trace attribute

MAX_TRACE_CUSTOM_ATTRIBUTES

public static final int MAX_TRACE_CUSTOM_ATTRIBUTES = 5

Maximum allowed number of attributes allowed in a trace.

MAX_TRACE_NAME_LENGTH

public static final int MAX_TRACE_NAME_LENGTH = 100

Maximum allowed length of the name of the Trace

Public methods

getAttribute

public @Nullable String getAttribute(@NonNull String attribute)

Returns the value of an attribute.

Parameters
@NonNull String attribute

name of the attribute to fetch the value for

Returns
@Nullable String

The value of the attribute if it exists or null otherwise.

getAttributes

public @NonNull Map<StringStringgetAttributes()

Returns the map of all the attributes added to this HttpMetric.

Returns
@NonNull Map<StringString>

map of attributes and its values currently added to this HttpMetric

putAttribute

public void putAttribute(@NonNull String attribute, @NonNull String value)

Sets a String value for the specified attribute. Updates the value of the attribute if the attribute already exists. If the HttpMetric has been stopped, this method returns without adding the attribute. The maximum number of attributes that can be added to a HttpMetric are MAX_TRACE_CUSTOM_ATTRIBUTES.

Parameters
@NonNull String attribute

name of the attribute

@NonNull String value

value of the attribute

removeAttribute

public void removeAttribute(@NonNull String attribute)

Removes an already added attribute from the HttpMetric. If the HttpMetric has already been stopped, this method returns without removing the attribute.

Parameters
@NonNull String attribute

name of the attribute to be removed from the running Traces.

setHttpResponseCode

public void setHttpResponseCode(int responseCode)

Sets the httpResponse code of the request

Parameters
int responseCode

valid values are greater than 0. Invalid usage will be logged.

setRequestPayloadSize

public void setRequestPayloadSize(long bytes)

Sets the size of the request payload

Parameters
long bytes

valid values are greater than or equal to 0. Invalid usage will be logged.

setResponseContentType

public void setResponseContentType(@Nullable String contentType)

Content type of the response such as text/html, application/json, etc...

Parameters
@Nullable String contentType

valid string of MIME type. Invalid usage will be logged.

setResponsePayloadSize

public void setResponsePayloadSize(long bytes)

Sets the size of the response payload

Parameters
long bytes

valid values are greater than or equal to 0. Invalid usage will be logged.

start

public void start()

Marks the start time of the request

stop

public void stop()

Marks the end time of the response and queues the network request metric on the device for transmission. Check logcat for transmission info.

Extension functions

PerformanceKt.trace

public final void PerformanceKt.trace(
    @NonNull HttpMetric receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull HttpMetricUnit> block
)

Measures the time it takes to run the block wrapped by calls to start and stop using HttpMetric.

PerformanceKt.trace

public final void PerformanceKt.trace(
    @NonNull HttpMetric receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull HttpMetricUnit> block
)

Measures the time it takes to run the block wrapped by calls to start and stop using HttpMetric.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-perf-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.