mailgun/lemma — pull request #2 by russjones (2014-12-05 02:37:20+00:00)
Title: Emit stats to statsd.

**Purpose**

A good way to look for malicious activity is successful and unsuccessful authentication/encryption events. Currently we have no way of doing this. This commit starts emitting these metrics to statsd. This way we can display it on a dashbaord and if we see a spike of unsuccessful authentication or encryption events we know someone may be trying to attack our system or something is going wrong on our end.

**Implementation**

* Before doing anything else, create a `metrics.Service` by calling `metrics.New` with the hostname and port of the statsd server. Optionally a prefix can be given to prefix all metrics. By deafult this is 'lemma.{hostname}'.
* If you don't call `metrics.New` you can still call the `EmitSuccess` and `EmitFailure` methods, but nothing will happen. This is done to keep code clean, so instead of a `if` check every time you want to emit a metric, you can just try to emit the metric. It will be sent if you have the service configured.
* Call `metrics.EmitSuccess` or `metrics.EmitFailure` whenever you wish to emit a metric. They are goroutine safe.
* Currently, the above two methods are called by `lemma.httpsign.auth.AuthenticateRequestWithKey` and `lemma.httpsign.secret.OpenWithKey`.

**Related Commits**
https://github.com/mailgun/pylemma/pull/3