TrafficScript is Traffic Manager's scripting and configuration language that lets you specify precisely how Traffic Manager must handle each type of request, in as much detail as you need.
Without TrafficScript, you would have to configure your load balancer with a single, ‘lowest-common-denominator’ policy that describes how to handle all your network traffic. With TrafficScript, you control how Traffic Manager handles your traffic, inspecting and modifying each request and response as you wish, and pulling in each of Traffic Manager's features as you require.
TrafficScript is a high-level programming language used to create ‘rules’ which are invoked by the traffic manager each time a transaction request or response is received:
TrafficScript rules have full access to all request and response data, and give the you full control over how end users interact with the load balanced services. They are commonly used to selectively enable particular Traffic Manager features (for example, bandwidth control, caching or security policies) and to modify request and response data to handle error cases or augment web page data.
Although TrafficScript is a new language, the syntax is intentionally familiar. It is deeply integrated with the traffic management kernel for two reasons:
You can use TrafficScript to create a wide range of solutions and the familiar syntax means that complex code can be prototyped and deployed rapidly.
Example 1 - Modifying Requests
# Is this request a video download? $url = http.getPath(); if( string.wildmatch( $url, "/videos/*.flv" ) ) { # Rewrite the request to target an f4v container, not flv $url = string.replace( $url, ".flv", ".f4v" ); http.setPath( $url ); # We encode flash videos at 1088 Kbits max. Apply a limit to 2 Gbit # to control download tools and other greedy clients response.setBandwidthClass( "Videos 2Mbits" ); # We don't want to cache the response in the Stingray cache, even if # the HTTP headers state that it is cacheable http.cache.disable(); }
A simple request rule that modifies the request and instructs the traffic manager to apply bandwidth and cache customizations
TrafficScript's close integration with the traffic management kernel makes it as easy to rewrite HTTP responses as HTTP requests:
Example 2 - Modifying Responses
$url = http.getResponseHeader( "Content-Type" ); if( !string.startsWith( $url, "text/html" ) ) break; $response = http.getResponseBody(); $response = string.replaceAll( $response, "http://intranet.mycorp.com/", "https://extranet.mycorp.com/" ); http.setResponseBody( $response );
A response rule that makes a simple replacement to change links embedded in HTTP responses
TrafficScript can invoke external systems in a synchronous or asynchronous fashion:
These capabilities allow the Traffic Manager to interface with external systems to retrieve data, verify credentials or initiate external control-plane actions.
Example 3 - Accessing an external source (Google News)
$type = http.getResponseHeader( "Content-Type" ); if( $type != "text/html" ) break; # Stop processing this rule $res = http.request.get( "https://ajax.googleapis.com/ajax/services/search/news?". "v=1.0&q=Riverbed" ); $r = json.deserialize( $res ); $rs = $r['responseData']['results']; $html .= "<ul>\n"; foreach( $e in $rs ) { $html .= '<li>' . '<a href="'.$e['unescapedUrl'].'">'.$e['titleNoFormatting'].'</a>'. '</li>'; } $html .= "</ul>\n"; $body = http.getResponseBody(); $body = string.replace( $body, "<!--RESULTS-->", $html); http.setResponseBody( $body );
An advanced response rule that queries an external datasource and inserts additional data into the web page response
TrafficScript rules may also invoke Java Extensions. Extensions may be written in any language that targets the JVM, such as Python or Ruby as well as Java. They allow developers to use third-party code libraries and to write sophisticated rules that maintain long-term state or perform complex calculations.
Getting started with RuleBuilder
The full TrafficScript language gives you access to over 200 functions, with the support of a proper programming language - variables, tests, loops and other flow control. You can write TrafficScript rules much as you'd write Perl scripts (or Python, JavaScript, Ruby, etc).
The RuleBuilder gives you a UI that lets you configure tests, and actions which are executed if one-of or all-of the tests are satisfied. The tests and actions you can use are predefined, and cover a subset of the full functions of TrafficScript. You can use the RuleBuilder much as you'd use the filtering rules in your email client.
RuleBuilder provides a simple way to create basic policies to control Traffic Manager
If you're not familiar with programming languages, then RuleBuilder is a great way to get started. You can create simple policies to control Traffic Manager's operation, and then, with one click, transform them into the equivalent TrafficScript rule so that you can learn the syntax and extend them as required. There's a good example to start with in the Stop hot-linking and bandwidth theft! article.