We’re wrapping up our blog series on automating the configuration of your observability tools with a final post about tags as code and workloads as code, the last two examples of observability as code. In part one, you worked with examples to learn about dashboards as code and an introduction to Hashicorp’s Terraform, and in part two, you learned how to create alerts as code and synthetic monitoring as code.
By the end of this third and final post, you’ll have hands-on experience with a total of five examples of observability as code using New Relic and Terraform.
Before you begin working on this part, make sure you have completed part one and part two.
Typically, the content in this blog series would be presented as a workshop. We've made Implement automated configurations with observability as code a blog series, so anyone at any time can follow along "hands-on," using sample code, the Instruqt online workshop, and videos in the blog posts. For the best experience, use all three:
- See and run the full code that we'll be walking through in this three-part series.
- Follow the hands-on workshop in Instruqt.
- Read along with this blog post, including the embedded videos.
If you haven't used Instruqt before, these two videos give an overview and some instructions:
Tags as code
Tagging is a simple concept that a lot of engineers understand. We know we need to do it. But we don't really do it, maybe because we don't have enough time, or we don't have a way to do it handling large volumes. In this section, you’ll learn how to apply tags using Terraform. Tags are also a good way to create reporting in your dashboards.
When you have multiple applications or infrastructure using the same account, it can be hard to find a specific object in your overview dashboard. If you add tags to your entities, you can segment them into logical groups.
In New Relic, tags are key-value pairs that you can add to various sets of data, or entities in New Relic, such as apps and hosts that you monitor, agents, dashboards, and workloads. An example of the pair of values might be team: operations
. Use tags for grouping, searching and filtering the data about your entities.
In this tutorial for tags as code, we'll continue to use data from our sample FoodMe restaurant app.
Create tags as code
It’s possible to add tags later in New Relic, but here’s how to save time and add them automatically as part of the deployment workflow when you’re deploying an app or a host.
To see everything we are covering in this section, watch this video. If you need a refresher on working with Terraform, go to Getting started with New Relic and Terraform. You can also work along with these steps with code samples in GitHub and the hands-on workshop in Instruqt.
Example newrelic_entity_tags resource
Here's an example from Resource: newrelic_entity_tags in the Terraform registry that shows tags in Terraform with New Relic:
resource "newrelic_entity_tags" "foo" {
# The guid of the entity to tag.
guid = data.newrelic_entity.foo.guid
# A nested block that describes an entity tag
tag {
key = "my-key"
values = ["my-value", "my-other-value"]
}
tag {
key = "my-key-2"
values = ["my-value-2"]
}
}
For more details on this New Relic entity, see Resource: newrelic_entity_tags.
These next example main.tf
and variables.tf
files are based on code samples in the Getting Started with the New Relic Provider documentation.
Example main.tf file complete code
# get the New Relic terraform provider
terraform {
required_version = "~> 1.0"
required_providers {
newrelic = {
source = "newrelic/newrelic"
}
}
}
# configure the New Relic provider
provider "newrelic" {
account_id = (var.nr_account_id)
api_key = (var.nr_api_key) # usually prefixed with 'NRAK'
region = (var.nr_region) # Valid regions are US and EU
}
# data source to get information about a specific entity in New Relic that already exists.
data "newrelic_entity" "app_name" {
# Note: This must be an exact match of your app name in New Relic (Case sensitive)
name = (var.nr_appname)
type = "APPLICATION"
domain = "APM"
}
# resource to create, update, and delete tags for a New Relic entity.
resource "newrelic_entity_tags" "app_name" {
guid = data.newrelic_entity.app_name.guid
# A nested block that describes an entity tag
tag {
key = "O11yAsCode"
values = ["Hashicorp", "Terraform", "HCL"]
}
tag {
key = "Workloads"
values = ["FoodMe"]
}
}
Example variables.tf file complete code
# your unique New Relic account ID
variable "nr_account_id" {
default = "XXXXX"
}
# your User API key
variable "nr_api_key" {
default = "XXXXX"
}
# valid regions are US and EU
variable "nr_region" {
default = "US"
}
# your unique New Relic App ID
variable "nr_appname" {
default = "FoodMe-XXXXX"
}
What the final result looks like
Now that you've deployed tags as code, your final result with the sample FoodMe app should look like this in New Relic:
Workloads as code
Now, using what you learned in tags as code, we're going to start using workloads to group entities.
In New Relic, you can use workloads to group and monitor entities based on teams or sets of responsibilities. A workload can include any New Relic monitored entity, including services, browser apps, mobile apps, databases, hosts, and dashboards. A workload can even include other workloads. For example, if your organization has complex teams, they might need to divide and overlap workloads.
Then you can get aggregated activity and health data from all the services in your stack. As described in our workload documentation, workloads give you visibility into the availability and consumption of resources across an entire service, and you can define what information is relevant to you. Use workloads to group entities that are important for a team or project, and then it’s easier to better browse and isolate the relevant data.
Of course you can create workloads in the New Relic UI, but you can automate them with code. We’ll start with a simple example that uses the New Relic API. Automating the workload provisioning in New Relic is a good prerequisite step for using some advanced features like service level management and errors inbox.
Create workloads as code
To see everything we are covering in this section, watch this video. If you need a refresher on working with Terraform, go to Getting started with New Relic and Terraform. You can also work along with these steps with code samples in GitHub and the hands-on workshop in Instruqt.
Optional: As seen in the video, here’s a guide for installing the New Relic browser monitoring agent.
Example newrelic_workload resource
Here's an example from Resource: newrelic_workload in the Terraform registry. We’ll use the New Relic API for this simple example and use the workload name to search for entities.
resource "newrelic_workload" "foo" {
# The workload's name
name = "Example workload"
# The New Relic account ID where you want to create the workload
account_id = 12345678
# A list of search queries that define a dynamic workload
entity_search_query {
# The query
query = "name like '%Example application%'"
}
}
For more details on searching, see the NerdGraph tutorial: View entity data in our docs and Resource: newrelic_workload in the Terraform docs.
These next example main.tf
and variables.tf
files are based on code samples in the Getting Started with the New Relic Provider documentation.
Example main.tf file complete code
# get the New Relic terraform provider
terraform {
required_version = "~> 1.0"
required_providers {
newrelic = {
source = "newrelic/newrelic"
}
}
}
# Configure the New Relic provider
provider "newrelic" {
account_id = (var.nr_account_id)
api_key = (var.nr_api_key) # usually prefixed with 'NRAK'
region = (var.nr_region) # Valid regions are US and EU
}
# resource to create, update, and delete a New Relic workload.
resource "newrelic_workload" "O11y_asCode-Workloads-TF" {
name = "O11y_asCode-Workloads-TF"
account_id = (var.nr_account_id)
# Include entities with a set of tags.
entity_search_query {
query = "tags.Workloads='FoodMe'"
}
}
Example variables.tf file complete code
# your unique New Relic account ID
variable "nr_account_id" {
default = "XXXXX"
}
# your User API key
variable "nr_api_key" {
default = "XXXXX"
}
# valid regions are US and EU
variable "nr_region" {
default = "US"
}
What the final result looks like
Now that you've deployed workloads as code, your final result with the sample FoodMe app should look like this in New Relic:
Closing thoughts
Congratulations for completing this series. You’ve now learned five observability as code examples with Terraform and New Relic:
- dashboards as code
- alerts as code
- synthetic monitoring as code
- tags as code
- workloads as code
My closing video includes recommendations on how to plan ahead to implement observability as code in your environment.
Next steps
For more in depth details on observability as code, see Observability as code: automating your observability configuration to drive value.
If you're not already using New Relic, sign up for a free account to try some more tutorials. Your free account includes 100 GB/month of data ingest, one full platform user, unlimited basic users, 500 synthetics checks, and unlimited ping monitors.
The views expressed on this blog are those of the author and do not necessarily reflect the views of New Relic. Any solutions offered by the author are environment-specific and not part of the commercial solutions or support offered by New Relic. Please join us exclusively at the Explorers Hub (discuss.newrelic.com) for questions and support related to this blog post. This blog may contain links to content on third-party sites. By providing such links, New Relic does not adopt, guarantee, approve or endorse the information, views or products available on such sites.