# `Zexbox.Metrics.Client`
[🔗](https://github.com/Intellection/zexbox/blob/main/lib/zexbox/metrics/client.ex#L1)

A client module for writing metrics to InfluxDB.

## Overview

This module provides functions to write metrics to InfluxDB, a time-series database that allows for efficient storage and querying of data with a timestamp.

To use this module, you need to have the `Zexbox.Metrics.Connection` application running. Make sure to start it as an application in your supervision tree:

```elixir
def start(_type, _args) do
  children = [
    {Zexbox.Metrics.Connection, []}
  ]
  Supervisor.start_link(children, strategy: :one_for_one)
end
```

## Writing Metrics
The main function provided by this module is write_metric/1. It accepts a metric data structure and writes it to InfluxDB.

## Examples

### Writing Custom metrics

```elixir
iex> metric = %Zexbox.Metrics.Series.Generic{
...>   measurement: "my_measurement",
...>   fields: %{
...>     "my_field" => 1
...>   },
...>   tags: %{
...>     "my_tag" => "my_value"
...>   }
...> }
iex> Zexbox.Metrics.Client.write_metric(metric)
{:ok, %Zexbox.Metrics.Series.Generic{
  measurement: "my_measurement",
  fields: %{
    "my_field" => 1
  },
  tags: %{
    "my_tag" => "my_value"
  }
}}
```

### Error Handling
If there is an error while writing the metric, the function will log the error using the Logger module without crashing the process,

# `series`

```elixir
@type series() :: Zexbox.Metrics.ControllerSeries.t() | Zexbox.Metrics.Series.t()
```

# `write_metric`

```elixir
@spec write_metric(series()) :: tuple()
```

Write a metric to InfluxDB.

Skips writing (and returns `{:ok, metrics}`) when metrics are disabled for
the current process or for any process in its caller chain (e.g. a request
that called `Zexbox.Metrics.disable_for_process/0`).

## Examples

    iex> Zexbox.Metrics.Client.write_metric(%ControllerSeries{})
    {:ok, %ControllerSeries{}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
