Skip to main content

Request Transformation

Request Transformation gives you the ability to rewrite your requests before they are forwarded to your service instances. You can provide custom logic written in Javascript to transform headers, method, uri, and even the body of your request before it is forwarded by Diffy to your service instances.

Injection Points

There are 4 places - all, primary, secondary, candidate - in the Diffy topology where request transformation can happen. We refer to these as Injection Points. We can access these injection points by clicking on the JS button in the Diffy UI as highlighted button in the image below. Request Transformation We can now select an injection point by clicking on the corresponding node in the transformation dialog and write the corresponding transformation in the text area.

All

The tranformation logic written for all is applied to all requests before being multicast to primary, secondary, and candidate.

Primary, Secondary, Candidate

The transformation written for primary, secondary, or candidate will only be applied to the copy of the request being sent to that specific service instance. i.e. The transformation written for candidate will only tranform the traffic sent to the candidate instance and will not affect the traffic sent to primary and secondary in any way.

Grammar

The transformation is written as a self-contained Javascript function that takes a request object as its input and returns a request object as its output. The Javascript environment for request transformations does not allow access to external libraries to imported via require or import. The same rule applies to all external dependencies including network and filesystem. The sturcture of a request object is demonstrated in the following example transformation:

(request) => {
const {
uri, // String like - '/users/321/name?includeLastName=true'
method, // String like - 'GET', 'POST'
headers, // Json object with string values like - { "auth_token" : "SOME***SECRECT***TOKEN" }
body, // String like - 'hello world', '{ "hello" : "world"}', '<html>hello world</html>'
routingMode // (experimental) String like - 'all', 'primary', 'secondary', 'candidate', 'none'
} = request;

return {
uri,
method,
headers,
body,
routingMode
}
}

The above example is a noop transformation that returns an unmodified request.

Experimental

Please note that the following features may be dropped or modified at any time without warning.

routingMode

Specifying a routingMode as one of 'all', 'primary', 'secondary', 'candidate', or 'none' controls the per-request routing behaviour in the following manner: 'all' - this is the default behavior. The request is routed to 'primary', 'secondary', and 'candidate' and the responses are analyzed and added to the report. 'primary' - the request is only routed to 'primary' and the response is returned to the client. There is no analysis performed on the response. 'secondary' - same as 'primary' with the only difference being that the request is routed to 'secondary'. 'candidate' - same as 'primary' with the only difference being that the request is routed to 'candidate'. 'none' - the request is dropped and a 200 OK empty response is returned by Diffy.

For a detailed discussion on the motivation for Request Transformation, please refere to this blog post.