現在、このページは英語版のみです。

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.

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.