forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request newrelic#1349 from NodeJS-agent/nwolfe/start-segment
NODE-1012 Added `startSegment` to replace `createTracer`
- Loading branch information
Showing
9 changed files
with
381 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
var newrelic = require('newrelic') | ||
|
||
// Segments can only be created inside of transactions. They could be automatically | ||
// generated HTTP transactions or custom transactions. | ||
newrelic.startBackgroundTransaction('bg-tx', function transHandler() { | ||
var 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) | ||
tx.end() | ||
}) | ||
}) | ||
|
||
function someTask(cb) { | ||
myAsyncTask(function firstCb(err, result) { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
myNextTask(result, function secondCb(err, output) { | ||
cb(err, output) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
var newrelic = require('newrelic') | ||
|
||
// 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. 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) | ||
}) | ||
}) | ||
|
||
function someTask() { | ||
return myAsyncTask().then(function thenNext(result) { | ||
return myNextTask(result) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
var newrelic = require('newrelic') | ||
|
||
// 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. | ||
|
||
var output = await newrelic.startSegment('myCustomSegment', false, someTask) | ||
console.log(output) | ||
}) | ||
|
||
async function someTask() { | ||
var result = await myAsyncTask() | ||
var output = await myNextTask(result) | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
var newrelic = require('newrelic') | ||
|
||
// 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. | ||
|
||
var output = newrelic.startSegment('myCustomSegment', false, function timedFunction() { | ||
return someSyncTask() | ||
}) | ||
console.log(output) | ||
}) | ||
|
||
function someSyncTask() { | ||
var result = mySyncTask() | ||
var output = myNextTask(result) | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.