Composition Engine

Here at Charodic we use Librato and Graphite. Librato for several reasons. As you all know Librato can't compose metrics using functions like Graphite (ex.: Graph Data > Apply Function > Calculate > Ratio). So we built an engine to compose metrics based on existing ones. 
Metrics are sent to the composition engine before flushing it to backends. Compositions are calculate based on rules defined at a source file. For example: 

Rule:
```javascript
{
      name: "#0.ctr.#1",
      regexp:[
        /^([a-z]+)\.click\.([a-z]+)/,
        /^([a-z]+)\.view\.([a-z]+)/
      ],
      compose: function(click, view) {
        return 100 * (click / view);
      }
}
```

Source Metrics:
```javascript
{
    counters: {
        "site.click.page": 10,
        "site.view.page": 100
    }
}
````


The engine will store it matching metric to an array [10, 100] where 10 is the first matching regexp and 100 is the second matching regexp. After all matches in metrics the engine starting applying collected metrics to user-defined compose functions. So it will become:

```javascript
click = 10;
view = 100;
```

Applied to: 

```javascript
compose: function(click, view) {
    return 100 * (click / view);
}
```

Will result: 10

The output compose metric will be named like name attribute defined in the rule object. The sharps are back reference to the matching strings, so: 

#0 = site
#1 = page

Then, output metric will be:

site.ctr.page = 10

The final metrics object to be flushed to backends will look like:

```javascript
{
    counters: {
        "site.click.page": 10,
        "site.view.page": 100,
        "site.ctr.page": 100
    }
}
````

Cheers!