Send Auth0 events to Azure Monitor Application Insights

This article shows how to send Auth0 events received by Azure Event Grid to Azure Monitor Application Insights.

Prerequisites

Create an Azure Event Grid stream on Auth0.

Create an Azure function

  1. Create an Azure function by following instructions from the Create a local project section of Quickstart: Create a JavaScript function in Azure using Visual Studio Code.
    1. Select Azure Event Grid trigger for the function template instead of HTTP trigger as mentioned in the quickstart.

    2. Continue to follow the steps, but use the following index.js.

      Important

      Update the package.json to include applicationinsights as a dependency.

      index.js

      const appInsights = require("applicationinsights");
      appInsights.setup();
      const appInsightsClient = appInsights.defaultClient;
      
      // Event Grid always sends an array of data and may send more
      // than one event in the array. The runtime invokes this function
      // once for each array element, so we are always dealing with one.
      // See: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-grid-trigger?tabs=
      module.exports = async function (context, eventGridEvent) {
          context.log(typeof eventGridEvent);
          context.log(eventGridEvent);
      
          // As written, the Application Insights custom event will not be
          // correlated to any other context or span. If the custom event
          // should be correlated to the parent function invocation, use
          // the tagOverrides property. For example:
          // var operationIdOverride = {
              // "ai.operation.id": context.traceContext.traceparent
          // };
          // client.trackEvent({
              // name: "correlated to function invocation",
              // tagOverrides: operationIdOverride,
              // properties: {}
          // });
      
          context.log(`Sending to App Insights...`);
      
          appInsightsClient.trackEvent({
              name: "Event Grid Event",
              properties: {
                  eventGridEvent: eventGridEvent
              }
          });
      
          context.log(`Sent to App Insights successfully`);
      };
      
  2. Create an Azure function app using instructions from Quick function app create.
  3. Deploy your function to the function app on Azure using instructions from Deploy project files.

Create event subscription for partner topic using function

  1. In the Azure portal, navigate to the Event Grid partner topic created by your Auth0 log stream.

    Screenshot showing the Event Grid Partner Topics page with Add Event Subscription button selected.

  2. On the Create Event Subscription page, follow these steps:

    1. Enter a name for the event subscription.

    2. For Endpoint type, select Azure Function.

      Screenshot showing the Create Event Subscription page with Azure Functions selected as the endpoint type.

    3. Click Select an endpoint to specify details about the function.

  3. On the Select Azure Function page, follow these steps.

    1. Select the Azure subscription that contains the function.
    2. Select the resource group that contains the function.
    3. Select your function app.
    4. Select your Azure function.
    5. Then, select Confirm Selection.
  4. Now, back on the Create Event Subscription page, select Create to create the event subscription.

  5. After the event subscription is created successfully, you see the event subscription in the bottom pane of the Event Grid Partner Topic - Overview page.

  6. Select the link to your Azure function at the bottom of the page.

  7. On the Azure Function page, select Application Insights under Settings on the left menu.

  8. Select Application Insights link and then select View Application Insights data.

  9. Once your Auth0 logs are generated, your data should now be visible in Application Insights

    Note

    You can use steps in the article to handle events from other event sources too. For a generic example of sending Event Grid events to Azure Blob Storage or Azure Monitor Application Insights, see this example on GitHub.

Next steps