Skip to content

Commit

Permalink
chore: fixed custom instrumentation examples by defining specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Aug 1, 2024
1 parent 8860ca6 commit f91cb9e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion custom-instrumentation/instrument-messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cd newrelic-node-examples/messaging-app
3. Install dependencies and run application

```sh
npm ci
npm install
PORT=3000 NEW_RELIC_LICENSE_KEY=<Your New Relic License Key> npm start
```

Expand Down
25 changes: 12 additions & 13 deletions custom-instrumentation/instrument-messages/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ newrelic.instrumentMessages({ absolutePath: niftyPath, moduleName: 'nifty-messag
// misc key/value parameters can be recorded as a part of the trace segment
const params = { message, queueName }

return {
return new shim.specs.MessageSpec({
callback: shim.LAST,
destinationName: queueName,
destinationType: shim.QUEUE,
parameters: params
}
})
})

console.log(`[NEWRELIC] instrumenting method 'purge'`)
Expand All @@ -39,21 +39,20 @@ newrelic.instrumentMessages({ absolutePath: niftyPath, moduleName: 'nifty-messag
// misc key/value parameters can be recorded as a part of the trace segment
const params = { queueName }

return {
return new shim.specs.MessageSpec({
callback: shim.LAST,
destinationName: queueName,
destinationType: shim.QUEUE,
parameters: params
}
})
})

console.log(`[NEWRELIC] instrumenting callbacks of method 'getMessage'`)
shim.recordConsume(Client.prototype, 'getMessage', {
shim.recordConsume(Client.prototype, 'getMessage', new shim.specs.MessageSpec({
destinationName: shim.FIRST,
callback: shim.LAST,
messageHandler(shim, fn, name, args) {
const err = args[0]
const msg = args[1]
after({ args }) {
const [err, msg] = args
if (msg) {
console.log(
`[NEWRELIC] getMessage on queue ${msg.queueName} returned a message: '${msg.msg}'`
Expand All @@ -71,10 +70,10 @@ newrelic.instrumentMessages({ absolutePath: niftyPath, moduleName: 'nifty-messag
parameters: { err }
}
}
})
}))

console.log(`[NEWRELIC] instrumenting callbacks of method 'subscribe'`)
shim.recordSubscribedConsume(Client.prototype, 'subscribe', {
shim.recordSubscribedConsume(Client.prototype, 'subscribe', new shim.specs.MessageSubscribeSpec({
consumer: shim.LAST,
// This handler will be called in whatever context our subscribed
// message handler is called. In index.js, this is the
Expand All @@ -85,13 +84,13 @@ newrelic.instrumentMessages({ absolutePath: niftyPath, moduleName: 'nifty-messag
// Note that we are not recording the subscription call itself,
// only the the consume calls made because a subscription was made
// earlier.
messageHandler(shim, consumer, name, args) {
const msg = consumer[0]
messageHandler(shim, args) {
const msg = args[0]
console.log(`[NEWRELIC] subscribe on queue ${msg.queueName} returned a message: '${msg.msg}'`)
return {
destinationName: msg.queueName,
destinationType: shim.QUEUE
}
}
})
}))
}})
2 changes: 1 addition & 1 deletion custom-instrumentation/instrument-messages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "ISC",
"dependencies": {
"express": "^4.19.2",
"newrelic": "^11.10.1"
"newrelic": "^12.0.0"
},
"devDependencies": {
"@newrelic/eslint-config": "^0.2.0"
Expand Down
6 changes: 3 additions & 3 deletions custom-instrumentation/instrument-webframework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cd newrelic-node-examples/custom-instrumentation/instrument-webframework
4. Install dependencies and run the application

```sh
npm ci
npm install
npm start
```

Expand All @@ -26,10 +26,10 @@ npm start
Using curl:
```sh
# Fetch users
curl localhost:3000/api/users
curl http://localhost:3000/api/users

# Fetch homepage
curl localhost:3000/home
curl http://localhost:3000/home
```

Using browser:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ function instrumentMyWebFramework(shim, myModule, moduleName) {
shim.wrapMiddlewareMounter(Framework.prototype, ['all', 'get'], {
route: shim.FIRST,
wrapper: function wrapMiddleware(shim, fn, name, route) {
return shim.recordMiddleware(fn, {
return shim.recordMiddleware(fn, new shim.specs.MiddlewareSpec({
route: route,
type: shim.MIDDLEWARE,
req: shim.FIRST,
res: shim.SECOND,
next: shim.THIRD
})
}))
}
})

shim.recordRender(Framework.prototype, 'render', {
shim.recordRender(Framework.prototype, 'render', new shim.specs.RenderSpec({
view: shim.FIRST,
callback: shim.LAST
})
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"author": "New Relic",
"license": "ISC",
"dependencies": {
"newrelic": "^11.10.1"
"newrelic": "^12.0.0"
}
}

0 comments on commit f91cb9e

Please sign in to comment.