# Metrics optimizer API

## Overview

The **Metrics Optimizer API** allows you to drop irrelevant metrics even after ingestion. This API can be used to block or unblock specific metrics and retrieve a list of blocked metrics.

## Authentication

To authenticate requests, create a [customized API key](https://coralogixstg.wpengine.com/docs/api-keys/) with the **Metrics TCOPolicies** role preset assigned to it. Alternatively, selectively add the following permissions to your key:

| Description                   | Action                       |
| ----------------------------- | ---------------------------- |
| `View metrics TCO policies`   | `METRICS.TCO:READ`           |
| `Manage metrics TCO policies` | `METRICS.TCO:UPDATEPOLICIES` |

Find out more about customized API keys [here](https://coralogixstg.wpengine.com/docs/api-keys/).

### Using `grpcurl`

Use your customized API key in the Authorization request header to successfully connect.

```text
grpcurl -H "Authorization: Bearer API_KEY_HERE"
```

Then select the endpoint associated with your Coralogix [domain](https://coralogix.com/docs/user-guides/account-management/account-settings/coralogix-domain/index.md).

```text
-d @ api.[[DOMAIN_VALUE]]:443
```

For the Metrics Cost Optimizer API, the service name is `MetricsBlockingRulesService`.

```text
com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService
```

The complete request should look like this:

```text
grpcurl -H "Authorization: Bearer API_KEY_HERE" \\
    -d <parameters>
    api.[[DOMAIN_VALUE]]:443 \\
    com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.<method>
```

## Supported APIs

| API                                                                                | Description                             |
| ---------------------------------------------------------------------------------- | --------------------------------------- |
| com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.**Block** | Blocks the specified metric             |
| com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.**List**  | Retrieves a list of all blocked metrics |
| com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.**Allow** | Unblocks (allows) the specified metric  |

## Examples

The following section presents an array of sample requests.

### Block a metric by name

This method blocks the specified metric. You can block a single metric or multiple metrics at once. To send multiple metrics by name send a array with comma separated metric names. For a single metric, using an array is optional.

**Example: Blocking a single metric**

```json
{
  "ruleExpressions": {
    "byMetricName": {
      "name": "block_metric"
    }
  }
}
```

**Example: Blocking multiple metrics**

```json
{
  "ruleExpressions": [{
    "byMetricName": {
      "name": "block_metric1"
    },
    "byMetricName": {
      "name": "block_metric2"
    },
    "byMetricName": {
      "name": "block_metric3"
    }
  }]
}
```

**Example: API call**

```json
grpcurl -H "Authorization: <..>" \
    -d '{
            "ruleExpression":[{
                "byMetricName":{
                    "name":"blocked_metric"
                 }
             }]
        }' \
    api.[[DOMAIN_VALUE]]:443 \
    com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.Block
```

### List blocked metrics

Retrieve a list of all blocked metrics, including their ruleId, expressions, and metric names. This method can be used to identify metrics that need to be unblocked.

**Sample API call**

```json
grpcurl -H "Authorization: <..>" api.[[DOMAIN_VALUE]]:443 \
    com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.List
```

**Sample response**

```json
{
  "rules": [
    {
      "ruleId": "3",
      "ruleExpression": {
        "byMetricName": {
          "name": "code:prometheus_http_requests_total:sum1"
        }
      }
    },
    {
      "ruleId": "4",
      "ruleExpression": {
        "byMetricName": {
          "name": "code:prometheus_http_requests_total:sum2"
        }
      }
    },
    {
      "ruleId": "6",
      "ruleExpression": {
        "byMetricName": {
          "name": "test_metric"
        }
      }
    },
    {
      "ruleId": "36",
      "ruleExpression": {
        "byMetricName": {
          "name": "go_gc_duration_seconds_count"
        }
      }
    },
    {
      "ruleId": "37",
      "ruleExpression": {
        "byMetricName": {
          "name": "go_gc_duration_seconds_sum"
        }
      }
    },
    {
      "ruleId": "39",
      "ruleExpression": {
        "byMetricName": {
          "name": "blocked_metric1"
        }
      }
    }
  ]
}
```

### Unblock a metric with `ruleId`

This method removes a blocking rule, unblocking the specified metric.

You can unblock a single metric or multiple metrics at once. To send multiple metrics by ruleId, send an array with comma-separated ruleIds. For a single metric, using an array is optional.

**Example: Allowing a single metric**

```json
{
  -d '{"ruleIds":39}'
}
```

**Example: Allowing multiple metrics**

```json
{
  -d '{"ruleIds":[3,4,6,36]}
}
```

**Example: API call**

```json
grpcurl -H "Authorization: <..>" \
    -d '{"ruleIds":[36]} \
    api.[[DOMAIN_VALUE]]:443 \
    com.coralogix.metrics.metrics_blocking_rules.MetricsBlockingRulesService.Allow
```

## **Response handling**

The APIs return standardized responses, including the following error codes:

| **Error Code** | **Description**                      | **Method**         |
| -------------- | ------------------------------------ | ------------------ |
| 200            | Success                              | All Methods        |
| 500            | Internal Server Error                | All Methods        |
| 401            | Unauthorized                         | All Methods        |
| 403            | Forbidden                            | All Methods        |
| 404            | Metric or Rule ID not found          | Allow Method       |
| 422            | Invalid Rule Expression or ID Format | Block/Allow Method |
| 409            | Metric Already Blocked               | Block Method       |
| 400            | Bad Request                          | Block Method       |

## Support

**Need help?**

Our world-class customer success team is available 24/7 to walk you through your setup and answer any questions that may come up.

Feel free to reach out to us **via our in-app chat** or by sending us an email to [support@coralogix.com](mailto:support@coralogix.com).
