Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module github.com/newrelic/terraform-provider-newrelic/v2

go 1.23.6

replace github.com/newrelic/newrelic-client-go/v2 => github.com/newrelic/newrelic-client-go/v2 v2.62.1-0.20250613191201-02f8af57f3d5

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/go-agent/v3 v3.30.0
github.com/newrelic/go-insights v1.0.3
github.com/newrelic/newrelic-client-go/v2 v2.61.2
github.com/newrelic/newrelic-client-go/v2 v2.62.1-0.20250613191201-02f8af57f3d5
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ github.com/newrelic/go-agent/v3 v3.30.0 h1:ZXHCT/Cot4iIPwcegCZURuRQOsfmGA6wilW+S
github.com/newrelic/go-agent/v3 v3.30.0/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go/v2 v2.61.2 h1:gjWHv9TvnjDwHUKT/6RA8kWOfS4T31t+3iLNBYJ0YpA=
github.com/newrelic/newrelic-client-go/v2 v2.61.2/go.mod h1:GG7CtbtIZIF6bPBLkBU1uB+tP8gSu9B8iRFTtzWoT+k=
github.com/newrelic/newrelic-client-go/v2 v2.62.1-0.20250613191201-02f8af57f3d5 h1:IhJLDCpTwljOSWKVVi6urLlXg+LfVGGxF0Ljh248cok=
github.com/newrelic/newrelic-client-go/v2 v2.62.1-0.20250613191201-02f8af57f3d5/go.mod h1:GG7CtbtIZIF6bPBLkBU1uB+tP8gSu9B8iRFTtzWoT+k=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
5 changes: 5 additions & 0 deletions newrelic/resource_newrelic_nrql_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ func resourceNewRelicNrqlAlertCondition() *schema.Resource {
Optional: true,
Description: "How long we wait until the signal starts evaluating. The maximum delay is 7200 seconds (120 minutes)",
},
"polling_frequency": {
Type: schema.TypeInt,
Optional: true,
Description: "The frequency at which the NRQL will be executed and the results will be evaluated, in seconds. Only valid for NRQL conditions with CloudCost, KubernetesCost, or CloudCostEstimate event types. Conditions for these event types are evaluated in a polling fashion.",
},
"aggregation_timer": {
Type: schema.TypeString,
Optional: true,
Expand Down
54 changes: 54 additions & 0 deletions newrelic/resource_newrelic_nrql_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,28 @@ func TestAccNewRelicNrqlAlertCondition_SignalSeasonality(t *testing.T) {
})
}

func TestAccNewRelicNrqlAlertCondition_PollingFrequency(t *testing.T) {
resourceName := "newrelic_nrql_alert_condition.foo"
rName := acctest.RandString(5)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicNrqlAlertConditionDestroy,
Steps: []resource.TestStep{
// Test: Create condition with polling frequency
{
Config: testAccNewRelicNrqlAlertConditionStaticWithPollingFrequency(
rName,
),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicNrqlAlertConditionExists(resourceName),
),
},
},
})
}

func testAccCheckNewRelicNrqlAlertConditionDestroy(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
client := providerConfig.NewClient
Expand Down Expand Up @@ -1549,3 +1571,35 @@ resource "newrelic_nrql_alert_condition" "foo" {
}
`, name)
}

func testAccNewRelicNrqlAlertConditionStaticWithPollingFrequency(
name string,
) string {
return fmt.Sprintf(`
resource "newrelic_alert_policy" "foo" {
name = "tf-test-%[1]s"
}

resource "newrelic_nrql_alert_condition" "foo" {
policy_id = newrelic_alert_policy.foo.id

name = "tf-test-%[1]s"
type = "static"
enabled = false
violation_time_limit_seconds = 3600
aggregation_window = 3600
polling_frequency = 3600

nrql {
query = "SELECT count(*) FROM CloudCost"
}

critical {
operator = "above"
threshold = 1
threshold_duration = 3600
threshold_occurrences = "ALL"
}
}
`, name)
}
15 changes: 15 additions & 0 deletions newrelic/structures_newrelic_nrql_alert_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@ func expandCreateSignal(d *schema.ResourceData) (*alerts.AlertsNrqlConditionCrea
signal.EvaluationDelay = &value
}

if pollingFrequency, ok := d.GetOk("polling_frequency"); ok {
value := pollingFrequency.(int)
signal.PollingFrequency = &value
}

return &signal, nil
}

Expand Down Expand Up @@ -575,6 +580,11 @@ func expandUpdateSignal(d *schema.ResourceData) (*alerts.AlertsNrqlConditionUpda
signal.EvaluationDelay = &value
}

if pollingFrequency, ok := d.GetOk("polling_frequency"); ok {
value := pollingFrequency.(int)
signal.PollingFrequency = &value
}

return &signal, nil
}

Expand Down Expand Up @@ -736,7 +746,12 @@ func flattenSignal(d *schema.ResourceData, signal *alerts.AlertsNrqlConditionSig
if err := d.Set("evaluation_delay", signal.EvaluationDelay); err != nil {
return fmt.Errorf("[DEBUG] Error setting nrql alert condition `evaluation_delay`: %v", err)
}
}

if signal.PollingFrequency != nil {
if err := d.Set("polling_frequency", signal.PollingFrequency); err != nil {
return fmt.Errorf("[DEBUG] Error setting nrql alert condition `polling_frequency`: %v", err)
}
}

return nil
Expand Down
24 changes: 24 additions & 0 deletions newrelic/structures_newrelic_nrql_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func TestExpandNrqlAlertConditionInput(t *testing.T) {

signalSeasonality := alerts.NrqlSignalSeasonalities.Daily

pollingFrequency := 3600

cases := map[string]struct {
Data map[string]interface{}
ExpectErr bool
Expand Down Expand Up @@ -424,6 +426,28 @@ func TestExpandNrqlAlertConditionInput(t *testing.T) {
NrqlConditionCreateBase: alerts.NrqlConditionCreateBase{},
},
},
"polling frequency not nil": {
Data: map[string]interface{}{
"nrql": []interface{}{nrql},
"polling_frequency": 3600,
},
Expanded: &alerts.NrqlConditionCreateInput{
NrqlConditionCreateBase: alerts.NrqlConditionCreateBase{
Signal: &alerts.AlertsNrqlConditionCreateSignal{
PollingFrequency: &pollingFrequency,
},
},
},
},
"polling frequency nil": {
Data: map[string]interface{}{
"nrql": []interface{}{nrql},
"polling_frequency": nil,
},
Expanded: &alerts.NrqlConditionCreateInput{
NrqlConditionCreateBase: alerts.NrqlConditionCreateBase{},
},
},
}

r := resourceNewRelicNrqlAlertCondition()
Expand Down
29 changes: 14 additions & 15 deletions website/docs/r/nrql_alert_condition.html.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: 'newrelic'
page_title: 'New Relic: newrelic_nrql_alert_condition'
sidebar_current: 'docs-newrelic-resource-nrql-alert-condition'
layout: "newrelic"
page_title: "New Relic: newrelic_nrql_alert_condition"
sidebar_current: "docs-newrelic-resource-nrql-alert-condition"
description: |-
Create and manage a NRQL alert condition for a policy in New Relic.
---
Expand All @@ -17,6 +17,7 @@ Before upgrading to version 2.0.0 or later, it is recommended to upgrade to the
## Example Usage

##### Type: `static` (default)

```hcl
resource "newrelic_alert_policy" "foo" {
name = "foo"
Expand Down Expand Up @@ -62,6 +63,7 @@ resource "newrelic_nrql_alert_condition" "foo" {
}
}
```

See additional [examples](#additional-examples).

## Argument Reference
Expand All @@ -82,10 +84,10 @@ The following arguments are supported:
- `critical` - A list containing the `critical` threshold values. At least one `critical` or `warning` threshold must be defined. See [Terms](#terms) below for details.
- `warning` - A list containing the `warning` threshold values. At least one `critical` or `warning` threshold must be defined. See [Terms](#terms) below for details.
- `violation_time_limit` - (Optional) **DEPRECATED:** Use `violation_time_limit_seconds` instead. Sets a time limit, in hours, that will automatically force-close a long-lasting incident after the time limit you select. Possible values are `ONE_HOUR`, `TWO_HOURS`, `FOUR_HOURS`, `EIGHT_HOURS`, `TWELVE_HOURS`, `TWENTY_FOUR_HOURS`, `THIRTY_DAYS` (case insensitive).<br>
<small>\***Note**: One of `violation_time_limit` _or_ `violation_time_limit_seconds` must be set, but not both.</small>
<small>\***Note**: One of `violation_time_limit` _or_ `violation_time_limit_seconds` must be set, but not both.</small>

- `violation_time_limit_seconds` - (Optional) Sets a time limit, in seconds, that will automatically force-close a long-lasting incident after the time limit you select. The value must be between 300 seconds (5 minutes) to 2592000 seconds (30 days) (inclusive). <br>
<small>\***Note**: One of `violation_time_limit` _or_ `violation_time_limit_seconds` must be set, but not both.</small>
<small>\***Note**: One of `violation_time_limit` _or_ `violation_time_limit_seconds` must be set, but not both.</small>

- `fill_option` - (Optional) Which strategy to use when filling gaps in the signal. Possible values are `none`, `last_value` or `static`. If `static`, the `fill_value` field will be used for filling gaps in the signal.
- `fill_value` - (Optional, required when `fill_option` is `static`) This value will be used for filling gaps in the signal.
Expand All @@ -100,6 +102,7 @@ The following arguments are supported:
- `evaluation_delay` - (Optional) How long we wait until the signal starts evaluating. The maximum delay is 7200 seconds (120 minutes).
- `slide_by` - (Optional) Gathers data in overlapping time windows to smooth the chart line, making it easier to spot trends. The `slide_by` value is specified in seconds and must be smaller than and a factor of the `aggregation_window`.
- `signal_seasonality` - (Optional) Seasonality under which a condition's signal(s) are evaluated. Only available for baseline conditions. Valid values are: `NEW_RELIC_CALCULATION`, `HOURLY`, `DAILY`, `WEEKLY`, or `NONE`. To have New Relic calculate seasonality automatically, set to `NEW_RELIC_CALCULATION`. To turn off seasonality completely, set to `NONE`.
- `polling_frequency` - (Optional) The frequency at which the NRQL will be executed and the results will be evaluated, in seconds. Polling cannot happen more frequently than 3600 seconds (1 hour), and must be specified in whole hour increments. Example: Frequency may be 3600 seconds (1 hour), but not 5400 seconds (1.5 hours). Polling cannot happen less frequently than the duration of the warning term, critical term, or 24 hours, whichever is shortest. Example: If the warning term duration is 2 days and the critical term duration is 3 days, the polling must be no less frequent than once every 86400 seconds (24 hours). Example: If the warning term duration is 6 hours and the critical term duration is 12 hours, the polling must be no less frequent than once every 21600 seconds (6 hours). Default polling frequency will be 3600 seconds (1 hour). Only valid for NRQL conditions with CloudCost, KubernetesCost, or CloudCostEstimate event types. Conditions for these event types are evaluated in a polling fashion.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `polling_frequency` - (Optional) The frequency at which the NRQL will be executed and the results will be evaluated, in seconds. Polling cannot happen more frequently than 3600 seconds (1 hour), and must be specified in whole hour increments. Example: Frequency may be 3600 seconds (1 hour), but not 5400 seconds (1.5 hours). Polling cannot happen less frequently than the duration of the warning term, critical term, or 24 hours, whichever is shortest. Example: If the warning term duration is 2 days and the critical term duration is 3 days, the polling must be no less frequent than once every 86400 seconds (24 hours). Example: If the warning term duration is 6 hours and the critical term duration is 12 hours, the polling must be no less frequent than once every 21600 seconds (6 hours). Default polling frequency will be 3600 seconds (1 hour). Only valid for NRQL conditions with CloudCost, KubernetesCost, or CloudCostEstimate event types. Conditions for these event types are evaluated in a polling fashion.
- `polling_frequency` - (Optional) The frequency, in seconds, at which the NRQL will be executed and results evaluated. When using `polling_frequency`, consider these important points:
- Default polling frequency is 3600 seconds (1 hour).
- Polling cannot happen more frequently than 3600 seconds (1 hour), and must be specified in whole hour increments. For example, you can set `polling_frequency` to 3600 seconds (1 hour), but not 5400 seconds (1.5 hours).
- Polling cannot happen less frequently than the duration of the warning term, critical term, or 24 hours, whichever is shortest. For instance:
- If the warning term duration is 2 days and the critical term duration is 3 days, polling must be no less frequent than once every 86400 seconds (24 hours).
- If the warning term duration is 6 hours and the critical term duration is 12 hours, polling must be no less frequent than once every 21600 seconds (6 hours).
- `polling_frequency` is only valid for NRQL conditions with CloudCost, KubernetesCost, or CloudCostEstimate event types. Conditions for these event types are evaluated in a polling fashion.

Optimized the text to make it visually clear :) please consider this change.


## NRQL

Expand All @@ -108,11 +111,11 @@ The `nrql` block supports the following arguments:
- `query` - (Required) The NRQL query to execute for the condition.
- `data_account_id` - (Optional) The account ID to use for the alert condition's query as specified in the the `query` field. If `data_account_id` is not specified, then the condition's query will be evaluated against the `account_id`. Note that the `account_id` must have read privileges for the `data_account_id` or else the condition will be invalid.
- `evaluation_offset` - (Optional) **DEPRECATED:** Use `aggregation_method` instead. Represented in minutes and must be within 1-20 minutes (inclusive). NRQL queries are evaluated based on their `aggregation_window` size. The start time depends on this value. It's recommended to set this to 3 windows. An offset of less than 3 windows will trigger incidents sooner, but you may see more false positives and negatives due to data latency. With `evaluation_offset` set to 3 windows and an `aggregation_window` of 60 seconds, the NRQL time window applied to your query will be: `SINCE 3 minutes ago UNTIL 2 minutes ago`. `evaluation_offset` cannot be set with `aggregation_method`, `aggregation_delay`, or `aggregation_timer`.<br>
- `since_value` - (Optional) **DEPRECATED:** Use `aggregation_method` instead. The value to be used in the `SINCE <X> minutes ago` clause for the NRQL query. Must be between 1-20 (inclusive). <br>
- `since_value` - (Optional) **DEPRECATED:** Use `aggregation_method` instead. The value to be used in the `SINCE <X> minutes ago` clause for the NRQL query. Must be between 1-20 (inclusive). <br>

## Terms

~> **NOTE:** The direct use of the `term` has been deprecated, and users should use `critical` and `warning` instead. What follows now applies to the named priority attributes for `critical` and `warning`, but for those attributes the priority is not allowed. At least one `critical` or `warning` term must be defined.
~> **NOTE:** The direct use of the `term` has been deprecated, and users should use `critical` and `warning` instead. What follows now applies to the named priority attributes for `critical` and `warning`, but for those attributes the priority is not allowed. At least one `critical` or `warning` term must be defined.

NRQL alert conditions support up to two terms. At least one `term` must have `priority` set to `critical` and the second optional `term` must have `priority` set to `warning`.

Expand All @@ -121,10 +124,10 @@ The `term` block supports the following arguments:
- `operator` - (Optional) Valid values are `above`, `above_or_equals`, `below`, `below_or_equals`, `equals`, or `not_equals` (case insensitive). Defaults to `equals`. Note that when using a `type` of `baseline`, the only valid option here is `above`.
- `priority` - (Optional) `critical` or `warning`. Defaults to `critical`.
- `threshold` - (Required) The value which will trigger an incident.
<br>For _baseline_ NRQL alert conditions, the value must be in the range [1, 1000]. The value is the number of standard deviations from the baseline that the metric must exceed in order to create an incident.
<br>For _baseline_ NRQL alert conditions, the value must be in the range [1, 1000]. The value is the number of standard deviations from the baseline that the metric must exceed in order to create an incident.
- `threshold_duration` - (Optional) The duration, in seconds, that the threshold must violate in order to create an incident. Value must be a multiple of the `aggregation_window` (which has a default of 60 seconds).
<br>For _baseline_ NRQL alert conditions, the value must be within 120-86400 seconds (inclusive).
<br>For _static_ NRQL alert conditions, the value must be within 60-86400 seconds (inclusive).
<br>For _baseline_ NRQL alert conditions, the value must be within 120-86400 seconds (inclusive).
<br>For _static_ NRQL alert conditions, the value must be within 60-86400 seconds (inclusive).

- `threshold_occurrences` - (Optional) The criteria for how many data points must be in violation for the specified threshold duration. Valid values are: `all` or `at_least_once` (case insensitive).
- `duration` - (Optional) **DEPRECATED:** Use `threshold_duration` instead. The duration of time, in _minutes_, that the threshold must violate for in order to create an incident. Must be within 1-120 (inclusive).
Expand Down Expand Up @@ -154,10 +157,8 @@ In addition to all arguments above, the following attributes are exported:
- `id` - The ID of the NRQL alert condition. This is a composite ID with the format `<policy_id>:<condition_id>` - e.g. `538291:6789035`.
- `entity_guid` - The unique entity identifier of the NRQL Condition in New Relic.


## Additional Examples


##### Type: `baseline`

[Baseline NRQL alert conditions](https://docs.newrelic.com/docs/alerts/new-relic-alerts/defining-conditions/create-baseline-alert-conditions) are dynamic in nature and adjust to the behavior of your data. The example below demonstrates a baseline NRQL alert condition for alerting when transaction durations are above a specified threshold and dynamically adjusts based on data trends.
Expand Down Expand Up @@ -218,11 +219,10 @@ $ terraform import newrelic_nrql_alert_condition.foo 538291:6789035:baseline
$ terraform import newrelic_nrql_alert_condition.foo 538291:6789035:static
```

~> **NOTE:** The value of `conditionType` in the import composite ID must be a valid condition type - `static` or `baseline`. Also note that deprecated arguments will *not* be set when importing.
~> **NOTE:** The value of `conditionType` in the import composite ID must be a valid condition type - `static` or `baseline`. Also note that deprecated arguments will _not_ be set when importing.

Users can find the actual values for `policy_id` and `condition_id` from the New Relic One UI under respective policy and condition.


## Tags

Manage NRQL alert condition tags with `newrelic_entity_tags`. For up-to-date documentation about the tagging resource, please check [newrelic_entity_tags](entity_tags.html#example-usage)
Expand Down Expand Up @@ -285,7 +285,6 @@ resource "newrelic_entity_tags" "my_condition_entity_tags" {
}
```


## Upgrade from 1.x to 2.x

There have been several deprecations in the `newrelic_nrql_alert_condition`
Expand Down
Loading