Skip to content

middleware-labs/middleware-android

Repository files navigation

Middleware Android SDK


Build Status Maven Central GitHub release (latest SemVer) Build Status


Features

  • Access to OpenTelemetry APIs
  • OkHttp3 instrumentation for monitoring HTTP events
  • Middleware APIs for sending custom events & recording exceptions
  • Slow / Freeze render detection
  • Custom logging
  • Network Change Detection
  • ANR Detection
  • Crash Reporting
  • Android Activity & Fragment lifecycle events

Requirements

  • Android Minimum SDK Version : 21

Pre-requisites

Before using the Middleware Android SDK, ensure you have:

An account with Middleware to obtain the RUM (Real User Monitoring) access token and target URL. Visit installation docs section of Real-User-Monitoring from Middleware dashboard.

Steps

  1. Create New Application
  2. Obtain the accountKey & target once application is created.

Getting Started

The Middleware Android SDK provides instrumentation for monitoring various aspects of your Android application. With this SDK, you can track and analyze the features listed above, viewing the results in the Middleware RUM section and RUM dashboard.

Setup

Install Middleware Android SDK

implementation 'io.github.middleware-labs:android-sdk:+'

Configure of Middleware Android Instrumentation

class MiddlewareApplication extends Application {
   private final String targetUrl = "<target-url>";
   private final String rumAccessToken = "<your-access-token>";

   @Override
   public void onCreate() {
      super.onCreate();

    Middleware.builder()
        .setGlobalAttributes(Attributes.of(APP_VERSION, BuildConfig.VERSION_NAME))
        .setTarget(targetUrl)
        .setServiceName("sample-android-app-1")
        .setProjectName("Mobile-SDK-Android")
        .setRumAccessToken(rumAccessToken)
        .setSlowRenderingDetectionPollInterval(Duration.ofMillis(1000))
        .setDeploymentEnvironment("PROD")
        .build(this);
}

Documentation

Configurations

Methods that can be used for setting instrumentation & configure your application.

OptionDescription
setRumAccessToken(String) Sets the RUM account access token to authorize client to send telemetry data to Middleware
setTarget(String) Sets the target URL to which you want to send telemetry data. For example - https://app.middleware.io
setService(String) Sets the service name for your application. This can be used furthur for filtering by service name.
setDeploymentEnvironment(String) Sets the environment attribute on the spans that are generated by the instrumentation. For Example - PROD | DEV
disableCrashReporting() Disable crash reporting. By default it is enabled.
disableAnrDetection() Disable Application Not Responding Detection. By default it is enabled.
disableNetworkMonitor() Disable network change detection. By default it is enabled.
disableSlowRenderingDetection() Disable slow or frozen frame renders. By default it is enabled.
setSlowRenderingDetectionPollInterval(Duration) Sets the default polling for slow or frozen render detection. Default value in milliseconds is 1000

HTTP Instrumentation Configuration

OkHttp

private Call.Factory buildOkHttpClient(Middleware middleware) {
   return middleware.createRumOkHttpCallFactory(new OkHttpClient());
}

Manually instrumentation for android application

Global Attributes

Global attributes are key-value pairs that are used for attaching the global information for the reported data. These values can be useful for custom or user specific tags that can be attached while sending data to Middleware.

How to set global attributes?
Middleware.builder()
        .setGlobalAttributes(
            Attributes.builder()
                    .put("key", "value")
                    .put(StandardAttributes.APP_VERSION, BuildConfig.VERSION_NAME)
                    .build());

Custom Events

You can also send custom events and workflows using addEvent and startWorkflow APIs respectively

How to send custom event?
Middleware.getInstance().addEvent("You clicked on Button", BUTTON_ATTRIBUES);
How to start workflow?
Span loginWorkflow = Middleware.getInstance().startWorkflow("User Login Flow");
How to end workflow?
loginWorkflow.end();

Configure error reporting

You can report exceptions, errors and any messages using addException(Throwable) We will show this on our Middleware Dashboard.

Middleware.getInstance().addException(new RuntimeException("Something went wrong!"), Attributes.empty())

Custom Logs

You can add custom logs such as debug, error, warn, info these logs will be shown on Middleware Logs Dashboard

Middleware logInstance = Middleware.getInstance();
logInstance.d("TAG", "I am debug");
logInstance.e("TAG", "I am error");
logInstance.i("TAG", "I am info");
logInstance.w("TAG", "I am warn");

Enable Session Recording

By default session recording is enabled capture all activities. To disable session recording you can use .disableSessionRecording()

Sanitizing view elements

To blur sensitive information in session recording use the following method :

    final Middleware instance = Middleware.getInstance();
    final TextView someTextView = findViewById(R.id.some_text_view;
    instance.addSanitizedElement(someTextView);