Aniket
Aniket

Reputation: 201

How to sync analytics and diagnostic events, errors, exceptions, crashes to Azure application app insights or blob storage from Android application

I have an Android application where I synced custom events, exceptions and crashes to Microsoft App Center using Android App Center SDK. Later I added AppInsight export to sync them to Azure App Insights. However, there are issues with syncing exceptions and errors from App Center to AppInsights. So I was only able to sync custom events to AppInsights.

Now that Microsoft has decided to retire the App Center, I want to replace it in another way. Azure AppInsight Android SDK was also deprecated way back.

Does anybody know the proper way to sync the Analytics and Diagnostic data from the Android application to Azure Application Insights?

Upvotes: 1

Views: 236

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3415

Without relying on deprecated SDKs, we can send telemetry data directly from your Android app to Azure Application Insights using its REST API. This allows you to capture custom events, exceptions, and other telemetry data.

Telemetry Client Class:

import okhttp3.*;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Map;

public class AppInsightsClient {
    private static final String INSTRUMENTATION_KEY = "YOUR_INSTRUMENTATION_KEY";
    private static final String APP_INSIGHTS_URL = "https://dc.services.visualstudio.com/v2/track";
    private OkHttpClient client;

    public AppInsightsClient() {
        client = new OkHttpClient();
    }

    public void trackEvent(String eventName, Map<String, String> properties) {
        try {
            JSONObject json = new JSONObject();
            json.put("iKey", INSTRUMENTATION_KEY);
            JSONObject data = new JSONObject();
            data.put("name", eventName);
            data.put("properties", new JSONObject(properties));
            json.put("data", data);

            RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json"));
            Request request = new Request.Builder().url(APP_INSIGHTS_URL).post(body).build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (!response.isSuccessful()) {
                        throw new IOException("Unexpected code " + response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void trackException(Exception e) {
        Map<String, String> properties = Map.of(
            "exception.message", e.getMessage(),
            "exception.type", e.getClass().getName()
        );
        trackEvent("Exception", properties);
    }
}
  • Track events, exceptions in Android app.
public class MainActivity extends AppCompatActivity {
    private AppInsightsClient insightsClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        insightsClient = new AppInsightsClient();

        // Example: Track a custom event
        insightsClient.trackEvent("AppStarted", Map.of("user", "JohnDoe"));

        // Example: Track an exception
        try {
            // Some risky code
        } catch (Exception e) {
            insightsClient.trackException(e);
        }
    }
}

Custom events: enter image description here

  • We can store the telemetry data in Azure Blob Storage for further processing or backup for that add Azure Storage SDK Dependency.
dependencies {
    implementation 'com.azure:azure-storage-blob:12.14.0'
}

Blob Storage Client:

import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;

public class BlobStorageClient {
    private static final String CONNECTION_STRING = "YOUR_BLOB_STORAGE_CONNECTION_STRING";
    private BlobServiceClient blobServiceClient;

    public BlobStorageClient() {
        blobServiceClient = new BlobServiceClientBuilder().connectionString(CONNECTION_STRING).buildClient();
    }

    public void uploadLog(String containerName, String blobName, String logContent) {
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.getBlobClient(blobName);
        blobClient.upload(new ByteArrayInputStream(logContent.getBytes()), logContent.length(), true);
    }
}

Upvotes: 0

Related Questions