diff --git a/examples/README.md b/examples/README.md index e5cba80ca9..b6bed12892 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,17 +1,7 @@ # node-newrelic examples -This directory contains examples of using the New Relic Node.js agent. You can run them by copying them into a new directory that contains a `newrelic.js` configuration file, like this: +We are in the process of migrating these examples over to [newrelic-node-examples](https://github.com/newrelic/newrelic-node-examples) as more robust applications. If you are looking for the api/background-transactions, api/distributed-tracing, or api/segments examples, they are now [here](https://github.com/newrelic/newrelic-node-examples/tree/main/custom-instrumentation) as stand-alone applications. -```bash -$ mkdir node-newrelic-examples -$ cd node-newrelic-examples -$ npm i newrelic -$ cp node_modules/newrelic/newrelic.js . -$ # edit newrelic.js with your configuration details, like app name -$ wget https://raw.githubusercontent.com/newrelic/node-newrelic/main/examples/api/background-transactions/example1-basic.js -$ node example1-basic.js -``` - -Metrics generated by the examples will then show up in your New Relic One interface. +The *shim* subdirectory and its revised contents will also be moved over to newrelic-node-examples at a later date. To request additional examples, please [file an issue](https://github.com/newrelic/node-newrelic/issues)! diff --git a/examples/api/background-transactions/example-addCustomAttribute.js b/examples/api/background-transactions/example-addCustomAttribute.js deleted file mode 100644 index 7fdf8fac70..0000000000 --- a/examples/api/background-transactions/example-addCustomAttribute.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -/* -`addCustomAttribute` adds a custom attribute to an existing transaction. -It takes `name` and `value` parameters, adding them to the object reported to New Relic. - -In this example, we create a background transaction in order to modify it. -Once run, a transaction will be reported that has the attribute `hello` with the value `world`. -*/ - -newrelic.startBackgroundTransaction('myCustomTransaction', function handle() { - const transaction = newrelic.getTransaction() - newrelic.addCustomAttribute('hello', 'world') - transaction.end() -}) diff --git a/examples/api/background-transactions/example-addCustomAttributes.js b/examples/api/background-transactions/example-addCustomAttributes.js deleted file mode 100644 index 8b114a1090..0000000000 --- a/examples/api/background-transactions/example-addCustomAttributes.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') // eslint-disable-line node/no-extraneous-require - -/* -`addCustomAttributes` adds custom attributes to an existing transaction. -It takes an `attributes` object as its sole parameter, -adding its keys and values as attributes to the transaction. - -Internally, the agent uses `addCustomAttribute` to add these attributes to the transaction. -Much like this: - -```javascript -for (const [key, value] of Object.entries(attributes)) { - newrelic.addCustomAttribute(key, value) -} -``` - -In this example, we create a background transaction in order to modify it. -Once run, a transaction will be reported that has the attribute `hello` with the value `world`. -*/ - -newrelic.startBackgroundTransaction('myCustomTransaction', function handle() { - const transaction = newrelic.getTransaction() - newrelic.addCustomAttributes({ hello: 'world' }) - transaction.end() -}) diff --git a/examples/api/background-transactions/example-addCustomSpanAttribute.js b/examples/api/background-transactions/example-addCustomSpanAttribute.js deleted file mode 100644 index 15ae835acf..0000000000 --- a/examples/api/background-transactions/example-addCustomSpanAttribute.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') // eslint-disable-line node/no-extraneous-require - -/* -`addCustomSpanAttribute` adds a custom span attribute to an existing transaction. -It takes `name` and `value` parameters, adding them to the span reported to New Relic. - -In this example, we create a background transaction in order to modify it. -Once run, a transaction will be reported that has the span attribute `hello` with the value `world`. -*/ - -newrelic.startBackgroundTransaction('myCustomTransaction', function handle() { - const transaction = newrelic.getTransaction() - newrelic.addCustomSpanAttribute('hello', 'world') - transaction.end() -}) diff --git a/examples/api/background-transactions/example-addCustomSpanAttributes.js b/examples/api/background-transactions/example-addCustomSpanAttributes.js deleted file mode 100644 index 38af556ea3..0000000000 --- a/examples/api/background-transactions/example-addCustomSpanAttributes.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') // eslint-disable-line node/no-extraneous-require - -/* -`addCustomSpanAttributes` adds custom span attributes to an existing transaction. -It takes an `attributes` object as its sole parameter, -adding its keys and values as span attributes to the transaction. - -Internally, the agent uses `addCustomSpanAttribute` to add these span attributes to the transaction. -Much like this: - -```javascript -for (const [key, value] of Object.entries(attributes)) { - newrelic.addCustomSpanAttribute(key, value) -} -``` - -In this example, we create a background transaction in order to modify it. -Once run, a transaction will be reported that has the span attribute `hello` with the value `world`. -*/ - -newrelic.startBackgroundTransaction('myCustomTransaction', function handle() { - const transaction = newrelic.getTransaction() - newrelic.addCustomSpanAttributes({ hello: 'world' }) - transaction.end() -}) diff --git a/examples/api/background-transactions/example-recordCustomEvent.js b/examples/api/background-transactions/example-recordCustomEvent.js deleted file mode 100644 index 5897e098c4..0000000000 --- a/examples/api/background-transactions/example-recordCustomEvent.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2021 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') // eslint-disable-line node/no-extraneous-require - -/* -`recordCustomEvent` records a custom event, allowing you to set the event's name -and attributes. - -An event's name may have alphanumerics (a-z, A-Z, 0-9) as well as ':', '_', and -' ' characters. The event's attributes are an object whose keys can be strings, -numbers, or booleans. - -This method is synchronous. The event is queued to be reported during the next -harvest cycle. -*/ - -newrelic.recordCustomEvent('my_app:my_event', { - custom: 'properties', - n: 1, - ok: true -}) diff --git a/examples/api/background-transactions/example1-basic.js b/examples/api/background-transactions/example1-basic.js deleted file mode 100644 index bc91d4367a..0000000000 --- a/examples/api/background-transactions/example1-basic.js +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -const transactionName = 'myCustomTransaction' - -// `startBackgroundTransaction()` takes a name, group, and a handler function to -// execute. The group is optional. The last parameter is the function performing -// the work inside the transaction. Once the transaction starts, there are -// three ways to end it: -// -// 1) Call `transaction.end()`. The `transaction` can be received by calling -// `newrelic.getTransaction()` first thing in the handler function. Then, -// when you call `transaction.end()` timing will stop. -// 2) Return a promise. The transaction will end when the promise resolves or -// rejects. -// 3) Do neither. If no promise is returned, and `getTransaction()` isn't -// called, the transaction will end immediately after the handle returns. - -// Here is an example for the first case. -newrelic.startBackgroundTransaction(transactionName, function handle() { - const transaction = newrelic.getTransaction() - doSomeWork(function cb() { - transaction.end() - }) -}) - -/* - * Function to simulate async work. - * - */ -function doSomeWork(callback) { - setTimeout(function work() { - callback() - }, 500) -} diff --git a/examples/api/background-transactions/example2-grouping.js b/examples/api/background-transactions/example2-grouping.js deleted file mode 100644 index 45edaa461b..0000000000 --- a/examples/api/background-transactions/example2-grouping.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -const transactionName = 'myCustomTransaction' - -// The second parameter to `startBackgroundTransaction` may be a group to -// organize related background transactions on APM. More on this can be found -// on our documentation website: -// https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page#txn-type-dropdown -const groupName = 'myTransactionGroup' - -newrelic.startBackgroundTransaction(transactionName, groupName, function handle() { - const transaction = newrelic.getTransaction() - doSomeWork(function cb() { - transaction.end() - }) -}) - -/* - * Function to simulate async work. - * - */ -function doSomeWork(callback) { - setTimeout(function work() { - callback() - }, 500) -} diff --git a/examples/api/background-transactions/example3-results.js b/examples/api/background-transactions/example3-results.js deleted file mode 100644 index 949fbfb0ed..0000000000 --- a/examples/api/background-transactions/example3-results.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -const transactionName = 'myCustomTransaction' - -// The return value of the handle is passed back from `startBackgroundTransaction`. -const result = newrelic.startBackgroundTransaction(transactionName, function handle() { - return 42 -}) - -console.log(result) // Prints "42" diff --git a/examples/api/background-transactions/example4-promises.js b/examples/api/background-transactions/example4-promises.js deleted file mode 100644 index 423e8bd07e..0000000000 --- a/examples/api/background-transactions/example4-promises.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -const transactionName = 'myCustomTransaction' - -// startBackgroundTransaction() takes a name, group, and a handler function to -// execute. The group is optional. The last parameter is the function performing -// the work inside the transaction. Once the transaction starts, there are -// three ways to end it: -// -// 1) Call `transaction.end()`. The `transaction` can be received by calling -// `newrelic.getTransaction()` first thing in the handler function. Then, -// when you call `transaction.end()` timing will stop. -// 2) Return a promise. The transaction will end when the promise resolves or -// rejects. -// 3) Do neither. If no promise is returned, and `getTransaction()` isn't -// called, the transaction will end immediately after the handle returns. - -// Here is an example for the second case. -newrelic - .startBackgroundTransaction(transactionName, function handle() { - return doSomeWork() - .then(function resolve() { - // Handle results... - }) - .catch(function reject(error) { - newrelic.noticeError(error) - // Handle error... - }) - }) - .then(function afterTransaction() { - // Note that you can continue off of the promise at this point, but the - // transaction has ended and this work will not be associated with it. - }) - -/* - * Function to simulate async function that returns a promise. - */ -function doSomeWork() { - return new Promise(function executor(resolve) { - setTimeout(function work() { - resolve(42) - }, 500) - }) -} diff --git a/examples/api/distributed-tracing/example1-background.js b/examples/api/distributed-tracing/example1-background.js deleted file mode 100644 index d16ac65591..0000000000 --- a/examples/api/distributed-tracing/example1-background.js +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') // eslint-disable-line node/no-extraneous-require - -/* -For context on how to use acceptDistributedTraceHeaders and insertDistributedTraceHeaders, -first read "Enable distributed tracing with agent APIs": - -https://docs.newrelic.com/docs/distributed-tracing/enable-configure/language-agents-enable-distributed-tracing/ - -You can use insertDistributedTraceHeaders and acceptDistributedTraceHeaders to -link different transactions together. In this example, a custom web transaction -is linked to a background transaction, for example to instrument tracing of -interactions with resources external to your application. - -`insertDistributedTraceHeaders` modifies the headers map that is passed in by adding W3C Trace Context headers and New Relic Distributed Trace headers. The New Relic headers can be disabled with `distributed_tracing.exclude_newrelic_header: true` in the config. - -`acceptDistributedTraceHeaders` is used to instrument the called service for inclusion in a distributed trace. It links the spans in a trace by accepting a payload generated by `insertDistributedTraceHeaders` or generated by some other W3C Trace Context compliant tracer. This method accepts the headers of an incoming request, looks for W3C Trace Context headers, and if not found, falls back to New Relic distributed trace headers. -*/ - -// Give the agent some time to start up. -setTimeout(runTest, 2000) - -function runTest() { - // Start an outer transaction - newrelic.startWebTransaction('Custom web transaction', function outerHandler() { - // Call newrelic.getTransaction to retrieve a handle on the current transaction. - const transactionHandle = newrelic.getTransaction() - - // Generate the payload right before creating the linked transaction. - const headers = {} - transactionHandle.insertDistributedTraceHeaders(headers) - - // Start an inner transaction that handles some nested task - newrelic.startBackgroundTransaction('Background task', function innerHandler() { - const backgroundHandle = newrelic.getTransaction() - // Link the outer transaction by accepting its headers as a payload - // with the inner transaction's handle - backgroundHandle.acceptDistributedTraceHeaders(headers) - // End the transactions - backgroundHandle.end(transactionHandle.end) - }) - }) -} diff --git a/examples/api/segments/example1-callbacks.js b/examples/api/segments/example1-callbacks.js deleted file mode 100644 index 41300e166d..0000000000 --- a/examples/api/segments/example1-callbacks.js +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -/* - * We'll stub out an async task that runs as part of monitoring a segment - */ -function myAsyncTask(callback) { - const sleep = new Promise((resolve) => { - setTimeout(resolve, 1) - }) - sleep.then(() => { - callback(null, 'hello world') - }) -} - -/* - * Then we stub out the task that handles that task's result, - * to show how the result is passed throughthe segment handler - */ -function myNextTask(greetings, callback) { - callback(null, `${greetings}, it's me!`) -} - -/* - * This task will be run as its own segment within our transaction handler - */ -function someTask(callback) { - myAsyncTask(function firstCb(err1, result) { - if (err1) { - return callback(err1) - } - - myNextTask(result, function secondCb(err2, output) { - callback(err2, output) - }) - }) -} - -// Segments can only be created inside of transactions. They could be automatically -// generated HTTP transactions or custom transactions. -newrelic.startBackgroundTransaction('bg-tx', function transHandler() { - const tx = newrelic.getTransaction() - - // `startSegment()` takes a segment name, a boolean if a metric should be - // created for this segment, the handler function, and an optional callback. - // The handler is the function that will be wrapped with the new segment. When - // a callback is provided, the segment timing will end when the callback is - // called. - - newrelic.startSegment('myCustomSegment', false, someTask, function cb(err, output) { - // Handle the error and output as appropriate. - console.log(output) // "hello world, it's me!" - tx.end() - }) -}) diff --git a/examples/api/segments/example2-promises.js b/examples/api/segments/example2-promises.js deleted file mode 100644 index 7d66cc4729..0000000000 --- a/examples/api/segments/example2-promises.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -/* - * We'll stub out an async task that runs as part of monitoring a segment - */ -async function myAsyncTask() { - await new Promise((resolve) => { - setTimeout(resolve, 1) - }) - return 'hello world' -} - -/* - * Then we stub out the task that handles that task's result, - * to show how the result is passed throughthe segment handler - * - */ -function myNextTask(greetings) { - return `${greetings}, it's me!` -} - -/* - * This task will be run as its own segment within our transaction handler - */ -function someTask() { - return myAsyncTask().then(function thenNext(result) { - return myNextTask(result) - }) -} - -// Segments can only be created inside of transactions. They could be automatically -// generated HTTP transactions or custom transactions. -newrelic.startBackgroundTransaction('bg-tx', function transHandler() { - const tx = newrelic.getTransaction() - - // `startSegment()` takes a segment name, a boolean if a metric should be - // created for this segment, the handler function, and an optional callback. - // The handler is the function that will be wrapped with the new segment. If - // a promise is returned from the handler, the segment's ending will be tied - // to that promise resolving or rejecting. - - return newrelic.startSegment('myCustomSegment', false, someTask).then(function thenAfter(output) { - console.log(output) // "hello world, it's me!" - tx.end() - }) -}) diff --git a/examples/api/segments/example3-async.js b/examples/api/segments/example3-async.js deleted file mode 100644 index 23acd39f15..0000000000 --- a/examples/api/segments/example3-async.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -/* - * We'll stub out an async task that runs as part of monitoring a segment - */ -async function myAsyncTask() { - await new Promise((resolve) => { - setTimeout(resolve, 1) - }) - return 'hello world' -} - -/* - * Then we stub out the task that handles that task's result, - * to show how the result is passed throughthe segment handler. - */ -async function myNextTask(greetings) { - await new Promise((resolve) => { - setTimeout(resolve, 1) - }) - return `${greetings}, it's me!` -} - -/** - * This task will be run as its own segment within our transaction handler - */ -async function someTask() { - const result = await myAsyncTask() - return await myNextTask(result) -} - -// Segments can only be created inside of transactions. They could be automatically -// generated HTTP transactions or custom transactions. -newrelic.startBackgroundTransaction('bg-tx', async function transHandler() { - // `startSegment()` takes a segment name, a boolean if a metric should be - // created for this segment, the handler function, and an optional callback. - // The handler is the function that will be wrapped with the new segment. - // Since `async` functions just return a promise, they are covered just the - // same as the promise example. - - const output = await newrelic.startSegment('myCustomSegment', false, someTask) - console.log(output) -}) diff --git a/examples/api/segments/example4-sync-assign.js b/examples/api/segments/example4-sync-assign.js deleted file mode 100644 index 3e7746c9c3..0000000000 --- a/examples/api/segments/example4-sync-assign.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2020 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const newrelic = require('newrelic') - -/* - * We'll stub out an async task that runs as part of monitoring a segment. - */ -function mySyncTask() { - return 'hello world' -} - -/* - * Then we stub out the task that handles that task's result, - * to show how the result is passed throughthe segment handler. - * - * @param greetings - */ -function myNextTask(greetings) { - return `${greetings}, it's me!` -} - -/* - * This task will be run as its own segment within our transaction handler - */ -function someTask() { - const result = mySyncTask() - return myNextTask(result) -} - -// Segments can only be created inside of transactions. They could be automatically -// generated HTTP transactions or custom transactions. -newrelic.startBackgroundTransaction('bg-tx', function transHandler() { - // `startSegment()` takes a segment name, a boolean if a metric should be - // created for this segment, the handler function, and an optional callback. - // The handler is the function that will be wrapped with the new segment. - - const output = newrelic.startSegment('myCustomSegment', false, function timedFunction() { - return someTask() - }) - console.log(output) // "hello world, it's me!" -})