# HttpMetric

# HttpMetric


```
public class HttpMetric
```

<br />

*** ** * ** ***

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` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#MAX_ATTRIBUTE_KEY_LENGTH() = 40` Maximum allowed length of the Key of the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/Trace` attribute |
| `static final int` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#MAX_ATTRIBUTE_VALUE_LENGTH() = 100` Maximum allowed length of the Value of the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/Trace` attribute |
| `static final int` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#MAX_TRACE_CUSTOM_ATTRIBUTES() = 5` Maximum allowed number of attributes allowed in a trace. |
| `static final int` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#MAX_TRACE_NAME_LENGTH() = 100` Maximum allowed length of the name of the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/Trace` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/Nullable.html https://developer.android.com/reference/kotlin/java/lang/String.html` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#getAttribute(java.lang.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html attribute)` Returns the value of an attribute. |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/util/Map.html<https://developer.android.com/reference/kotlin/java/lang/String.html, https://developer.android.com/reference/kotlin/java/lang/String.html>` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#getAttributes()()` Returns the map of all the attributes added to this HttpMetric. |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#putAttribute(java.lang.String,java.lang.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html attribute, @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html value)` Sets a String value for the specified attribute. |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#removeAttribute(java.lang.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html attribute)` Removes an already added attribute from the HttpMetric. |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#setHttpResponseCode(int)(int responseCode)` Sets the httpResponse code of the request |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#setRequestPayloadSize(long)(long bytes)` Sets the size of the request payload |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#setResponseContentType(java.lang.String)(@https://developer.android.com/reference/kotlin/androidx/annotation/Nullable.html https://developer.android.com/reference/kotlin/java/lang/String.html contentType)` Content type of the response such as text/html, application/json, etc... |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#setResponsePayloadSize(long)(long bytes)` Sets the size of the response payload |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#start()()` Marks the start time of the request |
| `void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#stop()()` Marks the end time of the response and queues the network request metric on the device for transmission. |

| ### Extension functions |
|---|---|
| `final void` | `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/PerformanceKt.https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric#(com.google.firebase.perf.metrics.HttpMetric).trace(kotlin.Function1)( @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric receiver, @https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-extension-function-type/index.html @https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html Function1<@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html> block )` Measures the time it takes to run the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/package-summary#(com.google.firebase.perf.metrics.HttpMetric).trace(kotlin.Function1)` wrapped by calls to start and stop using `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric`. |

## Constants

### MAX_ATTRIBUTE_KEY_LENGTH

```
public static final int MAX_ATTRIBUTE_KEY_LENGTH = 40
```

Maximum allowed length of the Key of the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/Trace` attribute

### MAX_ATTRIBUTE_VALUE_LENGTH

```
public static final int MAX_ATTRIBUTE_VALUE_LENGTH = 100
```

Maximum allowed length of the Value of the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/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 `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/Trace`

## Public methods

### getAttribute

```
public @Nullable String getAttribute(@NonNull String attribute)
```

Returns the value of an attribute.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html attribute` | name of the attribute to fetch the value for |

| Returns |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/Nullable.html https://developer.android.com/reference/kotlin/java/lang/String.html` | The value of the attribute if it exists or null otherwise. |

### getAttributes

```
public @NonNull Map<String, String> getAttributes()
```

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/util/Map.html<https://developer.android.com/reference/kotlin/java/lang/String.html, https://developer.android.com/reference/kotlin/java/lang/String.html>` | 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 `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/FirebasePerformanceAttributable#MAX_TRACE_CUSTOM_ATTRIBUTES()`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html attribute` | name of the attribute |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html https://developer.android.com/reference/kotlin/java/lang/String.html 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 |
|---|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/Nullable.html https://developer.android.com/reference/kotlin/java/lang/String.html 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 HttpMetric, Unit> block
)
```

Measures the time it takes to run the `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/package-summary#(com.google.firebase.perf.metrics.HttpMetric).trace(kotlin.Function1)` wrapped by calls to start and stop using `https://firebase.google.com/docs/reference/android/com/google/firebase/perf/metrics/HttpMetric`.