Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added example of using startWebTransaction #290

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions custom-instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

This folder contains example applications using the [Agent API](https://newrelic.github.io/node-newrelic/API.html) to do custom instrumentation.

* [instrument](./instrument) - example application that uses the [newrelic.instrument API](https://newrelic.github.io/node-newrelic/API.html#instrument) and associated [shim API](https://newrelic.github.io/node-newrelic/Shim.html) to instrument a simple library
* [instrumentDatastore](./instrument-datastore) - example application that uses the [newrelic.instrumentDatastore API](https://newrelic.github.io/node-newrelic/API.html#instrumentDatastore) and [datastore shim API](https://newrelic.github.io/node-newrelic/DatastoreShim.html) to instrument a toy datastore called Simple Datastore
* [instrumentWebframework](./instrument-webframework) - example application that uses the [newrelic.instrumentWebframework API](https://newrelic.github.io/node-newrelic/API.html#instrumentWebframework) and associated [WebFramework shim API](https://newrelic.github.io/node-newrelic/WebFrameworkShim.html) to instrument a hypothetical web framework
* [instrumentMessages](./instrument-messages) - example application that uses the [newrelic.instrumentMessages API](https://newrelic.github.io/node-newrelic/API.html#instrumentMessages) and associated [messaging shim API](https://newrelic.github.io/node-newrelic/MessageShim.html) to instrument a toy messaging library called Nifty Messages
* Instrument library code
* [instrument](./instrument) - example application that uses the [newrelic.instrument API](https://newrelic.github.io/node-newrelic/API.html#instrument) and associated [shim API](https://newrelic.github.io/node-newrelic/Shim.html) to instrument a simple library
* [instrumentDatastore](./instrument-datastore) - example application that uses the [newrelic.instrumentDatastore API](https://newrelic.github.io/node-newrelic/API.html#instrumentDatastore) and [datastore shim API](https://newrelic.github.io/node-newrelic/DatastoreShim.html) to instrument a toy datastore called Simple Datastore
* [instrumentWebframework](./instrument-webframework) - example application that uses the [newrelic.instrumentWebframework API](https://newrelic.github.io/node-newrelic/API.html#instrumentWebframework) and associated [WebFramework shim API](https://newrelic.github.io/node-newrelic/WebFrameworkShim.html) to instrument a hypothetical web framework
* [instrumentMessages](./instrument-messages) - example application that uses the [newrelic.instrumentMessages API](https://newrelic.github.io/node-newrelic/API.html#instrumentMessages) and associated [messaging shim API](https://newrelic.github.io/node-newrelic/MessageShim.html) to instrument a toy messaging library called Nifty Messages
* [Attributes, Events, and Metrics](./attributes-events-metrics) - example application that demonstrates how to create custom [attributes](https://newrelic.github.io/node-newrelic/API.html#addCustomAttribute), [events](https://newrelic.github.io/node-newrelic/API.html#recordCustomEvent), and [metrics](https://newrelic.github.io/node-newrelic/API.html#recordMetric).
* [Background Transactions](./background-transactions) - example application that uses the newrelic API to create [background transactions](https://newrelic.github.io/node-newrelic/API.html#startBackgroundTransaction)
* [Segments](./segments) - example application that demonstrates how to use the [newrelic.startSegment API](https://newrelic.github.io/node-newrelic/API.html#startSegment) in a variety of cases: callback-based, promise-based, asyncronously, and syncronously
* [Distributed tracing](./distributed-tracing/) - example application that demonstrates distributed tracing
* [Segments](./segments) - example application that demonstrates how to use the [newrelic.startSegment API](https://newrelic.github.io/node-newrelic/API.html#startSegment) in a variety of cases: callback-based, promise-based, asyncronously, and syncronously
* [Start Web Transaction](./start-web-transaction/) - example application that demonstrates how to use [newrelic.startWebTransaction](https://newrelic.github.io/node-newrelic/API.html#startWebTransaction).

## Purpose of Instrumentation

Expand Down
2 changes: 1 addition & 1 deletion custom-instrumentation/attributes-events-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This example provides an application to demonstrate how to share custom attribut
1. Clone or fork this repository.
2. Navigate to this example's sub directory
```
cd newrelic-node-examples/custom-instrumentation/attributes-and-events
cd newrelic-node-examples/custom-instrumentation/attributes-events-metrics
```
3. Install dependencies and run application.
```
Expand Down
16 changes: 16 additions & 0 deletions custom-instrumentation/start-web-transaction/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2022 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

module.exports = {
extends: '@newrelic',
parserOptions: {
ecmaVersion: 'latest'
},
rules: {
'no-console': 'off'
}
}
28 changes: 28 additions & 0 deletions custom-instrumentation/start-web-transaction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Sample Start Web Transactions Application
This example demonstrates how to wrap server code in [startWebTransaction](https://newrelic.github.io/node-newrelic/API.html#startWebTransaction) to record custom web transactions. This wraps [socket.io](https://github.com/socketio/socket.io) events in web transactions and records custom segments for the work. It also contains a [socket.io](https://github.com/socketio/socket.io) client that interacts with the web socket server.

## Getting Started

1. Clone or fork this repository.
2. Navigate to this example's sub directory
```
cd newrelic-node-examples/custom-instrumentation/start-web-transaction
```
3. Install dependencies and run application.
```
npm install
cp env.sample .env
# Fill out `NEW_RELIC_LICENSE_KEY` in .env and save
# Start the application
npm start
npm run client
```

## Exploring Telemetry
After running both the server and client, navigate to your application in APM & Services. Locate the 'Example Start Web Transaction App' service and then select Transactions on the left-side of the screen. You should see your requests in a few minutes in a similar fashion to below.

![transactions](./images/transactions.png)

To see a breakdown of a transaction, navigate to `Distributed Tracing`. Open a trace group for `/websocket/new-message`. You should see the custom segment as well as a full breakdown of the trace.

![distributed-trace](./images/dt.png)
41 changes: 41 additions & 0 deletions custom-instrumentation/start-web-transaction/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

const io = require('socket.io-client')

const socket = io.connect('http://localhost:3000', { reconnect: true })
let responses = 0

socket.on('pong', () => {
responses += 1
console.log('ping pong success')
})

socket.on('message-received', (data) => {
responses += 1
console.log(`message received: ${JSON.stringify(data)}`)
})

socket.on('update-done', (data) => {
responses += 1
console.log(`update received: ${JSON.stringify(data)}`)
})

socket.emit('ping')

for (let i = 1; i < 21; i++) {
socket.emit('update', { name: `Message ${i}`, msg: 'Hello world' })
socket.emit('new-message', `this is message ${i}`)
}

setInterval(() => {
if (responses === 41) {
console.log('done processing events from server')
// eslint-disable-next-line no-process-exit
process.exit(0)
}
}, 500)
Loading
Loading