README.md

# ExMetrics

Another Elixir metrics library, ExMetrics is a thin wrapper built on top of
[`statix`](https://github.com/lexmag/statix) providing convenient macros
for timing function calls.

## Installation

```elixir
def deps do
  [
    {:ex_metrics, "~> 0.1.0"}
  ]
end
```

## Configuration

`ex_metrics` supports the following global configuration options:

* `prefix`: A prefix for all metric names, default `nil`
* `host`: The StatsD host, default `127.0.0.1`
* `port`: The port to send metrics, default `8125`
* `tags`: Global tags to be sent with all metrics, default is none.

## Usage

### Metrics Collection

`ex_metrics` exposes the following functions for metrics collection (the same as `statix`):

* `ExMetrics.decrement/1,2,3`
* `ExMetrics.gauge/2,3`
* `ExMetrics.histogram/2,3`
* `ExMetrics.increment/1,2,3`
* `ExMetrics.measure/2,3`
* `ExMetrics.set/2,3`
* `ExMetrics.timing/2,3`

### `deftimed` Macro

Functions can be timed easily with the `deftimed` macro:

```elixir
defmodule MyApp do
  use ExMetrics.FunctionTimer

  deftimed foo do
    # Times foo/0 function and sends metric name: "function_call.elixir.myapp.foo_0"
  end

  @metric_name "custom_metric_name"
  @metric_options [tags: [key: :value]]
  deftimed bar do
    # Times bar/0 function and sends metric with name @metric_name and options @metric_options
  end

  # Metric names and options can be configured on a per-function-header level.
  @metric_name "baz_value1"
  deftimed baz(:value1) do
  end

  @metric_name "baz_value2"
  deftimed baz(:value2) do
  end

  @metric_name "baz_default"
  deftimed baz(value) do
  end
end
```

### Plug

Response times can be automatically captured and reported with the `ExMetrics.Plug`:

```elixir
defmodule MyRouter do
  use Plug.Router

  plug(:accepts, ["json", "urlencoded"])
  plug(ExMetrics.Plug)

  get("/", MyController, :action)                   # Metric name: response_time.root
  get("/v2/users/:id/edit", UsersController, :edit) # Metric name: response_time.v2.users.id.edit
end
```

## Development

### Testing

Unit tests with a code coverage report generated by Coveralls can be run with

```
$ mix coveralls.html
```

### Linting

The linter can be run with

```
$ mix credo
```

### Static Analysis

Static type analysis can be run with

```
$ mix dialyzer
```