Forecast log costs pre-production
Tags: grafana, logs, datadog, logcosts
Published: 2023-09-10 14:42:40

Have you ever been burned by unexpected log costs in Grafana, Datadog, New Relic or GCP Cloud Logging?

I'm pretty sure it's quite common! In many engineering teams, log cost accounts for more than 10% of the total cloud spend.

This is insane! The most insane thing is that you first pay the bill and then you find the culprit. This doesn't sound like following proper software engineering practices.

That's why we're built [Cortisol](https://github.com/CortisolAI/cortisol)! It is an open-source command-line tool designed specifically for web services. It offers easy-to-use cost estimation and forecasting capabilities tailored to main observability tools like Datadog, New Relic, Grafana and GCP Cloud Logging. 

Cortisol assists users in planning and optimizing their log costs before deploying their web services. It operates on a foundation inspired by Locust, allowing users to define user behavior using a regular Python script 💰📉.

Let's get started then!

## Installation

### Prerequisities

Cortisol requires the following one of the following Python versions: 3.8, 3.9, 3.10 or 3.11

### Install Cortisol

At the command line:

    pip install cortisol

If you have an Apple M1 CPU, we suggest installing using Poetry as a dependency management. Otherwise, the underline gevent library may not work.

## Your first log cost estimation

Let's dive right in and get our hands dirty with Cortisol! As an integral part of your software development workflow, Cortisol CLI brings predictability to managing log costs. 

First things first! We need a RESTful service and so you'll need to do the following steps:

1. Clone this example repo https://github.com/CortisolAI/getting-started-example
2. `cd getting-started-example`
3. `mkvirtualenv getting-started-cortisol`
4. `python -m app.main` which will make the service available at `http://127.0.0.1:8080/`

And, now, it's time to create your first cortisol file. Copy and paste the following in a file named `cortisolfile.py` in the root path of getting-started-example repo:

```python
from locust import task

from cortisol.cortisollib.users import CortisolHttpUser


class WebsiteUser(CortisolHttpUser):
    @task
    def my_task(self):
        self.client.get("/")

```

Here we define a class for the users that we will be simulating. It must always inherit from `CortisolHttpUser <cortisollib.users.CortisolHttpUser>`.

We've declared 1 task by decorating two methods with ``@task``. The `my_task` method calls the root path of the restful service that just returns a simple JSON response.

You can define multiple tasks for each resource of your web service.

Almost there! It's time to run the cortisol command and get your first log cost estimates. Before we do that, make sure you know the root path of where the getting-started-example repo is located. For illustration purposes, let's assume the path is `/some/path/getting-started-example/`.

Switch to another terminal window, enable the `getting-started-cortisol` virtual env and run the following command in the terminal:

```terminal
cortisol logs cost-estimate --host http://127.0.0.1:8080 --users 10 --spawn-rate 5 --run-time 10s --cortisol-file cortisolfile.py --log-file cortisol_app.log
```

You'll get some results after 10 seconds that look like these ones:

![Results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88m4dii1cpwglp88vwge.png)

Before we dive into the results, let's understand what load testing ran in the background in order to receive the latter log cost estimates.

- `--host http://127.0.0.1:8080` the FAST API runs at `http://127.0.0.1:8080`
- `--users 10` The peak number of concurrent users is 10
- `--spawn-rate 5` Spawn 5 users per second
- `--run-time 10s` Stop after 10 seconds
- `--cortisol-file cortisolfile.py` path to cortisolfile
- `--log-file cortisol_app.log` path to where logs are saved

Let's get back to the results. The total log volume per month of running this FAST API with the defined user behaviour in the cortisolfile and in the cortisol command arguments is going to be available at the top of the table. The log costs per observability tool are per month and are explained below:

- Datadog: 30 day log retention and billed annually
- Grafana: Cloud Pro plan is chosen
- New Relic: Pro plan is chosen
- GCP Cloud Logging: based on the pricing on their website

Please, note that free tiers have been included to all the costs above