HTTP Decision Graph Comes to Life

Published Friday, March 20, 2009 by Bryan

Sometime last year, a flowchart describing the processing of an http request (as rendered by Alan Dean) made its way around the net.

I thought, "Wouldn't it be cool if your webserver could plot the path it took on that chart in handling a request?"

Well, now it can - if you're using the latest Webmachine. The above graph is scaled down from a tool I created that is packaged with the just-released Webmachine 1.0.

You can read all about the tool on the Webmachine Debugging page, but the basic idea is: draw an edge between each node in the graph that is traversed during the processing of a request. Clicking on decisions brings up a panel that details what resource module functions were called at that decision (as well as what their parameters and return values were). There are also panels for the details about the request itself (method, path, headers, body) and the response (code, headers, body).

I've put up three example traces from BeerRiot's beer_resource:

  1. The first hits /beer/1, and runs successfully all the way to 200: trace-example-200.html
  2. The second hits /beer/9999, which doesn't exist, and runs to 404: trace-example-404.html
  3. The third hits /beer/536, which used to exist, but has since been merged with beer 202, so it runs to 301: trace-example-301.html

Update: The above examples have been lost to history. But, you can still find live examples as part of a new post I wrote in 2017.

You're floored, right? You just can't wait to get your hands on it, right? </modesty> Well, doing so is easy. Once you have your Webmachine resource written, just change your init/0 function from:

init(Config) ->
   {ok, Config}.

to:

init(Config) ->
   {{trace, "/tmp"}, Config}.

Recompile and reload the module, then issue an HTTP request to it. You should see a file show up in /tmp with the extension .wmtrace.

Now open the Erlang shell for your webmachine application and type:

wmtrace_resource:add_dispatch_rule("wmtrace", "/tmp").

Tab over to your browser and hit /wmtrace/. You should see the list of .wmtrace files in your /tmp directory. Click on one of them, and you'll be in the trace inspection utility.

For any pre-webmachine-1.0 users, getting access to this utility requires converting your resources to the new webmachine with referential transparency, but I can tell you from experience that that process is largely mechanical, and not that time consuming. I translated BeerRiot's ~7000loc in about 2 hours (including testing).

I'd love to hear feedback about the trace tool. It's the product of about three days of hacking (one proof-of-concept, one nearly-complete-rewrite, one actual-improvement), so I'm not hard-set on much of anything.