Greg Shackles: Easily Instrument HTTP Calls in Your Apps

Lately I’ve been doing a lot of work around adding instrumentation to basically all the things, which naturally includes my apps. It’s common practice to instrument things on the server-side of the API using awesome tools like NewRelic, but that’s only part of the equation. Obviously we need the server to respond as quickly as possible so being aware of that metric is essential, but on top of that the user also has to wait for that request to get to the server and back over networks that are often pretty slow. How long are you making users watch a loading spinner for when they’re trying to get something done?

Depending on what your apps are doing it probably makes sense to instrument more than just HTTP calls to be able to get some visibility into this, but I’ll just start with HTTP here. When I was spiking some ideas on how to implement this I went down a few different paths before I realized I was overlooking the most obvious/simple solution that we’ll go through here.

HttpClient and HttpMessageHandler

If you’re using HttpClient to make your HTTP calls (if you’re not, you probably should be!), you may or may not know that it has a constructor that takes in a HttpMessageHandler instance. The awesome part about that is that it means we can create a simple class that will be inserted into the pipeline of every HTTP call being made from that HttpClient instance.

Here’s what that a simple implementation might look like:

class InstrumentedHttpClientHandler : HttpClientHandler  
  protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    var successful = false;
    var stopwatch = Stopwatch.StartNew();

      var result = await base.SendAsync(request, cancellationToken);
      successful = result.IsSuccessStatusCode;

      return result;

      // TODO: publish metric

This implementation simply wraps the base implementation of SendAsync with a stopwatch so the behavior will be the same except that now you’ll get access to how long the call took via stopwatch.ElapsedMilliseconds, and whether or not it was successful. One thing to be careful of when adding instrumentation is that you don’t want to slow things down any more than necessary in order to measure it. In this case there’s minimal overhead, especially when dealing with network calls that will reliably take a decent chunk of time anyway.

With this data you can now publish your metrics in whatever way makes sense for your application, be it to the device logs, an API, etc. Not only is it useful to know how you’re performing right now, it’s even more important to know how you’re trending over time? Did your last release make things better or worse? Without tracking it you’ll never know.


If you’re using ModernHttpClient (again, if you’re not, you should be!) you may have noticed that this is the same approach used by that library to intercept requests happening in HttpClient. You can still take advantage of this instrumentation approach if you’re using ModernHttpClient. In fact, all you need to do is change the base class of InstrumentedHttpClientHandler to ModernHttpClient’s NativeMessageHandler and you’re all set!

Adam J Wolf: Weekly Xamarin Newsletter Issue #67

Get Started with Xamarin 4 Today Bryan Costanich, from Xamarin Inc., invites you to get up to speed on Xamarin 4.0. Introducing Xamarin 4: Everything You Need to Build Great Apps Nat Friedman, from Xamarin Inc., announces Xamarin 4. Sort ListView Alphabetically in Xamarin Android Val Okafor shows his LINQ powers in this ListView demo. […]

The post Weekly Xamarin Newsletter Issue #67 appeared first on Syntax is my UI.

Xamarin: Get Started with Xamarin 4 Today

Xamarin 4 is a fantastic release, designed to make building apps with Xamarin faster, easier, and more delightful than ever before, and the Xamarin Education team has worked incredibly hard to deliver the resources you need to get up and running with it. In both the documentation and Xamarin University teams, we have created a […]

The post Get Started with Xamarin 4 Today appeared first on Xamarin Blog.

Greg Shackles: Pluralsight: Beginning Automated Testing of Xamarin Applications

Recently I had the pleasure of working with my friend Jesse Liberty on a new Pluralsight course focused on helping you get started building Xamarin apps in a test-driven fashion. In under an hour he walks you through building out a real app using tests right from the start, running those tests on both iOS and Android, and also getting into writing UI tests to run locally as well as in Test Cloud.

If you’re looking for a good way to see testing in action with mobile development, please check it out!

Beginning Automated Testing of Xamarin Applications

Xamarin: Xamarin and Microsoft Create the New Standard for Enterprise Mobility

Today and tomorrow, Microsoft is hosting their annual Visual Studio Connect(); event and we were honored to join with them in the announcement of an expansion of our partnership to include the tools, services, training, and workflow that organizations need to go mobile quickly. During the live streamed event, Microsoft executives and technical experts present […]

The post Xamarin and Microsoft Create the New Standard for Enterprise Mobility appeared first on Xamarin Blog.

Xamarin: Introducing Xamarin 4: Everything You Need to Build Great Apps

Today, we are extremely excited to introduce Xamarin 4, a complete mobile solution to help you build amazing mobile apps from start to finish. Xamarin 4 is packed full of major enhancements to the Xamarin Platform and Xamarin Test Cloud, but today we’re also proud to announce the general availability of our real-time app monitoring […]

The post Introducing Xamarin 4: Everything You Need to Build Great Apps appeared first on Xamarin Blog.

Adam J Wolf: Weekly Xamarin Newsletter Issue #66

MvvmLight Navigation Extension Daniel Hindrikes adds a modal navigation extension to MvvmLight. Building iBeacon apps using Xamarin.iOS Video Tutorial Jim Bennett launches his new series on YouTube about iBeacons. Announcing Xamarin’s New Elite Consulting Partners! Greg Urquhart, from Xamarin Inc., announces Xamarin’s new consulting partner program. The ViewModel (Day 4) Adam Pedley adds another post […]

The post Weekly Xamarin Newsletter Issue #66 appeared first on Syntax is my UI.