Skip to content

Instantly share code, notes, and snippets.

@gspncr
Last active October 29, 2019 12:09
Show Gist options
  • Save gspncr/16ee570e3bb5b9e911a2b68eb891a446 to your computer and use it in GitHub Desktop.
Save gspncr/16ee570e3bb5b9e911a2b68eb891a446 to your computer and use it in GitHub Desktop.
New Relic Insights Workshop

Welcome to the New Relic Insights workshop!

Below are the code snippets that we will use while we are going through the workshop. You can watch, follow along, or come back to these later (the gist is public).

Questions?

The best place for questions is the New Relic Explorers Hub, but if you have questions on the workshop you can drop me a line or pop a question below if you are happy for it to be public and is workshop related 🤓

Contents

Custom Attributes with APM

Custom Attributes with Synthetics

Custom Events with Event API

Dashboard API

Query API

Custom Attributes with APM

For the first example we will send custom attributes to New Relic Insights from a demo application, Wordpress.

We will be using PHP Custom Attributes that we can add to our Wordpress application.

Viewing the Wordpress Codex, we have identified how to capture a display name and email address from signed in users to a Wordpress blog.

You can implement the below in the Wordpress Twenty Seventeen theme.

<?php
#add the below beneath get_header(); in file /var/www/html/wordpress/wp-content/themes/twentyseventeen/index.php
newrelic_add_custom_parameter ('userDisplayName', $current_user->display_name);
newrelic_add_custom_parameter ('userEmail', $current_user->user_email);
?>

Now navigate back to your blog (make sure you are logged in), refresh the homepage. Check New Relic Insights for your event and you should see an event with your username and email address associated with the Event in Insights.

It would be a good idea to display the visits in a table

I added a new user, see above I can view the visits for both of my users by querying all the users in the last week.

NRQL: FROM Transaction SELECT userDisplayName, userEmail WHERE userDisplayName IS NOT NULL SINCE 1 week ago

You can be creative and find specific attributes, such as the transaction time for users.

NRQL: FROM Transaction SELECT average(duration) WHERE userDisplayName IS NOT NULL SINCE 1 week ago FACET userEmail

Here we can see user sessions. We could even have used uniqueCount to find unique user visits in a specific time period.

NRQL: FROM Transaction SELECT count(userEmail) WHERE userDisplayName IS NOT NULL SINCE 1 week ago FACET userEmail

Custom Attributes with Synthetics

For this activity we will check the latest news from the UK Government Food Agency. The food agency publishes an RSS feed with the latest allergy alerts.

This exercise will involve adding custom attributes to Synthetics to capture new alerts. We will use the $$util.insights functions in the Synthetics check.

This activity involves:

  1. Create a Synthetics API Check to query the RSS feed for allergy alerts
  2. Extract from the latest entry the title, description, and publication date and forward this information to Insights using $util.insights
var parseString = require('xml2js').parseString;
var $http = require('request');
// Get the New Relic status RSS feed
$http.get('https://www.food.gov.uk/rss-feed/alerts-allergy', function(err, response, body) {
parseString(body, function(err, result){
// Parse the RSS, and get the latest incident
var latestIncident = result.rss.channel[0].item[0];
// Push the incident details to Insights
$util.insights.set('newsTitle', latestIncident.title[0]);
$util.insights.set('Description', latestIncident.description[0]);
$util.insights.set('Date', latestIncident.pubDate[0]);
console.log(latestIncident.title[0]);
console.log(latestIncident.description[0]);
console.log(latestIncident.pubDate[0]);
});
});

Now navigate back to Insights. After the script has run successfully, execute the nrql:

FROM SyntheticCheck SELECT latest(custom.Date), latest(custom.newsTitle), latest(custom.Description)

The results should look similar to: (will show the latest news story of course!)

Custom Events with Event API

We will use the Event API to send some fake data into New Relic Insights. Be aware that each execution of the demo script (as provided below) will consume 4 events out of your usage credits.

Get to know: your Insert Key

Navigate to Insights -> Manage Data -> API Keys -> + -> Save your notes

Get to know: Your Account ID

Your account ID is the identifier in the URL you use for Insights

https://insights.newrelic.com/accounts/ /query

If you are using the US region, your URL will include .eu before .newrelic.com. You should check the script below to include the .eu prefix if you have an account in the EU region.

Remember to modify the Insert Key and Account ID in the commands!

Two Step

echo '[ { "eventType":"demoData", "account":3, "amount":259.54 }, { "eventType":"demoData", "account":5, "amount":12309, "product":"Golden mousemat" }, { "eventType":"demoData", "account":5, "amount":123.09, "product":"Silver mousemat" }, { "eventType":"demoData", "account":7, "amount":10000, "product":"Golden webcam cover", "promoCode": "AnythingFor10k" } ]' > example.json

gzip -c example.json | curl --data-binary @- -X POST -H "Content-Type: application/json" -H "X-Insert-Key: YOUR-INSERT-KEY" -H "Content-Encoding: gzip" https://insights-collector.newrelic.com/v1/accounts/YOUR-ACCOUNT-ID/events

One Step (set API Key and account ID)

echo '[ { "eventType":"demoData", "account":3, "amount":259.54 }, { "eventType":"demoData", "account":5, "amount":12309, "product":"Golden mousemat" }, { "eventType":"demoData", "account":5, "amount":123.09, "product":"Silver mousemat" }, { "eventType":"demoData", "account":7, "amount":10001, "product":"Golden webcam cover", "promoCode": "AnythingFor10k" } ]' > example.json && gzip -c example.json | curl --data-binary @- -X POST -H "Content-Type: application/json" -H "X-Insert-Key: YOUR-insert-KEY" -H "Content-Encoding: gzip" https://insights-collector.newrelic.com/v1/accounts/YOUR-ACCOUNT-ID/events

On success, you should see be returned:

{"success":true, "uuid":"random-character-keys-as-identifier"}

View all of the recent events: FROM demoData SELECT *

View transactions that used a promo code: FROM demoData SELECT * WHERE promoCode IS NOT NULL

View the sum of all purchases: FROM demoData SELECT sum(amount)

View the total spend of products from account 5, and count the number of purchases, and facet this by the products purchased: FROM demoData SELECT sum(amount), count(product ) WHERE account = 5 FACET product

The example funnel used

SELECT funnel(session, 
    WHERE pageUrl like '%/wordpress/' AS 'Blog Home Page', 
    WHERE pageUrl like '%/wordpress/wp-login.php' AS 'Log In', 
    WHERE pageUrl like '%/uncategorized%' AS 'View uncategorised posts') 
    FROM PageView 
    SINCE 12 hours ago

New Relic Dashboard API

You can use the Dashboard API to list dashboards, create a dashboard, download the json for a dashboard, update a dashboard and modify a dashboard.

Show a dashboard Use the below query to show the JSON for the dashboard you created in the last exercise.

https://api.newrelic.com/v2/dashboards/<dashboard-id>.json

Provide header X-Api-Key: {{apiKey}}

Create a dashboard Use the below query to update the table element from being 2 widgets wide, to being 3 widgets wide. Save it as something new.

https://api.newrelic.com/v2/dashboards.json

Provide header X-Api-Key: {{apiKey}} ; Content-Type: application/json

Body:

{
    "dashboard": {
        "id": 1010834,
        "title": "GS: insights workshop",
        "description": null,
        "icon": "bar-chart",
        "created_at": "2019-10-14T13:43:57Z",
        "updated_at": "2019-10-14T13:43:57Z",
        "visibility": "all",
        "editable": "editable_by_all",
        "ui_url": "https://insights.newrelic.com/accounts/1147177/dashboards/1010834",
        "api_url": "https://api.newrelic.com/v2/dashboards/1010834",
        "owner_email": "gspenceruk@gmail.com",
        "metadata": {
            "version": 1
        },
        "widgets": [
            {
                "visualization": "billboard",
                "layout": {
                    "width": 1,
                    "height": 1,
                    "row": 1,
                    "column": 1
                },
                "widget_id": 12786823,
                "account_id": 1147177,
                "data": [
                    {
                        "nrql": "from PageView SELECT count(session) since 30 minutes ago"
                    }
                ],
                "presentation": {
                    "title": "Page Views in the last 30 minutes",
                    "notes": null,
                    "threshold": {
                        "red": null,
                        "yellow": null
                    }
                }
            },
            {
                "visualization": "facet_bar_chart",
                "layout": {
                    "width": 1,
                    "height": 1,
                    "row": 1,
                    "column": 2
                },
                "widget_id": 12786824,
                "account_id": 1147177,
                "data": [
                    {
                        "nrql": "FROM PageView SELECT count(session) SINCE 24 hours ago FACET city LIMIT 10"
                    }
                ],
                "presentation": {
                    "title": "Cities with the most traffic last 24 hours",
                    "notes": null,
                    "drilldown_dashboard_id": 1010769
                }
            },
            {
                "visualization": "billboard",
                "layout": {
                    "width": 1,
                    "height": 1,
                    "row": 1,
                    "column": 3
                },
                "widget_id": 12786825,
                "account_id": 1147177,
                "data": [
                    {
                        "nrql": "FROM Transaction SELECT average(databaseDuration) as 'DB Duration (ms)' SINCE 1 hour ago"
                    }
                ],
                "presentation": {
                    "title": "Average DB Duration (ms) for last hour",
                    "notes": null,
                    "threshold": {
                        "red": 0.5,
                        "yellow": 0.05
                    }
                }
            },
            {
                "visualization": "facet_table",
                "layout": {
                    "width": 3,
                    "height": 1,
                    "row": 2,
                    "column": 1
                },
                "widget_id": 12786826,
                "account_id": 1147177,
                "data": [
                    {
                        "nrql": "SELECT average(duration), count(*) FROM Transaction FACET name SINCE 1 hour ago LIMIT 5"
                    }
                ],
                "presentation": {
                    "title": "Five slowest transactions for the last hour",
                    "notes": null,
                    "drilldown_dashboard_id": null
                }
            }
        ],
        "filter": {
            "event_types": [
                "PageView"
            ],
            "attributes": [
                "city"
            ]
        }
    }
}

Querying data through API

You can send a request to Insights and have the result of the query returned as a JSON object.

https://insights-api.newrelic.com/v1/accounts/<Your-Account-ID>/query?nrql=SELECT%20average(duration)%20FROM%20PageView

And the NRQL query is passed as a URL encoded parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment