Skip to content

Commit

Permalink
Merge pull request #141 from newrelic/liz/custom-events-mdx
Browse files Browse the repository at this point in the history
Add custom-events guide and ruby syntax support
  • Loading branch information
LizBaker authored Jun 22, 2020
2 parents a7afc7c + f21af13 commit f1b5f91
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"node-sass": "^4.14.1",
"normalize.css": "^8.0.1",
"prism-react-renderer": "^1.1.1",
"prismjs": "^1.20.0",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
Expand Down
5 changes: 5 additions & 0 deletions src/components/CodeSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import FeatherIcon from './FeatherIcon';
import styles from './CodeSnippet.module.scss';
import useClipboard from '../hooks/useClipboard';
import useFormattedCode from '../hooks/useFormattedCode';
import Prism from 'prism-react-renderer/prism';

(typeof global !== 'undefined' ? global : window).Prism = Prism;

require('prismjs/components/prism-ruby');

const CodeSnippet = ({ children, copy, className, lineNumbers }) => {
const language = className.replace('language-', '');
Expand Down
4 changes: 4 additions & 0 deletions src/data/sidenav.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"displayName": "Create a Flex integration",
"url": "/noop"
},
{
"displayName": "Add a custom New Relic event",
"url": "/noop"
},
{
"displayName": "Write NRQL queries",
"url": "/noop"
Expand Down
Binary file added src/images/UC2-sec2-query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions src/markdown-pages/custom-events.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
path: '/collect-data/custom-events'
duration: '30 min'
title: 'Create custom New Relic events'
template: 'GuideTemplate'
description: 'Create custom New Relic events'
---

## _Measure what you need by creating your own event types._
<br/>

Whereas adding [custom attributes](/collect-data/custom-attributes) adds metadata to an existing event, a custom event creates an entirely new event type. Create custom events to define, visualize, and get alerts on additional data, just as you would with any data we provide from our core agents.
<br/>

_Custom events can be inserted through the Agent APIs or directly via the Insights Insert API. The following example shows how to send a custom event named CLIRun that tracks when a command line tool written in Ruby has its process exit due to an exception._

```ruby
# Hook into the runtime 'at_exit' event
at_exit do
# Name the custom event
payload = { 'eventType' => 'CLIRun' }

# Check to see if the process is exiting due to an error
if $!.nil? || $!.is_a?(SystemExit) && $!.success?
payload[:status] = 0
else
# Gather any known errors
errors = ""
(Thread.current[:errors] ||= []).each do |err|
errors += "#{err}\n"
end
payload[:errors] = errors
end

# Send the errors to New Relic as a custom event
insights_url = URI.parse("https://insights-collector.newrelic.com/v1/accounts/YOUR_ACCOUNT_ID/events")
headers = { "x-insert-key" => "YOUR_API_KEY", "content-type" => "application/json" }

http = Net::HTTP.new(insights_url.host, insights_url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(insights_url.request_uri, headers)
request.body = payload.to_json

puts "Sending run summary to Insights: #{payload.to_json}"
begin
response = http.request(request)
puts "Response from Insights: #{response.body}"
rescue Exception => e
puts "There was an error posting to Insights. Error: #{e.inspect}"
end
end
```
<br/>

_Here, a NRQL query retrieves information about the custom event, and the result can be added to a dashboard._
<br/>

![nrql query example](../images/UC2-sec2-query.png)

```
SELECT count(*) FROM CLIRun FACET errors SINCE 1 week ago
```
<br/>

[Learn more about custom events.](https://docs.newrelic.com/docs/insights/insights-data-sources/custom-data/introduction-event-api)

0 comments on commit f1b5f91

Please sign in to comment.