> For the complete documentation index, see [llms.txt](https://docs.luciq.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.luciq.ai/android/set-up-luciq-for-android/set-up-application-performance-monitoring/set-up-network-monitoring.md).

# Set up Network Monitoring

Configure network monitoring for your Android app with Luciq. Track HTTP requests and responses to optimize your app's network performance.

### Getting Started

In order to allow Luciq [App Performance Monitoring](https://www.luciq.ai/platform) to capture network requests, you will need to include our Gradle plugin in your project. Currently, we support capturing requests made via `OkHttpClient` and `HTTPURLConnection`.

1. Add the dependency to your project's `build.gradle` file.

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
plugins {
  id("ai.luciq.library") version "18.0.0" apply false // adds luciq plugin
}
```

{% endcode %}
{% endtab %}

{% tab title="Groovy" %}
{% code overflow="wrap" %}

```java
buildscript {
    dependencies {
        // ...
        classpath 'ai.luciq.library:luciq-plugin:13.0.1'
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
Use the same version for the Luciq Gradle plugin and the Luciq SDK. From SDK version 19.14.0, a plugin newer than the SDK skips the instrumentation that SDK doesn't support and logs a build warning.
{% endhint %}

2. Add the plugin in your application's `build.gradle`:

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
plugins { 
  id("luciq-apm") // Luciq APM Plugin
}
```

{% endcode %}
{% endtab %}

{% tab title="Groovy" %}
{% code overflow="wrap" %}

```java
apply plugin: 'luciq-apm'
```

{% endcode %}
{% endtab %}
{% endtabs %}

> 📘 The Plugin DSL in Kotlin is supported starting SDK version 12.4.1.

{% hint style="success" %}
The Plugin DSL in Kotlin is supported starting SDK version 12.4.1.
{% endhint %}

**Plugin Alternatives:**

{% hint style="warning" %}
`LuciqAPMOkhttpInterceptor` is deprecated. We recommend the Luciq Gradle plugin above, or [Unified Network Interception](/android/set-up-luciq-for-android/logs-and-profiling/unified-network-interception.md).
{% endhint %}

You can always use a manual approach to capture requests made via the `OkHttpClient` by adding our Interceptor and EventListener using the below code snippets:

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
OkHttpClient.Builder()
    // add your interceptors here
    .addInterceptor(LuciqAPMOkhttpInterceptor())

    // Add Luciq EventListener
    .eventListener(LuciqApmOkHttpEventListener(/** optional: add your own EventListener here **/))
    // Or Add Luciq EventListenerFactory
    .eventListenerFactory(LuciqApmOkHttpEventListener.Factory(/** optional: add your own EventListener factory here **/))
    .build()
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code overflow="wrap" %}

```java
new OkHttpClient.Builder()
        // add your interceptors here
        .addInterceptor(new LuciqAPMOkhttpInterceptor())

        // Add Luciq EventListener
        .eventListener(new LuciqApmOkHttpEventListener(/** optional: add your own EventListener here **/))
        // Or Add Luciq EventListenerFactory
        .eventListenerFactory(new LuciqApmOkHttpEventListener.Factory(/** optional: add your own EventListener factory here **/))
        .build();
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="success" %}

#### GraphQL Support

Luciq supports capturing requests made via the `OkHttpClient` including `GraphQl` and `HTTPURLConnection`.
{% endhint %}

{% hint style="info" %}

#### APM Network Logs are independent from Bug and Crash Reporting

Please note that [Network Logs](/android/set-up-luciq-for-android/logs-and-profiling/report-logs-for-android.md#network-logs) that are captured in Bug and Crash reports will **not** be captured by APM, the only way to enabled Network performance monitoring in APM is by adding this plugin. Enabling Network performance monitoring has **no** effect on Network Logs captured in Bug and Crash reports, these are configured separately and are completely independent from APM.
{% endhint %}

***

### gRPC Support

Luciq supports the logging of gRPC requests. You'll first need to add the gRPC intereceptor module to your dependencies.

#### Integration

With Luciq already integrated and added to your dependencies, you'll only need to add the following dependency to your applications `build.gradle`:

{% tabs %}
{% tab title="Gradle" %}
{% code overflow="wrap" %}

```gradle
implementation 'ai.luciq.library:luciq-apm-grpc-interceptor:$LUCIQ_VERSION
```

{% endcode %}
{% endtab %}
{% endtabs %}

> 📘 Minimum SDK Version
>
> The gRPC interceptor is supported starting SDK version 10.13.0.

{% hint style="success" %}

#### Minimum SDK Version

The gRPC interceptor is supported starting SDK version 10.13.0.
{% endhint %}

#### Intercepting gRPC requests

{% hint style="warning" %}
`LuciqAPMGrpcInterceptor` is deprecated. Use `LuciqGrpcInterceptor` from `ai.luciq.library:luciq-grpc-interceptor` instead; see [Unified Network Interception](/android/set-up-luciq-for-android/logs-and-profiling/unified-network-interception.md#grpc).
{% endhint %}

When creating a gRPC channel using your preferred builder, create a new instance of `LuciqAPMGrpcInterceptor` and attach it to the builder using intercept method as shown in the example below:

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
OkHttpChannelBuilder.forAddress(host, port)
    .intercept(LuciqAPMGrpcInterceptor())
    .build()
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code overflow="wrap" %}

```java
OkHttpChannelBuilder.forAddress(host, port)
    .intercept(LuciqAPMGrpcInterceptor())
    .build();
```

{% endcode %}
{% endtab %}
{% endtabs %}

***

### Trace Attributes

{% hint style="warning" %}
`APM.addOnNetworkTraceListener` and `APM.removeOnNetworkTraceListener` are deprecated as of SDK version 19.14.0. Use `Luciq.addOnNetworkTraceListener` and `Luciq.removeOnNetworkTraceListener`, available from 19.14.0, instead. The new `OnNetworkTraceListener`, `UrlPredicate` and `NetworkTrace` types live in `ai.luciq.core.networklogs.contract`, so migrating means changing the imports. In the new `NetworkTrace`, `responseCode` is `statusCode`, and `totalDuration` and `startTime` (milliseconds) are `durationMicros` and `startMicros` (microseconds).
{% endhint %}

Sometimes, you may need to add additional data or attributes to your network traces. This can be done using the API below.

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
import ai.luciq.core.networklogs.contract.NetworkTrace
import ai.luciq.core.networklogs.contract.OnNetworkTraceListener
import ai.luciq.core.networklogs.contract.UrlPredicate

val listener = object : OnNetworkTraceListener(
    UrlPredicate { url: String ->
        // Add filter here the URL
        // return boolean
        true
    }
) {
    override fun addAttributesOnFinish(trace: NetworkTrace): Map<String, String> {
        // Return map of Pairs (i.e. keys and values)
        return mapOf(
            // Up to five attributes
            "Key" to "Value"
        )
    }
}
Luciq.addOnNetworkTraceListener(listener)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code overflow="wrap" %}

```java
import ai.luciq.core.networklogs.contract.NetworkTrace;
import ai.luciq.core.networklogs.contract.OnNetworkTraceListener;

OnNetworkTraceListener listener = new OnNetworkTraceListener(url -> {
    // Add filter here the URL
    // return boolean
    return true;
}) {
    @Override
    public Map<String, String> addAttributesOnFinish(@NonNull NetworkTrace trace) {
        Map<String, String> map = new HashMap<>();
        map.put("Key", "Value");
        // Up to five attributes
        return map;
    }
};
Luciq.addOnNetworkTraceListener(listener);
```

{% endcode %}
{% endtab %}
{% endtabs %}

To stop adding attributes, remove the listener you registered.

{% tabs %}
{% tab title="Kotlin" %}
{% code overflow="wrap" %}

```kotlin
Luciq.removeOnNetworkTraceListener(listener)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code overflow="wrap" %}

```java
Luciq.removeOnNetworkTraceListener(listener);
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can add up to **20 unique trace attributes** per each trace.
{% endhint %}

***

### Payload Size

By default, Luciq captures the payload size of the request and response; this should help with determining if the payload size could be causing any issues with network delays. The payload size is the size of the payload itself, so it does not include the header's size.

Luciq automatically buckets payload sizes into different buckets based on the distribution of the data per network group, with a minimum sensitivity of 10 bytes per bucket.

***

### Disabling/Enabling Network Performance Monitoring

Once you include the plugin as described in the [Getting Started](#getting-started) section, capturing network requests is enabled by default. However, you can still disable Network performance monitoring by configuring the plugin in your `build.gradle` file as described below:

{% tabs %}
{% tab title="Groovy" %}
{% code overflow="wrap" %}

```groovy
Luciq {
    APM {
        networkInterceptingEnabled false
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
Please note that Luciq does **not** control network logs captured in Bug and Crash reports, it only controls APM.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.luciq.ai/android/set-up-luciq-for-android/set-up-application-performance-monitoring/set-up-network-monitoring.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
