The correction is integrated into the TP (See the green boxes)
What you will learn in this TP
- Describe alerts and SLOs in Terraform
- Configure thresholds, window and loss of signal
- Route an issue to a destination with a workflow
- Test an alert end to end
All steps and instructions are shown. Recommended for this lab: autonomous
Introduction #
Context. The shop goes to production. The team requires alerts and the SLO to be reviewed and versioned like code: you describe them in Terraform, then prove the alert really arrives.
Prerequisites: a free New Relic account (license key), a machine with Docker, curl and jq, the Lab 1 application running, Terraform 1.6 or later · Estimated time: 60 min
An alert policy, an error rate condition, a workflow to your e-mail and an availability SLO, created and destroyed by Terraform, with a notification received.Setup #
Guided steps, with their solution.
1. Prepare Terraform
Why? The provider acts with a user key, not with the license key which only sends data.
Create a USER key, exportNEW_RELIC_API_KEY,NEW_RELIC_ACCOUNT_IDandNEW_RELIC_REGION, writemain.tfand runterraform plan. How many resources will be created?See the correction
mkdir -p ~/nr-tf && cd ~/nr-tf export NEW_RELIC_API_KEY='<user key NRAK-…>' NEW_RELIC_ACCOUNT_ID=<account id> NEW_RELIC_REGION=US # or EU cat > main.tf <<'EOF' terraform { required_providers { newrelic = { source = "newrelic/newrelic", version = "~> 3.102" } } } provider "newrelic" {} variable "email" { type = string } data "newrelic_entity" "boutique" { name = "boutique-tp" type = "APPLICATION" domain = "APM" } resource "newrelic_alert_policy" "boutique" { name = "TP boutique" incident_preference = "PER_CONDITION_AND_TARGET" } resource "newrelic_nrql_alert_condition" "erreurs" { policy_id = newrelic_alert_policy.boutique.id type = "static" name = "Boutique : taux d'erreur > 5 %" description = "Voir Errors inbox de boutique-tp, puis les logs en contexte." enabled = true violation_time_limit_seconds = 86400 aggregation_window = 60 aggregation_method = "event_flow" aggregation_delay = 120 nrql { query = "SELECT percentage(count(*), WHERE error IS true) FROM Transaction WHERE appName = 'boutique-tp'" } critical { operator = "above" threshold = 5 threshold_duration = 300 threshold_occurrences = "all" } expiration_duration = 600 open_violation_on_expiration = true close_violations_on_expiration = false } resource "newrelic_notification_destination" "mail" { name = "TP mail" type = "EMAIL" property { key = "email" value = var.email } } resource "newrelic_notification_channel" "mail" { name = "TP mail" type = "EMAIL" destination_id = newrelic_notification_destination.mail.id product = "IINT" property { key = "subject" value = "{{ issueTitle }}" } } resource "newrelic_workflow" "boutique" { name = "TP boutique" muting_rules_handling = "NOTIFY_ALL_ISSUES" issues_filter { name = "politique TP" type = "FILTER" predicate { attribute = "labels.policyIds" operator = "EXACTLY_MATCHES" values = [newrelic_alert_policy.boutique.id] } } destination { channel_id = newrelic_notification_channel.mail.id } } resource "newrelic_service_level" "disponibilite" { guid = data.newrelic_entity.boutique.guid name = "Boutique - disponibilité" description = "Part des transactions sans erreur" events { account_id = data.newrelic_entity.boutique.account_id valid_events { from = "Transaction" where = "appName = 'boutique-tp'" } bad_events { from = "TransactionError" where = "appName = 'boutique-tp' AND error.expected IS FALSE" } } objective { target = 99.0 time_window { rolling { count = 7 unit = "DAY" } } } } EOF terraform init -input=false && terraform plan -var email=vous@exemple.fr # Plan: 6 to add (policy, condition, destination, channel, workflow, service level)2. Understand the condition
Why? Each setting changes when on-call gets woken up: you must be able to justify it.
Explainthreshold_occurrences = "all",aggregation_delayand the threeexpiration_*settings. Why open an incident on expiration?See the correction
threshold_occurrences = all: every minute of the 5 minutes must exceed 5% ("for at least") aggregation_delay = 120: event flow waits 2 min for late data before closing a window expiration_duration = 600: without data for 10 min, the signal is considered lost open_violation_on_expiration = true: an application that no longer answers has no transactions, hence no errors: without this option, a total outage would go unnoticed3. Apply
Why? Applying from code guarantees production and the Git repository are identical.
Apply the configuration with your address, then find the policy, the workflow and the SLO in the UI.See the correction
terraform apply -auto-approve -var email=vous@exemple.fr terraform state list4. Trigger the alert
Why? An alert that was never tested is an alert nobody knows works.
For 8 minutes, send orders whose number is a multiple of 17 (all failing), follow the incident then the issue, and check the e-mail arrives.See the correction
end=$((SECONDS+480)); while [ $SECONDS -lt $end ]; do curl -s -o /dev/null localhost:8000/commande/$((17 * (RANDOM % 50 + 1))); sleep 0.5; done # NRQL : SELECT * FROM NrAiIncident WHERE policyName = 'TP boutique' SINCE 30 minutes ago5. Read the SLO and destroy
Why? The error budget shows the incident's effect on the promised reliability; destroying avoids orphan alerts.
Open Service levels: what share of the weekly error budget did the incident consume? Then destroy every resource and stop the containers.See the correction
# With ~800 failed requests out of ~1,200 valid ones, the 99% SLO is widely missed: # the budget (1% of valid requests) is exhausted, which would freeze releases. terraform destroy -auto-approve -var email=vous@exemple.fr docker rm -f boutique newrelic-infra
Validate your work
Check that each goal above is reached and that you get the result described in the introduction.
Difficulty level : ●●●●○ (4/5)
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Alerts, service levels and incidents in New Relic
Setting up useful alerting in New Relic: policies and NRQL conditions, static and anomaly thresho...
Automating New Relic and controlling costs
Managing New Relic as code and keeping the bill under control: NerdGraph API, newrelic CLI, newre...
New Relic best practices
Best practices to succeed with New Relic: adoption approach, account organization, naming and tag...
Service-Level Objectives (new platform) in Dynatrace
Defining and steering SLOs in the Dynatrace Service-Level Objectives app: templates and custom DQ...
Grafana SLO in Grafana Cloud
Grafana SLO in Grafana Cloud: SLI, objective and error budget, creating a ratio SLO, fast and slo...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
Datadog best practices: tags, costs, alerts and governance
Checklist for using Datadog effectively: unified tagging, cost control (hosts, logs, custom metri...
Monitors, SLOs, dashboards and Datadog as code
Create effective monitors (metrics, logs, APM, composites, anomalies), SLOs, dashboards, and mana...
NRQL: querying all your New Relic data
Mastering NRQL, New Relic's query language: SELECT … FROM structure, filters, aggregation functio...