README.md

# StatusCodeTracker

A library for tracking HTTP status code rates and service health monitoring. It helps monitor the rate of 5xx error codes and flags the service as unhealthy when it reaches a configured threshold.

## Installation

Add `status_code_tracker` to your list of dependencies in `mix.exs`:

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

## Configuration

Configure the error threshold and time window in your application config:

```elixir
config :status_code_tracker, :settings,
  time_window_seconds: 60,  # default: 60 seconds
  error_threshold: 10,     # default: 10 errors
  keep_unhealthy?: true, ## default: false 
```

## Usage

1. You can add it to your router like this:
   ```elixir
    scope "/health" do
      get("/", StatusCodeTracker.HealthPlug, [json: true, body: "{\"status\":\"success\"}"])
    end
   ```
or you can add it to your endpoint like this:
```elixir
plug StatusCodeTracker.Plug, path: "/health"
```


## How it Works

The library uses an ETS table to store timestamps of 5xx errors. When a request results in a 5xx status code, the timestamp is recorded. The health check function counts how many errors occurred in the configured time window and compares it to the threshold.

A periodic cleanup process removes old timestamps to prevent memory growth.

## License

MIT License. See LICENSE file for details.