Tags help you group, search, filter, and focus the data about your entities, which can be anything from applications to hosts to services. Tagging entities using the New Relic CLI is a good candidate for automation.
In this 5-minute guide, you use the New Relic CLI to add multiple tags to one of your entities.
Before you begin
For this guide you need a New Relic user key: You can view and create them in the API keys UI.
Install the New Relic CLI
You can download the New Relic CLI for macOS, Windows, and Linux. You can also download pre-built binaries for all platforms, including .deb and .rpm packages, and our Windows x64 .msi installer.
Linux
Install Snapcraft, and then run:
$sudo snap install newrelic-cli
macOS
Install Homebrew, and then run:
$brew install newrelic-cli
Windows
Install Scoop, and then run:
$scoop bucket add newrelic-cli https://github.com/newrelic/newrelic-cli.git$scoop install newrelic-cli
Create your New Relic CLI profile
New Relic CLI profiles contain credentials and settings that you can apply to any CLI command.
To create your first CLI profile, run the profiles add
command. Don't forget to set the region of your New Relic account: use -r
to set either us
or eu
(this is required).
$# Create the tutorial account$newrelic profiles add --profile tutorial --apiKey YOUR_NEW_RELIC_USER_KEY -r YOUR_REGION$# Set the profile as default$newrelic profiles default --profile tutorial
Search for an entity
Your New Relic account might have hundreds of entities: Have a quick look by opening the Entity explorer.
In the terminal, run entity search
to retrieve a list of entities from your account as JSON. In this example, you're searching for all entities with "test" in their name.
$# Change the `name` to match any of your existing entities$newrelic entity search --name "test"
If there are matching entities in your account, the query yields data in JSON format similar to this workload example.
Select an entity from the results and look for its guid
value; the guid
is the unique identifier of the entity. Copy or write it down.
{ "accountId": 123456789, "domain": "NR1", "entityType": "WORKLOAD_ENTITY", "guid": "F7B7AE59FDED4204B846FB08423DB18E", "name": "Test workload", "reporting": true, "type": "WORKLOAD" },
Add tags and tag lists to your entity
Using your entity guid
, you can add tags right away. Invoke the entities tags create
command.
If you want to add multiple tags, use tag sets: While tags are key-value pairs separated by colons, tag sets are comma-separated lists of tags. For example:
tag1:value1,tag2:value2
Important
Adding tags is an asynchronous operation: it could take a little while for the tags to get created.$# Adding a single tag$newrelic entity tags create --guid GUID --tag key:value$# Adding multiple tags$newrelic entity tags create --guid GUID --tag tag1:test,tag2:test
Check that the tags are there
To make sure that the tags have been added to your entities, retrieve them using the entity tags get
command. All tags associated with your entity are retrieved as a JSON array.
newrelic entity tags get --guid GUID
Tip
Tags can be deleted at any time by invoking the entity tags delete
command followed by the same arguments you used to create them.
Tip
Values can be deleted at any time by invoking the entity tags delete-values
command followed by the same arguments you used to create them.
[ { "Key": "tag1", "Values": ["true"] }, { "Key": "tag2", "Values": ["test"] }, { "Key": "tag3", "Values": ["testing"] } // ...]
Next steps
Have a look at all the New Relic CLI commands. For example, you could create a New Relic workflow using workload create
.
If you'd like to engage with other community members, visit our New Relic Explorers Hub page. We welcome feature requests or bug reports on GitHub.