-
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
feat: 'elasticsearchCaptureBodyUrls' config option #2873
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9206c16
feat: 'elasticsearchCaptureBodyUrls' config option
trentm f7e21b4
enable capturing ES span.context.http.url even with contextManager=patch
trentm 0452ebc
Merge branch 'main' into trentm/elasticsearch_capture_body_urls
trentm 8001549
refactor setElasticsearchDbContext -> getElasticsearchDbStatement
trentm 19b7c7b
cannot use dep aliases with the npm in node v8.6; just don't bother
trentm 29bd7e9
test fix to use that actual configured host (different in Docker setup)
trentm 87dbe9b
configuration doc entry; config.test.js additions
trentm df0b9e7
Merge branch 'main' into trentm/elasticsearch_capture_body_urls
trentm e5f8e68
changelog entry
trentm f8631c0
future-proof comment suggestion from alan
trentm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,64 +10,50 @@ | |
// - elasticsearch - the legacy Elasticsearch JS client | ||
// - @elastic/elasticsearch - the new Elasticsearch JS client | ||
|
||
const querystring = require('querystring') | ||
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. 👍 dings the "two |
||
|
||
// URL paths matching the following pattern will have their query params and | ||
// request body captured in the span (as `context.db.statement`). We match | ||
// a complete URL path component to attempt to avoid accidental matches of | ||
// user data, like `GET /my_index_search/...`. | ||
const pathIsAQuery = /\/(_search|_msearch|_count|_async_search|_sql|_eql)(\/|$)/ | ||
|
||
// (This is exported for testing.) | ||
exports.pathIsAQuery = pathIsAQuery | ||
|
||
// Set the span's `context.db` from the Elasticsearch request querystring and | ||
// body, if the request path looks like it is a query API. | ||
// | ||
// Some ES endpoints, e.g. '_search', support both query params and a body. | ||
// We encode both into 'span.context.db.statement', separated by '\n\n' | ||
// if both are present. E.g. for a possible msearch: | ||
// | ||
// search_type=query_then_fetch&typed_keys=false | ||
// | ||
// {} | ||
// {"query":{"query_string":{"query":"pants"}}} | ||
// | ||
// `path`, `query`, and `body` can all be null or undefined. | ||
exports.setElasticsearchDbContext = function (span, path, query, body) { | ||
if (path && pathIsAQuery.test(path)) { | ||
const parts = [] | ||
if (query) { | ||
if (typeof (query) === 'string') { | ||
parts.push(query) | ||
} else if (typeof (query) === 'object') { | ||
const encodedQuery = querystring.encode(query) | ||
if (encodedQuery) { | ||
parts.push(encodedQuery) | ||
} | ||
} | ||
} | ||
if (body) { | ||
if (typeof (body) === 'string') { | ||
parts.push(body) | ||
} 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 (_ignoredErr) {} | ||
} else if (typeof (body) === 'object') { | ||
try { | ||
parts.push(JSON.stringify(body)) | ||
} catch (_ignoredErr) {} | ||
} | ||
// Only capture the ES request body if the request path matches the | ||
// `elasticsearchCaptureBodyUrls` config. | ||
function shouldCaptureBody (path, elasticsearchCaptureBodyUrlsRegExp) { | ||
if (!path) { | ||
return false | ||
} | ||
for (var i = 0; i < elasticsearchCaptureBodyUrlsRegExp.length; i++) { | ||
const re = elasticsearchCaptureBodyUrlsRegExp[i] | ||
if (re.test(path)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
span.setDbContext({ | ||
type: 'elasticsearch', | ||
statement: parts.join('\n\n') | ||
}) | ||
/** | ||
* Get an appropriate `span.context.db.statement` for this ES client request, if any. | ||
* https://github.com/elastic/apm/blob/main/specs/agents/tracing-instrumentation-db.md#elasticsearch_capture_body_urls-configuration | ||
* | ||
* @param {string | null} path | ||
* @param {string | null} body | ||
* @param {RegExp[]} elasticsearchCaptureBodyUrlsRegExp | ||
* @return {string | undefined} | ||
*/ | ||
function getElasticsearchDbStatement (path, body, elasticsearchCaptureBodyUrlsRegExp) { | ||
if (body && shouldCaptureBody(path, elasticsearchCaptureBodyUrlsRegExp)) { | ||
if (typeof (body) === 'string') { | ||
return body | ||
} 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 { | ||
return body.map(JSON.stringify).join('\n') + '\n' // ndjson | ||
} catch (_ignoredErr) {} | ||
} else if (typeof (body) === 'object') { | ||
try { | ||
return JSON.stringify(body) | ||
} catch (_ignoredErr) {} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
getElasticsearchDbStatement | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No objection, but why
Array<string>
instead ofstring[]
?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.
Honestly just copying syntax from elsewhere in this file. https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#arrays says both are fine and doesn't claim a preference, so I'm fine with either way. Both are used in index.d.ts.