Using Application Insights to detect issues

Loading

Application Insights, part of the Azure Monitor suite, is a powerful tool for monitoring the performance, availability, and usage of applications. For PowerApps Portals (Power Pages), it helps detect issues in real time, ensuring a smooth user experience. By leveraging telemetry data, you can track errors, performance bottlenecks, and user behavior, enabling quicker identification and resolution of issues.

In this guide, we will explore how to set up and use Application Insights for detecting issues in PowerApps Portals and integrate it seamlessly into your portal environment.


1. What is Application Insights?

Application Insights provides:

  • Real-time monitoring of applications
  • Automatic data collection for performance, errors, and usage
  • Insights into user interactions, crashes, and slow response times
  • Powerful analytics tools to investigate and diagnose issues

In PowerApps Portals, it can help you detect:

  • Page load times and bottlenecks
  • JavaScript errors
  • Failed HTTP requests
  • Slow database queries or issues with the Dataverse backend
  • User session metrics

2. Why Use Application Insights for PowerApps Portals?

  • Early detection of issues: Application Insights helps detect performance bottlenecks, crashes, and exceptions as soon as they occur.
  • Better error tracking: Helps track JavaScript errors and failed HTTP requests that may not be easily visible through traditional methods.
  • Performance optimization: By monitoring response times, you can identify slow-loading pages and optimize them.
  • Behavior analysis: Gain insights into how users interact with your portal, what features they use, and which ones they abandon.
  • User impact: Track issues that are affecting users and address them before they escalate.

3. Setting Up Application Insights for PowerApps Portals

Step 1: Create an Application Insights Resource in Azure

  1. Go to the Azure Portal.
  2. Search for Application Insights in the search bar and select it.
  3. Click + Add to create a new Application Insights resource.
  4. Fill in the necessary details like Resource Group, Region, and Name.
  5. Once created, note the Instrumentation Key (or Connection String) for later use.

Step 2: Integrating Application Insights with PowerApps Portals

To send telemetry data to Application Insights, you need to add the Application Insights JavaScript SDK to your PowerApps Portal.

  1. Access Portal Management:
    • Go to your PowerApps Portal and open the Portal Management App.
  2. Add Application Insights SDK:
    • Open the Web Files section.
    • Create a new Web File with the following properties:
      • Name: application-insights.js
      • Type: JavaScript
      • Content: Insert the following script with your Instrumentation Key.
      window.appInsights = window.appInsights || function (a) { function b(c) { d[c] = function () { var e = arguments; d.push([c, e]) } } var d = []; var e = ["TrackEvent", "TrackPageView", "TrackException", "TrackMetric", "TrackDependency", "TrackTrace", "Flush"]; for (var f = 0; f < e.length; f++) b(e[f]); var g = a || {}; var h = g.config || {}; h.instrumentationKey = 'YOUR_INSTRUMENTATION_KEY'; var i = document.createElement("script"); i.src = "https://az416426.vo.msecnd.net/scripts/a/ai.0.js"; i.type = "text/javascript"; i.async = true; var j = document.getElementsByTagName("script")[0]; j.parentNode.insertBefore(i, j); }(); Replace YOUR_INSTRUMENTATION_KEY with the key you obtained from Azure.
  3. Set Web File for Loading:
    • Ensure this JavaScript file is loaded on all pages by configuring the Header or Footer to reference the application-insights.js Web File.
    • To add the script globally, modify the Site Settings or Content Snippet to include the file, ensuring it is executed on each page load.

4. Configuring Telemetry and Data Collection

Step 1: Customizing Telemetry Data

You can modify the data that’s tracked using Application Insights based on specific needs:

Tracking Page Views

You can track page views in PowerApps Portals by using the following code:

window.appInsights.trackPageView({ 
name: "Home Page",
properties: { customProperty: "Value" }
});

This can help you track which pages are being accessed and how long it takes for users to load these pages.

Tracking User Events

You can track user actions like clicks, form submissions, or other custom events:

window.appInsights.trackEvent("FormSubmission", { 
formName: "Contact Us",
status: "Success"
});

This will track a custom event whenever a user submits the contact form, allowing you to monitor form submission success or failure.


Step 2: Collecting Errors and Exceptions

JavaScript Errors are automatically tracked by Application Insights. However, you can also manually track exceptions or errors that might not trigger the built-in handlers.

try {
// Some operation that could fail
let result = riskyOperation();
} catch (error) {
window.appInsights.trackException({ exception: error });
}

This helps in capturing any client-side errors that might occur in the portal.


5. Analyzing the Data in Application Insights

Once your PowerApps Portal is sending telemetry data to Application Insights, you can begin analyzing it.

Key Insights to Monitor:

  1. Performance Metrics:
    • Page Load Time: Monitor the page load time to ensure your portal loads quickly for all users.
    • Response Time: Track the time taken for requests to be served by the server.
  2. Failed Requests:
    • Monitor failed HTTP requests, including 4xx and 5xx errors, to detect issues like broken links, 404 errors, or server-side failures.
  3. JavaScript Exceptions:
    • Look for unexpected exceptions, which might affect the user experience (e.g., failing to submit a form).
  4. Custom Events:
    • Analyze how users are interacting with your portal by examining custom event data, such as button clicks or form submissions.
  5. User Sessions:
    • Track user sessions, including session duration, to understand user behavior.

Step 3: Creating Alerts

You can set up alerts in Application Insights to notify you when certain thresholds are exceeded, such as:

  • High error rate for JavaScript errors
  • Long page load times
  • Failed requests or server-side exceptions
  1. In Azure Portal, go to your Application Insights resource.
  2. Under Alerts, click + New Alert Rule.
  3. Set conditions such as Error Count greater than a threshold.
  4. Set up an action (email, webhook, etc.) to notify your team of issues.

6. Troubleshooting and Resolving Issues

Use Application Insights Analytics to query telemetry data and perform root cause analysis.

For example, you can run a query like this:

requests
| where success == "False"
| summarize count() by url, bin(timestamp, 1h)

This will show you the failed requests over time, helping identify patterns and pinpointing exactly when and where failures are happening.

Leave a Reply

Your email address will not be published. Required fields are marked *