How to group metrics by domain

MCCZ 1 Reputation point
2020-05-12T05:42:45.24+00:00

For a web app serving multiple customer domains, I would like to gather metrics like Data Out and Requests per domain (abc.contoso.com, def.example.com).

For example, I would like to know that abc.contoso.com generated 25 GB Data Out and served 5 000 requests in March while def.example.com generated 5 GB Data Out and served 4 000 requests.

Is there a way to get these info? How?

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,330 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,932 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Manu Philip 18,681 Reputation points MVP
    2020-05-12T17:38:39.38+00:00

    Hi,
    Here is a simple PS script, which shows the graphical view of the stats you are looking for. Note that, this is a very basic script works for a single website and you need to enhance it further to work on multiple sites

    Note: Set the appropriate values for the parameters as per your setup. ResourceName "manutest" ResourceGroupName "manurg1"

    Give Start and End Time accordingly

    # inputs
    $WarningPreference = 'SilentlyContinue'
    $TimeGrain = [timespan]::FromMinutes(1)
    $Start = [datetime]::Now.AddMinutes(-40)
    $End = [datetime]::Now.AddMinutes(-1)
    
    # capture resource metrics
    $Resource = Get-AzResource -ResourceName "manutest" -ResourceGroupName "manurg1" -ResourceType "Microsoft.Web/sites"
    $ResourceID = $Resource.ResourceId
    
    $MetricName = 'BytesSent'
    $Splat = @{
        ResourceId = $ResourceID 
        MetricName = $MetricName
        TimeGrain = $TimeGrain 
        StartTime = $Start
        EndTime = $End
    }
    $Data = Get-AzMetric @Splat
    $Datpoints = $data.data.average.foreach({[int]$_})
    
    # plot the graph
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter
    
    $MetricName = 'BytesReceived'
    $Splat = @{
        ResourceId = $ResourceID 
        MetricName = $MetricName
        TimeGrain = $TimeGrain 
        StartTime = $Start
        EndTime = $End
    }
    $Data = Get-AzMetric @Splat
    $Datpoints = $data.data.average.foreach({[int]$_})
    
    # plot the graph
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter
    
    $MetricName = 'Requests'
    $Splat = @{
        ResourceId = $ResourceID 
        MetricName = $MetricName
        TimeGrain = $TimeGrain 
        StartTime = $Start
        EndTime = $End
    }
    $Data = Get-AzMetric @Splat
    $Datpoints = $data.data.average.foreach({[int]$_})
    
    # plot the graph
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
    Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter
    

    Regards,
    Manu

    1 person found this answer helpful.

  2. Manu Philip 18,681 Reputation points MVP
    2020-05-14T05:55:14.3+00:00

    Hello,

    I have modified the script to loop through all the domains in a selected Resource Group as per your request and providing here

    Note that the following variables to be given when asked

    1. Input the Resource Group Name
    2. Input the number of day's report you are looking for (View looks better when you select minimum days)

    Code is prepared as below: Save it as a PS1 file and you are ready to go :)

    $RgName = Read-Host "Please enter the Resource Group Name that the Websites belongs to"
    $Days= Read-Host "Please enter Last #Days needed to be checked in the report"
    $ResourceNames = Get-AzResource -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
    
    foreach ($ResourceName in $ResourceNames)
    {
    $WebappName = $ResourceName.Name 
    $URLName=(Get-AzWebApp -Name $WebappName).DefaultHostName
    
    # inputs
     $WarningPreference = 'SilentlyContinue'
     $TimeGrain = [timespan]::FromHours(1)
    
     $Start = [datetime]::Now.AddDays(-$Days)
     $End = [datetime]::Now
    
     # capture resource metrics
     $Resource = Get-AzResource -ResourceName $WebappName -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
     $ResourceID = $Resource.ResourceId
    
     $MetricName = 'BytesSent'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
    
     # plot the graph
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Line
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
    
     $MetricName = 'BytesReceived'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
    
     # plot the graph
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Line
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
    
     $MetricName = 'Requests'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
    
     # plot the graph
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Bar
     Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
     }
    
    0 comments No comments

  3. MCCZ 1 Reputation point
    2020-05-14T06:15:14.54+00:00

    Hi Manu,

    thanks for your updated answer. I understand it iterates through all the web apps in a given resource group nor through all the domains served by a single web app. As there still seems to be a misunderstanding, I'll try to reiterate the question using an example.

    I have a single web app, let's say ContosoCms whose purpose is to serve webs on three domains - www.contoso.com, www.example.com and support.example.com. We will scale up the ContosoCms based on the total traffic not based on the number of domains it serves. As of now there is just a single instance of the web app serving all three domains.

    For this single web app, I need to group Data Out and Requests metrics based on the domain it was served on.

    Served domain by the single ContosoCms web app | Data Out | Requests
    www.contoso.com                                |   300 MB |  2 532 requests
    www.example.com                                |   234 MB |  2 544 requests
    support.example.com                            | 4 500 MB | 53 423 requests
    
    0 comments No comments

  4. Manu Philip 18,681 Reputation points MVP
    2020-05-15T09:35:39.743+00:00

    Hello,

    I have further modified the script to print the required statistics from different websites within the shell itself. Please check the enhanced code.
    When asked, give the name of the WebAppName where the websites belongs to

    $WebAppName = Read-Host "Please enter the WebApp Name that the Website(s) belongs to"
    $Days= Read-Host "Please enter Last #Days needed to be checked in the report"
    $WebApp= Get-AzWebApp -Name $WebAppName
    $RgName=$WebApp.ResourceGroup
    
    $ResourceNames = Get-AzResource -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
    
    foreach ($ResourceName in $ResourceNames)
    {
    $WebappName = $ResourceName.Name 
    $URLName=(Get-AzWebApp -Name $WebappName).DefaultHostName
    
    # inputs
     $WarningPreference = 'SilentlyContinue'
     $TimeGrain = [timespan]::FromDays(1)
    
     $Start = [datetime]::Now.AddDays(-$Days)
     $End = [datetime]::Now
    
     # capture resource metrics
     $Resource = Get-AzResource -ResourceName $WebappName -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
     $ResourceID = $Resource.ResourceId
    
     $MetricName = 'Requests'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
     Write-Output $URLName
     Write-Output $MetricName
     Write-Output $data.data | format-table TimeStamp, Total -AutoSize
    
     $MetricName = 'BytesReceived'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
     Write-Output $URLName
     Write-Output $MetricName
     Write-Output $data.data | format-table TimeStamp, Total -AutoSize
    
     $MetricName = 'BytesSent'
     $Splat = @{
         ResourceId = $ResourceID 
         MetricName = $MetricName
         TimeGrain = $TimeGrain 
         StartTime = $Start
         EndTime = $End
     }
     $Data = Get-AzMetric @Splat
     $Datpoints = $data.data.average.foreach({[int]$_})
     Write-Output $URLName
     Write-Output $MetricName
     Write-Output $data.data | format-table TimeStamp, Total -AutoSize
    }
    

    Regards,
    Manu

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.