-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure correct run context for 'ws' instrumentation
Refs: #2430
- Loading branch information
Showing
3 changed files
with
68 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env node --unhandled-rejections=strict | ||
|
||
// A small example showing Elastic APM tracing of the 'ws' package. | ||
// | ||
// Currently Elastic APM will create a span for `ws.send()` calls. | ||
// For tracing spans to be created, there must be an active transaction. | ||
// Typically, a transaction is automatically started for incoming HTTP | ||
// requests to a Node.js server. However, because this script is not running | ||
// an HTTP server, we manually start transactions in our websocket server and | ||
// client. More details at: | ||
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html | ||
|
||
const apm = require('../').start({ // elastic-apm-node | ||
serviceName: 'example-trace-ws' | ||
}) | ||
|
||
var WebSocket = require('ws') | ||
var PORT = 4567 | ||
|
||
// Server | ||
const wss = new WebSocket.Server({ port: PORT }) | ||
wss.on('connection', function (ws) { | ||
ws.on('message', function (message) { | ||
console.log('server on "message": %j', message) | ||
const tserver = apm.startTransaction('tserver') | ||
ws.send('pong') | ||
tserver.end() | ||
}) | ||
}) | ||
|
||
// Client | ||
const ws = new WebSocket('ws://localhost:' + PORT) | ||
ws.on('open', function () { | ||
const tclient = apm.startTransaction('tclient') | ||
console.log('client: send "ping"') | ||
ws.send('ping', function () { | ||
console.log('client: "ping" has been sent') | ||
}) | ||
console.log('client: send "ring"') | ||
ws.send('ring') | ||
tclient.end() | ||
}) | ||
ws.on('message', function (message) { | ||
console.log('client on "message": %j', message) | ||
wss.close() | ||
}) |
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