Skip to content

Commit

Permalink
feat(nrql_alert_conditions): Add data_account_id for NRQL alert condi…
Browse files Browse the repository at this point in the history
…tion (#1184)
  • Loading branch information
founddrama authored Jul 1, 2024
1 parent bd0bfe8 commit 44e918d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/alerts/nrql_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,21 @@ type NrqlConditionTerm struct {
// NrqlConditionQuery represents the NRQL query object returned in a NerdGraph response object.
type NrqlConditionQuery struct {
Query string `json:"query,omitempty"`
DataAccountId *int `json:"dataAccountId,omitempty"`
EvaluationOffset *int `json:"evaluationOffset,omitempty"`
}

// NrqlConditionCreateQuery represents the NRQL query object for create.
type NrqlConditionCreateQuery struct {
Query string `json:"query,omitempty"`
DataAccountId *int `json:"dataAccountId,omitempty"`
EvaluationOffset *int `json:"evaluationOffset,omitempty"`
}

// NrqlConditionUpdateQuery represents the NRQL query object for update.
type NrqlConditionUpdateQuery struct {
Query string `json:"query"`
DataAccountId *int `json:"dataAccountId,omitempty"`
EvaluationOffset *int `json:"evaluationOffset"`
}

Expand Down Expand Up @@ -711,6 +714,7 @@ const (
nrql {
evaluationOffset
query
dataAccountId
}
enabled
entityGuid
Expand All @@ -725,8 +729,8 @@ const (
thresholdOccurrences
}
type
violationTimeLimit
violationTimeLimitSeconds
violationTimeLimit
violationTimeLimitSeconds
expiration {
closeViolationsOnExpiration
expirationDuration
Expand All @@ -735,7 +739,7 @@ const (
signal {
aggregationWindow
evaluationOffset
evaluationDelay
evaluationDelay
fillOption
fillValue
aggregationMethod
Expand Down
75 changes: 75 additions & 0 deletions pkg/alerts/nrql_conditions_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,78 @@ func TestIntegrationNrqlConditions_StreamingMethods(t *testing.T) {
}
}()
}

func TestIntegrationNrqlConditions_DataAccountId(t *testing.T) {
t.Parallel()

testAccountID, err := mock.GetTestAccountID()
if err != nil {
t.Skipf("%s", err)
}

var nrqlConditionCreateWithDataAccountId = NrqlConditionCreateBase{
Enabled: true,
Name: fmt.Sprintf("test-nrql-condition-%s", testNrqlConditionRandomString),
Nrql: NrqlConditionCreateQuery{
Query: "SELECT rate(sum(apm.service.cpu.usertime.utilization), 1 second) * 100 as cpuUsage FROM Metric WHERE appName like 'Dummy App'",
DataAccountId: &testAccountID,
},
Terms: []NrqlConditionTerm{
{
Threshold: &nrqlConditionBaseThreshold,
ThresholdOccurrences: ThresholdOccurrences.AtLeastOnce,
ThresholdDuration: 600,
Operator: AlertsNRQLConditionTermsOperatorTypes.ABOVE,
Priority: NrqlConditionPriorities.Critical,
},
},
ViolationTimeLimitSeconds: 3600,
Signal: &AlertsNrqlConditionCreateSignal{
AggregationWindow: &nrqlConditionBaseAggWindow,
FillOption: &AlertsFillOptionTypes.STATIC,
FillValue: &nrqlConditionBaseSignalFillValue,
EvaluationDelay: &nrqlConditionEvaluationDelay,
AggregationMethod: &nrqlConditionBaseAggMethod,
AggregationDelay: &nrqlConditionBaseAggDelay,
},
}

var (
randStr = mock.RandSeq(5)
createDataAccountIdInput = NrqlConditionCreateInput{
NrqlConditionCreateBase: nrqlConditionCreateWithDataAccountId,
}
)

// Setup
client := newIntegrationTestClient(t)
testPolicy := AlertsPolicyInput{
IncidentPreference: AlertsIncidentPreferenceTypes.PER_POLICY,
Name: fmt.Sprintf("test-alert-policy-%s", randStr),
}
policy, err := client.CreatePolicyMutation(testAccountID, testPolicy)
require.NoError(t, err)

// Test: Create (static condition with dataAccountId field)
createdStaticWithDataAccountId, err := client.CreateNrqlConditionStaticMutation(testAccountID, policy.ID, createDataAccountIdInput)
require.NoError(t, err)
require.NotNil(t, createdStaticWithDataAccountId)
require.NotNil(t, createdStaticWithDataAccountId.ID)
require.NotNil(t, createdStaticWithDataAccountId.PolicyID)
require.NotNil(t, createdStaticWithDataAccountId.Nrql.DataAccountId)
require.Equal(t, &testAccountID, createdStaticWithDataAccountId.Nrql.DataAccountId)

// Test: Get (static condition with dataAccountId field)
readResult, err := client.GetNrqlConditionQuery(testAccountID, createdStaticWithDataAccountId.ID)
require.NoError(t, err)
require.NotNil(t, readResult)
require.Equal(t, &testAccountID, readResult.Nrql.DataAccountId)

// Deferred teardown
defer func() {
_, err := client.DeletePolicyMutation(testAccountID, policy.ID)
if err != nil {
t.Logf("error cleaning up alert policy %s (%s): %s", policy.ID, policy.Name, err)
}
}()
}

0 comments on commit 44e918d

Please sign in to comment.