-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ensure correct run context for '@elastic/elasticsearch' instrumentation #2550
Changes from 5 commits
a3a21c8
c470455
f17c58a
4ccebd8
26bea02
38effb0
5a11de6
94a2b2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,20 +28,13 @@ exports.pathIsAQuery = pathIsAQuery | |
// {"query":{"query_string":{"query":"pants"}}} | ||
// | ||
// `path`, `query`, and `body` can all be null or undefined. | ||
exports.setElasticsearchDbContext = function (span, path, query, body, isLegacy) { | ||
exports.setElasticsearchDbContext = function (span, path, query, body) { | ||
if (path && pathIsAQuery.test(path)) { | ||
// From @elastic/elasticsearch: A read of Transport.js suggests query and | ||
// body will always be serialized strings, however the documented | ||
// `TransportRequestParams` (`ConnectionRequestParams` in version 8) | ||
// allows for non-strings, so we will be defensive. | ||
// | ||
// From legacy elasticsearch: query will be an object and body will be an | ||
// object, or an array of objects, e.g. for bulk endpoints. | ||
const parts = [] | ||
if (query) { | ||
if (typeof (query) === 'string') { | ||
parts.push(query) | ||
} else if (isLegacy && typeof (query) === 'object') { | ||
} else if (typeof (query) === 'object') { | ||
const encodedQuery = querystring.encode(query) | ||
if (encodedQuery) { | ||
parts.push(encodedQuery) | ||
|
@@ -51,12 +44,18 @@ exports.setElasticsearchDbContext = function (span, path, query, body, isLegacy) | |
if (body) { | ||
if (typeof (body) === 'string') { | ||
parts.push(body) | ||
} else if (isLegacy) { | ||
if (Array.isArray(body)) { | ||
parts.push(body.map(JSON.stringify).join('\n')) // ndjson | ||
} else if (typeof (body) === 'object') { | ||
} else if (Buffer.isBuffer(body) || typeof body.pipe === 'function') { | ||
// Never serialize a Buffer or a Readable. These guards mirror | ||
// `shouldSerialize()` in the ES client, e.g.: | ||
// https://github.com/elastic/elastic-transport-js/blob/069172506d1fcd544b23747d8c2d497bab053038/src/Transport.ts#L614-L618 | ||
} else if (Array.isArray(body)) { | ||
try { | ||
parts.push(body.map(JSON.stringify).join('\n') + '\n') // ndjson | ||
} catch {} | ||
} else if (typeof (body) === 'object') { | ||
try { | ||
parts.push(JSON.stringify(body)) | ||
} | ||
} catch {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. REVIEW NOTE: try/catch around these in case there is a surprise circular ref or something. I'll be opening a separate ticket to improve our instrumentation patching so that we can capture the body and querystring as serialized by the client instead of having to do it (again) in the agent. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REVIEW NOTE: Kibana's HEAD is using
8.1.0-canary.2
now. I had to drop at least8.0.0-canary.35
because our tests depend on elastic/elasticsearch-js@76659b6 which special-cased handling of the "sort" param to client commands such that it is always passed as a query param to ES.