Skip to content

Commit

Permalink
Update app insights error
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitkumarr committed Jun 6, 2019
1 parent 75798ea commit 33c296f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public override ICommandLineParserResult ParseArgs(string[] args)
public override async Task RunAsync()
{
var subscriptions = await AzureHelper.GetSubscriptions(AccessToken, ManagementURL);
ColoredConsole.WriteLine("Retrieving Function App...");
var functionApp = await AzureHelper.GetFunctionApp(FunctionAppName, AccessToken, ManagementURL, allSubs: subscriptions);
if (UseBrowser)
{
Expand Down Expand Up @@ -83,15 +84,17 @@ public async Task OpenLiveStreamInBrowser(Site functionApp, ArmSubscriptionsArra
throw new CliException("Invalid Instrumentation Key found. Please make sure that the Application Insights is configured correctly.");
}

ColoredConsole.WriteLine("Retrieving Application Insights information...");
var appId = await AzureHelper.GetApplicationInsightIDFromIKey(iKey, AccessToken, ManagementURL, allSubs: allSubscriptions);
var armResourceId = AzureHelper.ParseResourceId(appId);
var componentId = $@"{{""Name"":""{armResourceId.Name}"",""SubscriptionId"":""{armResourceId.Subscription}"",""ResourceGroup"":""{armResourceId.ResourceGroup}""}}";

var liveMetricsUrl = string.Format(LiveMetricsUriTemplate, WebUtility.UrlEncode(componentId), WebUtility.UrlEncode(appId));

ColoredConsole.WriteLine("Launching web browser...");
if (StaticSettings.IsDebug)
{
ColoredConsole.WriteLine(VerboseColor($"Opening browser with URL- {liveMetricsUrl}"));
ColoredConsole.WriteLine(VerboseColor($"Launching browser with URL- {liveMetricsUrl}"));
}

Utilities.OpenBrowser(liveMetricsUrl);
Expand Down
9 changes: 7 additions & 2 deletions src/Azure.Functions.Cli/Helpers/AzureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private static async Task<Site> TryGetFunctionApp(string name, string accessToke

internal static async Task<string> GetApplicationInsightIDFromIKey(string iKey, string accessToken, string managementURL, ArmSubscriptionsArray allSubs = null)
{
const string appInsightsError = "Could not find the Application Insights using the configured Instrumentation Key.";

var allArmSubscriptions = (allSubs ?? await GetSubscriptions(accessToken, managementURL)).value;
var allSubscriptionIds = allArmSubscriptions.Select(sub => sub.subscriptionId);

Expand All @@ -95,14 +97,17 @@ internal static async Task<string> GetApplicationInsightIDFromIKey(string iKey,
};

var response = await ArmClient.HttpInvoke(HttpMethod.Post, url, accessToken, objectPayload: bodyObject);
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
throw new CliException(appInsightsError);
}

var result = await response.Content.ReadAsStringAsync();
var argResponse = JsonConvert.DeserializeObject<ArgResponse>(result);

// we need the first item of the first row
return argResponse.Data?.Rows?.FirstOrDefault()?.FirstOrDefault()
?? throw new CliException("Could not find the Application Insights using the configured Instrumentation Key.");
?? throw new CliException(appInsightsError);
}

internal static ArmResourceId ParseResourceId(string resourceId)
Expand Down

0 comments on commit 33c296f

Please sign in to comment.