skip to Main Content

There is an example how it is possible to query

  • LogAnalytics Workspace Logs or
  • Metrics for individual resources
    using Node.Js:

But I could not find if there is an option to query Logs from AppInsights or from resource directly.

I need it to automate reporting of the performance, so I am planning to query requests table (we send logs using https://github.com/microsoft/ApplicationInsights-Java). Currently report is done manually using Performance blade of the AppInsights – checking Avg and 99 percentile for requests with specific filters on URL

2

Answers


  1. How to query Azure App Insights logs using Node.JS

    • In Azure Portal, Create Application Insights Instance and Copy the Instrumentation key from the overview page

    enter image description here

    • Create a sample NodeJS Web App in Visual Studio code
    • We can add the instrumentation key in localhost or can be updated once after nodejs application is deployed to Azure.Here I have added the required application insight setting and deployed the App
    • In server.js, add
    let  appInsights = require('applicationinsights');
    appInsights.setup("cc580d32-a7eb-41d7-b0e0-90ea0889fd10");
    appInsights.start();
    
    • From the root folder of the Application, open the terminal and run
    npm install applicationinsights --save
    

    enter image description here

    • Deploy the Application to Azure
    • Browse the Application

    View Logs in Application Insights

    • Application Insights queries are based on KQL
    • Navigate to Azure Portal => Your Application Insights Instance => Logs under Monitoring = > Click on traces

    Metrics for individual resources using Node.Js

    • Navigate to metrics under Monitoring

    enter image description here

    Please refer Node.js services with Application Insights for more information

    Login or Signup to reply.
  2. You can setup logs ingestion from your application to Log Analytics Workspace using Diagnostic Settings. For example, if you host your app in WebApp you can send AppServiceHTTPLogs. And then in your Node.JS app you can use @azure/monitor-query package with a similar query:

     let dataset=AppServiceHTTPLogs
          | where CsHost == 'PUT_YOUR_HOSTNAME_HERE'
          | where ScStatus == 200
          | where CsUriStem contains 'FILTER_BY_URL_IF_YOU_NEED_IT';
      dataset
      | summarize arg_max(TimeTaken, CsUriStem)
      | union(dataset
        | summarize avg(TimeTaken), percentiles(TimeTaken, 99)
        | extend CsUriStem='Overall')
    

    That one is a close approximation of the performance blade from app insights.
    And then your whole app could be

    const azureLogAnalyticsWorkspaceId = "WORKSPACE_ID";
    const logsQueryClient = new LogsQueryClient(new DefaultAzureCredential());
    
    export async function runWebAppPerformance(startDate: Date, endDate: Date) {
     
      const query = "PUT_YOUR_QUERY_HERE";
      const result = await logsQueryClient.queryWorkspace(    
        azureLogAnalyticsWorkspaceId,
        query,  
        {    
          startTime: startDate, endTime: endDate
        }
      );
       
      if (result.status === LogsQueryResultStatus.Success) {
        const tablesFromResult: LogsTable[] = result.tables;
     
        if (tablesFromResult.length === 0) {
          console.log(`No results for query`);
          return;
        }
     
        processTables(tablesFromResult);
      } else {
        console.log(`Error processing the query - ${result.partialError}`);
      }
    }
    
    async function processTables(tablesFromResult: LogsTable[]) {
      const table = tablesFromResult[0];
    
      const urlIndex = table.columnDescriptors.findIndex(c => c.name === "CsUriStem");
      const timeTakenIndex = table.columnDescriptors.findIndex(c => c.name === "TimeTaken");
      const avgIndex = table.columnDescriptors.findIndex(c => c.name === "avg_TimeTaken");
      const ninetyNineindex = table.columnDescriptors.findIndex(c => c.name === "percentile_TimeTaken_99");
    
      for (const row of table.rows) {
        if (row[urlIndex] === "Overall"){
          console.log(`${row[urlIndex]} (ms):`);
          console.log(`Average: ${row[avgIndex]}; t 99%: ${row[ninetyNineindex]}`);      
        }
        else {
          console.log(`MAX (ms)`);
          console.log(`${row[urlIndex]}: t ${row[timeTakenIndex]}`);      
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search