Skip to content

Commit 1ef3a14

Browse files
delvedorwatson
authored andcommitted
feat(elasticsearch): add support @elastic/elasticsearch client
1 parent c0d6d73 commit 1ef3a14

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

lib/instrumentation/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var shimmer = require('./shimmer')
1111
var Transaction = require('./transaction')
1212

1313
var MODULES = [
14+
'@elastic/elasticsearch',
1415
'apollo-server-core',
1516
'bluebird',
1617
'cassandra-driver',
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict'
2+
3+
module.exports = function (elasticsearch, agent, { version, enabled }) {
4+
if (!enabled) return elasticsearch
5+
6+
function generateSpan (activeSpans, meta, params) {
7+
const span = agent.startSpan(null, 'db.elasticsearch.request')
8+
if (span === null) return null
9+
10+
const { request } = meta
11+
span.name = `Elasticsearch: ${params.method} ${params.path}`
12+
13+
span.setDbContext({
14+
type: 'elasticsearch',
15+
statement: params.body
16+
})
17+
18+
activeSpans.set(request.id, span)
19+
20+
return span
21+
}
22+
23+
class ApmClient extends elasticsearch.Client {
24+
constructor (opts) {
25+
super(opts)
26+
27+
const activeSpans = new Map()
28+
let hasPrepareRequestEvent = false
29+
30+
this.on('prepare-request', (err, { meta, params }) => {
31+
hasPrepareRequestEvent = true
32+
if (err) {
33+
return agent.captureError(err)
34+
}
35+
36+
generateSpan(activeSpans, meta, params)
37+
})
38+
39+
this.on('request', (err, { meta }) => {
40+
const { request } = meta
41+
let span = null
42+
if (err) {
43+
span = activeSpans.get(request.id)
44+
agent.captureError(err)
45+
if (span !== undefined) {
46+
span.end()
47+
activeSpans.delete(request.id)
48+
}
49+
return
50+
}
51+
52+
if (hasPrepareRequestEvent === false) {
53+
span = generateSpan(activeSpans, meta, meta.params)
54+
// if (span === null) return
55+
}
56+
57+
// TODO: can we signal somehow that here
58+
// we are starting the actual http request?
59+
})
60+
61+
this.on('response', (err, { meta }) => {
62+
if (err) {
63+
// TODO: rebuild error object to avoid serialization issues
64+
agent.captureError(err, { custom: err.message })
65+
// agent.captureError(err, { labels: { meta: JSON.stringify(err.meta) } })
66+
// agent.captureError(err, { custom: JSON.parse(JSON.stringify(err.meta)) })
67+
// agent.captureError(err, { custom: err.meta.meta.connection })
68+
}
69+
70+
const { request } = meta
71+
const span = activeSpans.get(request.id)
72+
if (span !== undefined) {
73+
span.end()
74+
activeSpans.delete(request.id)
75+
}
76+
})
77+
}
78+
}
79+
80+
return Object.assign(elasticsearch, { Client: ApmClient })
81+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"@commitlint/cli": "^8.0.0",
110110
"@commitlint/config-conventional": "^8.0.0",
111111
"@commitlint/travis-cli": "^8.0.0",
112+
"@elastic/elasticsearch": "^7.1.0",
112113
"@types/node": "^12.0.8",
113114
"apollo-server-express": "^2.6.3",
114115
"aws-sdk": "^2.477.0",

0 commit comments

Comments
 (0)