Skip to content

Commit

Permalink
feat: added examples of API usage
Browse files Browse the repository at this point in the history
there are examples of some of these methods in the codebase for the agent, I moved those here too

https://github.com/newrelic/node-newrelic/blob/main/api.js
  • Loading branch information
brnhensley committed May 6, 2024
1 parent 1cd08bc commit 4e3f4fb
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ Use these API calls to [expand your instrumentation with custom instrumentation]
You <DoNotTranslate>**must**</DoNotTranslate> handle custom transactions manually by calling `newrelic.getTransaction()` at the start of your transaction, and then call `transaction.end()` when you are finished. New Relic begins timing the transaction when `newrelic.startWebTransaction()` is called and ends the transaction when `transaction.end()` is called.

You can also return a promise to indicate the end of the transaction. Please note that if this promise rejects, it does not automatically hook into New Relic's error tracking. This needs to be done manually with [`noticeError()`](#noticeError).

Example:
```js
var newrelic = require('newrelic')
newrelic.startWebTransaction('/some/url/path', function() {
var transaction = newrelic.getTransaction()
setTimeout(function() {
// do some work
transaction.end()
}, 100)
})
```
</Collapser>

<Collapser
Expand All @@ -272,6 +284,18 @@ Use these API calls to [expand your instrumentation with custom instrumentation]
You <DoNotTranslate>**must**</DoNotTranslate> handle custom transactions manually by calling `newrelic.getTransaction()` at the start of your transaction, and then call `transaction.end()` when you are finished. New Relic begins timing the transaction when `newrelic.startBackgroundTransaction()` is called and ends the transaction when `transaction.end()` is called.

You can also return a promise to indicate the end of the transaction. Please note that if this promise rejects, it does not automatically hook into New Relic’s error tracking. This needs to be done manually with [`noticeError()`](#noticeError).

Example:
```js
var newrelic = require('newrelic')
newrelic.startBackgroundTransaction('Red October', 'Subs', function() {
var transaction = newrelic.getTransaction()
setTimeout(function() {
// do some work
transaction.end()
}, 100)
})
```
</Collapser>

<Collapser
Expand Down Expand Up @@ -314,6 +338,14 @@ Use these API calls to [expand your instrumentation with custom instrumentation]
* The optional `callback` is a function passed to the handler to fire after its work is done.

The agent begins timing the segment when `startSegment` is called. The segment is ended when either the `handler` finishes executing, or `callback` is fired, if it is provided.

Example:
```js
newrelic.startSegment('mySegment', false, function handler() {
// The returned promise here will signify the end of the segment.
return myAsyncTask().then(myNextTask)
})
```
</Collapser>
</CollapserGroup>

Expand Down Expand Up @@ -833,7 +865,17 @@ New Relic's Node.js agent includes additional API calls.
}
```

Calling this function multiple times will replace previously defined versions of this callback function.
Calling this function multiple times will replace previously defined versions of this callback function. Example:

```js
function myCallback(metadata) {
if (metadata['http.statusCode'] === '400') {
return 'Bad User Input'
}
}

newrelic.setErrorGroupCallback(myCallback)
```

To learn more about this function, please refer to our [example app](https://github.com/newrelic/newrelic-node-examples/tree/main/error-fingerprinting).
</Collapser>
Expand Down

0 comments on commit 4e3f4fb

Please sign in to comment.