Skip to content

Commit

Permalink
Add support for New Relic service levels (#21)
Browse files Browse the repository at this point in the history
Add support for New Relic service levels
  • Loading branch information
betabandido authored May 23, 2023
1 parent cce1e2a commit 36d7e4c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module "newrelic_monitoring" {
}
```

See `variables.tf` for more information on the input variables that the module accepts. For instance, the default values for an alert's duration and threshold can be overridden. The following example shows how to do so:
See [variables.tf](./variables.tf) for more information on the input variables that the module accepts. For instance, the default values for an alert's duration and threshold can be overridden. The following example shows how to do so:

```hcl
module "newrelic_monitoring" {
Expand All @@ -48,3 +48,20 @@ pagerduty_service_key = "The New Relic integration key for your service
```

You can obtain the integration key for your service with Terraform using a resource named `pagerduty_service_integration`.

## Creating Default SLOs

New Relic supports the creation of [service levels](https://docs.newrelic.com/docs/service-level-management/intro-slm/) for applications. This terraform module allows for creating two SLOs – one measuring request latency and the other measuring application availability. The SLOs use a default rolling time window spanning seven days. To create the SLOs, set the following variable:

```hcl
create_default_slos = true
```

Some parameters for the SLOs can be fine-tuned (see [variables.tf](./variables.tf) for more details). For instance, you can use the following snippet to specify that the latency duration should not exceed 100 ms with a target value of 99.99%.

```hcl
latency_slo_target = 99.99
latency_slo_duration_threshold = 0.1
```

For more details about these parameters, please check the [official documentation](https://docs.newrelic.com/docs/service-level-management/intro-slm/).
5 changes: 0 additions & 5 deletions alerts.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
data "newrelic_entity" "app" {
name = var.newrelic_fully_qualified_app_name
type = "APPLICATION"
}

resource "newrelic_alert_policy" "non_urgent" {
account_id = var.newrelic_account_id
name = "${var.newrelic_app_name} Non Urgent"
Expand Down
4 changes: 4 additions & 0 deletions app.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "newrelic_entity" "app" {
name = var.newrelic_fully_qualified_app_name
type = "APPLICATION"
}
59 changes: 59 additions & 0 deletions slos.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "newrelic_service_level" "latency" {
count = var.create_default_slos == true ? 1 : 0

guid = data.newrelic_entity.app.guid
name = "Latency"
description = "Proportion of requests that are served faster than a threshold."

events {
account_id = var.newrelic_account_id
valid_events {
from = "Transaction"
where = "entityGuid = '${data.newrelic_entity.app.guid}' AND (transactionType = 'Web')"
}
good_events {
from = "Transaction"
where = "entityGuid = '${data.newrelic_entity.app.guid}' AND (transactionType = 'Web') AND duration < ${var.latency_slo_duration_threshold}"
}
}

objective {
target = var.latency_slo_target
time_window {
rolling {
count = 7
unit = "DAY"
}
}
}
}

resource "newrelic_service_level" "availability" {
count = var.create_default_slos == true ? 1 : 0

guid = data.newrelic_entity.app.guid
name = "Availability"
description = "Proportion of requests that are served without errors."

events {
account_id = var.newrelic_account_id
valid_events {
from = "Transaction"
where = "entityGuid = '${data.newrelic_entity.app.guid}'"
}
bad_events {
from = "TransactionError"
where = "entityGuid = '${data.newrelic_entity.app.guid}' AND error.expected IS FALSE"
}
}

objective {
target = var.availability_slo_target
time_window {
rolling {
count = 7
unit = "DAY"
}
}
}
}
3 changes: 3 additions & 0 deletions test/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ module "newrelic_monitoring" {

enable_pagerduty_notifications = true
pagerduty_service_key = pagerduty_service_integration.newrelic.integration_key

create_default_slos = true
latency_slo_duration_threshold = 0.1
}
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,26 @@ variable "response_status_variable_name" {
the default value.
EOF
}

variable "create_default_slos" {
type = bool
default = false
description = "If true, two SLOs (latency and availability) will be created"
}

variable "latency_slo_target" {
type = number
default = 95.00
description = "Target value for latency SLO"
}

variable "latency_slo_duration_threshold" {
type = number
description = "Duration threshold for the latency SLO (in seconds)"
}

variable "availability_slo_target" {
type = number
default = 99.00
description = "Target value for availability SLO"
}

0 comments on commit 36d7e4c

Please sign in to comment.