How to add custom metrics in Fleio

If you want to be able to bill custom metrics that are sent over by gnocchi you will have to add a new billing resource in the django admin.

First of all you will have to enable django admin. Please see Django admin in order to enable the django-admin interface.

After that, you will have to login in the django admin and access the billing resources table (it should show in the billing app sub-menu).

From top-right corner you will have to click on the “ADD BILLING RESOURCE” button.

Configure as the following:

Display name: This is the resource type. Our example will be CUSTOM
Type: metric
Name: custom_1 (should be unique)
Definition:
{
  "metrics":[
    {
      "name":"custom-metric-1",
      "aggregation":"sum",
      "granularity":300,
      "reaggregation":"sum"
    },
    {
      "name":"custom-metric-2",
      "aggregation":"sum",
      "granularity":300,
      "reaggregation":"sum"
    }
  ]
}

After this, you will have to add a new dictionary in the settings.py file in order to configure the display names for your new custom metrics:

METRICS_DISPLAY_NAME =  {
    'custom-metric-1': 'Custom metric 1',
    'custom-metric-2': 'Custom metric 2'
}

After editing the settings.py file, you must restart all the fleio services (see Restarting Fleio).

If everything was done correctly, you should be able to add pricing rules based on the resource type that you previously defined.

Custom metrics based on external usage

In order to have pricing rules based on external usage (collected 3rd party apps), you will first have to add the data in Gnocchi.

The manual steps are the following. You will have to automate this using your preferred method. The following information must be sent to all regions that you’re using.

  1. Create the resource in gnocchi.

For this you will need two things:

  • resource type: example: custom_1

  • project ID: taken from Fleio using the api (/staffapi/openstack/projects)

Then use the gnocchi resource create command. In our example, the resource name will be custom_1, and we will use the resource type custom_1 and project_id project_1:

gnocchi resource create --type custom_1 -a project_id:project_1 custom_1
+-----------------------+-------------------------------------------------------------------+
| Field                 | Value                                                             |
+-----------------------+-------------------------------------------------------------------+
| created_by_project_id | c0beaea4c2b94c68b42f2f43e68e49e5                                  |
| created_by_user_id    | 2b107eeb74224a17a7fe108384c4c044                                  |
| creator               | 2b107eeb74224a17a7fe108384c4c044:c0beaea4c2b94c68b42f2f43e68e49e5 |
| ended_at              | None                                                              |
| id                    | 03580e05-031f-587c-b599-ad67d3e9838f                              |
| metrics               |                                                                   |
| original_resource_id  | custom_1                                                          |
| project_id            | project_1                                                         |
| revision_end          | None                                                              |
| revision_start        | 2019-10-17T09:21:54.396570+00:00                                  |
| started_at            | 2019-10-17T09:21:54.396552+00:00                                  |
| type                  | custom_1                                                          |
| user_id               | None                                                              |
+-----------------------+-------------------------------------------------------------------+
  1. Create metrics for your resource:

gnocchi metric create --resource-id custom_1 custom-metric-1
+---------------------+-------------------------------------------------------------------+
| Field               | Value                                                             |
+---------------------+-------------------------------------------------------------------+
| archive_policy/name | fleio_policy                                                      |
| creator             | 2b107eeb74224a17a7fe108384c4c044:c0beaea4c2b94c68b42f2f43e68e49e5 |
| id                  | e6bdbf4e-af88-4503-8aeb-d455819b7eb0                              |
| name                | custom-metric-1                                                   |
| resource_id         | 03580e05-031f-587c-b599-ad67d3e9838f                              |
| unit                | None                                                              |
+---------------------+-------------------------------------------------------------------+

2.1 Check the resource:

gnocchi resource show 03580e05-031f-587c-b599-ad67d3e9838f
+-----------------------+-------------------------------------------------------------------+
| Field                 | Value                                                             |
+-----------------------+-------------------------------------------------------------------+
| created_by_project_id | c0beaea4c2b94c68b42f2f43e68e49e5                                  |
| created_by_user_id    | 2b107eeb74224a17a7fe108384c4c044                                  |
| creator               | 2b107eeb74224a17a7fe108384c4c044:c0beaea4c2b94c68b42f2f43e68e49e5 |
| ended_at              | None                                                              |
| id                    | 03580e05-031f-587c-b599-ad67d3e9838f                              |
| metrics               | custom-metric-1: e6bdbf4e-af88-4503-8aeb-d455819b7eb0             |
| original_resource_id  | custom_1                                                          |
| project_id            | project_1                                                         |
| revision_end          | None                                                              |
| revision_start        | 2019-10-17T09:21:54.396570+00:00                                  |
| started_at            | 2019-10-17T09:21:54.396552+00:00                                  |
| type                  | custom_1                                                          |
| user_id               | None                                                              |
+-----------------------+-------------------------------------------------------------------+

As you can see it has the correct project_id, and the correct metrics.

  1. Add the measures to your previously created metric (metric_1)

gnocchi measures add -m 2019-10-17T11:00:00+03:00@100.0 e6bdbf4e-af88-4503-8aeb-d455819b7eb0

3.1 Check the added measures using gnocchi measures show:

gnocchi measures show e6bdbf4e-af88-4503-8aeb-d455819b7eb0
+---------------------------+-------------+-------+
| timestamp                 | granularity | value |
+---------------------------+-------------+-------+
| 2019-10-17T11:00:00+03:00 |      3600.0 | 100.0 |
| 2019-10-17T11:00:00+03:00 |      1800.0 | 100.0 |
| 2019-10-17T11:00:00+03:00 |       300.0 | 100.0 |
| 2019-10-17T11:10:00+03:00 |       300.0 | 100.0 |
+---------------------------+-------------+-------+

After that please follow our guide described in the first part of the page. Please see How to add custom metrics in Fleio

I have used the same names / metrics as in the guide, so it will be easier for you to understand the workflow. Project IDs can be obtained by using the fleio api. The general workflow to get the project id is the following:

{{ your-fleio-backend-url }}/staffapi/clients - get the client id
{{ your-fleio-backend-url }}/staffapi/billing/services - get the service id based on client id
{{ your-fleio-backend-url }}/staffapi/openstack/projects - get the project id based on service id