Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nrql_alert_conditions): Add data_account_id for NRQL alert condition #1184

Merged
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
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
sanderblue marked this conversation as resolved.
Show resolved Hide resolved
expiration {
closeViolationsOnExpiration
expirationDuration
Expand All @@ -735,7 +739,7 @@ const (
signal {
aggregationWindow
evaluationOffset
evaluationDelay
evaluationDelay
sanderblue marked this conversation as resolved.
Show resolved Hide resolved
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,
},
}
sanderblue marked this conversation as resolved.
Show resolved Hide resolved

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)
}
}()
}
Loading