HttpMetric

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

const Int

Maximum allowed length of the Key of the Trace attribute

const Int

Maximum allowed length of the Value of the Trace attribute

const Int

Maximum allowed number of attributes allowed in a trace.

const Int

Maximum allowed length of the name of the Trace

Public functions

String?
getAttribute(attribute: String)

Returns the value of an attribute.

(Mutable)Map<String!, String!>

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

Unit
putAttribute(attribute: String, value: String)

Sets a String value for the specified attribute.

Unit
removeAttribute(attribute: String)

Removes an already added attribute from the HttpMetric.

Unit
setHttpResponseCode(responseCode: Int)

Sets the httpResponse code of the request

Unit

Sets the size of the request payload

Unit

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

Unit

Sets the size of the response payload

Unit

Marks the start time of the request

Unit

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

Extension functions

inline Unit

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

inline Unit

This function 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

const val MAX_ATTRIBUTE_KEY_LENGTH = 40: Int

Maximum allowed length of the Key of the Trace attribute

MAX_ATTRIBUTE_VALUE_LENGTH

const val MAX_ATTRIBUTE_VALUE_LENGTH = 100: Int

Maximum allowed length of the Value of the Trace attribute

MAX_TRACE_CUSTOM_ATTRIBUTES

const val MAX_TRACE_CUSTOM_ATTRIBUTES = 5: Int

Maximum allowed number of attributes allowed in a trace.

MAX_TRACE_NAME_LENGTH

const val MAX_TRACE_NAME_LENGTH = 100: Int

Maximum allowed length of the name of the Trace

Public functions

getAttribute

fun getAttribute(attribute: String): String?

Returns the value of an attribute.

Parameters
attribute: String

name of the attribute to fetch the value for

Returns
String?

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

getAttributes

fun getAttributes(): (Mutable)Map<String!, String!>

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

Returns
(Mutable)Map<String!, String!>

map of attributes and its values currently added to this HttpMetric

putAttribute

fun putAttribute(attribute: String, value: String): Unit

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
attribute: String

name of the attribute

value: String

value of the attribute

removeAttribute

fun removeAttribute(attribute: String): Unit

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

Parameters
attribute: String

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

setHttpResponseCode

fun setHttpResponseCode(responseCode: Int): Unit

Sets the httpResponse code of the request

Parameters
responseCode: Int

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

setRequestPayloadSize

fun setRequestPayloadSize(bytes: Long): Unit

Sets the size of the request payload

Parameters
bytes: Long

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

setResponseContentType

fun setResponseContentType(contentType: String?): Unit

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

Parameters
contentType: String?

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

setResponsePayloadSize

fun setResponsePayloadSize(bytes: Long): Unit

Sets the size of the response payload

Parameters
bytes: Long

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

start

fun start(): Unit

Marks the start time of the request

stop

fun stop(): Unit

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

trace

inline fun HttpMetric.trace(block: HttpMetric.() -> Unit): Unit

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

trace

inline fun HttpMetric.trace(block: HttpMetric.() -> Unit): Unit

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.