Programmatically create a Stream Analytics job monitor

This article demonstrates how to enable monitoring for a Stream Analytics job. Stream Analytics jobs that are created via REST APIs, Azure SDK, or PowerShell do not have monitoring enabled by default. You can manually enable it in the Azure portal by going to the job’s Monitor page and clicking the Enable button or you can automate this process by following the steps in this article. The monitoring data will show up in the Metrics area of the Azure portal for your Stream Analytics job.

Prerequisites

Before you begin this process, you must have the following prerequisites:

  • Visual Studio 2019 or 2015
  • Azure .NET SDK downloaded and installed
  • An existing Stream Analytics job that needs to have monitoring enabled

Create a project

  1. Create a Visual Studio C# .NET console application.

  2. In the Package Manager Console, run the following commands to install the NuGet packages. The first one is the Azure Stream Analytics Management .NET SDK. The second one is the Azure Monitor SDK that will be used to enable monitoring. The last one is the Azure Active Directory client that will be used for authentication.

    Install-Package Microsoft.Azure.Management.StreamAnalytics
    Install-Package Microsoft.Azure.Insights -Pre
    Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory
    
  3. Add the following appSettings section to the App.config file.

    <appSettings>
      <!--CSM Prod related values-->
      <add key="ResourceGroupName" value="RESOURCE GROUP NAME" />
      <add key="JobName" value="YOUR JOB NAME" />
      <add key="StorageAccountName" value="YOUR STORAGE ACCOUNT"/>
      <add key="ActiveDirectoryEndpoint" value="https://login.microsoftonline.com/" />
      <add key="ResourceManagerEndpoint" value="https://management.azure.com/" />
      <add key="WindowsManagementUri" value="https://management.core.windows.net/" />
      <add key="AsaClientId" value="1950a258-227b-4e31-a9cf-717495945fc2" />
      <add key="RedirectUri" value="urn:ietf:wg:oauth:2.0:oob" />
      <add key="SubscriptionId" value="YOUR AZURE SUBSCRIPTION ID" />
      <add key="ActiveDirectoryTenantId" value="YOUR TENANT ID" />
    </appSettings>
    

    Replace values for SubscriptionId and ActiveDirectoryTenantId with your Azure subscription and tenant IDs. You can get these values by running the following PowerShell cmdlet:

    Get-AzureAccount
    
  4. Add the following using statements to the source file (Program.cs) in the project.

      using System;
      using System.Configuration;
      using System.Threading;
      using Microsoft.Azure;
      using Microsoft.Azure.Management.Insights;
      using Microsoft.Azure.Management.Insights.Models;
      using Microsoft.Azure.Management.StreamAnalytics;
      using Microsoft.Azure.Management.StreamAnalytics.Models;
      using Microsoft.IdentityModel.Clients.ActiveDirectory;
    
  5. Add an authentication helper method.

    public static string GetAuthorizationHeader()
    {
       AuthenticationResult result = null;
       var thread = new Thread(() =>
       {
          try
          {
              var context = new AuthenticationContext(
                 ConfigurationManager.AppSettings["ActiveDirectoryEndpoint"] +
                 ConfigurationManager.AppSettings["ActiveDirectoryTenantId"]);
              result = context.AcquireToken(
                  resource: ConfigurationManager.AppSettings["WindowsManagementUri"],
                  clientId: ConfigurationManager.AppSettings["AsaClientId"],
                  redirectUri: new Uri(ConfigurationManager.AppSettings["RedirectUri"]),
                  promptBehavior: PromptBehavior.Always);
          }
          catch (Exception threadEx)
          {
              Console.WriteLine(threadEx.Message);
          }
      });
    
      thread.SetApartmentState(ApartmentState.STA);
      thread.Name = "AcquireTokenThread";
      thread.Start();
      thread.Join();
    
      if (result != null)
      {
          return result.AccessToken;
      }
          throw new InvalidOperationException("Failed to acquire token");
    }
    

Create management clients

The following code will set up the necessary variables and management clients.

 string resourceGroupName = "<YOUR AZURE RESOURCE GROUP NAME>";
 string streamAnalyticsJobName = "<YOUR STREAM ANALYTICS JOB NAME>";

 // Get authentication token
 TokenCloudCredentials aadTokenCredentials =
     new TokenCloudCredentials(
         ConfigurationManager.AppSettings["SubscriptionId"],
         GetAuthorizationHeader());

 Uri resourceManagerUri = new
 Uri(ConfigurationManager.AppSettings["ResourceManagerEndpoint"]);

 // Create Stream Analytics and Insights management client
 StreamAnalyticsManagementClient streamAnalyticsClient = new
 StreamAnalyticsManagementClient(aadTokenCredentials, resourceManagerUri);
 InsightsManagementClient insightsClient = new
 InsightsManagementClient(aadTokenCredentials, resourceManagerUri);

Enable monitoring for an existing Stream Analytics job

The following code enables monitoring for an existing Stream Analytics job. The first part of the code performs a GET request against the Stream Analytics service to retrieve information about the particular Stream Analytics job. It uses the ID property (retrieved from the GET request) as a parameter for the Put method in the second half of the code, which sends a PUT request to the Insights service to enable monitoring for the Stream Analytics job.

Warning

If you have previously enabled monitoring for a different Stream Analytics job, either through the Azure portal or programmatically via the below code, we recommend that you provide the same storage account name that you used when you previously enabled monitoring.

The storage account is linked to the region that you created your Stream Analytics job in, not specifically to the job itself.

All Stream Analytics jobs (and all other Azure resources) in that same region share this storage account to store monitoring data. If you provide a different storage account, it might cause unintended side effects in the monitoring of your other Stream Analytics jobs or other Azure resources.

The storage account name that you use to replace <YOUR STORAGE ACCOUNT NAME> in the following code should be a storage account that is in the same subscription as the Stream Analytics job that you are enabling monitoring for.

// Get an existing Stream Analytics job
 JobGetParameters jobGetParameters = new JobGetParameters()
 {
     PropertiesToExpand = "inputs,transformation,outputs"
 };
 JobGetResponse jobGetResponse = streamAnalyticsClient.StreamingJobs.Get(resourceGroupName, streamAnalyticsJobName, jobGetParameters);

// Enable monitoring
ServiceDiagnosticSettingsPutParameters insightPutParameters = new ServiceDiagnosticSettingsPutParameters()
{
        Properties = new ServiceDiagnosticSettings()
       {
           StorageAccountName = "<YOUR STORAGE ACCOUNT NAME>"
       }
};
insightsClient.ServiceDiagnosticSettingsOperations.Put(jobGetResponse.Job.Id, insightPutParameters);

Get support

For further assistance, try our Azure Stream Analytics forum.

Next steps