@@ -58,6 +58,10 @@ public class VendorInfo
5858 private const string GetMethod = "GET" ;
5959 private const string PutMethod = "PUT" ;
6060
61+ private const string AzureWebsiteOwnerName = "WEBSITE_OWNER_NAME" ;
62+ private const string AzureWebsiteResourceGroup = "WEBSITE_RESOURCE_GROUP" ;
63+ private const string AzureWebsiteSiteName = "WEBSITE_SITE_NAME" ;
64+
6165 public VendorInfo ( IConfiguration configuration , IAgentHealthReporter agentHealthReporter , IEnvironment environment , VendorHttpApiRequestor vendorHttpApiRequestor , IFileWrapper fileWrapper )
6266 {
6367 _configuration = configuration ;
@@ -102,6 +106,10 @@ public IDictionary<string, IVendorModel> GetVendors()
102106 {
103107 vendorMethods . Add ( GetAzureFunctionVendorInfo ) ;
104108 }
109+ if ( _configuration . UtilizationDetectAzureAppService )
110+ {
111+ vendorMethods . Add ( GetAzureAppServiceVendorInfo ) ;
112+ }
105113
106114 foreach ( var vendorMethod in vendorMethods )
107115 {
@@ -155,6 +163,40 @@ public IVendorModel GetAzureFunctionVendorInfo()
155163 return new AzureFunctionVendorModel ( appName , cloudRegion ) ;
156164 }
157165
166+ public IVendorModel GetAzureAppServiceVendorInfo ( )
167+ {
168+ // WEBSITE_OWNER_NAME should container the subscription ID and the resource group name, separated by a '+' sign.
169+ var candidateSubscriptionId = GetProcessEnvironmentVariable ( AzureWebsiteOwnerName ) ;
170+ if ( string . IsNullOrWhiteSpace ( candidateSubscriptionId ) || ! candidateSubscriptionId . Contains ( '+' ) )
171+ {
172+ return null ;
173+ }
174+
175+ var subscriptionId = candidateSubscriptionId . Split ( '+' ) [ 0 ] ;
176+ var resourceGroupName = GetProcessEnvironmentVariable ( AzureWebsiteResourceGroup ) ;
177+ var siteName = GetProcessEnvironmentVariable ( AzureWebsiteSiteName ) ;
178+
179+ if ( ! IsValidAndLog ( subscriptionId , AzureWebsiteOwnerName )
180+ || ! IsValidAndLog ( resourceGroupName , AzureWebsiteResourceGroup )
181+ || ! IsValidAndLog ( siteName , AzureWebsiteSiteName ) )
182+ {
183+ return null ;
184+ }
185+
186+ return new AzureAppServiceVendorModel ( $ "/subscriptions/{ subscriptionId } /resourceGroups/{ resourceGroupName } /providers/Microsoft.Web/sites/{ siteName } ") ;
187+
188+ bool IsValidAndLog ( string value , string valueSource )
189+ {
190+ if ( string . IsNullOrWhiteSpace ( value ) )
191+ {
192+ Log . Finest ( $ "When building Azure App Service resource id, { valueSource } was null.") ;
193+ return false ;
194+ }
195+
196+ return true ;
197+ }
198+ }
199+
158200 private IVendorModel GetAwsVendorInfo ( )
159201 {
160202 var token = _vendorHttpApiRequestor . CallVendorApi ( new Uri ( AwsTokenUri ) , PutMethod , AwsName , new List < string > { AwsTokenDurationHeader } ) ;
0 commit comments