docs/sources/as-code/infrastructure-as-code/terraform/terraform-knowledge-graph/thresholds.md
Threshold configurations in Knowledge Graph allow you to define custom thresholds for request, resource, and health assertions. These configurations help you set specific limits and conditions for monitoring your services and infrastructure.
For information about managing thresholds in the Knowledge Graph UI, refer to Manage thresholds.
Create a file named thresholds.tf and add the following:
# Basic threshold configuration with all three types
resource "grafana_asserts_thresholds" "basic" {
provider = grafana.asserts
request_thresholds = [{
entity_name = "payment-service"
assertion_name = "ErrorRatioBreach"
request_type = "inbound"
request_context = "/charge"
value = 0.01
}]
resource_thresholds = [{
assertion_name = "Saturation"
resource_type = "container"
container_name = "worker"
source = "metrics"
severity = "warning"
value = 75
}]
health_thresholds = [{
assertion_name = "ServiceDown"
expression = "up < 1"
entity_type = "Service"
}]
}
Configure thresholds for different service request types and contexts:
# Multiple request thresholds for different services
resource "grafana_asserts_thresholds" "request_thresholds" {
provider = grafana.asserts
request_thresholds = [
{
entity_name = "api-service"
assertion_name = "ErrorRatioBreach"
request_type = "inbound"
request_context = "/api/v1/users"
value = 0.02
},
{
entity_name = "api-service"
assertion_name = "LatencyP99ErrorBuildup"
request_type = "inbound"
request_context = "/api/v1/orders"
value = 500
},
{
entity_name = "payment-gateway"
assertion_name = "RequestRateAnomaly"
request_type = "outbound"
request_context = "/payment/process"
value = 1000
}
]
}
Define resource thresholds for different severity levels:
# Resource thresholds for different severity levels
resource "grafana_asserts_thresholds" "resource_thresholds" {
provider = grafana.asserts
resource_thresholds = [
{
assertion_name = "Saturation"
resource_type = "container"
container_name = "web-server"
source = "metrics"
severity = "warning"
value = 75
},
{
assertion_name = "Saturation"
resource_type = "container"
container_name = "web-server"
source = "metrics"
severity = "critical"
value = 90
},
{
assertion_name = "ResourceRateBreach"
resource_type = "Pod"
container_name = "database"
source = "logs"
severity = "warning"
value = 80
}
]
}
Configure health checks with Prometheus expressions:
# Health thresholds with Prometheus expressions
resource "grafana_asserts_thresholds" "health_thresholds" {
provider = grafana.asserts
health_thresholds = [
{
assertion_name = "ServiceDown"
expression = "up{job=\"api-service\"} < 1"
entity_type = "Service"
},
{
assertion_name = "HighMemoryUsage"
expression = "memory_usage_percent > 85"
entity_type = "Service"
},
{
assertion_name = "DatabaseConnectivity"
expression = "db_connection_pool_active / db_connection_pool_max > 0.9"
entity_type = "Service"
}
]
}
Define comprehensive thresholds for production environments:
# Production environment with comprehensive thresholds
resource "grafana_asserts_thresholds" "production" {
provider = grafana.asserts
request_thresholds = [
{
entity_name = "frontend"
assertion_name = "ErrorRatioBreach"
request_type = "inbound"
request_context = "/"
value = 0.005
},
{
entity_name = "backend-api"
assertion_name = "LatencyP99ErrorBuildup"
request_type = "inbound"
request_context = "/api"
value = 200
}
]
resource_thresholds = [
{
assertion_name = "Saturation"
resource_type = "container"
container_name = "frontend"
source = "metrics"
severity = "warning"
value = 70
},
{
assertion_name = "Saturation"
resource_type = "container"
container_name = "backend-api"
source = "metrics"
severity = "critical"
value = 85
}
]
health_thresholds = [
{
assertion_name = "ServiceDown"
expression = "up < 1"
entity_type = "Service"
},
{
assertion_name = "NodeDown"
expression = "up{job=\"node-exporter\"} < 1"
entity_type = "Service"
}
]
}
grafana_asserts_thresholdsManage Knowledge Graph threshold configurations through the Grafana API. This resource allows you to define custom thresholds for request, resource, and health assertions.
| Name | Type | Required | Description |
|---|---|---|---|
request_thresholds | list(object) | No | List of request threshold configurations. Refer to request thresholds block for details. |
resource_thresholds | list(object) | No | List of resource threshold configurations. Refer to resource thresholds block for details. |
health_thresholds | list(object) | No | List of health threshold configurations. Refer to health thresholds block for details. |
Each request_thresholds block supports the following:
| Name | Type | Required | Description |
|---|---|---|---|
entity_name | string | Yes | The name of the entity to apply the threshold to. |
assertion_name | string | Yes | The name of the assertion to configure. |
request_type | string | Yes | The type of request (inbound, outbound). |
request_context | string | Yes | The request context or path to apply the threshold to. |
value | number | Yes | The threshold value. |
Each resource_thresholds block supports the following:
| Name | Type | Required | Description |
|---|---|---|---|
assertion_name | string | Yes | The name of the assertion to configure. |
resource_type | string | Yes | The type of resource (container, Pod, node). |
container_name | string | Yes | The name of the container to apply the threshold to. |
source | string | Yes | The source of the metrics (metrics, logs). |
severity | string | Yes | The severity level (warning, critical). |
value | number | Yes | The threshold value. |
Each health_thresholds block supports the following:
| Name | Type | Required | Description |
|---|---|---|---|
assertion_name | string | Yes | The name of the assertion to configure. |
expression | string | Yes | The Prometheus expression for the health check. |
entity_type | string | Yes | Entity type for the health threshold (for example, Service, Pod, Namespace, Volume). |
alert_category | string | No | Optional alert category label for the health threshold. |
resource "grafana_asserts_thresholds" "example" {
provider = grafana.asserts
request_thresholds = [{
entity_name = "api-service"
assertion_name = "ErrorRatioBreach"
request_type = "inbound"
request_context = "/api/v1/users"
value = 0.02
}]
resource_thresholds = [{
assertion_name = "Saturation"
resource_type = "container"
container_name = "web-server"
source = "metrics"
severity = "warning"
value = 75
}]
health_thresholds = [{
assertion_name = "ServiceDown"
expression = "up{job=\"api-service\"} < 1"
entity_type = "Service"
}]
}
Consider the following best practices when configuring thresholds with Terraform.
After applying the Terraform configuration, verify that: