Skip to main content

Druid request decoration

Pivot allows you to specify a hook to decorate Druid requests prior to sending them. This can be used to add custom non-standard headers if needed.

In the config, define a decorator by adding a key of decorators that is a named map pointing to a relative JavaScript file:

decorators:
testDecorator: './druid-request-decorator.js'

Then the contract is that your module should export a function druidRequestDecorator that has to return a decorator.

A decorator is a function that gets called on every request and receives a Druid query and may return an object with the key headers where you can set whatever headers you want.

Here is an example decorator:

exports.version = 1;

// logger - is just a collection of functions that you should use instead of console to have your logs included with the Pivot logs
// options - is an object with the following keys:
// * cluster: Cluster - the cluster object
exports.druidRequestDecoratorFactory = function(logger, params) {
var options = params.options;
var myUsername = options.myUsername; // pretend we store the username and password
var myPassword = options.myPassword; // in the config

if (!myUsername) throw new Error('must have username');
if (!myPassword) throw new Error('must have password');

logger.log('Decorator init for username: ' + myUsername);

var auth = 'Basic ' + Buffer.from(myUsername + ':' + myPassword).toString('base64');

// decoratorRequest: DecoratorRequest - is an object that has the following keys:
// * method: string - the method that is used (POST or GET)
// * url: string -
// * query: Druid.Query -
return function(decoratorRequest) {
var decoration = {
headers: {
Authorization: auth,
'X-I-Like': 'Koalas',
},
};

// This can also be async if instead of a value, a promise is returned.
return decoration;
};
};

Adding a decorator in the config it is also required to specify it by name in the connection configuration screen. For the given Druid connection enter testDecorator.

This would result in all Druid requests being tagged as:

decoration example