Skip to content

Commit 52c2f0e

Browse files
committed
Merge branch 'master' into trentm/metrics-actually-disabled
2 parents ec17cfa + 6547c1e commit 52c2f0e

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory holds example programs demonstrating the use of the Elastic
2+
Node.js APM agent (`elastic-apm-node`).

examples/trace-memcached.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
// A small example showing Elastic APM tracing of a script using `memcached`.
4+
//
5+
// By default this will use a Memcached on localhost. You can use:
6+
// npm run docker:start
7+
// to start a Memcached container (and other containers used for testing of
8+
// this project).
9+
10+
const apm = require('../').start({ // elastic-apm-node
11+
serviceName: 'example-trace-memcached'
12+
})
13+
14+
const Memcached = require('memcached')
15+
16+
const HOST = process.env.MEMCACHED_HOST || '127.0.0.1'
17+
const PORT = 11211
18+
const client = new Memcached(`${HOST}:${PORT}`, { timeout: 500 })
19+
20+
// For tracing spans to be created, there must be an active transaction.
21+
// Typically, a transaction is automatically started for incoming HTTP
22+
// requests to a Node.js server. However, because this script is not running
23+
// an HTTP server, we manually start a transaction. More details at:
24+
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html
25+
apm.startTransaction('t0')
26+
27+
client.version(function (err, data) {
28+
console.log('Version: data=%j (err=%s)', data, err)
29+
30+
client.set('foo', 'bar', 10, function (err) {
31+
console.log('Set: foo (err=%s)', err)
32+
33+
client.get('foo', function (err, data) {
34+
console.log('Get foo: %s (err=%s)', data, err)
35+
36+
client.get('foo', function (err, data) {
37+
console.log('Get foo (again): %s (err=%s)', data, err)
38+
39+
apm.endTransaction()
40+
client.end()
41+
})
42+
})
43+
})
44+
})

examples/trace-mysql.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env node --unhandled-rejections=strict
2+
3+
// A small example showing Elastic APM tracing of a script using `mysql`.
4+
//
5+
// By default this will use a MySQL on localhost with user 'root'. You can use:
6+
// npm run docker:start
7+
// to start a MySQL container (and other containers used for testing of
8+
// this project).
9+
10+
const apm = require('../').start({ // elastic-apm-node
11+
serviceName: 'example-trace-mysql'
12+
})
13+
14+
const mysql = require('mysql')
15+
16+
const client = mysql.createConnection({
17+
user: process.env.MYSQL_USER || 'root'
18+
})
19+
client.connect(function (err) {
20+
console.warn('Connected (err=%s)', err)
21+
})
22+
23+
// 1. Callback style
24+
// For tracing spans to be created, there must be an active transaction.
25+
// Typically, a transaction is automatically started for incoming HTTP
26+
// requests to a Node.js server. However, because this script is not running
27+
// an HTTP server, we manually start a transaction. More details at:
28+
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html
29+
apm.startTransaction('t1')
30+
client.query('SELECT 1 + 1 AS solution', (err, res) => {
31+
if (err) {
32+
console.log('[t1] Failure: err is', err)
33+
} else {
34+
console.log('[t1] Success: solution is %s', res[0].solution)
35+
}
36+
apm.endTransaction()
37+
})
38+
39+
// 2. Event emitter style
40+
const t2 = apm.startTransaction('t2')
41+
const q = client.query('SELECT 1 + 1 AS solution')
42+
q.on('error', function (err) {
43+
console.log('[t2] Failure: err is', err)
44+
})
45+
q.on('result', function (row) {
46+
console.log('[t2] solution is', row.solution)
47+
})
48+
q.on('end', function () {
49+
console.log('[t2] End')
50+
t2.end()
51+
})
52+
53+
// Lazily shutdown client after everything above is finished.
54+
setTimeout(() => {
55+
console.log('Done')
56+
client.end()
57+
}, 1000)

examples/trace-pg.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node --unhandled-rejections=strict
2+
// A small example showing Elastic APM tracing of a script using `pg`.
3+
//
4+
// By default this will use a Postgres on localhost with user 'postgres'.
5+
// You can use:
6+
// npm run docker:start
7+
// to start a Postgres container (and other containers used for testing of
8+
// this project).
9+
10+
const apm = require('../').start({ // elastic-apm-node
11+
serviceName: 'example-trace-pg'
12+
})
13+
14+
const { Client, Query } = require('pg')
15+
16+
const client = new Client({
17+
user: process.env.PGUSER || 'postgres'
18+
})
19+
client.connect(function (err) {
20+
console.warn('Connected (err=%s)', err)
21+
})
22+
23+
// 1. Callback style
24+
// For tracing spans to be created, there must be an active transaction.
25+
// Typically, a transaction is automatically started for incoming HTTP
26+
// requests to a Node.js server. However, because this script is not running
27+
// an HTTP server, we manually start a transaction. More details at:
28+
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html
29+
apm.startTransaction('t1')
30+
client.query('SELECT $1::text as message', ['Hello world!'], (err, res) => {
31+
if (err) {
32+
console.log('[t1] Failure: err is', err)
33+
} else {
34+
console.log('[t1] Success: message is %s', res.rows[0].message)
35+
}
36+
apm.endTransaction()
37+
})
38+
39+
// 2. Using streaming style, i.e. using a `Submittable` as node-postgres calls it.
40+
const t2 = apm.startTransaction('t2')
41+
var q = client.query(new Query('select 1 + 1 as solution'))
42+
q.on('error', (err) => {
43+
console.log('[t2] Failure: err is', err)
44+
t2.end()
45+
})
46+
q.on('row', (row) => {
47+
console.log('[t2] solution is %s', row.solution)
48+
})
49+
q.on('end', () => {
50+
console.log('[t2] Success')
51+
t2.end()
52+
})
53+
54+
// 3. Promise style
55+
apm.startTransaction('t3')
56+
client.query('select 1 + 1 as solution')
57+
.then(function (result) {
58+
console.log('[t3] Success: solution is %s', result.rows[0].solution)
59+
})
60+
.catch(function (err) {
61+
console.log('[t3] Failure: err is', err)
62+
})
63+
.finally(function () {
64+
apm.endTransaction()
65+
})
66+
67+
// TODO: 4. async/await style
68+
69+
// Lazily shutdown client after everything above is finished.
70+
setTimeout(() => {
71+
console.log('Done')
72+
client.end()
73+
}, 1000)

0 commit comments

Comments
 (0)