Showing posts with label metrics. Show all posts
Showing posts with label metrics. Show all posts

Reading Notes #224

Cloud


Programming


Miscellaneous


Get more from Azure application Insights

Application Insights is an incredible tool that will brings to you a gigantic quantity of information about your application. In a previous post: Tons statistics and metrics for Microsoft Azure websites (not only in Asp.Net), I was explaining how to get started with App Insights. In this post, I want to explain how you can create your own customs events or metrics.

The setup


I will use an Asp.Net MVC named SimpleWebApp that will consume an Azure Api App named FrankApiApp. For the demo purposes I will have both projects in the same solution, but let's pretend that the ApiApp is not us, and that we don't control what inside.

To create an Azure Api App you will need Visual Studio 2015 or Visual Studio 2013 with minimum Azure SDK 2.6 installed. When creating the app, select Asp.Net Web Application, then Azure Api App (Preview).

Azure_API_App

For this post, we simply need one function. Here the code of the Get method that will wait and return us a string:
    // GET api/values
    public IEnumerable<string> Get()
    {
        var rnd = new Random();
        var waitFor = rnd.Next(500, 3000);

        System.Threading.Thread.Sleep(waitFor);
        var msg = String.Format("You have wait {0}. Sorry about that.", waitFor);

        return new string[] { waitFor.ToString(), msg };
    }

The application where telemetries will be added is a simple Asp.Net Web Application with MVC, created with the default template in Visual Studio. To add App Insights, just right-click on the project and select Add Application Insights Telemetry.

Add_Azure_API_App_CLient

Now we need to add a reference of the Azure API App. That could be easily done. Right-click, once again, on the project and Select Add, then Azure API App Client. A new window will appear, and we only need to select our just deployed API App.

Select_API_App[3]

The Base


Of course, just by adding the code spinet to the head of _Layout.cshtml you will have a lot of information about your server, your application but also the client experience and browser. This is all very useful, and it worth the time to navigate/ dig in the Azure portal to understand how your application is going/ doing.


Custom Metric


An interesting information could be to measure the time for each call to FrankApiApp. Let's started by adding a custom metric. To do that we will add a function in the HomeController and a new tab in that menu of the _Layout.cshtml.
public class HomeController : Controller
{
    private TelemetryClient appInsights = new TelemetryClient();

    public ActionResult ApiCall()
    {
        var stopwatch = System.Diagnostics.Stopwatch.StartNew();
        var client = new FrankApiApp();
        var result = client.Values.Get();

        stopwatch.Stop();

        ViewBag.Message = result[1];

        appInsights.TrackMetric("TimeDoingNothing", stopwatch.Elapsed.TotalSeconds);

        return View("Contact");
    }
}   

First, we create an instance of TelemetryClient. Then before calling the FrankApiApp's Get method we start a stopwatch. And we just need to use the TrackMetric method to push the metric.

After deploying and navigating a little in the application, let see what's we have available for us. Open portal.azure.com, and navigate to your application Insights. Click on the Metrics Explorer, a new blade now open with three empty charts. Click on the timeline, then in the Metric texbox type "TimeDoingNothing" (new metric's name). Now, click on the grid (third chart), add the metric name and select Group By Continent. Interesting right?!

TimeDoingNothing


Even more


Application Insight gives the opportunity to track many things: PageViews, Events, Exception, Requests, Trace and of course Metrics. Here some alternative of our ApiCall function using the TrackEvent and TrackRequest.
// Using TrackEvent
public ActionResult ApiCall()
{
    var stopwatch = System.Diagnostics.Stopwatch.StartNew();

    var client = new FrankApiApp();
    var result = client.Values.Get();
    stopwatch.Stop();

    ViewBag.Message = result[1];

    var metrics = new Dictionary<string, double> {{"processingTime", stopwatch.Elapsed.TotalMilliseconds}};
    var properties = new Dictionary<string, string>  {{ "MessageSuggestedElapsed", result[0] } };

    // Send the event:
    appInsights.TrackEvent("APICallProcessed", properties, metrics);
    appInsights.TrackMetric("TimeDoingNothing", stopwatch.Elapsed.TotalSeconds);

    return View("Contact");
}



// Using TrackRequest
public ActionResult ApiCallWithTelemetry()
{
    appInsights.Context.Operation.Id = Guid.NewGuid().ToString();

    var stopwatch = System.Diagnostics.Stopwatch.StartNew();
    var client = new FrankApiApp();
    var result = client.Values.Get();

    stopwatch.Stop();

    ViewBag.Message = result[1];

    // Send the event:
    appInsights.TrackRequest("APICallRequest", DateTime.UtcNow, stopwatch.Elapsed, "200", true);
    appInsights.TrackMetric("TimeDoingNothing", stopwatch.Elapsed.TotalSeconds);

    return View("Contact");
}



Wrap it up


I couldn't show everything in one post, it's up to you to create new metrics, events and query them in Azure Application Insights. Share your discovery or questions in the comment section bellow.

References



Tons of statistics and metrics for Microsoft Azure websites (not only Asp.Net)

Your website is finally online. That great, good job. But now, you have a lot of questions: How much visits do I have? Which part of the site is mostly visited? Does the site performs well? In this post, I will show you some tools that exist in Microsoft Azure that will help you to get some answers.

Request and Error

When you create a website in Azure, you automatically got some monitoring. Go on portal.azure.com (the preview portal), and select your website.

Monitoring_2015-03-05_1540

This will be useful to see how many requests and errors you got. You could create some alerts, by clicking the "+" sign, and you would be notified by e-mail if the number of request is greater than x, over the last hour.

Analytics

The first time you will click on this section all the instruction will be given to you.
To collect end-user usage analytics about your application, insert the following script into each page you want to track. Place this code immediately before the closing </headtag, and before any other scripts. Your first data will appear automatically in just a few seconds.
To get analytics for the whole web site, in an Asp.Net MVC site, a good place will be: \Views\Shared\_Layout.cshtml.

In a Ghost blog, if you are using the default theme, the file will be /content/themes/casper/default.hbs. Otherwise, just replace "casper" by your theme name in the path.

Once your website is re-deployed, re-open the website blade and click again on the Analytics graph. And you will be able to see a lot of information: Session per browser, information on page's views, slowest pages, details on sessions, etc.


Analitics_tour


When you click on a graph, the Metrics Explorer blade will be visible. On the top of this blade, you will have many different options to customize your results. You will be able to add a chart, change the time range, add some filter and even set some alerts.


Metrics_explorer


Moreover, if you click on those Charts, tables or even on a row, you will have more details and options to fine-tune the result.


Diagnostics_Search


Application Insights

You thought it was enough? Well, Microsoft Azure still has one more tool for you, that will cover in this post: Application Insights. With this one, you will be able to see the health of your application by adding some tests, custom events, logs, errors, etc.
Adding Application Insights to your Asp.Net website, can easily be done via Visual Studio, like I explained in a previous post.

Since Azure is compatible with many different languages, chances are that you are using one of those. Let say you are running a node.js Ghost blog, how could you add Application Insights? By using website extensions. To add an extension you could use the Kudu interface. This interface is easy to access. In a browser, type the URL of the website but inject "scm" between the name of the application and the azurewebsites.net. Something like http://mybookmanager.scm.azurewebsites.net/


From_Kudu_Console2015-03-01_2044


Once you are in the Kudu interface, click on the tab Site Extensions, then section Gallery. Add the Application Insights Extension.
It's also possible to add a website extension using the website blade.


From_portal_2015-03-01_2101


This will gives you a lot of information already, but to add more customs metric in .Net by sure to add the Application Insights SDK to your porject. In node.js use the applicationinsights package from npm,

~Frank Boucher



References




Reading Notes #85

Suggestion of the week

Cloud

Databases

Integration

Programming

Miscellaneous

~Frank