forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace-hapi.js
executable file
·85 lines (72 loc) · 2.41 KB
/
trace-hapi.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
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
/*
* 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.
*/
'use strict';
// A small example showing Elastic APM tracing of a script using `@hapi/hapi`.
//
// Usage:
// node examples/trace-hapi.js
// curl -i http://localhost:3000/ # call the '/' handler
const apm = require('../').start({
serviceName: 'example-trace-hapi',
});
const Hapi = require('@hapi/hapi');
const init = async () => {
const server = Hapi.server({ port: 3000, host: 'localhost' });
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
// APM's instrumentation of Node's core "http" module automatically
// creates a transaction for each request.
server.log('info', 'hi to server.log from route /');
request.log('info', 'handling route /');
return 'hi';
},
});
// `server.log(...)` calls in the context of a request will have access to
// the APM transaction for this HTTP request (`apm.currentTransaction` et al).
server.events.on('log', (event, tags) => {
console.log('log event: traceIds=%j %j', apm.currentTraceIds, event.data);
});
// Called for `request.log(...)` calls.
server.events.on('request', (request, event, tags) => {
console.log(
'request event: traceIds=%j %j',
apm.currentTraceIds,
event.data,
);
});
// The 'response' event is emitted after the Node HTTP response has ended
// and the APM instrumentation has ended the APM transaction, so there will
// be no currentTransaction.
server.events.on('response', (request) => {
console.log(
'response event: traceIds=%j requestId=%s active=%s',
apm.currentTraceIds,
request.info.id,
request.active(),
);
});
// 'server.ext(...)' allows one to integrate in the Hapi request lifecycle.
// The 'onPreResponse' lifecycle event allows one to call the APM Transaction
// API before the response is complete.
server.ext('onPreResponse', (request, h) => {
console.log(
'onPreResponse: traceIds=%j requestId=%s active=%s',
apm.currentTraceIds,
request.info.id,
request.active(),
);
return h.continue;
});
await server.start();
console.log(
'Server running. Run "curl -i http://localhost:3000/" to call it.',
server.info.uri,
);
};
init();