forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace-redis.js
executable file
·76 lines (61 loc) · 2.06 KB
/
trace-redis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node --unhandled-rejections=strict
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
// A small example showing Elastic APM tracing the 'redis' package.
//
// This assumes a Redis server running on localhost. You can use:
// npm run docker:start redis
// to start an Redis docker container. Then `npm run docker:stop` to stop it.
const apm = require('../').start({
serviceName: 'example-trace-redis4',
spanCompressionEnabled: false,
});
const redis = require('redis');
async function useRedis() {
let res;
const client = redis.createClient({
name: 'example-trace-redis4', // This results in early `CLIENT SETNAME` sent in RedisClient.#initiateSocket()
database: 1, // This results in early `SELECT` sent in RedisClient.#initiateSocket()
});
await client.connect();
try {
res = await client.ping();
console.log('PING res: ', res);
} catch (err) {
console.log('PING err: ', err);
}
try {
res = await client.set('foo', 'bar');
console.log('SET res: ', res);
} catch (err) {
console.log('SET err: ', err);
}
try {
res = await client.get('foo');
console.log('GET res: ', res);
} catch (err) {
console.log('GET err: ', err);
}
try {
res = await client.multi().set('spam', 'eggs').get('spam').exec();
console.log('MULTI res: ', res);
} catch (err) {
console.log('MULTI err: ', err);
}
await client.quit();
}
async function main() {
// 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 a transaction. More details at:
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html
const trans = apm.startTransaction('trans');
Promise.all([useRedis()]).then(() => {
trans.end();
});
}
main();